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


In reply to Re: Ternary Operator for Multiple Arguments Passing by davido
in thread Ternary Operator for Multiple Arguments Passing by neversaint

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.