lib/Badger/Class/Vars.pm

filter: Statementstmt 97.0% 33 / 34 Branchbran 80.5% 29 / 36 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 87.9% 73 / 83 CC 21 SCAR 28.4
CC
21
Cov
88%
vars
21.1
export
2.0
BEGIN
1.0
CRAP 17.2
« blib/lib/Badger/Exception.pm ↑ Summary blib/lib/Badger/Test/Manager.pm »
1 #========================================================================
2 #
3 # Badger::Class::Vars
4 #
5 # DESCRIPTION
6 # Class mixin module for adding package variables to a class.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 #========================================================================
12  
13 package Badger::Class::Vars;
14  
15 3 ▶use Badger::Debug ':dump';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 use Badger::Class
17 3 version => 0.01,
18 debug => 0,
19 base => 'Badger::Base Badger::Exporter',
20 import => 'BCLASS',
21 constants => 'DELIMITER SCALAR ARRAY HASH REFS PKG',
22 utils => 'is_object',
23 messages => {
24 no_target => 'No target class specified to generate variables for',
25 no_vars => 'No vars specified to define',
26 bad_vars => 'Invalid vars specified: %s',
27 bad_var => 'Invalid variable name in vars: %s',
28 bad_hash => 'Invalid hash variable for %s in vars: %s',
29 bad_sigil => 'Unrecognised sigil for symbol: %s',
30 3 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32
33 sub export {
34 6 ▶ goto &vars if @_ > 2;
Subroutine export: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 4 2 6
35 }
36  
37  
38 sub vars {
39 9 ▶ my $class = shift;
Subroutine vars: called CC=20 SCAR=30.5
Pod documented
40 9 ▶ my $target = shift || return $class->error_msg('no_target');
Condition50%1/2
shift() || (return $class->error_msg('no_target'))
l!l
90
MC/DC0%0/1
shift() || (return $class->error_msg('no_target'))
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
41 9 ▶ my $vars = @_ == 1 ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 8 1 9
42 9 my ($symbol, $sigil, $name, $dest, $ref);
43  
44 # downgrade $target from a Badger::Class object to a package name
45 9 ▶ $target = $target->name
Branch: 1 branch
BranchTrueFalseTotal
if &is_object("Badger::Class", $target) 0 9 9
46 if is_object(BCLASS, $target);
47  
48 # split text string into lisy ref of variable names
49 9 ▶ $vars = [ split(DELIMITER, $vars) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $vars 2 7 9
50 unless ref $vars;
51  
52 # upgrade a list ref to a hash ref
53 9 ▶ $vars = { map { $_ => undef } @$vars }
Branch: 1 branch
BranchTrueFalseTotal
if ref $vars eq "ARRAY" 3 6 9
54 if ref $vars eq ARRAY;
55  
56 9 ▶ $class->error_msg( bad_vars => $vars )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $vars eq "HASH" 0 9 9
57 unless ref $vars eq HASH;
58  
59 9 ▶ $class->error_msg('no_vars')
Branch: 1 branch
BranchTrueFalseTotal
unless %$vars 0 9 9
60 unless %$vars;
61  
62 9 $class->debug("Defining vars for $target: ", $class->dump_data($vars))
63 if DEBUG;
64  
65 # This is a slightly simplified (stricter) version of the equivalent
66 # code in vars.pm with a little extra syntactic sugar supported.
67 # Unfortunately it's not possible to delegate to vars.pm because
68 # it will only export to its caller, and not to a third party package
69
70 9 ▶ while (($symbol, $ref) = each %$vars) {
Branch: 1 branch
BranchTrueFalseTotal
if (($symbol, $ref) = each %$vars) 37 9 46
71 3 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 # only accept: $WORD @WORD %WORD WORD
74 37 ▶ $symbol =~ /^([\$\@\%])?(\w+)$/
Branch: 1 branch
BranchTrueFalseTotal
$symbol =~ /^([\$\@\%])?(\w+)$/ or return $class->error_msg("bad_var", $symbol) 0 37 37
75 || return $class->error_msg( bad_var => $symbol );
76 37 ▶ ($sigil, $name) = ($1 || '$', $2);
Condition100%2/2
$1 || '$'
l!l
2512
MC/DC100%1/1
$1 || '$'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
77
78 # expand destination to full package name ($Your::Module::WORD)
79 37 $dest = $target.PKG.$name;
80  
81 37 $class->debug("var: $sigil$name => ", $ref || '\\'.$sigil.$dest, "\n")
82 if DEBUG;
83
84 37 ▶ if ($sigil eq '$') {
Branch: 3 branches
BranchTrueFalseTotal
if ($sigil eq '$') { } 22 15 37
elsif ($sigil eq '@') { } 8 7 15
elsif ($sigil eq '%') { } 7 0 7
85 *$dest = defined $ref
86 22 ▶ ? (ref $ref eq SCALAR ? $ref : do { my $copy = $ref; \$copy })
Branch: 2 branches
BranchTrueFalseTotal
defined $ref ? : 19 3 22
ref $ref eq 'SCALAR' ? : 0 19 19
87 : \$$dest;
88 }
89 elsif ($sigil eq '@') {
90 8 ▶ *$dest = defined $ref
Branch: 2 branches
BranchTrueFalseTotal
defined $ref ? : 5 3 8
ref $ref eq 'ARRAY' ? : 4 1 5
91 ? (ref $ref eq ARRAY ? $ref : [$ref])
92 : \@$dest;
93 }
94 elsif ($sigil eq '%') {
95 7 ▶ *$dest = defined $ref
Branch: 2 branches
BranchTrueFalseTotal
defined $ref ? : 4 3 7
ref $ref eq 'HASH' ? : 4 0 4
96 ? (ref $ref eq HASH
97 ? $ref
98 : return $class->error_msg( bad_hash => $symbol, $ref )
99 )
100 : \%$dest;
101 }
102 else {
103 # should never happen
104 0 return $class->error_msg( bad_sigil => $symbol );
105 }
106 }
107 }
108  
109 3 1;
110  
111 __END__
« blib/lib/Badger/Exception.pm ↑ Summary blib/lib/Badger/Test/Manager.pm »