in reply to Best Practice: How do I pass option parameters to functions?

On a more general note, it is my preference that any time a function (or method) accepts more than one parameter then it is time to use "named parameters" to prevent future headaches.

For example:

#!/usr/bin/perl -w use warnings; use strict; sub processDir { my $args = shift; # note, default for shift here is @_; my $ret = qx($args->{script} $args->{dir}); chomp($ret); print qq(Output:"$ret"\n) if $args->{verbose}; } # Normally via environment variable my $script = "echo -e"; # Normally via glob and processing in this script my @dirs = qw(dir1 dir2 dir3); # Normally via Getopt::Long my $verbose = 1; for my $dir (@dirs) { # Do some stuff, then: processDir( { dir => $dir, script => $script, verbose => $verbose +} ); # which would be exactly the same as: processDir( { script = > $script, dir => $dir, verbose => $verbose + } ); # or this: processDir( { verbose => $verbose, script => $script, dir => $dir +} ); # or any other combination you can think of. }

There is much, much more that can be said about this, but knowing about the idea should give you the start for some very interesting research.

Note: Yes, I know I have left out a LOT of potential gotchas in this very crude example. But I feel the essence is clear and a minimal amount of research should reveal a wealth of information on the concept that will stand OP in good stead for a long time to come.

Update: Another advantage of named parameters is that with a little thought you can deal with the situation where you might start out with a single parameter passed in the normal way, but then allow for named parameters in the future. This example is taken from (my module) DateTimeX::Fiscal::Fiscal5253 (Please read the docs on CPAN to understand just exactly what this allows):

sub contains { my $self = shift; my %args = @_ == 1 ? ( date => shift ) : @_; $args{date} ||= 'today'; $args{style} ||= $self->{_style}; croak 'Unknown parameter present' if scalar(keys(%args)) > 2; ... }

You will find several examples of how I use this technique in the source.

The answer to the question "Can we do this?" is always an emphatic "Yes!" Just give me enough time and money.