Conditions (if)
if-elsif-else, unless, and number/text comparison operators.
Simple enough for a 5-year-old to understand
if / else
my $score = 75;if ($score >= 50) { print "Passed\n";} else { print "Failed\n";}if-elsif-else, unless, and number/text comparison operators.
Simple enough for a 5-year-old to understand
my $score = 75;if ($score >= 50) { print "Passed\n";} else { print "Failed\n";}Conditions in Perl are written with if, elsif, and else. The key detail to watch out for is the spelling of the middle keyword: it is written as elsif, as a single word with a single e. Spellings from other languages will cause a syntax error in Perl.
Perl's truthiness rules are unique. The values considered false are: undef, the number zero, the empty string, and the single-character string "0". Everything else is true — even the string "0.0" is true.
Perl also offers the unless structure to read a condition in reverse. It is a readable way to write a negative condition, but it gets confusing when nested or used with else; it should be preferred for simple cases.
In input validation, you need to know the difference between an undefined value and an empty value; they are two different cases.
In single-line checks, postfix-style conditions shorten the code and are widely accepted in Perl.
Wrong
my $x = 5;
if ($x > 10) {
print "large\n";
} elseif ($x > 3) {
print "medium\n";
}In Perl, the middle branch is written as elsif; elseif or else if will cause a syntax error. This is a small spelling difference that varies from language to language, but the compiler shows no tolerance.
Correct
my $x = 5;
if ($x > 10) {
print "large\n";
} elsif ($x > 3) {
print "medium\n";
}Wrong
my $deger = "0.0";
if ($deger) {
print "filled\n";
}There is no error here, but the result is surprising: the string "0.0" is considered true and the block runs. Perl only considers the single-character string "0" to be false. If you want to perform a numerical check, you must explicitly compare the value with a number.
Correct
my $deger = "0.0";
if ($deger != 0) {
print "filled\n";
}Wrong
my $ad;
if ($ad eq "") {
print "empty\n";
}Because the variable is undefined, the comparison produces a "Use of uninitialized value" warning. Being undefined and being an empty string are different situations and must be tested separately. There is a separate function that checks for undefinedness.
Correct
my $ad;
if (!defined $ad) {
print "undefined\n";
}It is attaching a condition by writing if or unless at the end of a single expression. Curly braces are not needed and it improves readability in short checks. It is very common in Perl code and considered a language-specific idiom.
When the negative form of the condition reads more naturally. However, when unless is used together with else, a double negative occurs and it becomes hard to understand; a standard if should be preferred in that case.
Not in the standard language. The alternative is to write an elsif chain or map keys through a hash. Hash mapping is both faster and more readable in multi-branch situations.
The middle branch is written as elsif.
Values considered false are limited.
The string "0.0" is considered true.
Undefinedness is tested with defined.
Postfix-style conditions are for short checks.