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.
In reply to Re: Usage of flags in scripts
by Lawliet
in thread Usage of flags in scripts
by jordandanford
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |