
An if elsif statement in Perl has the following structure.
if (comparison) {
--do something--
}
elsif (comparison) {
--do something--
}
else {
--do something
}
For example.
if ($foo == 10) {
print "foo equals 10.";
}
elsif ($foo == 20) {
print "foo equals 20.";
}
else {
print "foo does not equal 10 or 20.";
}
Following are common if statements.
If variable or boolean is defined
#!/usr/bin/perl
if ($foo) {
print "\$foo is defined";
} else {
print "\$foo is NOT defined";
}
unless ($foo) {
print "\$foo is NOT defined";
} else {
print "\$foo is defined";
}
Equals | if ($foo == $bar) # integer if ($foo == True) # boolean if ($foo eq $bar) # string |
Does not equal | if ($foo != $bar) # integer if ($foo != False) # boolean if ($foo ne $bar) # string |
Greater than | if ($foo > $bar) # integer if ($foo gt $bar) # string |
Less than | if ($foo < $bar) # integer if ($foo lt $bar) # string |
Greater than or equal to | if ($foo >= $bar) # integer if ($foo ge $bar) # string |
Less than or equal to | if ($foo <= $bar) # integer if ($foo le $bar) # string |
If file or directory exists | if ( -e "/path/to/file.txt") if ( -e "/path/to/directory") |
If file or directory does not exist | if ( not -e "/path/to/file.txt") if ( not -e "/path/to/directory") |
If file is readable | if ( -r "/path/to/file.txt") |
If file is writable | if ( -w "/path/to/file.txt") |
If variable is empty (null) | if ($foo) |
If variable is not empty (not null) | if (not $foo) |
If array is empty (null) | if (@foo == 0) |
If array is not empty (not null) | if (@foo != 0) |
If variable contains | if ($foo =~ /bar/i) |
If variable does not contain | if ($foo !~ /bar/i) |
If multiple variables contain | if (grep{$_ =~ /hello/} ($foo, $bar)) |
If multiple variables do not contain | if (grep{$_ !~ /hello/} ($foo, $bar)) |
If array contains | if (grep(/bar/, @foo )) |
If array does not contain | if (not grep(/bar/, @foo )) |
If array contains exact match | if (grep(/^bar$/, @foo )) |
If array does not contain exact match | if (not grep(/^bar$/, @foo )) |
If hash contains | if (grep(/bar/, %hash )) |
If hash does not contain | if (not grep(/bar/, %hash )) |
If file contains / does not contain | refer to this article |
If string begins with | if ($foo =~ /^bar/) |
If string does not begin with | if ($foo !~ /^bar/) |
If string ends with | if ($foo =~ /bar$/) |
If string does not end with | if ($foo !~ /bar$/) |
Ignore case
The i option can be used to ignore case.
if ($foo =! /bar/i)
Multiple conditions
Multiple conditions can be evaluated by separating each condition with and or or.
if ($foo == "hello" and $bar == "world")
Or with or.
if ($foo == "hello" or $bar == "world")
Sometimes a syntax like this is used.
if ( ($foo == "hello") or ($bar == "world") )
However, this might be a better syntax. The ^ and $ regular expression characters are used to ensure the $foo variable equals hello or world (not contains).
if ( $foo =~ /^(hello|world)$/ )
Or the ^ and $ regular expression characters can be discarded to determine if the $foo variable contains hello or world.
if ( $foo =~ /hello|world/ )
Did you find this article helpful?
If so, consider buying me a coffee over at