| 1 | package PDL::NamedArgs; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | #use strict; | ||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # Items to export into callers namespace by default. Note: do not export | ||||||||||||||||||||||||||||
| 12 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||||||||||||||||||||
| 13 | # Do not simply export all your public functions/methods/constants. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # This allows declaration use PDL::NamedArgs ':all'; | ||||||||||||||||||||||||||||
| 16 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||||||||||||||||||||||||||||
| 17 | # will save memory. | ||||||||||||||||||||||||||||
| 18 | 1 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||||||||||||||||||||
| 19 | parseArgs | ||||||||||||||||||||||||||||
| 20 | ) ] ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | our @EXPORT = qw( | |||||||||||||||||||||||||||
| 25 | parseArgs | ||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | 1 | our $VERSION = '0.12'; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub parseArgs | ||||||||||||||||||||||||||||
| 30 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parseArgs: called CC=12 SCAR=25.3
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 8 | my($funcDef)=shift; | |||||||||||||||||||||||||||
| 32 | 8 | my(%named,@unnamed,@arg_names); | |||||||||||||||||||||||||||
| 33 | 8 | my($status,$i)=(0); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Build up the arg_names array and the arg_defaults hash | ||||||||||||||||||||||||||||
| 36 | 8 | for $i (split(/[ ,]+/,$funcDef)) | |||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||
| 38 | 40 | ▶ | if ($i =~ m/^([a-zA-Z]\w*)=(.*)$/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 16 | { push(@arg_names,lc($1)); $arg_defaults{lc($1)}=$2; } | |||||||||||||||||||||||||||
| 40 | else | ||||||||||||||||||||||||||||
| 41 | 24 | { push(@arg_names,lc($i)); } | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Walk thru the arguments passed and separate into %named & @unnamed arguments | ||||||||||||||||||||||||||||
| 45 | 8 | ▶ | while ($#_>=0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | { | ||||||||||||||||||||||||||||
| 47 | 30 | $i=shift; | |||||||||||||||||||||||||||
| 48 | 30 | ▶ | if (!ref($i) && grep(/^$i$/i,@arg_names)) # Named argument | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $i and grep /^$i$/i, @arg_names
MC/DC50%1/2
not ref $i and grep /^$i$/i, @arg_names
not ref $i
grep /^$i$/i, @arg_names
Truth table66%2/3
not(A) and B
A= not ref $iB= grep /^$i$/i, @arg_names
| |||||||||||||||||||||||||||||
| 49 | { | ||||||||||||||||||||||||||||
| 50 | 14 | ▶ | if (exists($named{$i})) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | { return ("Error: Argument $i multiple definitions"); } # Whoops, somebody went overboard... | |||||||||||||||||||||||||||
| 52 | 14 | $named{$i}=shift; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | else # Unnamed argument | ||||||||||||||||||||||||||||
| 55 | 16 | { push @unnamed,$i; } | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # Walk thru the argument names & make sure they are set, if not use the default if defined | ||||||||||||||||||||||||||||
| 59 | 8 | for $i (@arg_names) | |||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||
| 61 | 40 | ▶ | if (exists($named{$i})) # Argument already defined via named argument (Priority #1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 14 | { next; } | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # Argument is not defined | ||||||||||||||||||||||||||||
| 65 | 26 | ▶ | if (@unnamed) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 66 | 16 | { $named{$i}=shift(@unnamed); } # Grab one of the unnamed list if available (Priority #2) | |||||||||||||||||||||||||||
| 67 | elsif (exists($arg_defaults{$i})) | ||||||||||||||||||||||||||||
| 68 | 10 | { $named{$i}=$arg_defaults{$i}; } # Set to the default value if defined (Priority #3) | |||||||||||||||||||||||||||
| 69 | else | ||||||||||||||||||||||||||||
| 70 | 0 | { return ("Error: Missing $i argument"); } # Whoops, somebody forgot something... | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 8 | ▶ | if (@unnamed) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | { return ("Error: Too many arguments"); } # Whoops, somebody went overboard... | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 8 | return ($status,%named); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | 1; | |||||||||||||||||||||||||||
| 79 | __END__ | ||||||||||||||||||||||||||||