Hi,
My understanding of the ternary operator is (test) ? <do if true> : <do if false>

But in the code below I am not convinced the way it is working. Infact, it is working like (test) ? <do if false> : <do if true>

Monks, please bear with my ignorance...

my $fname = "My First Name"; my $mname = "My Middle Name"; my $lname = "My Last Name"; &tst_array1 ($fname, $mname, $lname); &tst_array2 ($fname, $mname, $lname); &tst_array3 ($fname, $mname, $lname); sub tst_array1 { my ($param1, $param2, $param3); $param1 = shift; @_ ? $param2 = "Shift" : $param2 = shift; @_ ? $param3 = "Shift" : $param3 = shift; print "In tst_array1:\n$param1, $param2, $param3\n"; } sub tst_array2 { my ($param1, $param2, $param3); $param1 = shift; @_ ? $param2 = shift : $param2 = "Shift"; @_ ? $param3 = shift : $param3 = "Shift"; print "In tst_array2:\n$param1, $param2, $param3\n"; } sub tst_array3 { $param1 = shift; print "\@_ = @_: \$param1 = $param1\n"; $param2 = shift; print "\@_ = @_: \$param2 = $param2\n"; $param3 = shift; print "\@_ = @_: \$param3 = $param3\n"; print "In tst_array3:\n$param1, $param2, $param3\n"; }
The result:
In tst_array1:
My First Name, My Middle Name, My Last Name
In tst_array2:
My First Name, Shift, Shift @_ = My Middle Name My Last Name: $param1 = My First Name @_ = My Last Name: $param2 = My Middle Name @_ = : $param3 = My Last Name
In tst_array3:
My First Name, My Middle Name, My Last Name

janitored by ybiC: Moved <code> tags such that they're only around codeblocks, and balanced <readmore> tags also. Retitled from "Perl Ternary operator" as per consideration


In reply to Confused by Perl ternary operator by newest_newbie

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.