Re: if/else syntax
by ikegami (Patriarch) 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
| [reply] [d/l] [select] |
|
so if the brackets are there to avoid problems, the C/perl syntax (a?b:c) also has those problems right ?
| [reply] [d/l] |
|
(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);
| [reply] [d/l] [select] |
|
|
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.
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
what you are looking for is:
if ($cond==1) {
do_good();
}
else {
do_bad();
}
-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
| [reply] [d/l] |
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)
| [reply] [d/l] [select] |
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();
}
| [reply] [d/l] |
Re: if/else syntax
by massa (Hermit) on Jun 26, 2009 at 17:46 UTC
|
$somevar == $anothervar and (someFunc(), 1) or anotherFunc();
but please don't. it's awful.
[]s, HTH, Massa (κς,πμ,πλ)
| [reply] [d/l] |
Re: if/else syntax
by Marshall (Canon) on Jun 27, 2009 at 04:56 UTC
|
Perl has a nice thing that allows "reversing" the if and a SINGLE action statement. single_action_statement...if(blah)...
This is not what you have here!
And
I would argue that your code C code is bad because it is not clear.
better:
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.
| [reply] [d/l] |
|
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 (κς,πμ,πλ)
| [reply] [d/l] [select] |
|
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.
| [reply] |