On Wednesday, Nov 19, 2003, at 15:27 US/Pacific, Freimuth,Robert wrote:
I have a subroutine that is contained within its own module and package. It
currently takes 7 different arguments, but only 2 of them are required
because I can set defaults for the other 5.
let me see if I understand your idea here you have something that sets the default values for any arguments not passed:
sub some_function
{
my ($req1,$req2, $opt1,$opt2,$opt3,$opt4,$opt5 ) s=
(undef,undef, 'val1', 'val2','val3','val4,'val5'); ($req1,$req2, $opt1,$opt2,$opt3,$opt4,$opt5 ) = @_;
....}
Since I anticipate using this
module in multiple programs, I'd like to keep the parameter list as generic
as possible (in terms of order and requirements).
That is a worthy cause, but remember that the arguments that will be passed in need to be passed in a specific order:
my ($input1, $input2) = qw/bob fried/;
my $answer1 = some_function($input1, $input2);
my $answer2 = some_function($input2, $input1);are not the same inputs, since the ordering is important.
For that reason, I'd like
to use the Getopt::Long module to parse the parameter list, but as far as I
can tell it will only look at @ARGV. I could simply do a @ARGV = @_, but I'd
hate to alter @ARGV without knowing what might still be there.
Have you thought about merely passing a reference to a Hash???? And then managing the excecptions?
the Getopt::Long module is way too 'heavy' for passing arguments around between functions.
This would allow you something like:
sub get_default_options
{
my $ref = shift; $ref->{req1} ||= 'val1';
$ref->{req2} ||= 'val2';
$ref->{req3} ||= 'val3';
$ref->{req4} ||= 'val4;
$ref->{req5} ||= 'val5;
} sub some_function
{
my $hash_ref = shift; die "not a hash ref\n"
unless( ref($hash_ref) eq 'HASH');
die "undefined option one"
unless( exists($hash_ref->{'opt1'};
die "undefined option two"
unless( exists($hash_ref->{'opt2'}; get_default_options($hash_ref);
...
}you might want to use caller - cf perldoc caller - to be a bit more explicit about how the CALLER was not nice to you...
Does anyone know if Getopt can be pointed to a different array other than
@ARGV? I couldn't find anything in the docs or in the archives.
if you do perldoc -m Getopt::Long you will notice that the module is using @ARGV, since it is a module for parsing the command line arguments, and not a general purpose parser.
Remember it is trying to deal with issues like
-a -ifg --bob=frodo
and that is WAY too heavy for merely passing between functions.
I am willing to write my own parser for the argument list (especially since I
only have to handle a maximum of 7 parameters), but I'd hate to reinvent the
wheel if there is an easier (and more flexible) way of doing it. My other
idea was to add the 5 non-required variables to @EXPORT_OK and set them in
the calling function before passing the remaining 2 required parameters, but
I'd rather not pollute too many namespaces.
If it is any help, I sooo think that this is a bad idea. Exporting 'defaults' is, well, messy as you note, and may not even be taking you in the right direction.
ciao drieux
---
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
