What does the syntax
if (1) {}
do?
I can't find documentation for this syntax and 1 is not being treated as a boolean. Am I right?.
What does the syntax
if (1) {}
do?
I can't find documentation for this syntax and 1 is not being treated as a boolean. Am I right?.
Technically, the if (1)
does nothing interesting, since it's an always-executed if-statement.
One common use of an if (1)
though is to wrap code you'd like to be able to quickly disable, say for debugging purposes. You can quickly change an if (1)
to if (0)
to disable the code.
1
is indeed being treated as a boolean value in this case, which always evaluates to true
.
This is basically an if
statement which always passes (making it unnecessary in the first place).
It runs the code block. It's equivalent to:
{
...
}
Added by ikegami:
No, it's not.
{ ... }
is a "bare loop", a loop that executes only once. It is affected by next
, last
and redo
.
if (1) { ... }
is not a loop. It is not affected by next
, last
and redo
.
>perl -E"for (1..3) { say $_; if (1) { next; } say $_; }"
1
2
3
>perl -E"for (1..3) { say $_; { next; } say $_; }"
1
1
2
2
3
3
However, they are similar in that both create a lexical scope.
When in doubt, use Deparse
:
$ perl -MO=Deparse -e'if (1) {}'
do {
()
};
-e syntax OK
Looks like 1 is being treated as a boolean true by virtue of the fact that the if
doesn't evaluate to false. See How do I use boolean variables in Perl?
In Perl, boolean statements are false if they return a value of 0
and true otherwise. A numeric value returns its value.
In that statement, the user is forcing the value of the if
statement to be true:
if (1) {
print "This statement is always true\n";
}
And, of course:
if (0) {
print "This statement is false, and you'll never see this printed\n";
}
The user could have written something like this too:
if ( 1 == 1 ) {
print "This statement is always true\n"
}
You don't usually see this in an if
statement. Why bother with an if
if the condition is always true? However, it's quite common for while
statements if you need an infinite loop:
while (1) {
print "Keep doing this over, and over again\n";
}
Usually, there's an escape clause:
my $count = 0
while (1) {
$count = $count + 1;
print "The Count is now $count\n";
last if $total > 100;
}
The only reason I can think someone would want to do this is some sort of debug checking. That is, most of the time the statement would be false, but if set to true, print out some sort of debug statement:
if (0) { #Change from "0" to "1" to debug
print "DEBUG: The value of foo = $foo\n";
}
However, there are better ways of handling something like this, such as through a constant:
use constant {
DEBUG => 0,
};
if (DEBUG) {
print "DEBUG: The value of foo = $foo\n";
}
Or through a debug subroutine.
In perl
, the following values are considered false
0
Empty String
Everything else is considered true
So, if (1)
is considered to always be true
if(1)
is indeed returning true every time, it does nothing except define a scope, similar to a code block