in reply to Command line options
For the specific case you cited, you don't really need to know where the value comes from--you just want the "right" one to "win". I tend to do it by calculating the least interesting value first, then overwriting with the "better" values. In that way, I don't really care which value wins. I just know that the last (most important) one does. For example:
use strict; use warnings; use Getopt::Long; # General default value my $foo = 'default'; # Let environment override the default if (defined $ENV{FUBAR}) { $foo = $ENV{FUBAR}; } # The command-line option overwrites if it exists GetOptions("foo=s"=>\$foo); print "The foo parameter is '$foo'\n";
A few different usages gives:
$ perl foo.pl The foo parameter is 'default' $ export FUBAR='Gragnar' $ perl foo.pl The foo parameter is 'Gragnar' $ perl foo.pl -foo=BARBAZ The foo parameter is 'BARBAZ' $
This technique has its shortcomings, obviously, such as when (a) the computation is expensive or (b) has undesirable side effects.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Command line options
by rpelak (Sexton) on Nov 01, 2011 at 20:26 UTC | |
by graff (Chancellor) on Nov 02, 2011 at 07:52 UTC | |
by rpelak (Sexton) on Nov 02, 2011 at 17:12 UTC |
In Section
Seekers of Perl Wisdom