Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Getopt::Long good style?

by wufnik (Friar)
on Jul 28, 2004 at 12:50 UTC ( [id://378013]=note: print w/replies, xml ) Need Help??


in reply to Getopt::Long good style?

hola monseiur Skeeve.

normally i would be absolutely the last person to wade in with any points re: style, mostly because my points re: code feng shui are invariably ignored, but being a heavy Getopt::Long user, i have one simple point i feel may be relevant.

i frequently find i use functions, within the key GetOptions call of Getopt::Long, to actually parse command line arguments. one such script needs to get a list of variables which i want to calculate conditional probabilities for: here is he call to GetOptions.

GetOptions(\%runconfig, "load=s" => \&loadAttributeDescriptions, "loadconfig=s" => \&loadconfig, "dumpconfig=s" => \$dumpconfig, "tables=s" => \$tables, "prec:i" => \$precision, "decorate" => \$decorate, "i=s@", # include a nominal field. "arff", # arff file [data mining] "max:i", # examine file to this length "quiet", # do not spit out csv "filter=s" => \&addfilter, "cp=s" => \&addcp, # array of [ant [ dependents ] ] "cptformat=s", # cp table format "prune", # get rid of 0 info columns "help" => \$help, "man" => \$man ) or pod2usage(-verbose=>1) && exit(1);
as you can see, it's long, and there are a number of functions that have to be called to deal with the vagaries of what i need & want.

at least some of these vagaries are useful. witness the function addcp: this allows me to tell the script which conditional probabilities i need evaluated, ie:
cptplus.pl --cp="charm|perlmonk,cyberpunk,neovictorian"
useful, but it requires the overhead of a function, which goes something like...
sub addcp{ my $cpstring = $_[1]; $cpstring =~ s/^([A-Za-z\-\_0-9]+)\|//; my $antecedent = $1 || return; my @dependents = split(/,/, $cpstring); my @cptrequest = ($antecedent, \@dependents); push @{$runconfig{"cp"}},(\@cptrequest); }
not particularly complex - but there are a number of these functions that i call. and do i want them littering the call to GetOptions, where they not only look ugly, but prevent me breaking at them easily in my debugger?

not really. if i were to implement all those functions anonymously, it would get to be a real mess there in GetOptions, and i would lose substantial brownie points if my grandmother took it into her head to look at the code. i mean, it's just not very neat and not very functional either, when it comes to debugging.

now you may object that your scripts are rarely as complex as the above, and it looks nicer. but i would say that from a defensive coding perspective, you'd be better using real subs. if your script is a worthy brainchild, you will use it more, and want to see it grow. give it the room it needs!

...wufnik

-- in the world of the mules there are no rules --

Replies are listed 'Best First'.
Re^2: Getopt::Long good style?
by Skeeve (Parson) on Jul 28, 2004 at 13:37 UTC
    To cut it short you mean: "It gets confusing if you overuse these anonymous subs", right?

    I do not intend to use any other sub than help as anonymous. I love to have the help text at the very beginning of my scripts, because you see the help as soon as you look into the script.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://378013]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-23 14:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found