File Coverage

blib/lib/Getopt/GetArgs.pm
Criterion Covered Total %
statement 29 29 100.0
branch 11 12 91.6
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 44 46 95.6


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2              
3             package Getopt::GetArgs;
4              
5 7     7   42908 use strict;
  7         17  
  7         308  
6 7     7   49 use vars qw(@ISA @EXPORT $VERSION);
  7         17  
  7         556  
7 7     7   38 use Exporter;
  7         16  
  7         8644  
8             @ISA = qw(Exporter);
9             @EXPORT = qw(GetArgs);
10             $VERSION = "1.03";
11              
12             =head1 NAME
13              
14             GetArgs - Perl module to allow enhanced argument passing,
15             including passing of case-insensitive named arguments as
16             well as positioned arguments.
17              
18             =head1 SYNOPSIS
19              
20             sub WHATEVER {
21             my @DEFAULT_ARGS =
22             ( Content => "Default content",
23             Verbose => 0
24             );
25             my %ARGS=GetArgs(@_,@DEFAULT_ARGS);
26             # do some stuff with $ARGS{Content}
27             # show all kinds of detail if $ARGS{Verbose}
28             }
29              
30             # a simple call to WHATEVER
31             WHATEVER( "Just deal with my content" );
32              
33             # a flexible call to WHATEVER
34             WHATEVER({ verbose => 1,
35             content => "This is my content",
36             });
37              
38             =head1 DESCRIPTION
39              
40             GetArgs needs to know
41             * what your subroutine was passed,
42             * and what it expected to be passed.
43             * If you like, you can also supply default values to use when an argument is not passed.
44            
45             Using this information, GetArgs will create a hash of arguments for you to use throughout your subroutine. Using GetArgs has several advantages:
46              
47             1) Calls to your subroutine can pass named arguments, making the code more readable.
48             2) If it's easier to pass a list of arguments as you normally would, that's fine.
49             3) With GetArgs your use of arguments in your subroutine code is more readable.
50             4) Your subroutines are no longer limited in the number of arguments they expect.
51             5) Arguments can be passed in any order (if passed inside the hash ref), thus
52             only the arguments relevant to that call need to be passed--unnecessary
53             arguments can be ignored.
54             6) Case is not important, as GetArgs matches argument names case insensitively.
55              
56             =head1 AUTHOR
57              
58             Special thanks to Sam Mefford who helped design and
59             wrote most of the code for the original version.
60              
61             Much polishing in preparation for release to CPAN
62             was performed by Earl Cahill. (earl@cpan.org)
63              
64             Maintained by Rob Brown (rob@roobik.com)
65              
66             =head1 COPYRIGHT
67              
68             Copyright (c) 2001, Rob Brown. All rights reserved.
69             Getopt::GetArgs is free software; you can redistribute
70             it and/or modify it under the same terms as Perl itself.
71              
72             $Id: GetArgs.pm,v 1.4 2001/06/08 06:26:44 rob Exp $
73              
74             =cut
75              
76             sub GetArgs (\@\@) {
77              
78             ### set up variables to take the referenced arguments
79 6     6 0 21020 my ($PASSED_ARGS_ref,$DEFAULT_ARGS_ref) = @_;
80 6         18 my (@arg_names,%default_values,$arg_name,$expect_name);
81              
82 6         40 %default_values=@$DEFAULT_ARGS_ref;
83 6         19 @arg_names=();
84 6         45 foreach ( 0..$#$DEFAULT_ARGS_ref ) {
85 36 100       132 push(@arg_names,$DEFAULT_ARGS_ref->[$_]) if ($_ % 2) == 0;
86             }
87              
88             ### hash that will be returned
89 6         19 my %returnARGS;
90              
91             ####################
92             ### Check if the last argument passed to our calling function is
93             ### a hash ref. If so use the hash values for arguments unless
94             ### the function was expecting a hash ref as the last argument.
95             ### Match passed keys to expected keys case-insensitively.
96             ####################
97             # is the last argument passed a hash ref?
98 6 100       40 if (ref($PASSED_ARGS_ref->[$#$PASSED_ARGS_ref]) eq "HASH") {
99             # is the corresponding expected parameter defaulting to a hash ref?
100 3 50       32 if ( ref($DEFAULT_ARGS_ref->[($#$PASSED_ARGS_ref * 2) + 1]) ne "HASH" ) {
101 3         12 my %arg_hash = %{pop @$PASSED_ARGS_ref};
  3         17  
102 3         24 foreach $arg_name (keys %arg_hash) {
103 6         27 foreach $expect_name (keys %default_values) {
104 18 100       390 if ($arg_name =~ /^$expect_name$/i) {
105 6         21 $returnARGS{$expect_name} = $arg_hash{$arg_name};
106             }
107             }
108             }
109             }
110             }
111              
112             ### for the remaining arguments of the calling function fill in
113             ### with a default value if they are not set, or overwrite with
114             ### the ordered list arguments of the calling function
115 6         23 foreach (@arg_names) {
116 18 100       86 if ( @$PASSED_ARGS_ref ) {
    100          
117 6         19 $returnARGS{$_} = shift(@$PASSED_ARGS_ref);
118             } elsif ( !defined($returnARGS{$_}) ) {
119 6         24 $returnARGS{$_} = $default_values{$_};
120             }
121             }
122              
123             ### all done
124 6         48 return %returnARGS;
125             }
126              
127             1;