| 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"
MC/DC100%1/1
$data ||= "positional"
$data
Truth table100%2/2
A
A= $data
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 25 | # prechew the ordering information | ||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | for (my $i = 0; $i < $#_; $i += 2) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$data ||= "copy"
$data
Truth table50%1/2
A
A= $data
| |||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$frame->{"sub"} and $sub == $frame->{"sub"}
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"}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 78 | 0 | $offset = $frame->{index}++; | |||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | elsif ($frame->{data} eq 'named') { | ||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | $offset = $frame->{order}{$name} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$sigil eq "\@" or $sigil eq "%"
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 "%"
| |||||||||||||||||||||||||||||||||
| 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
Condition0%0/5
$sigil eq '@' && (ref $value eq 'ARRAY' || croak("can't assign non-arrayref to '$sigil${name}'"))
ref $value eq "ARRAY" or croak("can't assign non-arrayref to '$sigil${name}'")
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"
| |||||||||||||||||||||||||||||||||
| 91 | if $sigil eq '@'; | ||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | ref $value eq 'HASH' || croak "can't assign non-hashref to '$sigil$name'" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$sigil eq '%' && (ref $value eq 'HASH' || croak("can't assign non-hashref to '$sigil${name}'"))
ref $value eq "HASH" or croak("can't assign non-hashref to '$sigil${name}'")
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"
| |||||||||||||||||||||||||||||||||
| 93 | if $sigil eq '%'; | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | $value = (ref $value eq 'ARRAY' ? [ @$value ] : { %$value }) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||