in reply to Ternary Operator for Multiple Arguments Passing

There is another problem with this bit of logic: If you want a default for the first argument ($based_on), you still have to specify it:
mycode.pl tp 5
if you do not, the script will assume the value you passed for $top is your $based_on, eg:
mycode.pl 5
So this logic model does not make sense. You can differentiate between the arguments for example by putting a '-' in front of the $based_on argument, or if the argument passed to $top is always numeric, you can use that as well. Best is to ensure your arguments are exactly right:
1 #!/usr/bin/perl -w 2 3 use strict; 4 5 my $based_on = ($ARGV[0] && ($ARGV[0] =~/tp|sn/)) ? shift : 't +p'; 6 my $top = ($ARGV[0] && ($ARGV[0] =~ /[1-5]/)) ? shift : 1; 7 scalar(@ARGV) && die "Arguments not parsed: " . join(':', @ARG +V) . "\n"; 8 9 print "$based_on $top\n";
-Reenen

Replies are listed 'Best First'.
Re^2: Ternary Operator for Multiple Arguments Passing
by Anonymous Monk on Dec 21, 2005 at 14:25 UTC
    Oops. I should have ^ and $ in my regexes to force the whole thing to match. -Reenen