Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

if/else syntax

by Anonymous Monk
on Jun 26, 2009 at 15:03 UTC ( #775080=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I was writing some code other day when a thought if it's possible to write a if/else without brackets.
When it's just a if perl got a nice syntax:
someFunc() if($someVar==$anotherVar);
but what if I want a else ?
in C it would be like:
if(someVar==anotherVar)someFunc(); else anotherFunc();
there is any way I can do this in perl ?
PS. I already know the (a?b:c) syntax, but I don't like it.

Replies are listed 'Best First'.
Re: if/else syntax
by ikegami (Pope) on Jun 26, 2009 at 15:08 UTC

    Brackets are mandatory on if statements in Perl.

    Note that, unlike C and Pascal, these are defined in terms of BLOCKs, not statements. This means that the curly brackets are required--no dangling statements allowed.

    The optionality of the brackets is a source of problems for C.

    In Perl 6, it's the parens around the condition that are optional. They're already optional around the condition of statement modifiers in Perl 5.

    someFunc() if ($someVar == $anotherVar); someFunc() if $someVar == $anotherVar; # Same thing
      so if the brackets are there to avoid problems, the C/perl syntax (a?b:c) also has those problems right ?

        (a?b:c) also has those problems right ?

        No, because the ":" isn't optional like else is, and because people don't indent the conditional operator like they would an if/else.

        It has a different problem, though. A frequent mistake with the conditional operator is to use it as follows:

        $c ? $x = $i : $y = $j;
        The above means
        ( $c ? $x = $i : $y ) = $j;
        but people expect it to mean
        $c ? ($x = $i) : ($y = $j);

        I don't think so. With if/else:

        if a if b c else d
        could mean
        if a { if b { c } else { d } } or if a { if b { c } } else { d }

        With the ternary operator that would be

        a ? b ? c : d : () and a ? b ? c : () : d

        so there's no abmbiguity.

        what you are looking for is:
        if ($cond==1) { do_good(); } else { do_bad(); }

        "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: if/else syntax
by suaveant (Parson) on Jun 26, 2009 at 15:07 UTC
    If you want an "else" with an if, you need the brackets, "if" after the statement is a modifier, not a control block... if you just need the else, use unless
    print "OK\n" unless $a == 1;

    Update: I suppose you could do

    print "OK\n" if $a == 1; print "Not OK\n" unless $a == 1;
    But don't. ;)

                    - Ant
                    - Some of my best work - (1 2 3)

Re: if/else syntax
by Herkum (Parson) on Jun 26, 2009 at 16:03 UTC

    The closest thing I can think of, is using a return statement in your call. So you have to be specific in your usage. FYI, I use this syntax all the time because it is very easy to read.

    sub checkConditions { my $value = shift; return someFunc() if $value == 0; return otherFunc(); }
Re: if/else syntax
by massa (Hermit) on Jun 26, 2009 at 17:46 UTC
    you could do
    $somevar == $anothervar and (someFunc(), 1) or anotherFunc();
    but please don't. it's awful.
    []s, HTH, Massa (κς,πμ,πλ)
Re: if/else syntax
by Marshall (Abbot) on Jun 27, 2009 at 04:56 UTC
    Perl has a nice thing that allows "reversing" the if and a SINGLE action statement.

    This is not what you have here!
    And I would argue that your code C code is bad because it is not clear.


    if ( $someVar == $anotherVar ) { someFunc(); } else { anotherFunc(); }
    You are making a HUGE mistake if you think that number of source code lines equals efficiency - I would recommend some advanced C classes or ASM classes. The compiler whether it be "Perl" or "C" can deal with the above construct VERY efficiently.
      One can start a holy war (specially in a Perl forum) about more characters/lines versus readability/maintainability. I, for instance, am of the school of thought that goes "as long it does not _obscure_ the meaning, the shorter the better". That way, I prefer
      $someVar == $anotherVar ? someFunc() : anotherFunc()
      over _any_ of the alternatives because it's elegant, concise and _absolutely_ clear. ikegami pointed out to me that ?: can get really messy really fast. I agree. But in the case at hand, it's unbeatable IMHO.
      []s, HTH, Massa (κς,πμ,πλ)
        I don't want to start a holy war either! Your formulation is completely acceptable to me. For a short thing like this, I use it all the time. The OP said that he/she didn't like this syntax. If so, then use if{}else{} (my opinion). I don't see a problem here. As a matter of style, I would put parens around the conditional, but that is a very minor point.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://775080]
Approved by suaveant
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2017-12-16 22:49 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (459 votes). Check out past polls.