[ACCEPTED]-What's the difference between 'eq' and '=~' in Perl?-operators

Accepted answer
Score: 36

eq is for testing string equality, == is the 5 same thing but for numerical equality.

The 4 =~ operator is for applying a regular expression 3 to a scalar.

For the gory details of every 2 Perl operator and what they're for, see 1 the perldoc perlop manpage.

Score: 18

As others have noted, ($a =~ /^pattern$/) uses the regular 14 expression engine to evaluate whether the 13 strings are identical, whereas ($a eq 'pattern') is the plain 12 string equality test.

If you really only 11 want to know whether two strings are identical, the 10 latter is preferred for reasons of:

  • Readability - It is more concise, containing fewer special characters.
  • Maintainability - With a regex pattern, you must escape any special characters that may appear in your string, or use extra markers such as \Q and \E. With a single-quoted string, the only character you need to escape is a single quote. (You also have to escape backslashes if they are followed by another backslash or the string delimiter.)
  • Performance - You don't incur the overhead of firing up the regex engine just to compare a string. If this happens several million times in your program, for example, the benefit is notable.

On the 9 other hand, the regex form is far more flexible 8 if you need to do something other than a plain 7 string equality test. See perldoc perlre for more on 6 regular expressions.

EDIT: Like most everyone 5 else before ysth, I missed the obvious functional difference 4 between them and went straight for more 3 abstract differences. I've clarified the 2 question but I'll leave the answer as a 1 (hopefully) useful reference.

Score: 6

eq -- Tests for string equality.

=~ -- Binds 3 a scalar expression to a pattern match.

See 2 here for more in-depth descriptions of all of 1 the operators.

Score: 4

"pattern\n" :)

$a = "pattern\n";
print "ok 1\n" if $a =~ /^pattern$/;
print "ok 2\n" if $a eq 'pattern';

Perhaps you meant /^pattern\z/.

0

Score: 3

=~ is the binding operator. It is used to 20 bind a value to either a pattern match (m//), a 19 substitution (s///), or a transliteration (tr// or 18 y//).

eq is the string equality operator; it compares 17 two values to determine whether or not they're 16 equal when considered as strings. There 15 is a peer == operator that does the same thing 14 only considering the values as numbers. (In 13 Perl, strings and numbers are mostly interchangeable 12 with conversions happening automatically 11 depending on how the values are used. Because 10 of this, when you want to compare two values 9 you must specify the type of comparison 8 to perform.)

In general, $var =~ m/.../ determines whether 7 or not the value of $var matches a pattern, not 6 whether it equals a particular value. However, in 5 this case the pattern is anchored at both 4 ends and contains nothing but literal characters, so 3 it's equivalent to a string comparison. It's 2 better to use eq here because it's clearer 1 and faster.

More Related questions