What's the difference between 'eq' and '=~' in Perl?

16,034

Solution 1

"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/.

Solution 2

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

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

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

Solution 3

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

If you really only want to know whether two strings are identical, the 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 other hand, the regex form is far more flexible if you need to do something other than a plain string equality test. See perldoc perlre for more on regular expressions.

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

Solution 4

eq -- Tests for string equality.

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

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

Solution 5

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

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

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

Share:
16,034
user150283
Author by

user150283

Updated on July 25, 2022

Comments

  • user150283
    user150283 almost 2 years

    What is the difference between these two operators? Specifically, what difference in $a will lead to different behavior between the two?

    $a =~ /^pattern$/
    
    $a eq 'pattern'