| 1 | package Perinci::Exporter; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | our $DATE = '2019-09-11'; # DATE | |||||||||||||||||||||||||||
| 4 | 4 | our $VERSION = '0.084'; # VERSION | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # IFUNBUILT | ||||||||||||||||||||||||||||
| 7 | # use strict 'subs', 'vars'; | ||||||||||||||||||||||||||||
| 8 | # use warnings; | ||||||||||||||||||||||||||||
| 9 | # END IFUNBUILT | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # what a generic name, this hash caches the wrapped functions, so that when | ||||||||||||||||||||||||||||
| 12 | # importer asks to import a wrapped function with default wrapping options, we | ||||||||||||||||||||||||||||
| 13 | # don't have to call wrap_sub again. customly wrapped functions are not cached | ||||||||||||||||||||||||||||
| 14 | # though. | ||||||||||||||||||||||||||||
| 15 | 4 | my %pkg_cache; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 2 | my $package = shift; | |||||||||||||||||||||||||||
| 19 | 2 | my @caller = caller(0); | |||||||||||||||||||||||||||
| 20 | 2 | install_import(@_, into => $caller[0]); | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 27 | ▶ | sub install_import { | ||||||||||||||||||||||||||
|
Subroutine
install_import: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 27 | my %instargs = @_; | |||||||||||||||||||||||||||
| 25 | 27 | ▶ | my @caller = caller($instargs{caller_level} || 0); | ||||||||||||||||||||||||||
|
Condition50%1/2
$instargs{'caller_level'} || 0
MC/DC0%0/1
$instargs{'caller_level'} || 0
$instargs{'caller_level'}
Truth table50%1/2
A
A= $instargs{'caller_level'}
| |||||||||||||||||||||||||||||
| 26 | 27 | ▶ | my $into = $instargs{into} || $caller[0]; | ||||||||||||||||||||||||||
|
Condition33%1/3
$instargs{'into'} || $caller[0]
MC/DC0%0/2
$instargs{'into'} || $caller[0]
$instargs{'into'}
$caller[0]
Truth table33%1/3
A || B
A= $instargs{'into'}B= $caller[0]
| |||||||||||||||||||||||||||||
| 27 | 27 | ▶ | return [400, "Please specify package to install import() to ". | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | "(either via 'into' or 'caller_level')"] | ||||||||||||||||||||||||||||
| 29 | unless $into; | ||||||||||||||||||||||||||||
| 30 | 28 | ▶ | my $import = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 31 | 28 | my $package = shift; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 28 | my @caller = caller(0); | |||||||||||||||||||||||||||
| 34 | do_export( | ||||||||||||||||||||||||||||
| 35 | { | ||||||||||||||||||||||||||||
| 36 | source => $into, | ||||||||||||||||||||||||||||
| 37 | target => $caller[0], | ||||||||||||||||||||||||||||
| 38 | default_exports => $instargs{default_exports}, | ||||||||||||||||||||||||||||
| 39 | extra_exports => $instargs{extra_exports}, | ||||||||||||||||||||||||||||
| 40 | default_wrap => defined($instargs{default_wrap}) ? $instargs{default_wrap} : 0, | ||||||||||||||||||||||||||||
| 41 | 28 | ▶ | default_on_clash => defined($instargs{default_on_clash}) ? $instargs{default_on_clash} : 'force', | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 42 | }, | ||||||||||||||||||||||||||||
| 43 | @_, | ||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||
| 45 | 27 | }; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 27 | *{"$into\::import"} = $import; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 28 | ▶ | sub do_export { | ||||||||||||||||||||||||||
|
Subroutine
do_export: called CC=61 SCAR=41.6
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 28 | my $expopts; | |||||||||||||||||||||||||||
| 53 | 28 | ▶ | if ($_[0] && ref($_[0]) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_[0] and ref $_[0] eq "HASH"
MC/DC0%0/2
$_[0] and ref $_[0] eq "HASH"
$_[0]
ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= $_[0]B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 54 | 28 | $expopts = shift; | |||||||||||||||||||||||||||
| 55 | } else { | ||||||||||||||||||||||||||||
| 56 | 0 | die "First argument to do_export() must be export options"; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 28 | my $source = $expopts->{source}; | |||||||||||||||||||||||||||
| 59 | 28 | my $target = $expopts->{target}; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # collect what symbols are available for exporting, along with their tags, | ||||||||||||||||||||||||||||
| 63 | # etc. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 28 | my %exports; | |||||||||||||||||||||||||||
| 68 | 28 | my $metas = \%{"$source\::SPEC"}; | |||||||||||||||||||||||||||
| 69 | 28 | ▶ | $metas ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$metas ||= {}
MC/DC0%0/1
$metas ||= {}
$metas
Truth table50%1/2
A
A= $metas
| |||||||||||||||||||||||||||||
| 70 | 28 | for my $k (keys %$metas) { | |||||||||||||||||||||||||||
| 71 | # for now we limit ourselves to subs | ||||||||||||||||||||||||||||
| 72 | 159 | ▶ | next unless $k =~ /\A\w+\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 159 | ▶ | my @tags = @{ $metas->{$k}{tags} || [] }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$metas->{$k}{'tags'} || []
MC/DC100%1/1
$metas->{$k}{'tags'} || []
$metas->{$k}{'tags'}
Truth table100%2/2
A
A= $metas->{$k}{'tags'}
| |||||||||||||||||||||||||||||
| 74 | 159 | ▶ | next if grep {$_ eq 'export:never'} @tags; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 134 | $exports{$k} = { | |||||||||||||||||||||||||||
| 76 | tags => \@tags, | ||||||||||||||||||||||||||||
| 77 | }; | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 28 | ▶ | for my $k (@{$expopts->{default_exports} || []}, | ||||||||||||||||||||||||||
|
Condition100%2/2
$expopts->{'default_exports'} || []
MC/DC100%1/1
$expopts->{'default_exports'} || []
$expopts->{'default_exports'}
Truth table100%2/2
A
A= $expopts->{'default_exports'}
| |||||||||||||||||||||||||||||
| 81 | 28 | @{"$source\::EXPORT"}) { | |||||||||||||||||||||||||||
| 82 | 54 | ▶ | if ($exports{$k}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 29 | push @{$exports{$k}{tags}}, 'export:default'; | |||||||||||||||||||||||||||
| 84 | } else { | ||||||||||||||||||||||||||||
| 85 | 25 | $exports{$k} = { | |||||||||||||||||||||||||||
| 86 | tags => [qw/export:default/], | ||||||||||||||||||||||||||||
| 87 | }; | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 28 | ▶ | for my $k (@{$expopts->{extra_exports} || []}, | ||||||||||||||||||||||||||
|
Condition100%2/2
$expopts->{'extra_exports'} || []
MC/DC100%1/1
$expopts->{'extra_exports'} || []
$expopts->{'extra_exports'}
Truth table100%2/2
A
A= $expopts->{'extra_exports'}
| |||||||||||||||||||||||||||||
| 92 | 28 | @{"$source\::EXPORT_OK"}) { | |||||||||||||||||||||||||||
| 93 | 54 | ▶ | if ($exports{$k}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | } else { | ||||||||||||||||||||||||||||
| 95 | 51 | $exports{$k} = { | |||||||||||||||||||||||||||
| 96 | tags => [], | ||||||||||||||||||||||||||||
| 97 | }; | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 28 | for my $k (keys %exports) { | |||||||||||||||||||||||||||
| 102 | 210 | push @{$exports{$k}{tags}}, 'all'; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # parse import() arguments | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 28 | my %impopts; # | |||||||||||||||||||||||||||
| 108 | my @imps; # requested symbols or tags to export, each element is: | ||||||||||||||||||||||||||||
| 109 | 28 | while (1) { | |||||||||||||||||||||||||||
| 110 | 55 | ▶ | last unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 28 | my $i = shift; | |||||||||||||||||||||||||||
| 112 | 28 | ▶ | if ($i =~ s!^-!!) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 4 | ▶ | die "Import option -$i requires argument" unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 3 | $impopts{$i} = shift; | |||||||||||||||||||||||||||
| 115 | 3 | next; | |||||||||||||||||||||||||||
| 116 | } else { | ||||||||||||||||||||||||||||
| 117 | 24 | my $el = {}; | |||||||||||||||||||||||||||
| 118 | 24 | ▶ | if (@_ && ref($_[0]) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ and ref $_[0] eq "HASH"
MC/DC100%2/2
@_ and ref $_[0] eq "HASH"
@_
ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 119 | 11 | my $io = shift; | |||||||||||||||||||||||||||
| 120 | 11 | $el->{$_} = $io->{$_} for keys %$io; | |||||||||||||||||||||||||||
| 121 | }; | ||||||||||||||||||||||||||||
| 122 | 24 | $el->{sym} = $i; | |||||||||||||||||||||||||||
| 123 | 24 | push @imps, $el; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 27 | ▶ | if (!@imps) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 5 | push @imps, {sym=>':default'}; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # find out existing symbols on the target package, so we can die on clash, | ||||||||||||||||||||||||||||
| 132 | # if that behavior's what the importer wants | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 27 | my %existing = _list_package_contents($target); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # recap information | ||||||||||||||||||||||||||||
| 137 | 27 | my $recap = {wrapped=>[]}; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # import! | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 27 | ▶ | $pkg_cache{$source} ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$pkg_cache{$source} ||= {}
MC/DC100%1/1
$pkg_cache{$source} ||= {}
$pkg_cache{$source}
Truth table100%2/2
A
A= $pkg_cache{$source}
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 27 | for my $imp (@imps) { | |||||||||||||||||||||||||||
| 144 | 29 | my @ssyms; # symbols from source package | |||||||||||||||||||||||||||
| 145 | 29 | ▶ | if ($imp->{sym} =~ s!^:!!) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 7 | @ssyms = grep { grep { | |||||||||||||||||||||||||||
| 147 | 107 | ▶ | "export:$imp->{sym}" eq $_ || $imp->{sym} eq $_ | ||||||||||||||||||||||||||
|
Condition100%3/3
"export:$imp->{'sym'}" eq $_ || $imp->{'sym'} eq $_
MC/DC100%2/2
"export:$imp->{'sym'}" eq $_ || $imp->{'sym'} eq $_
"export:$imp->{'sym'}" eq $_
$imp->{'sym'} eq $_
Truth table100%3/3
A || B
A= "export:$imp->{'sym'}" eq $_B= $imp->{'sym'} eq $_
| |||||||||||||||||||||||||||||
| 148 | 51 | } @{ $exports{$_}{tags} } } | |||||||||||||||||||||||||||
| 149 | keys %exports; | ||||||||||||||||||||||||||||
| 150 | } else { | ||||||||||||||||||||||||||||
| 151 | 22 | @ssyms = ($imp->{sym}); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 29 | for my $ssym (sort @ssyms) { | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 49 | ▶ | if (!$exports{$ssym}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 1 | die "$ssym is not exported by $source"; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # export to what target symbol? | ||||||||||||||||||||||||||||
| 161 | 48 | my $tsym; | |||||||||||||||||||||||||||
| 162 | 48 | ▶ | if ($imp->{as}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 1 | $tsym = $imp->{as}; | |||||||||||||||||||||||||||
| 164 | } else { | ||||||||||||||||||||||||||||
| 165 | 47 | $tsym = $ssym; | |||||||||||||||||||||||||||
| 166 | 47 | ▶ | if (my $prefix = defined($imp->{prefix}) ? $imp->{prefix} : $impopts{prefix}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 167 | 3 | $tsym = "$prefix$tsym"; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 47 | ▶ | if (my $suffix = defined($imp->{suffix}) ? $imp->{suffix} : $impopts{suffix}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 170 | 3 | $tsym = "$tsym$suffix"; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # clash? | ||||||||||||||||||||||||||||
| 175 | 48 | ▶ | if ($existing{$tsym}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 2 | ▶ | if ((defined($impopts{on_clash}) ? $impopts{on_clash} : $expopts->{default_on_clash}) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 177 | eq 'bail') { | ||||||||||||||||||||||||||||
| 178 | 2 | ▶ | die "Refusing to export ". | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | ($tsym eq $ssym ? $ssym : "$ssym (as $tsym)"). | ||||||||||||||||||||||||||||
| 180 | " to an existing symbol in package $target"; | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 46 | my $wrap; | |||||||||||||||||||||||||||
| 185 | SET_WRAP_OPTS: { | ||||||||||||||||||||||||||||
| 186 | 46 | my $default_wrap = ref $expopts->{default_wrap} eq 'HASH' ? | |||||||||||||||||||||||||||
| 187 | 0 | ▶ | {%{ $expopts->{default_wrap} }} : $expopts->{default_wrap}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 46 | my $default_wrap_opts = $default_wrap; | |||||||||||||||||||||||||||
| 189 | 46 | ▶ | $default_wrap_opts = {} if ref $default_wrap_opts ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 46 | ▶ | $default_wrap = {} if $default_wrap && ref $default_wrap ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$default_wrap and ref $default_wrap ne "HASH"
MC/DC50%1/2
$default_wrap and ref $default_wrap ne "HASH"
$default_wrap
ref $default_wrap ne "HASH"
Truth table66%2/3
A and B
A= $default_wrapB= ref $default_wrap ne "HASH"
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | $wrap = ref $imp->{wrap} eq 'HASH' ? | ||||||||||||||||||||||||||||
| 193 | 46 | ▶ | {%{ $imp->{wrap} }} : $imp->{wrap}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 46 | ▶ | $wrap = {} if $wrap && ref $wrap ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$wrap and ref $wrap ne "HASH"
MC/DC0%0/2
$wrap and ref $wrap ne "HASH"
$wrap
ref $wrap ne "HASH"
Truth table66%2/3
A and B
A= $wrapB= ref $wrap ne "HASH"
| |||||||||||||||||||||||||||||
| 195 | 46 | ▶ | if (defined $imp->{convert}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 1 | ▶ | die "Error when exporting $ssym: 'convert' option needs wrap=1 but wrap is disabled" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $wrap and not $wrap
MC/DC0%0/2
defined $wrap and not $wrap
defined $wrap
not $wrap
Truth table33%1/3
A and not(B)
A= defined $wrapB= not $wrap
| |||||||||||||||||||||||||||||
| 197 | if defined $wrap && !$wrap; | ||||||||||||||||||||||||||||
| 198 | 1 | ▶ | $wrap = $default_wrap_opts unless ref $wrap eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 1 | $wrap->{convert} = $imp->{convert}; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | 46 | for (qw/args_as result_naked curry timeout/) { | |||||||||||||||||||||||||||
| 203 | 183 | ▶ | if (defined $imp->{$_}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 5 | ▶ | die "Error when exporting $ssym: '$_' option needs wrap=1 but wrap is disabled" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $wrap and not $wrap
MC/DC50%1/2
defined $wrap and not $wrap
defined $wrap
not $wrap
Truth table66%2/3
A and not(B)
A= defined $wrapB= not $wrap
| |||||||||||||||||||||||||||||
| 205 | if defined $wrap && !$wrap; | ||||||||||||||||||||||||||||
| 206 | 4 | ▶ | $wrap = $default_wrap_opts unless ref $wrap eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 4 | ▶ | $wrap->{convert} ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$wrap->{"convert"} ||= {}
MC/DC0%0/1
$wrap->{"convert"} ||= {}
$wrap->{"convert"}
Truth table50%1/2
A
A= $wrap->{"convert"}
| |||||||||||||||||||||||||||||
| 209 | 4 | $wrap->{convert}{$_} = $imp->{$_}; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 45 | ▶ | $wrap = $default_wrap unless defined $wrap; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | #use DD; dd {ssym=>$ssym, wrap=>$wrap}; | ||||||||||||||||||||||||||||
| 215 | } # SET_WRAP_OPTS | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 45 | my $sub = \&{"$source\::$ssym"}; | |||||||||||||||||||||||||||
| 218 | DO_WRAP: { | ||||||||||||||||||||||||||||
| 219 | 45 | ▶ | last unless $wrap; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 12 | my $cache; | |||||||||||||||||||||||||||
| 222 | 12 | ▶ | if (keys(%$wrap) == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | # using default wrap options, we store the cached version of | ||||||||||||||||||||||||||||
| 224 | # these | ||||||||||||||||||||||||||||
| 225 | 6 | $cache = $pkg_cache{$source}{$ssym}{sub}; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | 12 | ▶ | if ($cache) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 1 | $sub = $cache; | |||||||||||||||||||||||||||
| 229 | 1 | push @{ $recap->{wrapped} }, $ssym; | |||||||||||||||||||||||||||
| 230 | } else { | ||||||||||||||||||||||||||||
| 231 | 11 | $sub = \&{"$source\::$ssym"}; | |||||||||||||||||||||||||||
| 232 | 11 | my $meta = $metas->{$ssym}; | |||||||||||||||||||||||||||
| 233 | 11 | ▶ | if (!$meta) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | #warn "Exporting $ssym to $target\::$tsym unwrapped ". | ||||||||||||||||||||||||||||
| 235 | # "because $ssym does not have metadata"; | ||||||||||||||||||||||||||||
| 236 | } else { | ||||||||||||||||||||||||||||
| 237 | 10 | require Perinci::Sub::Wrapper; | |||||||||||||||||||||||||||
| 238 | 10 | my $res = Perinci::Sub::Wrapper::wrap_sub( | |||||||||||||||||||||||||||
| 239 | %$wrap, | ||||||||||||||||||||||||||||
| 240 | sub_name => "$source\::$ssym", | ||||||||||||||||||||||||||||
| 241 | meta => $meta, | ||||||||||||||||||||||||||||
| 242 | ); | ||||||||||||||||||||||||||||
| 243 | 10 | ▶ | die "Can't wrap $ssym for $target: ". | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | "$res->[0] - $res->[1]" unless $res->[0] == 200; | ||||||||||||||||||||||||||||
| 245 | 10 | $sub = $res->[2]{sub}; | |||||||||||||||||||||||||||
| 246 | 10 | ▶ | $pkg_cache{$source}{$ssym}{sub} = $sub | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | if keys(%$wrap) == 0; | ||||||||||||||||||||||||||||
| 248 | 10 | push @{ $recap->{wrapped} }, $ssym; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | } # DO_WRAP | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | # finally, do the actual exporting! | ||||||||||||||||||||||||||||
| 254 | #say "Exporting $ssym -> $target\::$tsym"; #DEBUG# | ||||||||||||||||||||||||||||
| 255 | 45 | *{"$target\::$tsym"} = $sub; | |||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 45 | $existing{$tsym}++; | |||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | } # for @ssyms | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | } # for @imps | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 23 | $recap; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | # borrowed from Package::MoreUtil. this is actually not a proper implementation, | ||||||||||||||||||||||||||||
| 267 | # but since we want to avoid extra footprint by loading Package::Stash, we'll | ||||||||||||||||||||||||||||
| 268 | # get by for now. | ||||||||||||||||||||||||||||
| 269 | 27 | ▶ | sub _list_package_contents { | ||||||||||||||||||||||||||
|
Subroutine
_list_package_contents: called CC=12 SCAR=28.0
| |||||||||||||||||||||||||||||
| 270 | 27 | my $pkg = shift; | |||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 27 | ▶ | return () unless !length($pkg) || _package_exists($pkg); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not length $pkg or _package_exists($pkg)
MC/DC0%0/2
not length $pkg or _package_exists($pkg)
not length $pkg
_package_exists($pkg)
Truth table33%1/3
not(A) or B
A= not length $pkgB= _package_exists($pkg)
| |||||||||||||||||||||||||||||
| 273 | 27 | my $symtbl = \%{$pkg . "::"}; | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 27 | my %res; | |||||||||||||||||||||||||||
| 276 | 27 | ▶ | while (my ($k, $v) = each %$symtbl) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | 54 | ▶ | next if $k =~ /::$/; # subpackage | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | 54 | my $n; | |||||||||||||||||||||||||||
| 279 | 54 | ▶ | if ("$v" !~ /^\*/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | # constant | ||||||||||||||||||||||||||||
| 281 | 0 | $res{$k} = $v; | |||||||||||||||||||||||||||
| 282 | 0 | next; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | 54 | ▶ | if (defined *$v{CODE}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 3 | $res{$k} = *$v{CODE}; # subroutine | |||||||||||||||||||||||||||
| 286 | 3 | $n++; | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | 54 | ▶ | if (defined *$v{HASH}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | 0 | $res{"\%$k"} = \%{*$v}; # hash | |||||||||||||||||||||||||||
| 290 | 0 | $n++; | |||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | 54 | ▶ | if (defined *$v{ARRAY}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | 24 | $res{"\@$k"} = \@{*$v}; # array | |||||||||||||||||||||||||||
| 294 | 24 | $n++; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | 54 | ▶ | if (defined(*$v{SCALAR}) # XXX always defined? | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined *$v{"SCALAR"} and defined ${*$v;}
MC/DC0%0/2
defined *$v{"SCALAR"} and defined ${*$v;}
defined *$v{"SCALAR"}
defined ${*$v;}
Truth table33%1/3
A and B
A= defined *$v{"SCALAR"}B= defined ${*$v;}
| |||||||||||||||||||||||||||||
| 297 | 54 | && defined(${*$v})) { # currently we filter undef values | |||||||||||||||||||||||||||
| 298 | 0 | $res{"\$$k"} = \${*$v}; # scalar | |||||||||||||||||||||||||||
| 299 | 0 | $n++; | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 54 | ▶ | if (!$n) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | 27 | $res{"\*$k"} = $v; # glob | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 27 | %res; | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | # also borrowed from Package::MoreUtil | ||||||||||||||||||||||||||||
| 311 | 27 | ▶ | sub _package_exists { | ||||||||||||||||||||||||||
|
Subroutine
_package_exists: called CC=2 SCAR=8.4
| |||||||||||||||||||||||||||||
| 312 | 27 | my $pkg = shift; | |||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | # opt | ||||||||||||||||||||||||||||
| 315 | #return unless $pkg =~ /\A\w+(::\w+)*\z/; | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 27 | ▶ | if ($pkg =~ s!::(\w+)\z!!) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | 0 | return !!${$pkg . "::"}{$1 . "::"}; | |||||||||||||||||||||||||||
| 319 | } else { | ||||||||||||||||||||||||||||
| 320 | 27 | return !!$::{$pkg . "::"}; | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | 4 | 1; | |||||||||||||||||||||||||||
| 325 | # ABSTRACT: An exporter that groks Rinci metadata | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | __END__ | ||||||||||||||||||||||||||||