Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Re: if-else vs. tertiary effieciency?

by blaze (Friar)
on Sep 14, 2002 at 23:15 UTC ( [id://197949]=note: print w/replies, xml ) Need Help??


in reply to Re: if-else vs. tertiary effieciency?
in thread if-else vs. tertiary effieciency?

First of all, i'd write:print $var ? 'yes' : 'no' wouldnt this print yes or no depending on $var...it looks like the Anonymous Monk wants to print yes or no depending on whether $var has a value or not
$var ? print 'yes' : print 'no'
seems right to me, am i missing something?

Update: I just started learning how ?: works, so i just wanted to know why
print $var ? 'yes' : 'no'
will also work, i guess im not understanding how it evaluates $var...the way im reading ?: is if 1st arg (print $var) then second (yes) else third(no) so how does it know that you actually want to print yes or no? with the way im looking at it, it is
if(print $var){ 'yes' }else{ 'no' }
anyone care to explain to me, id appreciate it greatly

Replies are listed 'Best First'.
Re: Re: Re: if-else vs. tertiary effieciency?
by rir (Vicar) on Sep 15, 2002 at 03:38 UTC
    Think of it as:
    print ( $boolean ? $true_value : $false_value );
    or in your terms:
    print ( if($var}{ 'yes' }else{ 'no' } );
    I like this less because it is pseudo-code that looks
    much like code. So it seems to imply many things that
    aren't true. The conditional operator is only an
    operator not a statement.
      so the print isnt part of the ?: in fruiture's example...if thats the case then it makes beautiful sense and i cant believe i didnt see it before, if thats not the case im a little more lost than i was to begin with :)

      ill be sure to ++ you when i get my next set of votes ;)
        If in doubt, deparse:
        $ perl -MO=Deparse,-p -e'print $var ? "yes" : "no";' print(($var ? 'yes' : 'no')); -e syntax OK
        The ternary operator here has a higher precedence than the print that works as a list operator, so Perl does the right thing.

        Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://197949]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 01:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found