tags:

views:

177

answers:

6

for example:

if (something)
    function();
else
    nope();
+3  A: 

They can be:

$something ? function() : nope();

Update: More generally, it's because, as Jonathan points out, Larry says so. There are other cases that the curly brace syntax can be thrown out:

function() if $something;

nope() foreach @foo;

function() while <FH>;

Or even:

function() and nope() if $something;
BipedalShark
Or to use the original example: `function() if $something or nope() and 0`
mobrule
This isn't quite right: `function()` and `nope()` in your examples are not in its own scope. They are operating in the same scope as the entire conditional or foreach. I guess it depends on how you interpret the question: is he asking "why can't I have a one-liner in a separate scope without braces", or "why can't I simply have a conditional all on one line" (you answer the latter).
Ether
+1  A: 

usually you would use the conditional operator:

something ? function() : nope;

ennuikiller
+1  A: 

you mean like something ? function() : nope(); ?

klausbyskov
+11  A: 

It depends on what you mean by "why?".

The first turtle on the way down is that Perl control structures are defined in terms of BLOCKs, and not in terms of statements (as in C). And a BLOCK in Perl is delimited by curlies.

The next turtle on the way down would be Larry Wall's feelings about why BLOCKs belong there instead of statements!

Jonathan Feinberg
The first answer really just seems to be begging the question. Please describe the second turtle more — that's where the more interesting answer lies.
Rob Kennedy
Don't bother changing this answer, though, since it was evidently sufficient for the asker. Instead, please see the question I think ought to have been asked: http://stackoverflow.com/questions/1876851/why-do-perl-control-statements-require-braces
Rob Kennedy
Why would I "bother changing this answer?" I carefully qualified it by saying that "Why?" can be interpreted ant number of ways. No, I'm not "begging the question," because I'm not trying to establish some proposition by implicitly assuming its truth, which is what "begging the question" means. As for "the second turtle", you'd have to either read Larry Wall's mind, or find some documentation of the the rationale behind the design decision to use BLOCKs rather than statements in control structures.
Jonathan Feinberg
You might have changed this answer because I asked you to elaborate. If that was presumptuous, I apologize. Later, I decided that's not what I wanted anymore because it was really a different question, so I asked a new one and linked to it. And that question proves I don't have to read Wall's mind; there are others who have talked to him and learned the answer. You were begging the question by referring to the docs to explain why the language allows what it does, when I figured the docs are where one would *learn* about the brace requirement before asking for rationale.
Rob Kennedy
+1  A: 

Because Perl always requires braces around blocks - which simplifies its grammar a bit.

You always have to write:

if (something) { function(); } else { nope(); }

Or use a conditional operator as others have suggested.

Jonathan Leffler
+1  A: 

If you don't need the else, you can make a one-liner using if or unless at the end of a line.

For example:

function() if (something);

or

function() unless (something);
Ken Aspeslagh