| 1 | package Smart::Options::Declare; | ||||||||||||||||||||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use Smart::Options; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use PadWalker qw/var_name/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 11 | our @EXPORT = qw(opts opts_coerce); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | our $COERCE = { | ||||||||||||||||||||||||||||
| 13 | Multiple => { | ||||||||||||||||||||||||||||
| 14 | type => 'ArrayRef', | ||||||||||||||||||||||||||||
| 15 | 2 | ▶ | generater => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | if ( defined $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | return [ | ||||||||||||||||||||||||||||
| 18 | split( | ||||||||||||||||||||||||||||
| 19 | qr{,}, | ||||||||||||||||||||||||||||
| 20 | 2 | ▶ | ref( $_[0] ) eq 'ARRAY' ? join( q{,}, @{ $_[0] } ) : $_[0] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | ) | ||||||||||||||||||||||||||||
| 22 | ]; | ||||||||||||||||||||||||||||
| 23 | } else { | ||||||||||||||||||||||||||||
| 24 | 0 | return $_[0]; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 11 | }; | |||||||||||||||||||||||||||
| 29 | 11 | my %is_invocant = map{ $_ => undef } qw($self $class); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 28 | ▶ | sub opts { | ||||||||||||||||||||||||||
|
Subroutine
opts: called CC=22 SCAR=31.2
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | { | ||||||||||||||||||||||||||||
| 33 | 28 | package DB; | |||||||||||||||||||||||||||
| 34 | 28 | () = caller(1); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 28 | ▶ | if ( exists $is_invocant{ var_name( 1, \$_[0] ) || '' } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
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])
| |||||||||||||||||||||||||||||
| 38 | 6 | $_[0] = shift @DB::args; | |||||||||||||||||||||||||||
| 39 | 6 | shift; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 28 | my $opt = Smart::Options->new(); | |||||||||||||||||||||||||||
| 43 | 28 | $opt->type(config => 'Config'); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 28 | ▶ | for ( my $i = 0 ; $i < @_ ; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 36 | ▶ | ( my $name = var_name( 1, \$_[$i] ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | or Carp::croak('usage: opts my $var => TYPE, ...'); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 36 | $name =~ s/^\$//; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 36 | ▶ | if ($name =~ /_/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 5 | (my $newname = $name) =~ s/_/-/g; | |||||||||||||||||||||||||||
| 53 | 5 | $opt->alias($newname => $name); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 5 | $name = $newname; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 36 | my $rule = $_[$i+1]; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 36 | ▶ | if ($rule) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 32 | ▶ | if (ref($rule) && ref($rule) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $rule and ref $rule eq "HASH"
MC/DC50%1/2
ref $rule and ref $rule eq "HASH"
ref $rule
ref $rule eq "HASH"
Truth table66%2/3
A and B
A= ref $ruleB= ref $rule eq "HASH"
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 17 | ▶ | if ($rule->{default}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 2 | $opt->default($name => $rule->{default}); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 17 | ▶ | if ($rule->{required}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 3 | $opt->demand($name); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 17 | ▶ | if ($rule->{alias}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 6 | $opt->alias($rule->{alias} => $name); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 17 | ▶ | if ($rule->{comment}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 1 | $opt->describe($name => $rule->{comment}); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 17 | ▶ | if (my $isa = $rule->{isa}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 17 | ▶ | if ($isa eq 'Bool') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | $opt->boolean($name); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 17 | $opt->type($name => $isa); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||||||||||
| 87 | 15 | ▶ | if ($rule eq 'Bool') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $opt->boolean($name); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 15 | $opt->type($name => $rule); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | #auto set alias | ||||||||||||||||||||||||||||
| 95 | 36 | ▶ | if (length($name) > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 17 | $opt->alias(substr($name,0,1) => $name); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 36 | ▶ | $i++ if defined $_[$i+1]; # discard type info | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 28 | ▶ | while (my ($isa, $c) = each(%$COERCE)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 31 | $opt->coerce($isa => $c->{type}, $c->{generater}); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 28 | my $argv = $opt->parse; | |||||||||||||||||||||||||||
| 107 | 24 | ▶ | for ( my $i = 0 ; $i < @_ ; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 29 | ▶ | ( my $name = var_name( 1, \$_[$i] ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | or Carp::croak('usage: opts my $var => TYPE, ...'); | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 29 | $name =~ s/^\$//; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 29 | $_[$i] = $argv->{$name}; | |||||||||||||||||||||||||||
| 114 | 29 | ▶ | $i++ if defined $_[$i+1]; # discard type info | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 1 | ▶ | sub opts_coerce { | ||||||||||||||||||||||||||
|
Subroutine
opts_coerce: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 1 | my ($isa, $type, $generater) = @_; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | $COERCE->{$isa} = { type => $type, generater => $generater }; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 11 | 1; | |||||||||||||||||||||||||||
| 125 | __END__ | ||||||||||||||||||||||||||||