| 1 | #!/usr/bin/perl -w | ||||||||||||||
| 2 | |||||||||||||||
| 3 | package Getopt::GetArgs; | ||||||||||||||
| 4 | |||||||||||||||
| 5 | 7 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 7 | ▶ | use vars qw(@ISA @EXPORT $VERSION); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 7 | ▶ | use Exporter; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 7 | @ISA = qw(Exporter); | |||||||||||||
| 9 | 7 | @EXPORT = qw(GetArgs); | |||||||||||||
| 10 | 7 | $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 | 6 | ▶ | sub GetArgs (\@\@) { | ||||||||||||
|
Subroutine
GetArgs: called CC=11 SCAR=24.0
Pod
undocumented
| |||||||||||||||
| 77 | |||||||||||||||
| 78 | ### set up variables to take the referenced arguments | ||||||||||||||
| 79 | 6 | my ($PASSED_ARGS_ref,$DEFAULT_ARGS_ref) = @_; | |||||||||||||
| 80 | 6 | my (@arg_names,%default_values,$arg_name,$expect_name); | |||||||||||||
| 81 | |||||||||||||||
| 82 | 6 | %default_values=@$DEFAULT_ARGS_ref; | |||||||||||||
| 83 | 6 | @arg_names=(); | |||||||||||||
| 84 | 6 | foreach ( 0..$#$DEFAULT_ARGS_ref ) { | |||||||||||||
| 85 | 36 | ▶ | push(@arg_names,$DEFAULT_ARGS_ref->[$_]) if ($_ % 2) == 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | ### hash that will be returned | ||||||||||||||
| 89 | 6 | 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 | ▶ | if (ref($PASSED_ARGS_ref->[$#$PASSED_ARGS_ref]) eq "HASH") { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | # is the corresponding expected parameter defaulting to a hash ref? | ||||||||||||||
| 100 | 3 | ▶ | if ( ref($DEFAULT_ARGS_ref->[($#$PASSED_ARGS_ref * 2) + 1]) ne "HASH" ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | 3 | my %arg_hash = %{pop @$PASSED_ARGS_ref}; | |||||||||||||
| 102 | 3 | foreach $arg_name (keys %arg_hash) { | |||||||||||||
| 103 | 6 | foreach $expect_name (keys %default_values) { | |||||||||||||
| 104 | 18 | ▶ | if ($arg_name =~ /^$expect_name$/i) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | 6 | $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 | foreach (@arg_names) { | |||||||||||||
| 116 | 18 | ▶ | if ( @$PASSED_ARGS_ref ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 117 | 6 | $returnARGS{$_} = shift(@$PASSED_ARGS_ref); | |||||||||||||
| 118 | } elsif ( !defined($returnARGS{$_}) ) { | ||||||||||||||
| 119 | 6 | $returnARGS{$_} = $default_values{$_}; | |||||||||||||
| 120 | } | ||||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | ### all done | ||||||||||||||
| 124 | 6 | return %returnARGS; | |||||||||||||
| 125 | } | ||||||||||||||
| 126 | |||||||||||||||
| 127 | 7 | 1; | |||||||||||||