Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

"if" and compound statement

by juliosergio (Sexton)
on Feb 13, 2012 at 15:58 UTC ( [id://953484]=perlquestion: print w/replies, xml ) Need Help??

juliosergio has asked for the wisdom of the Perl Monks concerning the following question:

I'm new here, and in Perl as well. I thought I'd learnt that

if (1) { statement; }

is more or less the same as:

statement if (1);

However, I'm puzzled with the following, that uses the compound statement and doesn't work

{print "one\n"; print "two\n";} if (1);

Do you have any comments on this?

Thanks! -Sergio.

Replies are listed 'Best First'.
Re: "if" and compound statement
by BrowserUk (Patriarch) on Feb 13, 2012 at 16:13 UTC

    The postfix form of if is a statement modifier, not a block modifier. And unadorned curlies form a block.

    You could do either:

    print( "one\n"), print( "two\n" ) if (1);

    Or:

    do{ print "one\n"; print "two\n"; } if (1);

    Though there is little advantage of the latter over:

    if( 1 ){ print "one\n"; print "two\n"; }

    But most people will generally dislike it if you do either.

    As an aside, your example is not a good one, as two print statements can always be combined:

    print "one\ntwo\n" if (1);

    Personally, I find that there are cases for which compounding statements with a postfix if makes sense. Eg:

    while( my $input = <> ) { warn( "Bad input" ), next unless $input =~ m[...]; ... }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      As an aside, your example is not a good one, as two print statements can always be combined
      If you're going to nitpick about examples, I'm going to nitpick about your use of always. If the default filehandle is tied, if autodie is enabled, or if strings are overloaded, combining the given statements can lead the different results.

        Consider me duly chastened. Even though I only added the aside in an attempt to avoid the nitpickers :)


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

      Thanks! I really appreciate your comments..
      -Sergio.

Re: "if" and compound statement
by choroba (Cardinal) on Feb 13, 2012 at 16:05 UTC
    From perlsyn:
    Any simple statement may optionally be followed by a SINGLE modifier, just before the terminating semicolon (or block ending).
    Emphasis mine (not upper-case).
Re: "if" and compound statement
by educated_foo (Vicar) on Feb 13, 2012 at 16:07 UTC
    You can combine statements with "do", like so:
    do { statement; statement } if $condition;
Re: "if" and compound statement
by tobyink (Canon) on Feb 13, 2012 at 22:14 UTC

    Your question has already been well and truly answered, but as an aside, the parentheses around the condition with postfix if are optional. So you can write:

    statement if 1;

    They are not optional for the block form of if though. The following is incorrect:

    if 1 { statement; }

    It's also worth mentioning that Damian Conway's Perl Best Practices suggests that you never use the postfix form of if, and always use the block form. (PBP #63) I happen to disagree with that particular PBP; most people have some PBPs that they disagree with, but PBP is usually a good starting point when developing your own Perl coding style.

Re: "if" and compound statement
by GrandFather (Saint) on Feb 13, 2012 at 23:33 UTC

    Not just "doesn't work" - it's a syntax error which should suggest to you that the animal you have by the tail is not the one you thought you had.

    As a general thing we often suggest to newbies to always use strictures (use strict; use warnings;), but in this case it fails without needing them - however the advice is still good!

    True laziness is hard work
      As a general thing, it's often recommended to eat a serving of vegetables, and two pieces of fruit everyday. The OP's code would have failed regardless how much veggies and fruit he has consumed, however, the advice is still good!

        Sure, and if the OP had said he was a six year old Perl newbie I might have offered other good life advice too. But this is a Perl oriented site and the OP gave no such indication so I didn't. Strictures, strict in particular, are such useful tools for catching typos and other errors, that suggesting their use to self avowed Perl newbies is pretty much always good advice.

        True laziness is hard work
Re: "if" and compound statement
by ikegami (Patriarch) on Feb 14, 2012 at 06:06 UTC

    It's

    EXPR if EXPR;

    and not

    STATEMENT if EXPR;

    You can use do (do { ... }if EXPR;), but not a bare loop ({ ... } if EXPR;).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-18 13:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found