perlcapt has asked for the wisdom of the Perl Monks concerning the following question:
I'm looking for suggestions on how to beautify/clean_up my subroutine argument references. Here is the situation:
I have many options to my application, all of which are stored in an object attributes structure. Typical Getopt::Long elements looks like this:
Updated to reflect proper variable prefix for list refs. thanks to davido for waking me up about my syntax. -ben
What I find ugly is the use of $_[1] to reference the value that GetOptions parses for each option. $_[0] is the option name. $_[1,2] are used for values or references to hashes and lists. I suppose I could just pass the whole list to the object method, but that gets ugly since most of these methods are designed to store/retrieve attributes and defined by an AUTOLOAD subroutine down in the class i.e. package.my $opts = GetOptions ( "input|source|i:s" => sub { if($_[1]) { $new->source($_[1]); }else{ $new->source($last->source); } }, "verbose" => sub { $new->verbose($_[1]); }, # and so on
Updated to reflect proper variable prefix for list refs. thanks to davido for waking me up about my syntax. -ben
perlcapt
-ben
-ben
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: beautifying Getopt::Long in situ subs
by davido (Cardinal) on Apr 02, 2006 at 17:05 UTC |
Back to
Seekers of Perl Wisdom