in reply to Ternary Operator for Multiple Arguments Passing

Slightly opaque bug there. ;)

shift without args, in main code is the same as shift @ARGV; I'm sure we agree there. Well, when you shift the first value off of @ARGV, that changes the array, so in your second comparison, you're looking at what used to be the third element.

For example:

#perl mytest.pl sn 5 # Before anything else is done, @ARGV looks like: # $ARGV[0] = 'sn' # $ARGV[1] = '5' my $based_on = $ARGV[0] ? shift: 'tp'; # Now @ARGV looks like this: # $ARGV[0] = '5' # $ARGV[1] = undef my $top = $ARGV[1] ? shift: 1; # It's going to return '1' because $ARGV[1] was undef, due to # the first shift.

What you should try instead is:

my $based_on = defined( $ARGV[0] ) ? $ARGV[0] : 'tp'; my $top = defined( $ARGV[1] ) ? $ARGV[1] : 1;

...or using shift:

my $based_on = defined( $ARGV[0] ) ? shift : 'tp'; my $top = defined( $ARGV[1] ) ? shift : 1;

Dave

Replies are listed 'Best First'.
Re^2: Ternary Operator for Multiple Arguments Passing
by eric256 (Parson) on Dec 21, 2005 at 03:38 UTC

    Your last example has the same bug as the OP. It should be;

    my $based_on = defined( $ARGV[0] ) ? shift : 'tp'; my $top = defined( $ARGV[0] ) ? shift : 1;


    ___________
    Eric Hodges $_='y==QAe=e?y==QG@>@?iy==QVq?f?=a@iG?=QQ=Q?9'; s/(.)/ord($1)-50/eigs;tr/6123457/- \/|\\\_\n/;print;

      How funny, and what irony! :) It goes to show how easy it is to make that kind of mistake if you let your guard down. You're correct, of course, and that's what I intended to say.

      Cheers!


      Dave