Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: What is the best way to compare variables so that different types are non-equal?

by Anonymous Monk
on Jul 19, 2009 at 15:48 UTC ( #781451=note: print w/ replies, xml ) Need Help??


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

What you are seeing, are the effects of overload for Regexp objects. You can use == to compare

use strict; use warnings; my $re =qr{a}; my $re2 =qr{a}; warn $re == $re2; warn $re2 == $re2; warn 0+$re; warn 0+$re2; __END__ Warning: something's wrong at - line 8. 1 at - line 9. 2252896 at - line 10. 2252956 at - line 11.
Regexp objects are a special case (isn't everthing), you can call methods on them, but you can't dereference them


Comment on Re: What is the best way to compare variables so that different types are non-equal?
Download Code
Re^2: What is the best way to compare variables so that different types are non-equal?
by psini (Deacon) on Jul 19, 2009 at 16:01 UTC

    I really don't understand your example. I substituted warns with prints:

    use strict; use warnings; my $re =qr{a}; my $re2 =qr{a}; print $re == $re2,"\n"; print $re2 == $re2,"\n"; print 0+$re,"\n"; print 0+$re2,"\n";

    and this is the result:

    sini@ordinalfabetix:~$ ./x.pl 1 135589228 135591376 sini@ordinalfabetix:~$

    The last two lines are NOT equal, because are references to two different scalars. And, as a consequence, $re != $re2 (and $re == $re, but it was expected).

    So numerical comparison doesn't tell you if two regex are equal, but only if they are the same (reference).

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://781451]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (18)
As of 2014-04-18 20:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (472 votes), past polls