in reply to "if" and compound statement
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[...]; ... }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: "if" and compound statement
by juliosergio (Sexton) on Feb 13, 2012 at 16:48 UTC | |
|
Re^2: "if" and compound statement
by JavaFan (Canon) on Feb 13, 2012 at 17:10 UTC | |
by BrowserUk (Patriarch) on Feb 13, 2012 at 17:36 UTC |