| 1 | package Smart::Args::TypeTiny; | ||||||||||||||||||||||||||||
| 2 | 16 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 16 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 16 | our $VERSION = "0.14"; | |||||||||||||||||||||||||||
| 5 | 16 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 16 | ▶ | use PadWalker qw/var_name/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 16 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 16 | our @EXPORT = qw/args args_pos/; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 16 | $Carp::CarpInternal{+__PACKAGE__}++; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 16 | ▶ | use Smart::Args::TypeTiny::Check qw/check_rule/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 16 | my %is_invocant = map { ($_ => 1) } qw($self $class); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 57 | ▶ | sub args { | ||||||||||||||||||||||||||
|
Subroutine
args: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | { | ||||||||||||||||||||||||||||
| 19 | 57 | package DB; | |||||||||||||||||||||||||||
| 20 | # call of caller in DB package sets @DB::args, | ||||||||||||||||||||||||||||
| 21 | # which requires list context, but we don't need return values | ||||||||||||||||||||||||||||
| 22 | 57 | () = CORE::caller(1); | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 57 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 57 | ▶ | my $name = var_name(1, \$_[0]) || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
var_name(1, \$_[0]) || ''
MC/DC0%0/1
var_name(1, \$_[0]) || ''
var_name(1, \$_[0])
Truth table50%1/2
A
A= var_name(1, \$_[0])
| |||||||||||||||||||||||||||||
| 27 | 57 | ▶ | if ($is_invocant{$name}) { # seems instance/class method call | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 10 | $name =~ s/^\$//; | |||||||||||||||||||||||||||
| 29 | 10 | $_[0] = shift @DB::args; | |||||||||||||||||||||||||||
| 30 | 10 | ▶ | if (defined $_[1]) { # has rule? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 3 | $_[0] = check_rule($_[1], $_[0], 1, $name); | |||||||||||||||||||||||||||
| 32 | 2 | shift; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 9 | shift; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | my $args = (@DB::args == 1 && ref $DB::args[0] eq 'HASH') | ||||||||||||||||||||||||||||
| 39 | 56 | ▶ | ? +{ %{$DB::args[0]} } # must be hash | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@DB::args == 1 and ref $DB::args[0] eq "HASH"
MC/DC50%1/2
@DB::args == 1 and ref $DB::args[0] eq "HASH"
@DB::args == 1
ref $DB::args[0] eq "HASH"
Truth table66%2/3
A and B
A= @DB::args == 1B= ref $DB::args[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 40 | : +{ @DB::args }; # must be key-value list | ||||||||||||||||||||||||||||
| 41 | 56 | my $kv = {}; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # args my $var => RULE | ||||||||||||||||||||||||||||
| 44 | # ~~~~ ~~~~ | ||||||||||||||||||||||||||||
| 45 | # undef defined | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 56 | ▶ | for (my $i = 0; $i < @_; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 71 | ▶ | (my $name = var_name(1, \$_[$i])) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | or Carp::croak('Usage: args my $var => RULE, ...'); | ||||||||||||||||||||||||||||
| 50 | 71 | $name =~ s/^\$//; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # with rule (my $foo => RULE, ...) | ||||||||||||||||||||||||||||
| 53 | 71 | ▶ | if (defined $_[$i+1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 64 | $_[$i] = $kv->{$name} = check_rule($_[$i+1], $args->{$name}, exists $args->{$name}, $name); | |||||||||||||||||||||||||||
| 55 | 49 | delete $args->{$name}; | |||||||||||||||||||||||||||
| 56 | 49 | $i++; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | # without rule (my $foo, my $bar, ...) | ||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||
| 60 | 7 | ▶ | unless (exists $args->{$name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 1 | Carp::confess("Required parameter '$name' not passed"); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 6 | $_[$i] = $kv->{$name} = delete $args->{$name}; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 40 | for my $name (sort keys %$args) { | |||||||||||||||||||||||||||
| 68 | 3 | Carp::confess("Unexpected parameter '$name' passed"); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 37 | return $kv; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 28 | ▶ | sub args_pos { | ||||||||||||||||||||||||||
|
Subroutine
args_pos: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||
| 76 | 28 | package DB; | |||||||||||||||||||||||||||
| 77 | # call of caller in DB package sets @DB::args, | ||||||||||||||||||||||||||||
| 78 | # which requires list context, but we don't need return values | ||||||||||||||||||||||||||||
| 79 | 28 | () = CORE::caller(1); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 28 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 28 | ▶ | my $name = var_name(1, \$_[0]) || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
var_name(1, \$_[0]) || ''
MC/DC0%0/1
var_name(1, \$_[0]) || ''
var_name(1, \$_[0])
Truth table50%1/2
A
A= var_name(1, \$_[0])
| |||||||||||||||||||||||||||||
| 84 | 28 | ▶ | if ($is_invocant{$name}) { # seems instance/class method call | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 7 | $name =~ s/^\$//; | |||||||||||||||||||||||||||
| 86 | 7 | $_[0] = shift @DB::args; | |||||||||||||||||||||||||||
| 87 | 7 | ▶ | if (defined $_[1]) { # has rule? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 2 | $_[0] = check_rule($_[1], $_[0], 1, $name); | |||||||||||||||||||||||||||
| 89 | 1 | shift; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 6 | shift; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 27 | my $args = [@DB::args]; | |||||||||||||||||||||||||||
| 96 | 27 | my $kv = {}; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # args my $var => RULE | ||||||||||||||||||||||||||||
| 99 | # ~~~~ ~~~~ | ||||||||||||||||||||||||||||
| 100 | # undef defined | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 27 | ▶ | for (my $i = 0; $i < @_; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 38 | ▶ | (my $name = var_name(1, \$_[$i])) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | or Carp::croak('Usage: args_pos my $var => RULE, ...'); | ||||||||||||||||||||||||||||
| 105 | 38 | $name =~ s/^\$//; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # with rule (my $foo => RULE, ...) | ||||||||||||||||||||||||||||
| 108 | 38 | ▶ | if (defined $_[$i+1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 33 | $_[$i] = $kv->{$name} = check_rule($_[$i+1], $args->[0], @$args > 0, $name); | |||||||||||||||||||||||||||
| 110 | 25 | shift @$args; | |||||||||||||||||||||||||||
| 111 | 25 | $i++; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | # without rule (my $foo, my $bar, ...) | ||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||
| 115 | 5 | ▶ | unless (@$args > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | Carp::confess("Required parameter '$name' not passed"); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 5 | $_[$i] = $kv->{$name} = shift @$args; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 19 | ▶ | if (@$args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 2 | Carp::confess('Too many parameters passed'); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 17 | return $kv; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 16 | 1; | |||||||||||||||||||||||||||
| 130 | __END__ | ||||||||||||||||||||||||||||