Perl has distinct operators == and eq for numerical and lexical comparisons respectively. An asterisk "*" indicates that a comparison raises a warning.

Note: unlike PHP and JavaScript, Perl does not have a true boolean, only false. When a comparison succeeds, 1 is returned. Also, false is not directly accessible.

Perl lacks the === "strict equality" operator.

Some variables

my $false = (0 == 1); my @arr0 = (); my %hash0 = (); my @arr1 = ('perl'); my %hash1 = ('perl' => 'equality');

Note that the literal expression () behaves differently in equality comparisons from either @arr0 or %hash0 . Similarly ('perl') behaves differently from @arr1 and ('perl' => 'equality') behaves differently from %hash1 .

Numerical equality

== undef () '' $false 'perl' 0 '0' (0) ('0') @arr0 %hash0 1 '1' (1) ('1') @arr1 %hash1 undef true** true** true** true* true** true* true* true* true* true* true* false* false* false* false* false* false** () true** true** true** true* true** true* true* true* true* true* true* false* false* false* false* false* false** '' true** true** true** true* true** true* true* true* true* true* true* false* false* false* false* false* false** $false true* true* true* true true* true true true true true true false false false false false false* 'perl' true** true** true** true* true** true* true* true* true* true* true* false* false* false* false* false* false** 0 true* true* true* true true* true true true true true true false false false false false false* '0' true* true* true* true true* true true true true true true false false false false false false* (0) true* true* true* true true* true true true true true true false false false false false false* ('0') true* true* true* true true* true true true true true true false false false false false false* @arr0 true* true* true* true true* true true true true true true false false false false false false* %hash0 true* true* true* true true* true true true true true true false false false false false false* 1 false* false* false* false false* false false false false false false true true true true true true* '1' false* false* false* false false* false false false false false false true true true true true true* (1) false* false* false* false false* false false false false false false true true true true true true* ('1') false* false* false* false false* false false false false false false true true true true true true* @arr1 false* false* false* false false* false false false false false false true true true true true true* %hash1 false** false** false** false* false** false* false* false* false* false* false* true* true* true* true* true* true**

Lexical equality

eq undef () '' $false 'perl' 0 '0' (0) ('0') @arr0 %hash0 1 '1' (1) ('1') @arr1 %hash1 undef true** true** true* true* false* false* false* false* false* false* false* false* false* false* false* false* false* () true** true** true* true* false* false* false* false* false* false* false* false* false* false* false* false* false* '' true* true* true true false false false false false false false false false false false false false $false true* true* true true false false false false false false false false false false false false false 'perl' false* false* false false true false false false false false false false false false false false false 0 false* false* false false false true true true true true true false false false false false false '0' false* false* false false false true true true true true true false false false false false false (0) false* false* false false false true true true true true true false false false false false false ('0') false* false* false false false true true true true true true false false false false false false @arr0 false* false* false false false true true true true true true false false false false false false %hash0 false* false* false false false true true true true true true false false false false false false 1 false* false* false false false false false false false false false true true true true true false '1' false* false* false false false false false false false false false true true true true true false (1) false* false* false false false false false false false false false true true true true true false ('1') false* false* false false false false false false false false false true true true true true false @arr1 false* false* false false false false false false false false false true true true true true false %hash1 false* false* false false false false false false false false false false false false false false true

To catch warnings in Perl

local $SIG{'__WARN__'} = sub { my $warning = shift; # etc. };

Why does the table look like this?

When Perl needs to interpret an expression as a scalar, such as for binary comparisons, arithmetic or string operations, it follows these rules:

For a list expression such as (0) , ('0') , (1) or ('1') , the last expression in the list is used. If the list is empty i.e. () , "the last expression in the list" is considered to be undef . For an array expression such as @arr0 or @arr1 , the length of the array is used. For a hash expression such as %hash0 or %hash1 , a string such as '1/8' indicating the number of filled buckets in the hash is used.

If number is required, as for both sides of == , Perl follows these rules:

Strings are coerced into numbers. '0' and '1' become 0 and 1 respectively. '1/8' becomes 1 , with one warning. 'perl' and '' become 0 , with one warning. References (not shown here) are coerced into an integer representing the location in memory where the referent is stored. E.g. [] might become 3309920 . A scalar containing the false boolean, such as $false , becomes 0 . undef becomes 0 , with one warning. Now we have two numbers which can be compared in the usual fashion.

If a string is required, as for both sides of eq , Perl follows these rules:

Numbers are written out as strings. 0 and 1 become '0' and '1' respectively. Positive infinity becomes '1.#INF' , negative infinity becomes '-1.#INF' , NaN becomes '-1.#IND' . References (not shown here) become a string representing the referent's type and location in memory. E.g. [] might become 'ARRAY(0x87b1d8)' . A scalar containing the false boolean, such as $false , becomes the empty string '' . undef becomes the empty string '' , with one warning. Now we have two strings which can be compared in the usual fashion.

Observations

Unlike PHP and JavaScript, Perl expressions fall into equivalence classes; the operators == and eq are transitive.

and are transitive. A list expression, an array initialised from that list expression and a hash initialised from the same list expression can display radically different behaviour.

Further work