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'
'eq' operator in Perl is one of the string comparison operators used to check for the equality of the two strings. It is used to check if the string to its left is stringwise equal to the string to its right.
=~ is the Perl binding operator. It's generally used to apply a regular expression to a string; for instance, to test if a string matches a pattern: if ($string =~ m/pattern/) {
Perl has two types of comparison operator sets. Just like other mathematical operators, instead of performing operations, these operators compare scalars.
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.
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:
\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.)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.
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.
"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/.
=~
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With