blib/lib/Sub/Parameters.pm

filter: Statementstmt 80.2% 61 / 76 Branchbran 21.4% 6 / 28 Conditioncond 20.0% 4 / 20 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 93.7% 15 / 16 Podpod 0.0% 0 / 3 totaltotal 56.8% 87 / 153 CC 21 SCAR 41.2
CC
21
Cov
57%
_Parameter
135.6
__ANON__
4.5
Parameter
3.0
CRAP 61.3
↑ Summary
1 package Sub::Parameters;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Hook::LexWrap;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Devel::Caller qw(caller_cv called_with);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Devel::LexAlias qw(lexalias);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Attribute::Handlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 require Exporter;
11 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 our @EXPORT_OK = qw( Param );
13 2 our $VERSION = '0.03';
14  
15 2 my @stack;
16  
17 12 ▶sub UNIVERSAL::WantParam : ATTR(CODE) {
Subroutine WantParam: called CC=2 SCAR=8.1
Pod undocumented
18 12 my ($symbol, $sub, $data) = @_[1, 2, 4];
19  
20 12 ▶ $data ||= 'positional';
Condition100%2/2
$data ||= "positional"
l!l
102
MC/DC100%1/1
$data ||= "positional"
$data
Truth table100%2/2
A
A= $data
Aresult
11
00
21 wrap $symbol,
22 2 ▶ pre => sub {
Subroutine __ANON__: called CC=3 SCAR=15.1
23 2 my %order;
24 2 ▶ if ($data eq 'named') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data eq "named") 0 2 2
25 # prechew the ordering information
26 0 ▶ for (my $i = 0; $i < $#_; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#_) 0 0 0
27 0 $order{ $_[$i] } = $i + 1;
28 }
29 }
30 2 push @stack, { data => $data,
31 sub => $sub,
32 order => \%order,
33 args => \@_ };
34 },
35 0 ▶ post => sub { pop @stack };
Subroutine __ANON__: not called CC=1 SCAR=1.2
36 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38  
39 # you know, this would be a lot tidier if we could use ourselves
40 # already...
41  
42 1 ▶sub Param {
Subroutine Param: called CC=1 SCAR=0.0
Pod undocumented
43 1 local $Carp::CarpLevel = 3;
44 1 _Parameter(caller_cv(1), called_with(0), called_with(0,1), $_[0]);
45 }
46  
47 1 ▶sub UNIVERSAL::Parameter : ATTR(VAR) {
Subroutine Parameter: called CC=3 SCAR=11.1
Pod undocumented
48 # 4 is a magic number dependant on Attribute::Handlers
49 1 local $Carp::CarpLevel = 4;
50 1 ▶ croak "your perl is not new enough to use the :Parameter form"
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.007002" 0 1 1
51 if $] < 5.007002;
52  
53 1 my $sub = caller_cv($Carp::CarpLevel);
54 1 my $referent = $_[2];
55  
56 1 require PadWalker;
57 1 my %names = reverse %{ PadWalker::peek_sub( $sub ) };
58 1 ▶ my $fullname = $names{$referent}
Branch: 1 branch
BranchTrueFalseTotal
my $fullname = $names{$referent} or croak("couldn't find the name of $referent") 1 0 1
59 or croak "couldn't find the name of $referent";
60  
61 1 ++$Carp::CarpLevel;
62 1 _Parameter($sub, $referent, $fullname, $_[4]);
63 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 2 ▶sub _Parameter {
Subroutine _Parameter: called CC=16 SCAR=49.1
66 2 my ($sub, $referent, $fullname, $data) = @_;
67 2 ▶ $data ||= 'copy'; # valid values: qw(copy rw)
Condition50%1/2
$data ||= "copy"
l!l
02
MC/DC0%0/1
$data ||= "copy"
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
68  
69 2 my $frame = $stack[-1];
70 croak "attempt to use a Parameter in an undecorated subroutine"
71 2 ▶ unless $frame->{sub} && $sub == $frame->{sub};
Branch: 1 branch
BranchTrueFalseTotal
unless $frame->{"sub"} and $sub == $frame->{"sub"} 0 2 2
Condition33%1/3
$frame->{"sub"} and $sub == $frame->{"sub"}
!ll&&!rl&&r
002
MC/DC0%0/2
$frame->{"sub"} and $sub == $frame->{"sub"}
$frame->{"sub"} $sub == $frame->{"sub"}
Truth table33%1/3
A and B
A= $frame->{"sub"}B= $sub == $frame->{"sub"}
ABresult
0X0
100
111
72  
73 2 my ($sigil, $name) = ($fullname =~ /^([\$@%])(.*)$/);
74  
75 # set the offset based on the scheme
76 2 my $offset;
77 2 ▶ if ($frame->{data} eq 'positional') {
Branch: 2 branches
BranchTrueFalseTotal
if ($frame->{'data'} eq 'positional') { } 0 2 2
elsif ($frame->{'data'} eq 'named') { } 0 2 2
78 0 $offset = $frame->{index}++;
79 }
80 elsif ($frame->{data} eq 'named') {
81 0 ▶ $offset = $frame->{order}{$name}
Branch: 1 branch
BranchTrueFalseTotal
$offset = $frame->{"order"}{$name} or croak("can't find a parameter for '$sigil${name}'") 0 0 0
82 or croak "can't find a parameter for '$sigil$name'";
83 }
84 else {
85 2 croak "don't know what kind of processing to do!";
86 }
87  
88 0 ▶ if ( $sigil eq '@' || $sigil eq '%' ) { # expect refs
Branch: 1 branch
BranchTrueFalseTotal
if ($sigil eq "\@" or $sigil eq "%") 0 0 0
Condition0%0/3
$sigil eq "\@" or $sigil eq "%"
l!l&&r!l&&!r
000
MC/DC0%0/2
$sigil eq "\@" or $sigil eq "%"
$sigil eq "\@" $sigil eq "%"
Truth table0%0/3
A or B
A= $sigil eq "\@"B= $sigil eq "%"
ABresult
1X1
011
000
89 0 my $value = $frame->{args}[ $offset ];
90 0 ▶ ref $value eq 'ARRAY' || croak "can't assign non-arrayref to '$sigil$name'"
Branch: 1 branch
BranchTrueFalseTotal
if $sigil eq "\@" 0 0 0
Condition0%0/5
$sigil eq '@' && (ref $value eq 'ARRAY' || croak("can't assign non-arrayref to '$sigil${name}'"))
!ll&&!rl&&r
000
ref $value eq "ARRAY" or croak("can't assign non-arrayref to '$sigil${name}'")
l!l
00
MC/DC0%0/2
$sigil eq '@' && (ref $value eq 'ARRAY' || croak("can't assign non-arrayref to '$sigil${name}'"))
$sigil eq '@' ref $value eq "ARRAY"
Truth table0%0/3
A && B
A= $sigil eq '@'B= ref $value eq "ARRAY"
ABresult
0X0
100
111
91 if $sigil eq '@';
92 0 ▶ ref $value eq 'HASH' || croak "can't assign non-hashref to '$sigil$name'"
Branch: 1 branch
BranchTrueFalseTotal
if $sigil eq "%" 0 0 0
Condition0%0/5
$sigil eq '%' && (ref $value eq 'HASH' || croak("can't assign non-hashref to '$sigil${name}'"))
!ll&&!rl&&r
000
ref $value eq "HASH" or croak("can't assign non-hashref to '$sigil${name}'")
l!l
00
MC/DC0%0/2
$sigil eq '%' && (ref $value eq 'HASH' || croak("can't assign non-hashref to '$sigil${name}'"))
$sigil eq '%' ref $value eq "HASH"
Truth table0%0/3
A && B
A= $sigil eq '%'B= ref $value eq "HASH"
ABresult
0X0
100
111
93 if $sigil eq '%';
94  
95 0 ▶ $value = (ref $value eq 'ARRAY' ? [ @$value ] : { %$value })
Branch: 2 branches
BranchTrueFalseTotal
if $data ne "rw" 0 0 0
ref $value eq 'ARRAY' ? : 0 0 0
96 if $data ne 'rw';
97  
98 0 lexalias($sub, $sigil.$name, $value);
99 0 return;
100 }
101  
102 # simple scalars
103 0 ▶ if ($data eq 'rw') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data eq 'rw') { } 0 0 0
104 0 lexalias($sub, $sigil.$name, \$frame->{args}[ $offset ]);
105 }
106 else {
107 0 $$referent = $frame->{args}[ $offset ];
108 }
109 }
110  
111 2 1;
112 __END__
↑ Summary