blib/lib/Export/These.pm

filter: Statementstmt 86.4% 172 / 199 Branchbran 59.3% 38 / 64 Conditioncond 50.0% 9 / 18 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 19 / 19 Podpod n/a totaltotal 77.4% 240 / 310 SCAR 42.9
CC
48
Cov
78%
import
13.7
_self_export
12.6
_self_export
12.6
CRAP 72.7
↑ Summary
1 package Export::These;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION="v0.1.2";
7  
8 sub import {
9 7 ▶ my $package=shift;
Subroutine import: called CC=12 SCAR=26.2
10 7 my $exporter=caller;
11  
12 #Treat args as key value pairs, unless the value is a string.
13 #in this case it is the name of a symbol to export
14 7 my ($k, $v);
15  
16 2 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Locate or create the EXPORT, EXPORT_OK and EXPORT_TAGS package variables.
19 # These are used to accumulate our exported symbol names across multiple
20 # use Export::Terse ...; statements
21 #
22 7 my $export_ok= \@{"@{[$exporter]}::EXPORT_OK"};
23 7 my $export= \@{"@{[$exporter]}::EXPORT"};
24 7 my $export_tags= \%{"@{[$exporter]}::EXPORT_TAGS"};
25  
26 7 ▶ while(@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 16 7 23
27 16 $k=shift;
28  
29 16 ▶ die "Expecting symbol name or group name" if ref $k;
Branch: 1 branch
BranchTrueFalseTotal
if ref $k 0 16 16
30 16 my $r=ref $_[0];
31 16 ▶ unless($r){
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 12 4 16
32 12 push @$export, $k;
33 12 push @$export_ok, $k;
34 next
35 12 }
36 4 my $v=shift;
37  
38 4 for($k){
39 4 ▶ if(/export_ok$/ and $r eq "ARRAY"){
Branch: 3 branches
BranchTrueFalseTotal
if (/export_ok$/ and $r eq 'ARRAY') { } 0 4 4
elsif (/export$/ and $r eq 'ARRAY') { } 0 4 4
elsif ($r eq 'ARRAY') { } 4 0 4
Condition33%1/3
/export_ok$/ and $r eq "ARRAY"
!ll&&!rl&&r
400
MC/DC0%0/2
/export_ok$/ and $r eq "ARRAY"
/export_ok$/ $r eq "ARRAY"
Truth table33%1/3
A and B
A= /export_ok$/B= $r eq "ARRAY"
ABresult
0X0
100
111
40 0 push @$export_ok, @$v;
41 }
42 ▶ elsif(/export$/ and $r eq "ARRAY"){
Condition33%1/3
/export$/ and $r eq "ARRAY"
!ll&&!rl&&r
400
MC/DC0%0/2
/export$/ and $r eq "ARRAY"
/export$/ $r eq "ARRAY"
Truth table33%1/3
A and B
A= /export$/B= $r eq "ARRAY"
ABresult
0X0
100
111
43 0 push @$export, @$v;
44 0 push @$export_ok, @$v;
45 }
46 elsif($r eq "ARRAY"){
47 #Assume key is a tag name
48 4 push $export_tags->{$k}->@*, @$v;
49 4 push @$export_ok, @$v;
50 }
51 else {
52 0 die "Unkown export grouping: $k";
53 }
54 }
55 }
56  
57 # Generate the import sub here if it doesn't exist already
58  
59 7 local $"= " ";
60 7 my $exist=eval {*{\${$exporter."::"}{import}}{CODE}};
61 7 ▶ if($exist){
Branch: 1 branch
BranchTrueFalseTotal
if ($exist) 2 5 7
62 2 return;
63 }
64  
65 2 ▶ my $res=eval qq|
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _self_export: called CC=11 SCAR=25.3
Subroutine _self_export: called CC=11 SCAR=25.3
Subroutine _self_export: called CC=11 SCAR=25.3
Subroutine import: called CC=3 SCAR=11.4
Subroutine import: called CC=3 SCAR=11.4
Subroutine import: called CC=3 SCAR=11.4
Branch: 24 branches
BranchTrueFalseTotal
@_ ? : 1 1 2
if (/^:/) { } 1 9 10
if $group 1 0 1
if $t =~ /^\$/ 2 7 9
unless $found 0 9 9
if $type 9 4 13
if $@ 0 13 13
@_ ? : 2 2 4
if (/^:/) { } 2 4 6
if $group 2 0 2
if $t =~ /^\$/ 0 4 4
unless $found 0 4 4
if $type 2 6 8
if $@ 0 8 8
@_ ? : 0 1 1
if (/^:/) { } 0 0 0
if $group 0 0 0
if $t =~ /^\$/ 0 0 0
unless $found 0 0 0
if $type 0 0 0
if $@ 0 0 0
if ($ref) 1 2 3
if ($ref) 1 2 3
if ($ref) 1 0 1
Condition58%7/12
$type //= "CODE"
l!l
94
$type //= "CODE"
l!l
26
$type //= "CODE"
l!l
00
$Exporter::ExportLevel //= 0
l!l
30
$Exporter::ExportLevel //= 0
l!l
30
$Exporter::ExportLevel //= 0
l!l
10
MC/DC100%1/1
$type //= "CODE"
$type
MC/DC100%1/1
$type //= "CODE"
$type
MC/DC0%0/1
$type //= "CODE"
$type
MC/DC0%0/1
$Exporter::ExportLevel //= 0
$Exporter::ExportLevel
MC/DC0%0/1
$Exporter::ExportLevel //= 0
$Exporter::ExportLevel
MC/DC0%0/1
$Exporter::ExportLevel //= 0
$Exporter::ExportLevel
Truth table100%2/2
A
A= $type
Aresult
11
00
Truth table100%2/2
A
A= $type
Aresult
11
00
Truth table0%0/2
A
A= $type
Aresult
11
00
Truth table50%1/2
A
A= $Exporter::ExportLevel
Aresult
11
00
Truth table50%1/2
A
A= $Exporter::ExportLevel
Aresult
11
00
Truth table50%1/2
A
A= $Exporter::ExportLevel
Aresult
11
00
66 package $exporter;
67 no strict "refs";
68  
69  
70 sub _self_export {
71 shift;
72  
73 5 my \$ref_export_ok= \\\@@{[$exporter]}::EXPORT_OK;
74 5 my \$ref_export= \\\@@{[$exporter]}::EXPORT;
75 5 my \$ref_tags= \\\%@{[$exporter]}::EXPORT_TAGS;
76  
77 my \$target=shift;
78  
79 no strict "refs";
80 for(\@_ ? \@_ : \@\$ref_export){
81 my \@syms;
82 if(/^:/){
83 my \$name= s/^://r;
84  
85 my \$group=\$ref_tags->{\$name};
86 #die "Tag \$name does not exists" unless \$group;
87 push \@syms, \@\$group if \$group;
88 }
89 else {
90 #non tag symbol
91 my \$t=\$_;
92 \$t="\\\\\$t" if \$t =~ /^\\\$/;
93 my \$found=grep /\$t/, \@\$ref_export_ok;
94 die "\$_ is not exported from ".__PACKAGE__."\n" unless \$found;
95 push \@syms, \$_;
96 }
97
98 my \%map=(
99 '\$'=>"SCALAR",
100 '\@'=>"ARRAY",
101 '\%'=>"HASH",
102 '\&'=>"CODE"
103 );
104  
105 for(\@syms){
106 my \$prefix=substr(\$_,0,1);
107 my \$name=\$_;
108 my \$type=\$map{\$prefix};
109  
110 \$name=substr \$_, 1 if \$type;
111 \$type//="CODE";
112  
113 no warnings "redefine";
114 eval { *{\$target."::".\$name}= *{ \\\${__PACKAGE__ ."::"}{\$name}}{\$type}; };
115 die "Could not export \$prefix\$name from ".__PACKAGE__ if \$\@;
116  
117  
118 }
119 }
120  
121  
122 }
123  
124  
125 sub import {
126 my \$package=shift;
127 \$Exporter::ExportLevel//=0;
128 my \$target=(caller(\$Exporter::ExportLevel))[0];
129  
130 $exporter->_self_export(\$target, \@_);
131
132 local \$Exporter::ExportLevel=\$Exporter::ExportLevel+3;
133 my \$ref=eval {*{\\\${\$package."::"}{_reexport}}{CODE}};
134  
135 if(\$ref){
136 $exporter->_reexport(\$target, \@_);
137 }
138  
139 }
140  
141 1;
142 |;
143 5 ▶ die $@ unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 5 5
144 }
145 1 1;
146  
147  
148 =head1 NAME
149  
150 Export::These - Terse Symbol (Re)Exporting
151  
152  
153 =head1 SYNOPSIS
154  
155 A fine package, exporting subroutines,
156  
157 package My::ModA;
158  
159 use Export::These "dog", "cat", ":colors"=>[qw<blue green>];
160  
161 sub dog {...}
162 sub cat {...}
163 sub blue {...}
164 sub green {...}
165 1;
166  
167 Another package which would like to reexport the subs from My::ModA:
168  
169 package My::ModB;
170 use My::ModA;
171  
172 use Export::These ":colors"=>["more_colours"];
173  
174 sub _reexport {
175 my ($packate, $target, @names)=@_;
176 My::ModA->import(":colours") if grep /:colours/, @names;
177 }
178
179 sub more_colours { .... }
180 1;
181  
182  
183 Use package like usual:
184  
185 use My::ModB qw<:colors dog>
186  
187 # suburtines blue, green , more_colors and dog imported
188  
189  
190  
191 =head1 DESCRIPTION
192  
193 A module to make exporting symbols less verbose and facilitate reexporting of
194 symbols from dependencies with minimal input from the module author.
195  
196 By default listing a symbol for export, even in a group/tag, means it will be
197 automatically marked as 'export_ok', saving on duplication and managing two
198 separate lists.
199  
200 It B<DOES NOT> inherit from C<Exporter> nor does it utilise the C<import>
201 routine from C<Exporter>. It injects its own C<import> subroutine into the each
202 calling package. This injected subroutine adds the desired symbols to the
203 target package as you would expect.
204  
205 If the exporting package has a C<_reexport> subroutine, it is called when being
206 imported. This is the 'hook' location where its safe to call C<-E<gt>import> on
207 any dependencies modules it might want to export. The symbols from these
208 packages will automatically be installed into the target package with no extra
209 configuration needed.
210  
211 Finally warnings about symbols redefinition in the export process (i.e. exporting
212 to two subroutines with the same name into the same namespace) are silenced to
213 keep warning noise to a minimum. The last symbol definition will ultimately be
214 the one used.
215  
216  
217  
218 =head1 MOTIVATION
219  
220 Suppose you have a server module, which uses a configuration module to process
221 configuration data. However the main program (which imported the server module)
222 also needs to use the subroutines from the configuration module. The consumer
223 of the server module has to also add the configuration module as a dependency.
224  
225 With this module the server can simply reexport the required configuration
226 routines, injecting the dependency, in stead of hard coding it.
227  
228  
229 =head1 USAGE
230  
231 =head2 Specifying Symbols to Export
232  
233 use Export::These ...;
234  
235 The pragma takes a list of arguments to add to the C<@EXPORT> and C<EXPORT_OK>
236 variables. The items are taken as a name of a symbol or tag, unless the
237 following argument in the list is an array ref.
238  
239 eg:
240  
241 use Export::These qw<sym1 sym2>;
242  
243  
244 If the item name is "export_ok", then the items in the following array ref are
245 added to the C<@EXPORT_OK> variable.
246
247  
248 eg
249 use Export::These export_ok=>[qw<sym1>];
250  
251  
252 If the item name is "export", then the items in the following array ref are
253 added to the C<@EXPORT_OK> and the C<EXPORT> variables. This is the same as
254 simply listing the items at the top level.
255
256 eg
257  
258 use Export::These export=>[qw<sym1>];
259 # same as
260 # use Export::These qw<sym1>;
261  
262  
263 If the item has another name, it is a tag name and the items in the following
264 array ref are added to the C<%EXPORT_TAGS> variable and to C<@EXPORT_OK>
265  
266 eg use Export::These group1=>["sym1"];
267  
268  
269 The list can contain any combination of the above:
270  
271 eq use Export::These "sym1", group1=>["sym2", "sym3"], export_ok=>"sym4";
272  
273  
274 =head2 Rexporting Symbols
275  
276 If a subroutine called C<_reexport> exists in the exporting package, it will be
277 called on (with the -> notation) during import, after the normal symbols have
278 been processed. The first argument is the package name of exporter, the second
279 is the package name of the importer (the target), and the remaining arguments
280 are the names of symbols or tags to import.
281  
282 In this subroutine, you call C<import> on as any packages you want to reexport:
283  
284 eg
285 use Sub::Module;
286 use Another::Mod;
287  
288 sub _reexport {
289 my ($package, $target, @names)=@_;
290  
291 Sub::Module->import;
292 Another::Mod->import(@names);
293 ...
294 }
295  
296 =head2 Conditional Reexporting
297  
298 If you would only like to require and export on certain conditions, some extra
299 steps are needed to ensure correct setup of back end variables. Namely the
300 C<$Exporter::ExportLevel> variable needs to be localized and set to 0 inside a
301 block BEFORE calling the C<-E<gt>import> subroutine on the package.
302  
303 sub _reexport {
304 my ($package, $target, @names)=@_;
305  
306 if(SOME_CONDITION){
307 {
308 # In an localised block, reset the export level
309 local $Exporter::ExportLevel=0;
310 require Sub::Module;
311 require Another::Module;
312 }
313  
314 Sub::Module->import;
315 Another::Mod->import(@names);
316  
317 }
318 }
319  
320 =head2 Reexport Super Class Symbols
321  
322 Any exported symbols from the inheritance chain can be reexported in the same
323 manner, as long as they are package subroutines and not methods:
324  
325 eg
326  
327 package ModChild;
328 parent ModParent;
329  
330 # or
331
332 class ModChild :isa(ModParent)
333  
334
335 sub _reexport {
336 my ($package, $target, @names)=@_;
337 $package->SUPER::import(@names);
338 }
339  
340  
341 =head1 COMPARISON TO OTHER MODULES
342  
343 L<Import::Into> Provides clean way to reexport symbols, though you will have to
344 roll your own 'normal' export of symbols from you own package.
345  
346 L<Import::Base> Requires a custom package to group the imports and reexports
347 them. This is a different approach and might better suit your needs.
348  
349  
350 Reexporting symbols with C<Exporter> directly is a little cumbersome. You
351 either need to import everything into you module name space (even if you don't
352 need it) and then reexport from there. Alternatively you can import directly
353 into a package, but you need to know at what level in the call stack it is.
354 This is exactly what this module addresses.
355  
356  
357 =head1 REPOSITOTY and BUGS
358  
359 Please report and feature requests or bugs via the github repo:
360  
361 L<https://github.com/drclaw1394/perl-export-these.git>
362  
363 =head1 AUTHOR
364  
365 Ruben Westerberg, E<lt>drclaw@mac.comE<gt>
366  
367 =head1 COPYRIGHT AND LICENSE
368  
369 Copyright (C) 2023 by Ruben Westerberg
370  
371 Licensed under MIT
372  
373 =head1 DISCLAIMER OF WARRANTIES
374  
375 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
376 INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
377 FITNESS FOR A PARTICULAR PURPOSE.
378  
379 =cut
380  
↑ Summary