in reply to Usage of flags in scripts
Well, I believe everyone will recommend you use a module to do this. I suggest using either getopt::long or getopt::simple.
#!/usr/bin/perl use warnings; use strict; use Getopt::Long; use 5.010; # Just to use say() my ($mean, $sum); my $result = GetOptions ("mean" => \$mean); my @nums = @ARGV; foreach (@nums) { say; # Only calculate the mean if flag exists if ($mean) { $sum += $_; } } say $sum/@nums if $mean; __END__ $ perl mean.pl 1 3 2 1 3 2 $ perl mean.pl -m 1 3 2 1 3 2 2 $ perl mean.pl -mean 1 3 2 1 3 2 2
Notice how Getopt::Long allows the user to type just enough of the switch to trigger it. If there was another switch called meek, the user would have to distinguish which switch they were triggering by typing either -mee (or -meek) or -mea (or -mean).
Updated with example.
And you didn't even know bears could type.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Usage of flags in scripts
by Your Mother (Archbishop) on Jan 01, 2009 at 00:03 UTC | |
by McDarren (Abbot) on Jan 01, 2009 at 03:17 UTC | |
by JadeNB (Chaplain) on Jan 01, 2009 at 18:25 UTC | |
by Lawliet (Curate) on Jan 01, 2009 at 02:21 UTC | |
|
Re^2: Usage of flags in scripts
by jordandanford (Initiate) on Jan 01, 2009 at 01:24 UTC | |
by Lawliet (Curate) on Jan 01, 2009 at 02:03 UTC | |
by JadeNB (Chaplain) on Jan 01, 2009 at 18:28 UTC | |
by jordandanford (Initiate) on Jan 01, 2009 at 02:19 UTC | |
by Marshall (Canon) on Jan 01, 2009 at 03:00 UTC | |
by Marshall (Canon) on Jan 01, 2009 at 03:14 UTC |