http://qs321.pair.com?node_id=781447

ELISHEVA has asked for the wisdom of the Perl Monks concerning the following question:

It seems I am always learning something new about Perl. Today while debugging some graph navigation code I discovered something that surprised me a bit. I was naively using eq to see if we had visited a graph node before, but for some reason my code was insisting that we had already visited a node that I knew we hadn't visited before. It turned out that the reason for this confusing behavior was that qr{someregex} eq '(?-xism:someregex)' was returning true. For example, the following code:

use strict; use warnings; my $s='(?-xism:a)'; my $re=qr{a}; # regex and string are clearly different ref types print "-------Type------------\n"; print "ref $re (regex) is 'Regexp': " , (ref($re) eq 'Regexp' ?'true':'false'), "\n"; print "ref $s (string) is '': " , (ref($s) eq '' ?'true':'false'), "\n"; print "ref $s (string) is not 'Regexp': " , (ref($s) ne 'Regexp' ?'true':'false'), "\n"; # so why are they equal? print "-------Equality--------\n"; print "comparing literals: qr{a} eq '(?-xism:a)': " , (qr{a} eq '(?-xism:a)'?'true':'false'), "\n"; print "comparing variables: regex eq $s (string): " , ($s eq $re?'true':'false'), "\n";

prints

-------Type------------ ref (?-xism:a) (regex) is 'Regexp': true ref (?-xism:a) (string) is '': true ref (?-xism:a) (string) is not 'Regexp': true -------Equality-------- comparing literals: qr{a} eq '(?-xism:a)': true comparing variables: regex eq (?-xism:a) (string): true

I'm used to eq comparing numbers to strings - Perl considers them both to be scalars, but here Perl was considering two things that were clearly different types as "equal" - one a reference to a regex and the other a string. So my questions are:

Best, beth