blib/lib/Method/Autoload.pm

filter: Statementstmt 92.0% 58 / 63 Branchbran 57.6% 15 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 82.4% 94 / 114 CC 16 SCAR 12.5
CC
16
Cov
80%
AUTOLOAD
7.5
autoload
4.1
autoloaded
3.1
CRAP 3.5
↑ Summary
1 package Method::Autoload;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 our $VERSION='0.02';
7 6 our $AUTOLOAD;
8  
9 =head1 NAME
10  
11 Method::Autoload - Autoloads methods from a list of packages into the current package
12  
13 =head1 SYNOPSIS
14  
15 package MyPackage;
16 use base qw{Method::Autoload}
17  
18 =head1 DESCRIPTION
19  
20 The Method::Autoload base class package is used to autoload methods from a list of packages where you may not know what methods are available until run time. A good use of this package is programming support for user contributed packages or user contributed plugins.
21  
22 =head1 USAGE
23  
24 use MyPackage;
25 my $object=MyPackage->new(%hash); #provides new and initialize methods
26 $object->pushPackages("My::Bar"); #appends to "packages" array
27 $object->unshiftPackages("My::Foo"); #prepends to "packages" array
28  
29 use MyPackage;
30 my $object=MyPackage->new(packages=>["My::Foo", "My::Bar"]);
31 $object->foo; #from My::Foo
32 $object->bar; #from My::Bar
33  
34 =head1 CONSTRUCTOR
35  
36 =head2 new
37  
38 my $object=MyPackage->new(%hash);
39 my $object=MyPackage->new(package=>["My::Package1", "My::Package2"]);
40  
41 =cut
42  
43 6 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
44 6 my $this=shift;
45 6 ▶ my $class=ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
46 6 my $self={};
47 6 bless $self, $class;
48 6 $self->initialize(@_);
49 6 return $self;
50 }
51  
52 =head2 initialize
53  
54 =cut
55  
56 6 ▶sub initialize {
Subroutine initialize: called CC=1 SCAR=0.0
Pod documented
57 6 my $self=shift;
58 6 %$self=@_;
59 }
60  
61 =head1 METHODS PUBLIC
62  
63 =head2 packages
64  
65 Returns the current list of packages in the "packages" array.
66  
67 my @package=$object->packages; #()
68 my $package=$object->packages; #[]
69  
70 =cut
71  
72 25 ▶sub packages {
Subroutine packages: called CC=3 SCAR=11.0
Pod documented
73 25 my $self=shift;
74 25 ▶ $self->{"packages"}=[] unless ref($self->{"packages"}) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"packages"} eq "ARRAY" 0 25 25
75 25 ▶ return wantarray ? @{$self->{"packages"}} : $self->{"packages"};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 19 25
76 }
77  
78 =head2 pushPackages
79  
80 Pushes packages on to the "packages" array.
81  
82 $object->pushPackages("My::Bar");
83 $object->pushPackages(@packages);
84  
85 =cut
86  
87 1 ▶sub pushPackages {
Subroutine pushPackages: called CC=2 SCAR=7.0
Pod documented
88 1 my $self=shift;
89 1 ▶ push @{$self->packages}, @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
90 1 return $self->packages;
91 }
92  
93 =head2 unshiftPackages
94  
95 Unshifts packages on to the "packages" array. Use this if you want to override a "default" package. Please use with care.
96  
97 $object->unshiftPackages("My::Foo");
98 $object->unshiftPackages(@packages);
99  
100 =cut
101  
102 1 ▶sub unshiftPackages {
Subroutine unshiftPackages: called CC=2 SCAR=7.0
Pod documented
103 1 my $self=shift;
104 1 ▶ unshift @{$self->packages}, @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
105 1 return $self->packages;
106 }
107  
108 =head2 autoloaded
109  
110 Returns a hash of autoloaded methods and the classes that they came from.
111  
112 my %hash=$object->autoloaded; #()
113 my $hash=$object->autoloaded; #{}
114  
115 =cut
116  
117 10 ▶sub autoloaded {
Subroutine autoloaded: called CC=3 SCAR=11.4
Pod documented
118 10 my $self=shift;
119 10 ▶ $self->{"autoloaded"}={} unless ref($self->{"autoloaded"}) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"autoloaded"} eq "HASH" 3 7 10
120 0 ▶ return wantarray ? @{$self->{"autoloaded"}} : $self->{"autoloaded"};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 10 10
121 }
122  
123 =head1 METHODS PRIVATE
124  
125 =head2 DESTROY ("Global" method)
126  
127 We define DESTROY in this package so that it does not call AUTOLOAD but you may overload this method in your package, if you need it.
128  
129 =cut
130  
131 8 ▶sub DESTROY {return "0E0"};
Subroutine DESTROY: called CC=1 SCAR=0.0
132  
133 =head2 AUTOLOAD ("Global" method)
134  
135 AUTOLOAD is a "global" method. Please review the limitations on inheriting this method.
136  
137 =cut
138  
139 6 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=6 SCAR=20.2
140 6 my $self=shift;
141 6 my $method=$AUTOLOAD;
142 6 $method=~s/.*://;
143 #warn sprintf("Autoloading Method: %s\n", $method);
144 6 foreach my $class ($self->packages) {
145 8 ▶ if ($class->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can($method)) { } 6 2 8
146 #warn(sprintf(qq{Package "%s" is loaded and method "%s" is supported\n}, $class, $method));
147 6 $self->autoload($class, $method);
148 6 last; #for performance and in case another package defines method.
149 } else {
150 #warn sprintf("Loading Package: %s\n", $class);
151 2 $class->use;
152 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 2 0 2
153 #warn(sprintf(qq{Warning: Failed to use package "%s". Is it installed?\n}, $class));
154 } else {
155 0 ▶ if ($class->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can($method)) 0 0 0
156 0 $self->autoload($class, $method);
157 0 last; #for performance and in case another package defines method.
158 }
159 }
160 }
161 }
162 die(sprintf(qq{Error: Could not autoload method "%s" from packages %s.\n},
163 0 ▶ $method, join(", ", map {qq{"$_"}} $self->packages)))
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($method) 0 6 6
164 unless $self->can($method);
165 6 return $self->$method(@_);
166 }
167  
168 =head2 autoload
169  
170 my $subref=$object->autoload($class, $method);
171  
172 =cut
173  
174 6 ▶sub autoload {
Subroutine autoload: called CC=4 SCAR=14.1
Pod documented
175 6 my $syntax=q{Error: autoload syntax $obj->autoload($class, $method)};
176 6 ▶ my $self=shift or die($syntax);
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or die $syntax 6 0 6
177 6 ▶ my $class=shift or die($syntax);
Branch: 1 branch
BranchTrueFalseTotal
my $class = shift() or die $syntax 6 0 6
178 6 ▶ my $method=shift or die($syntax);
Branch: 1 branch
BranchTrueFalseTotal
my $method = shift() or die $syntax 6 0 6
179 6 my $sub=join("::", $class, $method);
180 #warn sprintf(qq{Importing method "%s" from class "%s"\n}, $method, $class);
181 6 $self->autoloaded->{$method}=$class;
182 6 ▶ no strict qw{refs};
Subroutine BEGIN: called CC=1 SCAR=0.0
183 6 return *$method=\&{$sub};
184 }
185  
186 =head1 BUGS
187  
188 DavisNetworks.com provides support services for all Perl applications including this package.
189  
190 =head1 SUPPORT
191  
192 =head1 AUTHOR
193  
194 Michael R. Davis
195 CPAN ID: MRDVT
196 STOP, LLC
197 domain=>michaelrdavis,tld=>com,account=>perl
198 http://www.stopllc.com/
199  
200 =head1 COPYRIGHT
201  
202 This program is free software licensed under the...
203  
204 The BSD License
205  
206 The full text of the license can be found in the LICENSE file included with this module.
207  
208 =head1 SEE ALSO
209  
210 L<Class::Std> AUTOMETHOD method,
211  
212 =cut
213  
214 6 1;
↑ Summary