for example:
if (something)
function();
else
nope();
for example:
if (something)
function();
else
nope();
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;
usually you would use the conditional operator:
something ? function() : nope;
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!
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.
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);