blib/lib/Import/These.pm

filter: Statementstmt 95.0% 57 / 60 Branchbran 90.6% 29 / 32 Conditioncond 75.0% 6 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 90.9% 101 / 111 SCAR 30.1
CC
20
Cov
92%
import
20.3
BEGIN
1.0
BEGIN
1.0
CRAP 20.2
↑ Summary
1 package Import::These;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use feature "say";
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 11 our $VERSION = 'v0.1.1';
9  
10 # Marker is pushed to end of argument list to aid in processing.
11 # Make random to prevent collisions.
12 #
13 11 my $marker=join "", map int rand 26, 1..16;
14  
15  
16 sub import {
17 11 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 13 ▶ my $package=shift;
Subroutine import: called CC=20 SCAR=30.1
20  
21 13 my $prefix="";
22 13 my $next_prefix="";
23 13 my $k;
24 my $version;
25 13 my $mod;
26 13 my $next_mod;
27 13 my $list;
28  
29  
30 13 push @_, $marker;
31
32 # Save the target export level
33 13 ▶ my $target_level=($Exporter::ExportLevel||0)+1;
Condition50%1/2
$Exporter::ExportLevel || 0
l!l
013
MC/DC0%0/1
$Exporter::ExportLevel || 0
$Exporter::ExportLevel
Truth table50%1/2
A
A= $Exporter::ExportLevel
Aresult
11
00
34  
35 13 my $i=0;
36 13 ▶ while(@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 44 11 55
37 44 $k=shift;
38  
39 # Check if the next item is a ref, or a scalar
40 #
41 44 my $r=ref $k;
42 44 ▶ if($r eq "ARRAY"){
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 7 37 44
elsif ($r eq '') { } 37 0 37
43 7 $list=$k;
44 }
45  
46 elsif($r eq ""){
47 37 ▶ if($k eq $marker){
Branch: 5 branches
BranchTrueFalseTotal
if ($k eq $marker) { } 11 26 37
elsif ($k eq '::') { } 1 25 26
elsif ($k =~ /^::.*?::$/) { } 2 23 25
elsif ($k =~ /::$/) { } 7 16 23
elsif ($k =~ /^v|^\d/) { } 4 12 16
48 }
49 elsif($k eq "::"){
50 # Exact. Clear prefix
51 1 $next_prefix="";
52 }
53 elsif($k =~ /^::.*?::$/){
54 # Double ended. Append new portion to prefix
55 2 $next_prefix=$prefix.substr $k, 2;
56 }
57 elsif($k =~/::$/){
58 # At end. Update prefix
59 7 $next_prefix=$k;
60 }
61 elsif($k =~ /^v|^\d/){
62 # Test if it looks like a version.
63 4 ▶ unless($i){
Branch: 1 branch
BranchTrueFalseTotal
if ($i) { } 3 1 4
64 # Actually a perl version
65 1 ▶ eval "use $k";
Subroutine BEGIN: called CC=1 SCAR=0.4
66 1 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 0 1
67 0 next;
68 }
69 else {
70 3 $version=$k;
71 }
72 }
73 else {
74 # Module name
75 12 $next_mod=$k;
76 }
77 }
78 else {
79 0 die "Scalar or array ref only";
80 }
81  
82 # Attempt to execute/load the current state
83
84 43 ▶ if($mod){
Branch: 1 branch
BranchTrueFalseTotal
if ($mod) 12 31 43
85 #say STDERR "REQUIRE $prefix$mod";
86 # Force export level to 0 so any imports of required package are
87 # relative to required package
88 12 local $Exporter::ExportLevel=0;
89 12 eval "require $prefix$mod;";
90 12 ▶ die "Could not require $prefix$mod: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 12 12
91  
92 # After the package has been required, set the target level for import
93 #
94 12 local $Exporter::ExportLevel=$target_level;
95  
96 12 ▶ if($version){
Branch: 1 branch
BranchTrueFalseTotal
if ($version) 3 9 12
97 3 "$prefix$mod"->VERSION($version);
98 }
99  
100 11 ▶ if($list and @$list){
Branch: 2 branches
BranchTrueFalseTotal
if ($list and @$list) { } 5 6 11
elsif ($list and @$list == 0) { } 1 5 6
Condition100%3/3
$list and @$list
!ll&&!rl&&r
515
MC/DC100%2/2
$list and @$list
$list @$list
Truth table100%3/3
A and B
A= $listB= @$list
ABresult
0X0
100
111
101 # List import
102 5 "$prefix$mod"->import(@$list);
103 }
104 ▶ elsif($list and @$list ==0){
Condition66%2/3
$list and @$list == 0
!ll&&!rl&&r
501
MC/DC50%1/2
$list and @$list == 0
$list @$list == 0
Truth table66%2/3
A and B
A= $listB= @$list == 0
ABresult
0X0
100
111
105 # no not import
106 }
107 else {
108 # Default import
109 5 "$prefix$mod"->import();
110 }
111 }
112
113 # Update the state
114 42 $mod=$next_mod;
115  
116 42 $next_mod=undef;
117 42 ▶ $prefix=$next_prefix if $next_prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $next_prefix 9 33 42
118 42 $next_prefix=undef;
119 42 $list=undef;
120 42 $version=undef;
121 42 $i++;
122 }
123 }
124  
125 11 __PACKAGE__;
126  
127 =head1 NAME
128  
129 Import::These - Terse, Prefixed and Multiple Imports with a Single Statement
130  
131 =head1 SYNOPSIS
132  
133 Any item ending with :: is a prefix. Any later items in the list will use the
134 prefix to create the full package name:
135  
136 #Instead of this:
137 #
138 use Plack::Middleware::Session;
139 use Plack::Middleware::Static;
140 use Plack::Middleware::Lint;
141 use IO::Compress::Gzip;
142 use IO::Compress::Gunzip;
143 use IO::Compress::Deflate;
144 use IO::Compress::Inflate;
145  
146  
147 # Do this
148 use Import::These qw<
149 Plack::Middleware:: Session Static Lint
150 IO::Compress:: Gzip GunZip Defalte Inflate
151 >;
152  
153  
154  
155 Any item exactly equal to :: clears the prefix:
156  
157 use Import::These "Prefix::", "Mod", "::", "Prefix::Another";
158 # Prefix::Mod
159 # Prefix::Another;
160  
161  
162 A item beginning with :: and ending with :: appends the item to the prefix:
163  
164 use Import::These "Plack::", "Test", "::Middleware::", "Lint";
165 # Plack::Test,
166 # Plack::Middleware::Lint;
167  
168  
169 Supports default, named/tagged, and no import:
170  
171 # Instead of this:
172 #
173 # use File::Spec::Functions;
174 # use File::Spec::Functions "catfile";
175 # use File::Spec::Functions ();
176  
177 # Do This:
178 #
179 use Import::These "File::Spec::", Functions,
180 Functions=>["catfile"],
181 Functions=>[]
182  
183 Supports Perl Version as first argument to list
184  
185 use Import::These qw<v5.36 Plack:: Test ::Middleware:: Lint>;
186 # use v5.36;
187 # Plack::Test,
188 # Plack::Middleware::Lint;
189  
190 Supports Module Version
191  
192 use Import::These qw<File::Spec:: Functions 1.3>;
193 # use File::Spec::Functions 1.3;
194 #
195 use Import::These qw<File::Spec:: Functions 1.3>, ["catfile"];
196 # use File::Spec::Functions 1.3 "catfile";
197
198 =head1 DESCRIPTION
199  
200 A tiny module for importing multiple modules in one statement utilising a
201 prefix. The prefix can be set, cleared, or appended multiple times in a list,
202 making long lists of imports much easier to type!
203  
204 It works with any package providing a C<import> subroutine (i.e. compatible
205 with L<Exporter>. It also is compatible with recursive exporters such as
206 L<Export::These> manipulating the export levels.
207  
208  
209 =head1 USAGE
210  
211 When using this pragma, the list of arguments are interpreted as either a Perl
212 version, prefix mutation, module name, module version or array ref of symbols
213 to import. The current value of the prefix is applied to module names as they
214 appear in the list.
215  
216  
217 =over
218  
219 =item The prefix always starts out as an empty string.
220  
221 =item The first item in the list is optionally a Perl version
222  
223 =item Module version optionally comes after a module name (prefixed or not)
224  
225 =item Symbols list optionally comes after a module name or module version if used
226  
227 =item The prefix can be set/cleared/appended as many times as needed
228  
229 =back
230  
231 =head2 Prefix Manipulation
232  
233 The current prefix is used for all module names as they occur. However, changes
234 to the prefix can be interleaved within module names.
235  
236  
237 =head3 Set the Prefix
238  
239 Name::
240  
241 # Prefix equals "Name::"
242  
243 Any item in the list ending in "::" with result in the prefix being set to item (including the ::)
244  
245 =head3 Append The Prefix
246  
247 ::Name::
248  
249 # Prefix equals "OLDPREFIX::Name::"
250
251 Any item in the list starting and ending with "::" will result in the prefix
252 having the item appended to it. The item has the leading "::" removed before
253 appending.
254  
255  
256 =head3 Clear the Prefix
257  
258 ::
259
260 #Prefix is ""
261
262 Any item in the list equal to "::" exactly will clear the prefix to an empty
263 string
264  
265 =head1 EXAMPLES
266  
267 The following examples make it easier to see the benefits of using this module:
268  
269 =head2 Simple Prefix
270  
271 A single prefix used for multiple packages:
272  
273 use Import::These qw<IO::Compress:: Gzip GunZip Defalte Inflate >;
274  
275 # Equivalent to:
276 # use IO::Compress::Gzip
277 # use IO::Compress::GunZip
278 # use IO::Compress::Deflate
279 # use IO::Compress::Inflate
280  
281 =head2 Appending Prefix
282  
283 Prefix is appended along the way:
284  
285 use Import::These qw<IO:: File ::Compress:: Gzip GunZip Defalte Inflate >;
286
287 # Equivalent to:
288 # use IO::File
289 # use IO::Compress::Gzip
290 # use IO::Compress::GunZip
291 # use IO::Compress::Deflate
292 # use IO::Compress::Inflate
293  
294 =head2 Reset Prefix
295  
296 Completely change (reset) prefix to something else:
297  
298 use Import::These qw<File::Spec Functions :: Compress:: Gzip GunZip Defalte Inflate >;
299  
300 # Equivalent to:
301 # use File::Spec::Functions
302 # use IO::Compress::Gzip
303 # use IO::Compress::GunZip
304 # use IO::Compress::Deflate
305 # use IO::Compress::Inflate
306  
307  
308 =head2 No Default Import
309  
310 use Import::These "File::Spec::", "Functions"=>[];
311  
312 # Equivalent to:
313 # use File::Spec::Functions ();
314
315 =head2 Import Names/groups
316  
317 use Import::These "File::Spec::", "Functions"=>["catfile"];
318  
319 # Equivalent to:
320 # use File::Spec::Functions ("catfile");
321  
322  
323 =head2 With Perl Version
324  
325 use Import::These "v5.36", "File::Spec::", "Functions";
326  
327 # Equivalent to:
328 # use v5.36;
329 # use File::Spec::Functions;
330  
331 =head2 With Module Version
332  
333 use Import::These "File::Spec::", "Functions", "v1.2";
334  
335 # Equivalent to:
336 # use File::Spec::Functions v1.2;
337  
338  
339 =head2 All Together Now
340  
341 use Import::These qw<v5.36 File:: IO ::Spec:: Functions v1.2>, ["catfile"], qw<:: IO::Compress:: Gzip GunZip Deflate Inflate>;
342  
343 # Equivalent to:
344 # use v5.36;
345 # use File::IO;
346 # use File::Spec::Functions v1.2 "catfile"
347 # use IO::Compress::Gzip;
348 # use IO::Compress::GunZip;
349 # use IO::Compress::Deflate;
350 # use IO::Compress::Inflate;
351  
352  
353 =head1 COMPARISON TO OTHER MODULES
354  
355 L<Import::Base> Performs can perform multiple imports, however requires a
356 custom package to group the imports and reexport them. Does not support
357 prefixes.
358  
359 L<use> is very similar however does not support prefixes.
360  
361  
362 L<import> works by loading ALL packages under a common prefix. Whether you need
363 them or not. That could be a lot of disk access and memory usage.
364  
365 L<modules> has automatic module installation using CPAN. However no
366 prefix support and uses B<a lot> of RAM for basic importing
367  
368 L<Importer> has some nice features but not a 'simple' package prefix. It also
369 looks like it only handles a single package per invocation
370  
371 =head1 REPOSITOTY and BUGS
372  
373 Please report and feature requests or bugs via the github repo:
374  
375 L<https://github.com/drclaw1394/perl-import-these.git>
376  
377 =head1 AUTHOR
378  
379 Ruben Westerberg, E<lt>drclaw@mac.comE<gt>
380  
381 =head1 COPYRIGHT AND LICENSE
382  
383 Copyright (C) 2023 by Ruben Westerberg
384  
385 Licensed under MIT
386  
387 =head1 DISCLAIMER OF WARRANTIES
388  
389 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
390 INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
391 FITNESS FOR A PARTICULAR PURPOSE.
392  
393 =cut
394
395  
↑ Summary