Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

operators

perl

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'
like image 754
user150283 Avatar asked Sep 17 '09 12:09

user150283


People also ask

What does EQ mean in Perl?

'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.

What does =~ mean in Perl?

=~ 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/) {

Is comparison operator in Perl?

Perl has two types of comparison operator sets. Just like other mathematical operators, instead of performing operations, these operators compare scalars.


5 Answers

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.

like image 181
friedo Avatar answered Sep 20 '22 18:09

friedo


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.

like image 20
Adam Bellaire Avatar answered Sep 20 '22 18:09

Adam Bellaire


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.

like image 40
Donut Avatar answered Sep 19 '22 18:09

Donut


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

like image 24
ysth Avatar answered Sep 22 '22 18:09

ysth


=~ 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.

like image 40
Michael Carman Avatar answered Sep 23 '22 18:09

Michael Carman