We can compare things, and find out whether our comparison statement is true or not. The operators we use for this are:
Table 6-1. Comparison operators
Operator | Example | Meaning |
---|---|---|
== | $a == $b | Equality (same as in C and other C-like languages) |
!= | $a != $b | Inequality (again, C-like) |
< | $a < $b | Less than |
> | $a > $b | Greater than |
<= | $a <= $b | Less than or equal to |
>= | $a >= $b | Greater than or equal to |
If we're comparing strings, we use a slightly different set of comparison operators, as follows:
Table 6-2. String comparison operators
Operator | Meaning |
---|---|
eq | Equality |
ne | Inequality |
lt | Less than (in "asciibetical" order) |
gt | Greater than |
le | Less than or equal to |
ge | Greater than or equal to |
Some examples:
69 > 42 # true "0" == 3 - 3 # true 'apple' gt 'banana' # false; apple is alphabetically before # banana 1 + 2 == "3com" # true - 3com is evaluated in numeric # context because we used == not eq |
Assigning undef to a variable name undefines it again, as does using the undef function with the variable's name as its argument.
We can also check whether things are defined (something is defined when it's had a value assigned to it), or whether an element of a hash exists.
To find out if something is defined, use Perl's defined function. You can't just use the name of the variable because the variable can be defined an still evaluate to false - for example, if you assign it the value 0.
$skippy = "bush kangaroo"; if (defined($skippy)) { print "Skippy is defined.\n"; } else { print "Skippy is undefined.\n"; } |
The defined function is described in the Camel on page 155.
To find out if an element of a hash exists, use the exists function:
my %animals = ( "Skippy" => "bush kangaroo", "Flipper" => "faster than lighting", ); if (exists($animals{"Blinky Bill"}) { print "Blinky Bill exists.\n"; } else { print "Blinky Bill doesn't exist.\n"; } |
There's a bit of explanation of the difference between a hash key "existing" and being "defined" on page 164 of the Camel.
One last quick example to clarify existence, definedness and truth:
my %miscellany = ( "apple" => "red", # exists, defined, true "howmany" => 0, # exists, defined, false "koala" => undef, # exists, undefined, false ); if (exists($miscellany("wombat")) { # doesn't exist print "Wombat exists\n"; } else { print "We have no wombats here.\n"; # this will happen } |
Boolean logic operators can be used to combine two or more Perl statements, either in a conditional test or elsewhere.
The short circuit operators come in two flavours: line noise, and English. Both do similar things but have different precedence. This causes great confusion. There are two ways of avoiding this: use lots of brackets, or read page 89 of the Camel book very, very carefully.
Alright, if you insist: and and or operators have very low precendence (i.e. they will be evaluated after all the other operators in the condition) whereas && and || have quite high precedence and may require parentheses in the condition to make it clear which parts of the statement are to be evaluated first.
Table 6-3. Boolean logic operators
English-like | C-style | Example | Result |
---|---|---|---|
and | && | $a && $b | True if both $a and $b are true; acts on $a then if $a is true, goes on to act on $b. |
or | || | $a || $b | True if either of $a and $b are true; acts on $a then if $a is false, goes on to act on $b. |
Here's how you can use them to combine conditions in a test:
$a = 1; $b = 2; $a == 1 and $b == 2 # true $a == 1 or $b == 5 # true $a == 2 or $b == 5 # false ($a == 1 and $b == 5) or $b == 2 # true (parenthesized expression # evaluated first) |
These operators aren't just for combining tests in conditional statements --- they can be used to combine other statements as well.
Here's a real, working example of the || short circuit operator:
open(INFILE, "input.txt") || die("Can't open input file: $!"); |
What is it doing?
The open() function can be found on page 191 of the Camel, if you want to look at how it works.
The && operator is less commonly used outside of conditional tests, but is still very useful. Its meaning is this: If the first operand returns true, the second will also happen. As soon as you get a false value returned, the expression stops evaluating.
($day eq 'Friday') && print "Have a good weekend!\n"; |
The typing saved by the above example is not necessarily worth the loss in readability, especially as it could also have been written:
print "Have a good weekend!\n" if $day eq 'Friday'; if ($day eq 'Friday') { print "Have a good weekend!\n"; } |
...or any of a dozen other ways. That's right, there's more than one way to do it.
The most common usage of the short circuit operators, especially || (or or) is to trap errors, such as when opening files or interacting with the operating system.
Short circuit operators are covered from page 89 of the Camel book.