blib/lib/later.pm

filter: Statementstmt 95.7% 67 / 70 Branchbran 75.0% 18 / 24 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 89.9% 98 / 109 CC 15 SCAR 12.7
CC
15
Cov
90%
_autoload
6.4
import
5.0
_use_module
5.0
CRAP 3.6
↑ Summary
1 #
2 # $Id: later.pm,v 1.9 2007-01-25 15:51:10 erwan Exp $
3 #
4 # postpone using a module until it is needed at runtime
5 #
6 # 2007-01-09 erwan First version
7 # 2007-01-22 erwan Support import arguments and object orientation
8 # 2007-01-23 erwan Support recursive 'use later' calls
9 # 2007-01-25 erwan More pod + debug messages
10 # 2014-04-23 aelshesh Patch to support autoload subs
11 #
12  
13 package later;
14  
15 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 9 our $VERSION = '0.05';
23  
24 10 ▶use vars qw($DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 10 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
26 10 ▶ $DEBUG ||= 0;
Condition50%1/2
$later::DEBUG ||= 0
l!l
010
MC/DC0%0/1
$later::DEBUG ||= 0
$later::DEBUG
Truth table50%1/2
A
A= $later::DEBUG
Aresult
11
00
27 }
28  
29 #--------------------------------------------------------------------
30 #
31 # '%modules' has the following structure:
32 #
33 # # name of the package doing 'use later'
34 # %modules = ( 'caller_package' => {
35 # # module to be imported in the caller_package, and with which import arguments
36 # 'module_1' => [ @import_arguments ],
37 # 'module_2' => [ @import_arguments ],
38 # ...
39 # }
40 #
41  
42 9 my %modules;
43  
44 #--------------------------------------------------------------------
45 #
46 # import - store the name and import arguments of a module to
47 # import later on in the caller package
48 #
49  
50 13 ▶sub import {
Subroutine import: called CC=5 SCAR=16.1
51 13 my $self = shift @_;
52 13 my $caller = caller(0);
53 13 my $latered = shift @_;
54  
55 13 ▶ croak "'use later' must be followed by a module name and eventual import arguments" if (!defined $latered);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $latered 1 12 13
56  
57 # NOTE: should we check that the $latered module exists? -> no: enable on the fly generation of modules...
58 # NOTE: if the caller package or the delayed package define an AUTOLOAD of their own, we will have a conflict...
59  
60 # add an AUTOLOAD to the caller module
61 12 ▶ if (!exists $modules{$caller}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $modules{$caller}) 11 1 12
62 11 $modules{$caller} = {};
63 11 ▶ print "use later: setting AUTOLOAD in package $caller\n" if ($DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $later::DEBUG 0 11 11
64 11 *{ qualify_to_ref('AUTOLOAD',$caller) } = *{ qualify_to_ref('_autoload','later') };
65 }
66  
67 12 $modules{$caller}->{$latered} = \@_;
68  
69 # add an AUTOLOAD even to the postponed module, to handle full name calls (ex: My::Module::foo() )
70 12 ▶ print "use later: setting AUTOLOAD in package $latered\n" if ($DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $later::DEBUG 0 12 12
71 12 *{ qualify_to_ref('AUTOLOAD',$latered) } = *{ qualify_to_ref('_autoload','later') };
72 }
73  
74 #--------------------------------------------------------------------
75 #
76 # _autoload - act as the caller package's AUTOLOAD. does a number
77 # of 'use $module' and call the undefined sub if it now defined
78 # otherwise die
79 #
80  
81 14 ▶sub _autoload {
Subroutine _autoload: called CC=6 SCAR=18.5
82  
83 # looking up a missing DESTROY sub is ok...
84 14 ▶ return if ($later::AUTOLOAD =~ /::DESTROY$/);
Branch: 1 branch
BranchTrueFalseTotal
if $later::AUTOLOAD =~ /::DESTROY$/ 1 13 14
85  
86 13 my ($pkg,$filename,$line) = caller;
87  
88 # do a 'use module' on each postponed module inside the caller's package
89 13 ▶ if (exists $modules{$pkg}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $modules{$pkg}) 13 0 13
90 13 foreach my $module (keys %{$modules{$pkg}}) {
91 10 _use_module($pkg,$module,@{$modules{$pkg}->{$module}});
92 8 delete $modules{$pkg}->{$module};
93 8 ▶ if ($module->can('autoload')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module->can("autoload")) 0 8 8
94 0 *{ qualify_to_ref('AUTOLOAD', $module) } = *{ qualify_to_ref('autoload', $module) };
95 }
96
97 }
98 }
99  
100 # if the originally undefined sub is now available, call it
101 11 ▶ if ($pkg->can($later::AUTOLOAD)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg->can($later::AUTOLOAD)) 8 3 11
102 8 goto &$later::AUTOLOAD;
103 }
104
105 # sub is still undefined. let's die...
106 3 die "Undefined subroutine &$later::AUTOLOAD called at $filename line $line\n";
107 }
108  
109 #--------------------------------------------------------------------
110 #
111 # _use_module - use a module with the right parameters
112 #
113  
114 10 ▶sub _use_module {
Subroutine _use_module: called CC=5 SCAR=16.1
115 10 my ($caller,$module,@args) = @_;
116  
117 10 ▶ print "use later: using module $module in package $caller\n" if ($DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $later::DEBUG 0 10 10
118  
119 # the main issue with the delayed 'eval "package; use *"' method is how
120 # to pass import arguments to the used module. I unfortunately didn't
121 # find any better way than converting them to a string with Data::Dumper.
122 # ugly. does not keep coderefs passed as import arguments...
123  
124 # change Data::Dumper indentation temporarily
125 10 my $indent = $Data::Dumper::Indent;
126 10 $Data::Dumper::Indent = 0;
127  
128 10 my $str = "";
129 10 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 3 7 10
130 3 $str = Dumper(\@args);
131 3 ▶ $str =~ s/^\$VAR1 = \[(.*)\];[\n\r]*$/$1/mg
Branch: 1 branch
BranchTrueFalseTotal
$str =~ s/^\$VAR1 = \[(.*)\];[\n\r]*$/$1/gm or croak("use later: Failed to parse import arguments for module $module inside package $caller. Arguments are: $str") 3 0 3
132 || croak "use later: Failed to parse import arguments for module $module inside package $caller. Arguments are: $str";
133 }
134 10 $Data::Dumper::Indent = $indent;
135  
136 # now use the delayed module
137 10 ▶ eval "package $caller; use $module $str;";
Subroutine BEGIN: called CC=1 SCAR=0.0
138 10 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 8 10
139 2 croak "use later: Failed to use package $module inside package $caller.\nEval says: $@";
140 }
141 }
142  
143 9 1;
144  
145 __END__
↑ Summary