>>>>> "SHC" == Shawn H Corey <[email protected]> writes:

  SHC> On Mon, 29 Mar 2010 12:25:12 -0400
  SHC> "Uri Guttman" <[email protected]> wrote:

  >> when will people learn??

  SHC> They can't learn to do it properly because so much Perl documentation
  SHC> is out of date.


and what is out of date regarding the ternary operator? 

   Conditional Operator
       Ternary "?:" is the conditional operator, just as in C.  It works much
       like an if-then-else.  If the argument before the ? is true, the
       argument before the : is returned, otherwise the argument after the :
       is returned.  For example:

           printf "I have %d dog%s.\n", $n,
                   ($n == 1) ? '' : "s";

       Scalar or list context propagates downward into the 2nd or 3rd
       argument, whichever is selected.

           $a = $ok ? $b : $c;  # get a scalar
           @a = $ok ? @b : @c;  # get an array
           $a = $ok ? @b : @c;  # oops, that's just a count!

       The operator may be assigned to if both the 2nd and 3rd arguments are
       legal lvalues (meaning that you can assign to them):

           ($a_or_b ? $a : $b) = $c;

       Because this operator produces an assignable result, using assignments
       without parentheses will get you in trouble.  For example, this:

           $a % 2 ? $a += 10 : $a += 2

       Really means this:

           (($a % 2) ? ($a += 10) : $a) += 2

       Rather than this:

           ($a % 2) ? ($a += 10) : ($a += 2)

       That should probably be written more simply as:

           $a += ($a % 2) ? 10 : 2;


that shows the precedence issue and even corrects it to the proper
single assignment. i wouldn't mind if it emphasized how wrong it is to
do side effects in it. but all those newbies who use it incorrectly and
then ask about why it seems to be broken, never would read that entry or
even read it to completion. so when i cover this topic here i always
LOUDLY emphasize its proper usage.

uri

-- 
Uri Guttman  ------  [email protected]  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to