Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by ikegami (Pope)
on Jul 19, 2009 at 16:21 UTC ( #781461=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?

For what other pairings of data types does eq ignore type?

eq pays no attention to the type whatsoever. eq stringifies its operands and compares those strings.

>perl -le"print( undef eq '' )" 1 >perl -le"print( 123 eq '123' )" 1 >perl -le"$r=\$s; print( $r eq sprintf('SCALAR(0x%x)', 0+$r) )" 1 >perl -le"print( qr/a/ eq '(?-xism:a)' )" 1 etc

I know there is a way to overload operators but I was under the impression that one had to "use overload" to empower it

You use use overload to add overloading to a class, not to decide whether or not overloading will occur.

Regex pattern objects are magical. Overloading doesn't even come into play.


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

    Regex pattern objects are magical. Overloading doesn't even come into play.

    Magical is right. This took me by surprise.

    use overload; use Scalar::Util qw( blessed ); use Test::More 'tests' => 4; my $rx = qr/a/; ok blessed $rx, 'Regexp is blessed'; ok ! overload::Overloaded( $rx ), 'Regexp is overloaded'; ok ! overload::Method( $rx, q{""} ), 'Regexp has q{""} overloaded'; isnt "$rx", overload::StrVal( $rx ), 'StrVal of regexp isnt regexp stringified';
      ok ! overload::Overloaded( $rx ), 'Regexp is overloaded'; ok ! overload::Method( $rx, q{""} ), 'Regexp has q{""} overloaded';
      I'm confused shouldn't it say "is not" and "has not"?

      Cheers Rolf

        You're right. I originally wrote the descriptions to match the tests, and then I was surprised when I ran them (they failed). After that, I reversed the tests and forgot to rephrase the descriptions.

Re^2: What is the best way to compare variables so that different types are non-equal?
by LanX (Canon) on Jul 19, 2009 at 23:31 UTC
    Regex pattern objects are magical. Overloading doesn't even come into play.

    so what about this???

    package Regexp; use overload q{""} => sub {return "overloaded" } ; package main; my $rx = qr/a/; print $rx;
    output: overloaded

    Cheers Rolf

    UPDATE: Finally got it! 8)

      so what about this???

      I'm not sure what you are asking.

      You transformed the class. Of course my statements about the original class won't necessarily apply to the transformed class.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (16)
As of 2014-07-23 15:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (145 votes), past polls