blib/lib/Export/These.pm

filter: Statementstmt 85.7% 163 / 190 Branchbran 60.9% 39 / 64 Conditioncond 50.0% 9 / 18 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 16 / 16 Podpod n/a totaltotal 76.8% 229 / 298 CC 48 SCAR 22.9
CC
48
Cov
78%
import
13.8
_self_export
12.7
_self_export
12.7
CRAP 9.9
↑ 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.1";
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 _self_export: called CC=11 SCAR=25.4
Subroutine _self_export: called CC=11 SCAR=25.4
Subroutine _self_export: called CC=11 SCAR=25.4
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 2 3
if (/^:/) { } 1 7 8
if $group 1 0 1
if $t =~ /^\$/ 1 6 7
unless $found 0 7 7
if $type 4 5 9
if $@ 0 9 9
@_ ? : 2 1 3
if (/^:/) { } 2 6 8
if $group 2 0 2
if $t =~ /^\$/ 1 5 6
unless $found 0 6 6
if $type 7 5 12
if $@ 0 12 12
@_ ? : 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
45
$type //= "CODE"
l!l
75
$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 eval { *{\$target."::".\$name}= *{ \\\${__PACKAGE__ ."::"}{\$name}}{\$type}; };
113 die "Could not export \$prefix\$name from ".__PACKAGE__ if \$\@;
114  
115  
116 }
117 }
118  
119  
120 }
121  
122  
123 sub import {
124 my \$package=shift;
125 \$Exporter::ExportLevel//=0;
126 my \$target=(caller(\$Exporter::ExportLevel))[0];
127  
128 $exporter->_self_export(\$target, \@_);
129
130 local \$Exporter::ExportLevel=\$Exporter::ExportLevel+3;
131 my \$ref=eval {*{\\\${\$package."::"}{_reexport}}{CODE}};
132  
133 if(\$ref){
134 $exporter->_reexport(\$target, \@_);
135 }
136  
137 }
138  
139 1;
140 |;
141 5 ▶ die $@ unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 5 5
142 }
143 1 1;
144  
145  
146 =head1 NAME
147  
148 Export::These - Terse Symbol (Re)Exporting
149  
150  
151 =head1 SYNOPSIS
152  
153 A fine package, exporting subroutines,
154  
155 package My::ModA;
156  
157 use Export::These "dog", "cat", ":colors"=>[qw<blue green>];
158  
159 sub dog {...}
160 sub cat {...}
161 sub blue {...}
162 sub green {...}
163 1;
164  
165 Another package which would like to reexport the subs from My::ModA:
166  
167 package My::ModB;
168 use My::ModA;
169  
170 use Export::These ":colors"=>["more_colours"];
171  
172 sub _reexport {
173 my ($packate, $target, @names)=@_;
174 My::ModA->import(":colours") if grep /:colours/, @names;
175 }
176
177 sub more_colours { .... }
178 1;
179  
180  
181 Use package like usual:
182  
183 use My::ModB qw<:colors dog>
184  
185 # suburtines blue, green , more_colors and dog imported
186  
187  
188  
189 =head1 DESCRIPTION
190  
191 A module to make exporting symbols less verbose and facilitate reexporting of
192 symbols from dependencies with minimal input from the module author.
193  
194 By default listing a symbol for export, even in a group/tag, means it will be
195 automatically marked as 'export_ok', saving on duplication and managing two
196 separate lists.
197  
198 It B<DOES NOT> inherit from C<Exporter> nor does it utilise the C<import>
199 routine from C<Exporter>. It injects its own C<import> subroutine into the each
200 calling package. This injected subroutine adds the desired symbols to the
201 target package as you would expect.
202  
203 If the exporting package has a C<_reexport> subroutine, it is called when being
204 imported. This is the 'hook' location where its safe to call C<-E<gt>import> on
205 any dependencies modules it might want to export. The symbols from these
206 packages will automatically be installed into the target package with no extra
207 configuration needed.
208  
209  
210  
211 =head1 MOTIVATION
212  
213 Suppose you have a server module, which uses a configuration module to process
214 configuration data. However the main program (which imported the server module)
215 also needs to use the subroutines from the configuration module. The consumer
216 of the server module has to also add the configuration module as a dependency.
217  
218 With this module the server can simply reexport the required configuration
219 routines, injecting the dependency, in stead of hard coding it.
220  
221  
222 =head1 USAGE
223  
224 =head2 Specifying Symbols to Export
225  
226 use Export::These ...;
227  
228 The pragma takes a list of arguments to add to the C<@EXPORT> and C<EXPORT_OK>
229 variables. The items are taken as a name of a symbol or tag, unless the
230 following argument in the list is an array ref.
231  
232 eg:
233  
234 use Export::These qw<sym1 sym2>;
235  
236  
237 If the item name is "export_ok", then the items in the following array ref are
238 added to the C<@EXPORT_OK> variable.
239
240  
241 eg
242 use Export::These export_ok=>[qw<sym1>];
243  
244  
245 If the item name is "export", then the items in the following array ref are
246 added to the C<@EXPORT_OK> and the C<EXPORT> variables. This is the same as
247 simply listing the items at the top level.
248
249 eg
250  
251 use Export::These export=>[qw<sym1>];
252 # same as
253 # use Export::These qw<sym1>;
254  
255  
256 If the item has another name, it is a tag name and the items in the following
257 array ref are added to the C<%EXPORT_TAGS> variable and to C<@EXPORT_OK>
258  
259 eg use Export::These group1=>["sym1"];
260  
261  
262 The list can contain any combination of the above:
263  
264 eq use Export::These "sym1", group1=>["sym2", "sym3"], export_ok=>"sym4";
265  
266  
267 =head2 Rexporting Symbols
268  
269 If a subroutine called C<_reexport> exists in the exporting package, it will be
270 called on (with the -> notation) during import, after the normal symbols have
271 been processed. The first argument is the package name of exporter, the second
272 is the package name of the importer (the target), and the remaining arguments
273 are the names of symbols or tags to import.
274  
275 In this subroutine, you call C<import> on as any packages you want to reexport:
276  
277 eg
278 use Sub::Module;
279 use Another::Mod;
280  
281 sub _reexport {
282 my ($package, $target, @names)=@_;
283  
284 Sub::Module->import;
285 Another::Mod->import(@names);
286 ...
287 }
288  
289 =head2 Conditional Reexporting
290  
291 If you would only like to require and export on certain conditions, some extra
292 steps are needed to ensure correct setup of back end variables. Namely the
293 C<$Exporter::ExportLevel> variable needs to be localized and set to 0 inside a
294 block BEFORE calling the C<-E<gt>import> subroutine on the package.
295  
296 sub _reexport {
297 my ($package, $target, @names)=@_;
298  
299 if(SOME_CONDITION){
300 {
301 # In an localised block, reset the export level
302 local $Exporter::ExportLevel=0;
303 require Sub::Module;
304 require Another::Module;
305 }
306  
307 Sub::Module->import;
308 Another::Mod->import(@names);
309  
310 }
311 }
312  
313 =head2 Reexport Super Class Symbols
314  
315 Any exported symbols from the inheritance chain can be reexported in the same
316 manner, as long as they are package subroutines and not methods:
317  
318 eg
319  
320 package ModChild;
321 parent ModParent;
322  
323 # or
324
325 class ModChild :isa(ModParent)
326  
327
328 sub _reexport {
329 my ($package, $target, @names)=@_;
330 $package->SUPER::import(@names);
331 }
332  
333  
334 =head1 COMPARISON TO OTHER MODULES
335  
336 L<Import::Into> Provides clean way to reexport symbols, though you will have to
337 roll your own 'normal' export of symbols from you own package.
338  
339 L<Import::Base> Requires a custom package to group the imports and reexports
340 them. This is a different approach and might better suit your needs.
341  
342  
343 Reexporting symbols with C<Exporter> directly is a little cumbersome. You
344 either need to import everything into you module name space (even if you don't
345 need it) and then reexport from there. Alternatively you can import directly
346 into a package, but you need to know at what level in the call stack it is.
347 This is exactly what this module addresses.
348  
349  
350 =head1 REPOSITOTY and BUGS
351  
352 Please report and feature requests or bugs via the github repo:
353  
354 L<https://github.com/drclaw1394/perl-export-these.git>
355  
356 =head1 AUTHOR
357  
358 Ruben Westerberg, E<lt>drclaw@mac.comE<gt>
359  
360 =head1 COPYRIGHT AND LICENSE
361  
362 Copyright (C) 2023 by Ruben Westerberg
363  
364 Licensed under MIT
365  
366 =head1 DISCLAIMER OF WARRANTIES
367  
368 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
369 INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
370 FITNESS FOR A PARTICULAR PURPOSE.
371  
372 =cut
373  
↑ Summary