line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package PDL::NamedArgs; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
583
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
40
|
|
4
|
|
|
|
|
|
|
#use strict; |
5
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
623
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( |
19
|
|
|
|
|
|
|
parseArgs |
20
|
|
|
|
|
|
|
) ] ); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our @EXPORT = qw( |
25
|
|
|
|
|
|
|
parseArgs |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
our $VERSION = '0.12'; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub parseArgs |
30
|
|
|
|
|
|
|
{ |
31
|
8
|
|
|
8
|
1
|
141
|
my($funcDef)=shift; |
32
|
8
|
|
|
|
|
9
|
my(%named,@unnamed,@arg_names); |
33
|
8
|
|
|
|
|
10
|
my($status,$i)=(0); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Build up the arg_names array and the arg_defaults hash |
36
|
8
|
|
|
|
|
39
|
for $i (split(/[ ,]+/,$funcDef)) |
37
|
|
|
|
|
|
|
{ |
38
|
40
|
100
|
|
|
|
92
|
if ($i =~ m/^([a-zA-Z]\w*)=(.*)$/) |
39
|
16
|
|
|
|
|
29
|
{ push(@arg_names,lc($1)); $arg_defaults{lc($1)}=$2; } |
|
16
|
|
|
|
|
41
|
|
40
|
|
|
|
|
|
|
else |
41
|
24
|
|
|
|
|
38
|
{ push(@arg_names,lc($i)); } |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Walk thru the arguments passed and separate into %named & @unnamed arguments |
45
|
8
|
|
|
|
|
25
|
while ($#_>=0) |
46
|
|
|
|
|
|
|
{ |
47
|
30
|
|
|
|
|
34
|
$i=shift; |
48
|
30
|
100
|
66
|
|
|
450
|
if (!ref($i) && grep(/^$i$/i,@arg_names)) # Named argument |
49
|
|
|
|
|
|
|
{ |
50
|
14
|
50
|
|
|
|
27
|
if (exists($named{$i})) |
51
|
0
|
|
|
|
|
0
|
{ return ("Error: Argument $i multiple definitions"); } # Whoops, somebody went overboard... |
52
|
14
|
|
|
|
|
39
|
$named{$i}=shift; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
else # Unnamed argument |
55
|
16
|
|
|
|
|
37
|
{ 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
|
|
|
|
|
10
|
for $i (@arg_names) |
60
|
|
|
|
|
|
|
{ |
61
|
40
|
100
|
|
|
|
65
|
if (exists($named{$i})) # Argument already defined via named argument (Priority #1) |
62
|
14
|
|
|
|
|
17
|
{ next; } |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# Argument is not defined |
65
|
26
|
100
|
|
|
|
39
|
if (@unnamed) |
|
|
50
|
|
|
|
|
|
66
|
16
|
|
|
|
|
26
|
{ $named{$i}=shift(@unnamed); } # Grab one of the unnamed list if available (Priority #2) |
67
|
|
|
|
|
|
|
elsif (exists($arg_defaults{$i})) |
68
|
10
|
|
|
|
|
24
|
{ $named{$i}=$arg_defaults{$i}; } # Set to the default value if defined (Priority #3) |
69
|
|
|
|
|
|
|
else |
70
|
0
|
|
|
|
|
0
|
{ return ("Error: Missing $i argument"); } # Whoops, somebody forgot something... |
71
|
|
|
|
|
|
|
} |
72
|
8
|
50
|
|
|
|
16
|
if (@unnamed) |
73
|
0
|
|
|
|
|
0
|
{ return ("Error: Too many arguments"); } # Whoops, somebody went overboard... |
74
|
|
|
|
|
|
|
|
75
|
8
|
|
|
|
|
54
|
return ($status,%named); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; |
79
|
|
|
|
|
|
|
__END__ |