lib/Badger/Class.pm

filter: Statementstmt 93.5% 462 / 494 Branchbran 63.9% 119 / 186 Conditioncond 56.4% 44 / 78 MC/DCmcdc 20.0% 10 / 50 Subroutinesub 88.1% 97 / 110 Podpod 100% 48 / 48 totaltotal 80.7% 780 / 966 CC 132 SCAR 14.1
CC
132
Cov
82%
mixins
10.8
all_vars
8.6
alias
8.2
CRAP 4.1
« lib/Badger/Filesystem/Directory.pm ↑ Summary blib/lib/Badger/Apps.pm »
1 #========================================================================
2 #
3 # Badger::Class
4 #
5 # DESCRIPTION
6 # Module implementing metaclass functionality for composing classes
7 # (equivalent to C<use base>) and other class-related actions.
8 #
9 # AUTHOR
10 # Andy Wardley <abw@wardley.org>
11 #
12 #========================================================================
13  
14 package Badger::Class;
15  
16 88 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 81 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 77 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 75 ▶use base 'Badger::Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 use constant {
21 72 base_id => 'Badger',
22 BCLASS => 'Badger::Class',
23 FILESYSTEM => 'Badger::Filesystem',
24 CONSTANTS => 'Badger::Constants',
25 EXPORTER => 'Badger::Exporter',
26 MIXIN => 'Badger::Mixin',
27 CODECS => 'Badger::Codecs',
28 UTILS => 'Badger::Utils',
29 DEBUGGER => 'Badger::Debug',
30 CONFIG => 'Badger::Class::Config',
31 METHODS => 'Badger::Class::Methods',
32 VARS => 'Badger::Class::Vars',
33 MESSAGES => 'MESSAGES',
34 VERSION => 'VERSION',
35 MIXINS => 'MIXINS',
36 THROWS => 'THROWS',
37 ISA => 'ISA',
38 NO_VALUE => "You didn't specify a value for the '%s' option",
39 74 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
40 use Badger::Constants
41 73 ▶ 'DELIMITER SCALAR ARRAY HASH CODE PKG REFS ONCE TRUE FALSE LOADED';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 use overload
43 70 '""' => 'name',
44 73 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 70 our $VERSION = 0.01;
47 70 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Badger::Class::DEBUG 70 0 70
48 70 our $LOADED = { };
49  
50 BEGIN {
51 # generate a compile time constant from $DEBUG
52 72 ▶ *DEBUG = sub() { $DEBUG };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
53 }
54  
55 #-----------------------------------------------------------------------
56 # Methods that we delegate to other modules. The module name is
57 # determined by calling the constant method (first argument on RHS
58 # which is auto-quoted by '=>', e.g. 'METHODS', 'ALIASES') against
59 # $self, allowing for sub-classes of Badger::Class to define different
60 # modules for this task. The second argument on the RHS is the method.
61 # The methods are generated a little further on in this module.
62 #-----------------------------------------------------------------------
63  
64 70 our $DELEGATES = {
65 # note the first argument on RHS is quto-quoted by =>
66 accessors => [ METHODS => 'accessors' ],
67 codec => [ CODECS => 'export_codec' ],
68 codecs => [ CODECS => 'export_codecs' ],
69 config => [ CONFIG => 'export' ],
70 constants => [ CONSTANTS => 'export' ],
71 filesystem => [ FILESYSTEM => 'export' ],
72 hash_methods => [ METHODS => 'hash' ],
73 mutators => [ METHODS => 'mutators' ],
74 slots => [ METHODS => 'slots' ],
75 init_method => [ METHODS => 'initialiser' ],
76 auto_can => [ METHODS => 'auto_can' ],
77 utils => [ UTILS => 'export' ],
78 vars => [ VARS => 'vars' ],
79 };
80  
81 70 *get_methods = \&accessors;
82 70 *set_methods = \&mutators;
83  
84  
85 #-----------------------------------------------------------------------
86 # Define exportable items and export hooks (see Badger::Exporter)
87 #-----------------------------------------------------------------------
88  
89 70 our $EXPORT_ANY = ['BCLASS'];
90 70 our $EXPORT_FAIL = \&_export_fail;
91 our $EXPORT_HOOKS = {
92 debug => [\&_debug_hook, 1],
93 dumps => [\&_dumps_hook, 1],
94 70 map { $_ => \&_export_hook }
95 qw(
96 base uber mixin mixins version constant constants words vars
97 config exports throws messages utils codec codecs filesystem
98 hooks methods alias slots accessors mutators get_methods
99 set_methods hash_methods init_method auto_can overload as_text
100 is_true
101 )
102 };
103  
104  
105 sub export {
106 1502 ▶ my ($class, $package, @args) = @_;
Subroutine export: called CC=2 SCAR=6.9
Pod documented
107 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 70 ▶ no warnings ONCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 1502 ▶ ${ $package.PKG.LOADED } ||= 1; # add $BADGER_LOADED to mark our scent
Condition100%2/2
${$package . '::' . "BADGER_LOADED";} ||= 1
l!l
11501
MC/DC100%1/1
${$package . '::' . "BADGER_LOADED";} ||= 1
${$package . '::' . "BADGER_LOADED";}
Truth table100%2/2
A
A= ${$package . '::' . "BADGER_LOADED";}
Aresult
11
00
110 1502 $class->SUPER::export($package, @args);
111 }
112  
113  
114 sub _export_hook {
115 8451 ▶ my ($class, $target, $key, $symbols) = @_;
Subroutine _export_hook: called CC=2 SCAR=7.1
116  
117 8451 ▶ croak sprintf(NO_VALUE, $key)
Branch: 1 branch
BranchTrueFalseTotal
unless @$symbols 0 8451 8451
118 unless @$symbols;
119  
120 8451 class($target, $class)->$key(shift @$symbols);
121 }
122  
123  
124 sub _export_fail {
125 76 ▶ my ($class, $target, $key, $symbols, $import) = @_;
Subroutine _export_fail: called CC=3 SCAR=11.4
126  
127 # look for any additional export hooks defined in $HOOKS, e.g.
128 # by a subclass or poked in via the hooks() method
129 76 ▶ my $hook = class($class)->hash_value( HOOKS => $key ) || return;
Condition50%1/2
Badger::Class::class($class)->hash_value('HOOKS', $key) || (return)
l!l
760
MC/DC0%0/1
Badger::Class::class($class)->hash_value('HOOKS', $key) || (return)
Badger::Class::class($class)->hash_value('HOOKS', $key)
Truth table50%1/2
A
A= Badger::Class::class($class)->hash_value('HOOKS', $key)
Aresult
11
00
130  
131 76 ▶ croak sprintf(NO_VALUE, $key)
Branch: 1 branch
BranchTrueFalseTotal
unless @$symbols 0 76 76
132 unless @$symbols;
133  
134 # We use the two-argument call to class() which tells it that we want
135 # a $class metaclass object rather than the default of Badger::Class.
136 # This is because subclasses may be calling this method so $class isn't
137 # always going to be Badger::Class
138 76 class($target, $class)->$hook(shift @$symbols);
139 }
140  
141  
142 sub _debug_hook {
143 1182 ▶ my ($class, $target, $key, $debug) = @_;
Subroutine _debug_hook: called CC=2 SCAR=7.1
144 1182 ▶ $debug = { default => $debug }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $debug eq "HASH" 1182 0 1182
145 unless ref $debug eq HASH;
146 1182 _autoload($class->DEBUGGER)->export($target, %$debug);
147 }
148  
149 sub _dumps_hook {
150 140 ▶ my ($class, $target, $key, $dumps) = @_;
Subroutine _dumps_hook: called CC=1 SCAR=0.0
151 140 _autoload($class->DEBUGGER)->export($target, dumps => $dumps);
152 }
153  
154  
155  
156 #-----------------------------------------------------------------------
157 # Define a lexical scope to enclose class lookup tables
158 #-----------------------------------------------------------------------
159  
160 # Badger::Class and each of its subclasses have their own metaclass
161 # table mapping class names to objects.
162 70 my $METACLASSES = { };
163  
164 {
165 # class/package name - define this up-front so we can use it below
166 70 sub CLASS {
167 # first argument is object or class name, otherwise return caller
168 131 ▶ @_ ? (ref $_[0] || $_[0])
Subroutine CLASS: called CC=3 SCAR=12.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 130 131
Condition33%1/3
ref $_[0] or $_[0]
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $_[0] or $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A or B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
169 : (caller())[0];
170 }
171  
172 # Sorry if this messes with your head. We want class() and classes()
173 # methods that create Badger::Class objects. However, we also want
174 # Badger::Class to be subclassable (e.g. Badger::Factory::Class), where
175 # class() and classes() return the subclass objects instead of the usual
176 # Badger::Class. So we have an UBER() class method whose job it is to
177 # create the class() and classes() methods for the relevant metaclass
178  
179 sub UBER {
180 # $pkg is the metaclass name, e.g. Badger::Class, but can also be
181 # subclasses, e.g. Badger::Factory::Class
182 101 ▶ my $pkg = shift || __PACKAGE__;
Subroutine UBER: called CC=3 SCAR=11.1
Pod documented
Condition50%1/2
shift() || __PACKAGE__
l!l
1010
MC/DC0%0/1
shift() || __PACKAGE__
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
183  
184 # $CLASSES is a lookup table mapping package names to Badger::Class
185 # objects. We need a new lookup table for each subclass of
186 # Badger::Class, so we reuse/create such a table in $METACLASSES,
187 # indexed by the metaclass name, e.g. Badger::Class, etc.
188 101 ▶ my $CLASSES = $METACLASSES->{ $pkg } ||= { };
Condition50%1/2
$METACLASSES->{$pkg} ||= {}
l!l
0101
MC/DC0%0/1
$METACLASSES->{$pkg} ||= {}
$METACLASSES->{$pkg}
Truth table50%1/2
A
A= $METACLASSES->{$pkg}
Aresult
11
00
189  
190 # We want to keep the class() subroutine as fast as possible as it
191 # gets called often. It's a tiny bit faster to declare a variable
192 # outside the closure and reuse it, rather than defining a new
193 # variable each time the closure is called. Ho hum.
194 101 my $class;
195  
196 # The class() subroutine is used to fetch/create a Badger::Class
197 # object for a package name. The first argument is the class name,
198 # or the caller's package if undefined and we look it up in $CLASSES.
199 # If we get a second argument then we're being asked to lookup an
200 # entry for a subclass of Badger::Class, e.g. Badger::Factory::Class,
201 # so we first lookup the correct $METACLASS table.
202 my $class_sub = sub {
203 12787 ▶ $class = @_ ? shift : (caller())[0];
Subroutine __ANON__: called CC=6 SCAR=18.3
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 12193 594 12787
204 12787 ▶ $class = ref $class || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
2276105110
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
205 return @_
206 ? $METACLASSES->{ $_[0] }->{ $class } ||= $_[0]->new($class)
207 12787 ▶ : $CLASSES->{ $class } ||= $pkg->new($class);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 8599 4188 12787
Condition66%4/6
@_ ? $$METACLASSES{$_[0]}{$class} ||= $_[0]->new($class) : $CLASSES->{$class} ||= $pkg->new($class)
l!l&&r!l&&!r
196752400
$$METACLASSES{$_[0]}{$class} ||= $_[0]->new($class)
l!l&&r!l&&!r
712814710
MC/DC0%0/2
@_ ? $$METACLASSES{$_[0]}{$class} ||= $_[0]->new($class) : $CLASSES->{$class} ||= $pkg->new($class)
@_ ? $$METACLASSES{$_[0]}{$class} ||= $_[0]->new($class) : $CLASSES->{$class} $pkg->new($class)
MC/DC0%0/2
$$METACLASSES{$_[0]}{$class} ||= $_[0]->new($class)
$$METACLASSES{$_[0]}{$class} $_[0]->new($class)
Truth table66%2/3
A ||= B
A= @_ ? $$METACLASSES{$_[0]}{$class} ||= $_[0]->new($class) : $CLASSES->{$class}B= $pkg->new($class)
ABresult
1X1
011
000
Truth table66%2/3
A ||= B
A= $$METACLASSES{$_[0]}{$class}B= $_[0]->new($class)
ABresult
1X1
011
000
208 101 };
209  
210 # The classes() method returns a list of Badger::Class objects for
211 # each class in the inheritance chain, starting with the object
212 # itself, followed by each base class, their base classes, and so on.
213 # As with class(), we use a generator to create a closure for the
214 # subroutine to allow the the class object name to be parameterised.
215 my $classes_sub = sub {
216 2 ▶ $class = shift || (caller())[0];
Subroutine __ANON__: called CC=2 SCAR=8.1
Condition33%1/3
shift() || (caller())[0]
l!l&&r!l&&!r
200
MC/DC0%0/2
shift() || (caller())[0]
shift() (caller())[0]
Truth table33%1/3
A || B
A= shift()B= (caller())[0]
ABresult
1X1
011
000
217 2 $class_sub->($class)->heritage;
218 101 };
219  
220 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
221 70 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
222 101 *{ $pkg.PKG.'CLASS' } = \&CLASS;
223 101 *{ $pkg.PKG.'class' } = $class_sub;
224 101 *{ $pkg.PKG.'bclass' } = $class_sub; # plan B
225 101 *{ $pkg.PKG.'classes' } = $classes_sub;
226 101 *{ $pkg.PKG.'_autoload' } = \&_autoload;
227  
228 101 $pkg->export_any('CLASS', 'class', 'bclass', 'classes');
229 }
230  
231 # call the UBER method to generate class() and classes() for this module
232 70 __PACKAGE__->UBER;
233 }
234  
235  
236  
237 #-----------------------------------------------------------------------
238 # generate additional delegate methods listed in $DELEGATES
239 #-----------------------------------------------------------------------
240  
241 class(CLASS)->methods(
242 map {
243 70 my $info = $DELEGATES->{ $_ };
244 910 my ($module, $method) = @$info;
245 $_ => sub {
246 2404 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
247 2404 _autoload($self->$module)->$method($self->{ name }, @_);
248 2404 return $self;
249 910 };
250 }
251 keys %$DELEGATES
252 );
253  
254  
255 #-----------------------------------------------------------------------
256 # constructor method
257 #-----------------------------------------------------------------------
258  
259 sub new {
260 1711 ▶ my ($class, $package) = @_;
Subroutine new: called CC=2 SCAR=7.1
Pod documented
261 1711 ▶ $package = ref $package || $package;
Condition33%1/3
ref $package || $package
l!l&&r!l&&!r
017110
MC/DC0%0/2
ref $package || $package
ref $package $package
Truth table33%1/3
A || B
A= ref $packageB= $package
ABresult
1X1
011
000
262 70 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
263 bless {
264 name => $package,
265 1711 symbols => \%{"${package}::"},
266 }, $class;
267 }
268  
269 sub id {
270 54 ▶ my $self = shift;
Subroutine id: called CC=5 SCAR=16.3
Pod documented
271 return @_
272 ? $self->{ id } = shift
273 54 ▶ : $self->{ id } ||= do {
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 54 54
Condition66%2/3
@_ ? $self->{"id"} = shift() : $self->{"id"} ||= do { my $pkg = $self->{"name"}; my $base = $self->base_id; if ($base eq $pkg) { $pkg = $1 if $pkg =~ /(\w+)$/; } else { $pkg =~ s/^$base:://; } ; $pkg =~ s/::/./g; lc $pkg }
l!l&&r!l&&!r
19350
MC/DC0%0/2
@_ ? $self->{"id"} = shift() : $self->{"id"} ||= do { my $pkg = $self->{"name"}; my $base = $self->base_id; if ($base eq $pkg) { $pkg = $1 if $pkg =~ /(\w+)$/; } else { $pkg =~ s/^$base:://; } ; $pkg =~ s/::/./g; lc $pkg }
@_ ? $self->{"id"} = shift() : $self->{"id"} do { my $pkg = $self->{"name"}; my $base = $self->base_id; if ($base eq $pkg) { $pkg = $1 if $pkg =~ /(\w+)$/; } else { $pkg =~ s/^$base:://; } ; $pkg =~ s/::/./g; lc $pkg }
Truth table66%2/3
A ||= B
A= @_ ? $self->{"id"} = shift() : $self->{"id"}B= do { my $pkg = $self->{"name"}; my $base = $self->base_id; if ($base eq $pkg) { $pkg = $1 if $pkg =~ /(\w+)$/; } else { $pkg =~ s/^$base:://; } ; $pkg =~ s/::/./g; lc $pkg }
ABresult
1X1
011
000
274 35 my $pkg = $self->{ name };
275 35 my $base = $self->base_id; # base to remove, e.g. Badger
276 35 ▶ if ($base eq $pkg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base eq $pkg) { } 1 34 35
277 1 ▶ $pkg = $1 if $pkg =~ /(\w+)$/; # Badger - Badger --> Badger
Branch: 1 branch
BranchTrueFalseTotal
if $pkg =~ /(\w+)$/ 1 0 1
278 } else {
279 34 $pkg =~ s/^${base}:://; # Badger::X::Y - Badger --> X::Y
280 }
281 35 $pkg =~ s/::/./g; # X::Y --> X.Y
282 35 lc $pkg; # X.Y --> x.y
283 };
284 }
285  
286  
287 #-----------------------------------------------------------------------
288 # methods to access symbol table
289 #-----------------------------------------------------------------------
290  
291 70 *pkg = \&name;
292 49904 ▶sub name { $_[0]->{ name } }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
293 0 ▶sub symbols { $_[0]->{ symbols } }
Subroutine symbols: not called CC=1 SCAR=6.9
Pod documented
294 0 ▶sub symbol { $_[0]->{ symbols }->{ $_[1] } }
Subroutine symbol: not called CC=1 SCAR=6.9
Pod documented
295 0 ▶sub scalar_ref { *{ $_[0]->{ symbols }->{ $_[1] } || return }{ SCALAR } }
Subroutine scalar_ref: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"symbols"}{$_[1]} or return 0 0 0
296 0 ▶sub array_ref { *{ $_[0]->{ symbols }->{ $_[1] } || return }{ ARRAY } }
Subroutine array_ref: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"symbols"}{$_[1]} or return 0 0 0
297 0 ▶sub hash_ref { *{ $_[0]->{ symbols }->{ $_[1] } || return }{ HASH } }
Subroutine hash_ref: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"symbols"}{$_[1]} or return 0 0 0
298 0 ▶sub code_ref { *{ $_[0]->{ symbols }->{ $_[1] } || return }{ CODE } }
Subroutine code_ref: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"symbols"}{$_[1]} or return 0 0 0
299 0 ▶sub glob_ref { *{ $_[0]->{ symbols }->{ $_[1] } || return }{ GLOB } }
Subroutine glob_ref: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"symbols"}{$_[1]} or return 0 0 0
300 0 ▶sub scalar { ${ scalar_ref(@_) || return } }
Subroutine scalar: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
scalar_ref(@_) or return 0 0 0
301 0 ▶sub array { @{ array_ref(@_) || return } }
Subroutine array: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
array_ref(@_) or return 0 0 0
302 0 ▶sub hash { %{ hash_ref(@_) || return } }
Subroutine hash: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
hash_ref(@_) or return 0 0 0
303  
304 sub import_symbol {
305 930 ▶ my ($self, $symbol, $ref) = @_;
Subroutine import_symbol: called CC=1 SCAR=0.0
Pod documented
306 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
307 70 ▶ no warnings ONCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
308 930 *{ $self->{ name }.PKG.$symbol } = $ref;
309 }
310  
311  
312 #-----------------------------------------------------------------------
313 # methods for accessing class variables that DTRT in subclasses
314 #-----------------------------------------------------------------------
315  
316 sub var {
317 230 ▶ my $self = shift;
Subroutine var: called CC=2 SCAR=6.9
Pod documented
318 230 my $name = shift;
319 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
320 70 ▶ no warnings ONCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
321  
322 # _debug("Looking for $self->{ name }", PKG, $name, " args: ", scalar(@_), " => ", join(', ', @_), "\n");
323 return @_
324 105 ? (${ $self->{name}.PKG.$name } = shift)
325 230 ▶ : ${ $self->{name}.PKG.$name };
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 105 125 230
326 }
327  
328 sub var_default {
329 44 ▶ my ($self, $name, $default) = @_;
Subroutine var_default: called CC=2 SCAR=6.9
Pod documented
330 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
331 70 ▶ no warnings ONCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
332  
333 44 ▶ return ${ $self->{name}.PKG.$name }
Condition66%2/3
${$self->{"name"} . '::' . $name;} ||= $default
l!l&&r!l&&!r
2420
MC/DC0%0/2
${$self->{"name"} . '::' . $name;} ||= $default
${$self->{"name"} . '::' . $name;} $default
Truth table66%2/3
A ||= B
A= ${$self->{"name"} . '::' . $name;}B= $default
ABresult
1X1
011
000
334 ||= $default;
335 }
336  
337 sub any_var {
338 265 ▶ my $self = shift;
Subroutine any_var: called CC=4 SCAR=13.9
Pod documented
339 265 my $name = shift;
340 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
341  
342 # remove any leading '$'
343 265 $name =~ s/^\$//;
344  
345 265 foreach my $pkg ($self->heritage) {
346 595 ▶ _debug("looking for $name in $pkg\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 595 595
347 595 ▶ return ${ $pkg.PKG.$name } if defined ${ $pkg.PKG.$name };
Branch: 1 branch
BranchTrueFalseTotal
if defined ${$pkg . '::' . $name;} 179 416 595
348 }
349  
350 86 return undef;
351 }
352  
353 sub any_var_in {
354 116 ▶ my $self = shift;
Subroutine any_var_in: called CC=7 SCAR=19.5
Pod documented
355 116 ▶ my $names = @_ == 1 ? shift : [@_];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 84 32 116
356 116 my ($pkg, $name);
357 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
358  
359 116 ▶ $names = [ split DELIMITER, $names ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $names eq "ARRAY" 82 34 116
360 unless ref $names eq ARRAY;
361  
362 # remove any leading '$'
363 116 $names = [ map { s/^\$//; $_ } @$names ];
364  
365 116 foreach $pkg ($self->heritage) {
366 245 foreach $name (@$names) {
367 380 ▶ _debug("looking for $name in $pkg\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 380 380
368 380 ▶ return ${ $pkg.PKG.$name } if defined ${ $pkg.PKG.$name };
Branch: 1 branch
BranchTrueFalseTotal
if defined ${$pkg . '::' . $name;} 84 296 380
369 }
370 }
371  
372 32 return undef;
373 }
374  
375 sub all_vars {
376 2078 ▶ my ($self, $name) = @_;
Subroutine all_vars: called CC=8 SCAR=21.5
Pod documented
377 2078 my $pkg = $self->{ name };
378 2078 my ($value, @values);
379 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
380 70 ▶ no warnings ONCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
381  
382 # remove any leading '$'
383 2078 $name =~ s/^\$//;
384  
385 # _debug("all_vars() caller: ", join(', ', caller()), "\n");
386  
387 2078 foreach my $pkg ($self->heritage) {
388 6407 ▶ _debug("looking for $name in ", $pkg || "UNDEF", "\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 6407 6407
Condition0%0/2
$pkg || 'UNDEF'
l!l
00
MC/DC0%0/1
$pkg || 'UNDEF'
$pkg
Truth table0%0/2
A
A= $pkg
Aresult
11
00
389 push(@values, $value)
390 6407 ▶ if defined ($value = ${ $pkg.PKG.$name });
Branch: 1 branch
BranchTrueFalseTotal
if defined($value = ${$pkg . '::' . $name;}) 3730 2677 6407
391 6407 ▶ _debug("got: $value\n") if DEBUG && $value;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() and $value 0 6407 6407
Condition33%1/3
&DEBUG() and $value
!ll&&!rl&&r
640700
MC/DC0%0/2
&DEBUG() and $value
&DEBUG() $value
Truth table33%1/3
A and B
A= &DEBUG()B= $value
ABresult
0X0
100
111
392 }
393  
394 2078 ▶ return wantarray ? @values : \@values;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1849 229 2078
395  
396 }
397  
398 sub list_vars {
399 155 ▶ my $self = shift; # must remove these from @_ here
Subroutine list_vars: called CC=6 SCAR=17.9
Pod documented
400 155 my $name = shift;
401 155 my $vars = $self->all_vars($name);
402 155 my (@merged, $list);
403  
404 # remove any leading '$'
405 155 $name =~ s/^\$//;
406  
407 155 foreach $list (@_, @$vars) { # use whatever is left in @_ here
408 159 ▶ next unless defined $list;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list 72 87 159
409 87 ▶ if (ref $list eq ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $list eq 'ARRAY') { } 66 21 87
410 66 ▶ next unless @$list;
Branch: 1 branch
BranchTrueFalseTotal
unless @$list 0 66 66
411 66 push(@merged, @$list);
412 }
413 else {
414 21 push(@merged, $list);
415 }
416 }
417  
418 # return \@merged;
419  
420 # NOTE TO SELF: this causes problems when doing something like
421 # foo( something_that_calls_list_vars() ) because list_vars assumed
422 # list context when we actually want a scalar ref. Must find where
423 # this is and fix it.
424 155 ▶ return wantarray ? @merged : \@merged;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 43 112 155
425  
426 }
427  
428 sub hash_vars {
429 74 ▶ my $self = shift; # must remove these from @_ here
Subroutine hash_vars: called CC=4 SCAR=14.1
Pod documented
430 74 my $name = shift;
431 74 my $vars = $self->all_vars($name);
432 74 my (%merged, $hash);
433  
434 # remove any leading '$'
435 74 $name =~ s/^\$//;
436  
437 # reverse the package vars so we get base classes first, followed by subclass,
438 # then we add any additional arguments on as well in the order specified
439 74 foreach $hash ( reverse(@$vars), @_ ) {
440 141 ▶ next unless defined $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash 95 46 141
441 46 ▶ unless (ref $hash eq HASH) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $hash eq "HASH") 0 46 46
442 0 warn "Ignoring $name configuration option (not a hash ref): $hash\n";
443 0 next;
444 }
445 46 @merged{ keys %$hash } = values %$hash;
446 }
447  
448 74 return \%merged;
449 }
450  
451 sub hash_value {
452 1847 ▶ my ($self, $name, $item, $default) = @_;
Subroutine hash_value: called CC=4 SCAR=14.2
Pod documented
453  
454 # remove any leading '$'
455 1847 $name =~ s/^\$//;
456  
457 # _debug("hash_value() caller: ", join(', ', caller()), "\n");
458  
459 1847 foreach my $hash ($self->all_vars($name)) {
460 1852 ▶ next unless ref $hash eq HASH;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 1852 1852
461 return $hash->{ $item }
462 1852 ▶ if defined $hash->{ $item };
Branch: 1 branch
BranchTrueFalseTotal
if defined $hash->{$item} 1847 5 1852
463 }
464  
465 0 return $default;
466 }
467  
468  
469 #-----------------------------------------------------------------------
470 # Methods to return immediate parent classes and all ancestor classes.
471 #-----------------------------------------------------------------------
472  
473 sub parents {
474 718 ▶ my $self = shift;
Subroutine parents: called CC=4 SCAR=14.2
Pod documented
475 718 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
71800
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
476 718 my $pkg = $self->{ name };
477 718 ▶ my $parents = $self->{ parents } ||= do {
Condition66%2/3
$self->{"parents"} ||= do { no strict 'refs'; _autoload($pkg); [map({Badger::Class::class($_);} @{$pkg . '::' . "ISA";})] }
l!l&&r!l&&!r
2414770
MC/DC0%0/2
$self->{"parents"} ||= do { no strict 'refs'; _autoload($pkg); [map({Badger::Class::class($_);} @{$pkg . '::' . "ISA";})] }
$self->{"parents"} do { no strict 'refs'; _autoload($pkg); [map({Badger::Class::class($_);} @{$pkg . '::' . "ISA";})] }
Truth table66%2/3
A ||= B
A= $self->{"parents"}B= do { no strict 'refs'; _autoload($pkg); [map({Badger::Class::class($_);} @{$pkg . '::' . "ISA";})] }
ABresult
1X1
011
000
478 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
479  
480 # make sure the module is loaded before we go looking at its @ISA
481 477 _autoload($pkg);
482 [
483 406 map { class($_) } # parents are immediate
484 477 @{ $pkg.PKG.ISA } # superclasses defined in @ISA
485 ];
486 };
487  
488 return wantarray
489 718 ▶ ? @$parents
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 718 718
490 : $parents;
491 }
492  
493 sub heritage {
494 2488 ▶ my $self = shift;
Subroutine heritage: called CC=5 SCAR=16.2
Pod documented
495 2488 ▶ my $heritage = $self->{ heritage } ||= do {
Condition66%2/3
$self->{"heritage"} ||= do { my(@pending) = $self; my(%seen, $item, @order); while (@pending) { next unless defined($item = pop @pending); unshift @order, $item; push @pending, reverse(@{$item->parents;}); } ; [reverse(grep({not $seen{$_}++;} @order))] }
l!l&&r!l&&!r
22682200
MC/DC0%0/2
$self->{"heritage"} ||= do { my(@pending) = $self; my(%seen, $item, @order); while (@pending) { next unless defined($item = pop @pending); unshift @order, $item; push @pending, reverse(@{$item->parents;}); } ; [reverse(grep({not $seen{$_}++;} @order))] }
$self->{"heritage"} do { my(@pending) = $self; my(%seen, $item, @order); while (@pending) { next unless defined($item = pop @pending); unshift @order, $item; push @pending, reverse(@{$item->parents;}); } ; [reverse(grep({not $seen{$_}++;} @order))] }
Truth table66%2/3
A ||= B
A= $self->{"heritage"}B= do { my(@pending) = $self; my(%seen, $item, @order); while (@pending) { next unless defined($item = pop @pending); unshift @order, $item; push @pending, reverse(@{$item->parents;}); } ; [reverse(grep({not $seen{$_}++;} @order))] }
ABresult
1X1
011
000
496 220 my @pending = ($self);
497 220 my (%seen, $item, @order);
498 220 ▶ while (@pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pending) 717 220 937
499 717 ▶ next unless defined ($item = pop @pending);
Branch: 1 branch
BranchTrueFalseTotal
unless defined($item = pop @pending) 0 717 717
500 717 unshift(@order, $item);
501 717 push(@pending, reverse @{ $item->parents });
502 }
503 220 [ reverse grep { ! $seen{$_}++ } @order ];
504 };
505 return wantarray
506 2488 ▶ ? @$heritage
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2488 0 2488
507 : $heritage;
508 }
509  
510  
511 #-----------------------------------------------------------------------
512 # class configuration methods - also available as import hooks
513 #-----------------------------------------------------------------------
514  
515 sub base {
516 2127 ▶ my $self = shift;
Subroutine base: called CC=6 SCAR=18.1
Pod documented
517 2127 ▶ my $bases = @_ == 1 ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 2127 0 2127
518 2127 my $pkg = $self->{ name };
519  
520 2127 ▶ $bases = [ split(DELIMITER, $bases) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $bases eq "ARRAY" 2127 0 2127
521 unless ref $bases eq ARRAY;
522  
523 # add each of $bases to @ISA and autoload it
524 2127 foreach my $base (@$bases) {
525 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
526 2312 ▶ next if $pkg->isa($base);
Branch: 1 branch
BranchTrueFalseTotal
if $pkg->isa($base) 251 2061 2312
527 2061 ▶ _debug("Adding $pkg base class $base\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 2061 2061
528 2061 push @{ $pkg.PKG.ISA }, $base;
529 2061 _autoload($base);
530 }
531 2127 return $self;
532 }
533  
534 sub mixin {
535 3 ▶ my $self = shift;
Subroutine mixin: called CC=4 SCAR=14.2
Pod documented
536 3 ▶ my $mixins = @_ == 1 ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 3 0 3
537  
538 3 ▶ $mixins = [ split(DELIMITER, $mixins) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $mixins eq "ARRAY" 3 0 3
539 unless ref $mixins eq ARRAY;
540  
541 3 foreach my $name (@$mixins) {
542 # $name = $target . $name if $name =~ /^::/;
543 # $self->debug("mixing $name into $self\n") if $DEBUG;
544 3 _autoload($name)->mixin($self->{ name });
545 }
546  
547 3 return $self;
548 }
549  
550 sub mixins {
551 3 ▶ my $self = shift;
Subroutine mixins: called CC=4 SCAR=23.7
Pod documented
552 3 $self->base(MIXIN);
553 3 $self->{ name }->mixins(@_);
554 3 return $self;
555  
556 0 ▶ my $syms = @_ == 1 ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
557 0 my $mixins = $self->var_default(MIXINS, [ ]);
558  
559 0 ▶ $syms = [ split(DELIMITER, $syms) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $syms eq "ARRAY" 0 0 0
560 unless ref $syms eq ARRAY;
561  
562 # $mixins->{ $_ }
563 0 push(@$mixins, @$syms);
564  
565 0 ▶ $self->debug("$self MIXINS are: ", $self->dump_data_inline($mixins), "\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 0 0
566  
567 0 $self->exports( any => $syms );
568  
569 }
570  
571 sub version {
572 1384 ▶ my ($self, $version) = @_;
Subroutine version: called CC=5 SCAR=16.8
Pod documented
573 1384 my $pkg = $self->{ name };
574 70 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
575 1384 ▶ _debug("Defining $pkg version $version\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 1384 1384
576  
577 # define $VERSION and VERSION()
578 1384 *{ $pkg.PKG.VERSION } = \$version
579 1384 unless defined ${ $pkg.PKG.VERSION }
580 1384 ▶ && ${ $pkg.PKG.VERSION };
Branch: 1 branch
BranchTrueFalseTotal
unless defined ${$pkg . '::' . "VERSION";} and ${$pkg . '::' . "VERSION";} 1384 0 1384
Condition33%1/3
defined ${$pkg . '::' . "VERSION";} and ${$pkg . '::' . "VERSION";}
!ll&&!rl&&r
138400
MC/DC0%0/2
defined ${$pkg . '::' . "VERSION";} and ${$pkg . '::' . "VERSION";}
defined ${$pkg . '::' . "VERSION";} ${$pkg . '::' . "VERSION";}
Truth table33%1/3
A and B
A= defined ${$pkg . '::' . "VERSION";}B= ${$pkg . '::' . "VERSION";}
ABresult
0X0
100
111
581 1384 ▶ *{ $pkg.PKG.VERSION } = sub() { $version }
Subroutine __ANON__: called CC=1 SCAR=0.0
582 1384 ▶ unless defined &{ $pkg.PKG.VERSION }; # CHECK THIS - was 'version'
Branch: 1 branch
BranchTrueFalseTotal
unless defined &{$pkg . '::' . "VERSION";} 1384 0 1384
583  
584 1384 return $self;
585 }
586  
587 sub constant {
588 797 ▶ my $self = shift;
Subroutine constant: called CC=5 SCAR=16.4
Pod documented
589 797 ▶ my $constants = @_ == 1 ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 775 22 797
590 797 my $pkg = $self->{ name };
591  
592 # split string into pairs of assignments, e.g. "foo=bar, baz=bam"
593 $constants = {
594 797 ▶ map { split /\s*=>?\s*/ }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $constants eq "HASH" 0 797 797
595 split(DELIMITER, $constants)
596 } unless ref $constants eq HASH;
597  
598  
599 797 ▶ while (my ($name, $value) = each %$constants) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$constants) 2449 797 3246
600 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
601 2449 my $v = $value; # new lexical variable to bind in closure
602 2449 ▶ _debug("Defining $pkg constant $name => $value\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 2449 2449
603 2449 ▶ *{ $pkg.PKG.$name } = sub() { $value };
Subroutine __ANON__: not called CC=1 SCAR=0.4
604 }
605 797 return $self;
606 }
607  
608 sub words {
609 493 ▶ my $self = shift;
Subroutine words: called CC=5 SCAR=16.5
Pod documented
610 493 ▶ my $words = @_ == 1 ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 493 0 493
611 493 my $pkg = $self->{ name };
612  
613 493 ▶ $words = [ split(DELIMITER, $words) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $words eq "ARRAY" 493 0 493
614 unless ref $words eq ARRAY;
615  
616 493 foreach (@$words) {
617 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
618 1287 my $word = $_; # new lexical variable to bind in closure
619 1287 ▶ _debug("Defining $pkg word $word\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 1287 1287
620 1287 ▶ *{ $pkg.PKG.$word } = sub() { $word };
Subroutine __ANON__: not called CC=1 SCAR=0.4
621 }
622 493 return $self;
623 }
624  
625 sub exports {
626 695 ▶ my $self = shift;
Subroutine exports: called CC=1 SCAR=0.0
Pod documented
627 695 my $pkg = $self->{ name };
628 695 $self->base(EXPORTER);
629 695 $pkg->exports(@_);
630 695 return $self;
631 }
632  
633 sub throws {
634 2 ▶ my ($self, $throws) = @_;
Subroutine throws: called CC=1 SCAR=0.0
Pod documented
635 2 $self->import_symbol(THROWS, \$throws);
636 2 return $self;
637 }
638  
639 sub messages {
640 638 ▶ my $self = shift;
Subroutine messages: called CC=6 SCAR=18.0
Pod documented
641 638 ▶ my $args = @_ && ref $_[0] eq HASH ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 637 1 638
Condition66%2/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
01637
MC/DC50%1/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
642 638 my $pkg = $self->{ name };
643 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
644 70 ▶ no warnings ONCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
645  
646 # if there aren't any existing $MESSAGES then we can store
647 # $messages in it and be done, otherwise we have to merge.
648 638 my $messages = ${ $pkg.PKG.MESSAGES };
649  
650 638 ▶ if ($messages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($messages) { } 1 637 638
651 1 ▶ _debug("merging $pkg messages: ", join(', ', keys %$args), "\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 1 1
652 1 @$messages{ keys %$args } = values %$args;
653 }
654 else {
655 637 ▶ _debug("adding $pkg messages: ", join(', ', keys %$args), "\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 637 637
656 637 ${ $pkg.PKG.MESSAGES } = $messages = $args;
657 }
658  
659 638 return $self;
660 }
661  
662 sub method {
663 25 ▶ my $self = shift;
Subroutine method: called CC=4 SCAR=13.9
Pod documented
664 25 my $name = shift;
665 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
666 70 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
667  
668 # method($name) can be used to fetch a method/sub
669 25 ▶ return $self->{ name }->can($name)
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 4 21 25
670 unless @_;
671  
672 # method($name => $code) or $method($name => $value) to define method
673 21 my $code = shift;
674 21 ▶ _debug("defining method: $self\::$name => $code\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 21 21
675  
676 21 *{ $self->{name}.PKG.$name } = ref $code eq CODE
677 ? $code
678 21 ▶ : sub { $code }; # constant method returns value
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
ref $code eq 'CODE' ? : 17 4 21
679  
680 21 return $self;
681 }
682  
683 sub methods {
684 713 ▶ my $self = shift;
Subroutine methods: called CC=6 SCAR=17.9
Pod documented
685 713 ▶ my $args = @_ && ref $_[0] eq HASH ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 75 638 713
Condition66%2/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
063875
MC/DC50%1/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
686 713 my $pkg = $self->{ name };
687 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
688 70 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
689  
690 713 ▶ while (my ($name, $code) = each %$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$args) 4967 713 5680
691 4967 ▶ _debug("defining method: $self\::$name => $code\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 4967 4967
692 4967 *{ $pkg.PKG.$name }
693 4967 ▶ = ref $code eq CODE ? $code : sub { $code };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
ref $code eq 'CODE' ? : 4966 1 4967
694 }
695 713 return $self;
696 }
697  
698 sub alias {
699 92 ▶ my $self = shift;
Subroutine alias: called CC=8 SCAR=21.1
Pod documented
700 92 ▶ my $args = @_ && ref $_[0] eq HASH ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 91 1 92
Condition66%2/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
0191
MC/DC50%1/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
701 92 my $pkg = $self->{ name };
702 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
703  
704 92 ▶ while (my ($names, $code) = each %$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$args) 97 92 189
705 97 ▶ _debug("defining alias: $self\::$names => $code\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 97 97
706 97 ▶ $code = $self->method($code)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 3 94 97
Condition33%1/3
$self->method($code) || croak("Invalid method specified for '${names}' alias: $code")
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->method($code) || croak("Invalid method specified for '${names}' alias: $code")
$self->method($code) croak("Invalid method specified for '${names}' alias: $code")
Truth table33%1/3
A || B
A= $self->method($code)B= croak("Invalid method specified for '${names}' alias: $code")
ABresult
1X1
011
000
707 || croak "Invalid method specified for '$names' alias: $code"
708 unless ref $code eq CODE;
709 97 foreach my $name (split(DELIMITER, $names)) {
710 97 *{ $pkg.PKG.$name } = $code;
711 }
712 }
713 92 return $self;
714 }
715  
716 sub overload {
717 516 ▶ my $self = shift;
Subroutine overload: called CC=4 SCAR=14.0
Pod documented
718 516 ▶ my $args = @_ && ref $_[0] eq HASH ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 74 442 516
Condition66%2/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
044274
MC/DC50%1/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
719 516 ▶ _debug("overload on $self->{name} : { ", join(', ', %$args), " }\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 516 516
720 516 overload::OVERLOAD($self->{name}, %$args);
721 516 return $self;
722 }
723  
724 sub as_text {
725 223 ▶ my ($self, $method) = @_;
Subroutine as_text: called CC=1 SCAR=0.0
Pod documented
726 223 $self->overload( '""' => $method, fallback => 1 );
727 }
728  
729 sub is_true {
730 219 ▶ my ($self, $arg) = @_;
Subroutine is_true: called CC=3 SCAR=11.7
Pod documented
731 219 ▶ my $method =
Branch: 2 branches
BranchTrueFalseTotal
$arg eq 0 ? : 0 219 219
$arg eq 1 ? : 219 0 219
732 $arg eq FALSE ? \&FALSE : # allow 0/1 as shortcut
733 $arg eq TRUE ? \&TRUE :
734 $arg;
735 219 $self->overload( bool => $method, fallback => 1 );
736 }
737  
738  
739 #-----------------------------------------------------------------------
740 # misc methods
741 #-----------------------------------------------------------------------
742  
743 sub instance {
744 2 ▶ my $self = shift;
Subroutine instance: called CC=1 SCAR=0.0
Pod documented
745 2 $self->{ name }->new(@_);
746 }
747  
748 sub loaded {
749 # "loaded" is defined as "has an entry in the symbol table"
750 # NOTE: this is incorrect - see comment in _autoload() wrt
751 # case-insensitive filesystems
752 3 ▶ keys %{ $_[0]->{ symbols } } ? 1 : 0;
Subroutine loaded: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
keys %{$_[0]{'symbols'};} ? : 2 1 3
753 }
754  
755 sub load {
756 120 ▶ my $self = shift;
Subroutine load: called CC=2 SCAR=7.1
Pod documented
757 120 ▶ _autoload($self->{ name }) || return;
Branch: 1 branch
BranchTrueFalseTotal
_autoload($self->{"name"}) or return 0 98 98
758 98 return $self;
759 }
760  
761 sub maybe_load {
762 54 ▶ my $self = shift;
Subroutine maybe_load: called CC=6 SCAR=18.3
Pod documented
763 54 ▶ return eval { $self->load } || do {
Condition66%2/3
eval { do { $self->load } } || do { _debug("maybe_load($self) caught error: $@\n") if &DEBUG(); my $name = join('\\W+', map({quotemeta $_;} split(/::/, $self->{'name'}, 0))); _debug("checking to see if we couldn't locate $name\n") if &DEBUG(); croak($@) if $@ and not $@ =~ /^Can't locate $name.*? in \@INC/; 0 }
l!l&&r!l&&!r
32022
MC/DC50%1/2
eval { do { $self->load } } || do { _debug("maybe_load($self) caught error: $@\n") if &DEBUG(); my $name = join('\\W+', map({quotemeta $_;} split(/::/, $self->{'name'}, 0))); _debug("checking to see if we couldn't locate $name\n") if &DEBUG(); croak($@) if $@ and not $@ =~ /^Can't locate $name.*? in \@INC/; 0 }
eval { do { $self->load } } do { _debug("maybe_load($self) caught error: $@\n") if &DEBUG(); my $name = join('\\W+', map({quotemeta $_;} split(/::/, $self->{'name'}, 0))); _debug("checking to see if we couldn't locate $name\n") if &DEBUG(); croak($@) if $@ and not $@ =~ /^Can't locate $name.*? in \@INC/; 0 }
Truth table66%2/3
A || B
A= eval { do { $self->load } }B= do { _debug("maybe_load($self) caught error: $@\n") if &DEBUG(); my $name = join('\\W+', map({quotemeta $_;} split(/::/, $self->{'name'}, 0))); _debug("checking to see if we couldn't locate $name\n") if &DEBUG(); croak($@) if $@ and not $@ =~ /^Can't locate $name.*? in \@INC/; 0 }
ABresult
1X1
011
000
764 22 ▶ _debug("maybe_load($self) caught error: $@\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 22 22
765 # Don't confuse "Can't locate Missing/Module/Used/In/Your/Module.pm"
766 # messages with "Can't locate Your/Module.pm". The former is an
767 # error that should be reported, the latter isn't. We convert the
768 # class name to a regex that matches any non-word directory separators
769 # e.g. Your::Module => Your\W+Module
770 my $name = join(
771 "\\W+",
772 59 map { quotemeta $_ }
773 split('::', $self->{ name })
774 22 );
775 22 ▶ _debug("checking to see if we couldn't locate $name\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 22 22
776 22 ▶ croak $@ if $@ && $@ !~ /^Can't locate $name.*? in \@INC/;
Branch: 1 branch
BranchTrueFalseTotal
if $@ and not $@ =~ /^Can't locate $name.*? in \@INC/ 1 21 22
Condition66%2/3
$@ and not $@ =~ /^Can't locate $name.*? in \@INC/
!ll&&!rl&&r
0211
MC/DC50%1/2
$@ and not $@ =~ /^Can't locate $name.*? in \@INC/
$@ not $@ =~ /^Can't locate $name.*? in \@INC/
Truth table66%2/3
A and not(B)
A= $@B= not $@ =~ /^Can't locate $name.*? in \@INC/
ABresult
0X0
100
111
777 21 0;
778 }
779 }
780  
781  
782 #-----------------------------------------------------------------------
783 # methods for building Badger::Class subclasses
784 #-----------------------------------------------------------------------
785  
786 sub uber {
787 31 ▶ my ($self, $base) = @_;
Subroutine uber: called CC=1 SCAR=0.0
Pod documented
788 31 my $pkg = $self->{ name };
789 31 $self->base($base);
790 31 $pkg->UBER;
791 31 return $self;
792 }
793  
794 sub hooks {
795 31 ▶ my $self = shift;
Subroutine hooks: called CC=6 SCAR=18.1
Pod documented
796 31 ▶ my $args = @_ == 1 ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 31 0 31
797 31 my $hooks = $self->var_default( HOOKS => { } );
798  
799 # split string into list ref
800 31 ▶ $args = [ split(DELIMITER, $args) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args 26 5 31
801 unless ref $args;
802  
803 # map list ref to hash ref
804 $args = {
805 31 ▶ map { $_ => $_ }
Branch: 1 branch
BranchTrueFalseTotal
if ref $args eq "ARRAY" 26 5 31
806 @$args
807 } if ref $args eq ARRAY;
808  
809 31 ▶ croak("Invalid hooks specified: $args")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 31 31
810 unless ref $args eq HASH;
811  
812 31 ▶ _debug("merging $self->{ name } hooks: ", join(', ', keys %$args), "\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 31 31
813  
814 31 @$hooks{ keys %$args } = values %$args;
815  
816 31 return $self;
817 }
818  
819  
820 #-----------------------------------------------------------------------
821 # autoload($module)
822 #
823 # Helper subroutine to autoload a module.
824 #-----------------------------------------------------------------------
825  
826 sub _autoload {
827 6387 ▶ my $class = shift;
Subroutine _autoload: called CC=8 SCAR=20.8
828 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
829 70 ▶ no warnings ONCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
830 6387 my $symbols = \%{"${class}::"};
831  
832 6387 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined ${$class . '::' . "BADGER_LOADED";} or scalar grep({not /::$/;} keys %$symbols) > 1) 649 5738 6387
Condition100%3/3
defined ${$class . '::' . "BADGER_LOADED";} or scalar grep({not /::$/;} keys %$symbols) > 1
l!l&&r!l&&!r
36982040649
MC/DC100%2/2
defined ${$class . '::' . "BADGER_LOADED";} or scalar grep({not /::$/;} keys %$symbols) > 1
defined ${$class . '::' . "BADGER_LOADED";} scalar grep({not /::$/;} keys %$symbols) > 1
Truth table100%3/3
A or B
A= defined ${$class . '::' . "BADGER_LOADED";}B= scalar grep({not /::$/;} keys %$symbols) > 1
ABresult
1X1
011
000
833 6387 defined ${ $class.PKG.LOADED }
834 105081 || scalar(grep { ! /::$/ } keys %$symbols) > 1 # any symbols defined other than import / sub-namespaces
835 ) {
836 649 ▶ _debug("autoloading $class\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 649 649
837 649 local $SIG{__DIE__};
838 70 ▶ eval "use $class";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
839 649 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 22 627 649
840  
841 # Problem here is that case-insensitive filesystems could load the
842 # wrong module. We could check the symbol table, but it will always
843 # have an 'import' entry because 'use' attempts to call import().
844 # So we assume a successful module load requires there to be a symbol
845 # table with entries other than a single 'import'
846 # [later] Oh blimey, it's worse than that. There may be other
847 # sub-namespaces.
848 return 0
849 36999 if scalar(grep { ! /::$/ } keys %$symbols) == 1
850 627 ▶ && exists $symbols->{ import };
Branch: 1 branch
BranchTrueFalseTotal
if scalar grep({not /::$/;} keys %$symbols) == 1 and exists $symbols->{"import"} 0 627 627
Condition33%1/3
scalar grep({not /::$/;} keys %$symbols) == 1 and exists $symbols->{"import"}
!ll&&!rl&&r
62700
MC/DC0%0/2
scalar grep({not /::$/;} keys %$symbols) == 1 and exists $symbols->{"import"}
scalar grep({not /::$/;} keys %$symbols) == 1 exists $symbols->{"import"}
Truth table33%1/3
A and B
A= scalar grep({not /::$/;} keys %$symbols) == 1B= exists $symbols->{"import"}
ABresult
0X0
100
111
851  
852 627 ▶ ${ $class.PKG.LOADED } ||= 1;
Condition100%2/2
${$class . '::' . "BADGER_LOADED";} ||= 1
l!l
54582
MC/DC100%1/1
${$class . '::' . "BADGER_LOADED";} ||= 1
${$class . '::' . "BADGER_LOADED";}
Truth table100%2/2
A
A= ${$class . '::' . "BADGER_LOADED";}
Aresult
11
00
853 }
854  
855 6365 return $class;
856 }
857  
858 sub _debug {
859 0 ▶ print STDERR @_;
Subroutine _debug: not called CC=1 SCAR=6.9
860 }
861  
862  
863 70 1;
864  
865 __END__
« lib/Badger/Filesystem/Directory.pm ↑ Summary blib/lib/Badger/Apps.pm »