Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

if else in the other way

by Anonymous Monk
on Oct 02, 2010 at 05:54 UTC ( #863050=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

how to use "else" with this conditional scheme:
my $a = 6;
print "yes" if $a == 6;

i know the other classic way works
my $a = 2; if ($a == 6) {print "yes";} else {print "no";}

Comment on if else in the other way
Download Code
Re: if else in the other way
by jwkrahn (Monsignor) on Oct 02, 2010 at 06:29 UTC

    You probably want something like this:

    print $a == 6 ? 'yes' : 'no';
Re: if else in the other way
by chromatic (Archbishop) on Oct 02, 2010 at 06:41 UTC

    There is no else clause available when you use the postfix if.

Re: if else in the other way
by biohisham (Priest) on Oct 02, 2010 at 10:29 UTC
    Or you can use this which is essentially similar to jwkrahn's but a little bit more readable in this context
    $a == 6 ? print "yes\n" : print "no\n";



    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .
      but a little bit more readable

      Debatable :-)

      For a simple yes/no print like this you are probably right. For lengthier outputs with, perhaps, a conditional part in the middle your way involves a lot of repeated text which can obscure meaning. Personally, in such cases I find this layout

      print qq{Fixed part }, ( conditional expression ) ? qq{True part } : qq{False part }, qq{Further fixed text here\n};

      more readable than this

      ( conditional expression ) ? print qq{Fixed part True part Further fixed text here\n} : print qq{Fixed part False part Further fixed text here\n};

      particularly if the condition doesn't require parentheses, but others will probably disagree. I would be more likely to write the latter using an if ( ... ) { ... } else { ... } construct anyway as the meaning is more widely familiar.

      Cheers,

      JohnGG

        I agree ;), and I agree that this is debatable, I am a fan of not repeating commands while using a ternary operator too, in fact, avoiding ternary operators all together should my code be maintained by someone else, however, my answer was relative to this simple requirement put forth by the OP, breaking jwkrahn's reply down for him/her.



        Excellence is an Endeavor of Persistence. A Year-Old Monk :D .

      Compared to jwkrahn's simple and clear version (with one print), this "improvement" (with print occurring twice) is a violation of the DRY principle.

      See also Perl Best Practices: Chapter 6 (Control Structures), Tabular Ternaries; and Chapter 2 (Code Layout), Ternaries.

Re: if else in the other way
by CountZero (Bishop) on Oct 02, 2010 at 12:59 UTC
    Using else:
    my $a = 2; unless ($a == 6) { print "no"; } else print "yes"; }

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: if else in the other way
by Marshall (Prior) on Oct 02, 2010 at 13:43 UTC
    Your first code doesn't have an "else".

    Yes, Perl does have the ternary operator like in C, but in a practical sense there is no difference in performance between that and "if", "else". Write the code that is the easiest to understand for you and the folks who will have to understand the code later. Spacing and indenting do matter..this is part of the art of programming.

    How "short" the code is, often just doesn't matter at all. How clear the code is, always matters. And that matters more than some "one size fits all coding standard". The code below just has different spacing than your 2nd version, but I hope the difference is clear.

    my $a = 2; if ($a == 6) {print "yes" } else {print "no" }
Re: if else in the other way
by snoopy (Deacon) on Oct 04, 2010 at 23:41 UTC
    Another way:
    print qw(no yes)[$a == 6]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://863050]
Approved by FalseVinylShrub
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2014-08-27 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (235 votes), past polls