Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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";


-------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:

  • For what other pairings of data types does eq ignore type?
  • Is there a way to override this so that things belonging to different data types are always not equal? Or is the rather verbose (ref($x) eq ref($y)) and ($x eq $y) the only way to do this?
  • I know there is a way to overload operators but I was under the impression that one had to "use overload" to empower it. Is that wrong? If so, how can I detect overloaded operators?
  • Is the ability to overload operators in any way connected to this behavior of eq? If not, how should I understand it?

Best, beth

In reply to What is the best way to compare variables so that different types are non-equal? by ELISHEVA

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (3)
    As of 2020-10-20 06:00 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (209 votes). Check out past polls.