<?xml version="1.0" encoding="windows-1252"?>
<node id="781460" title="Re: What is the best way to compare variables so that different types are non-equal?" created="2009-07-19 12:15:30" updated="2009-07-19 12:15:30">
<type id="11">
note</type>
<author id="585085">
kyle</author>
<data>
<field name="doctext">
&lt;ol&gt;
&lt;li&gt;For what other pairings of data types does &lt;c&gt;eq&lt;/c&gt; ignore type?
&lt;p&gt;Basically every type.  It forces each argument to a string before making a comparison.
&lt;/li&gt;
&lt;li&gt;Is there a way to override this so that things belonging to different data types are always not equal?
&lt;p&gt;I'd probably write a custom comparison sub to do it.
&lt;c&gt;
use Scalar::Util qw( blessed reftype );

sub comparifier {
    blessed $_[0] eq blessed $_[1]
 &amp;&amp; reftype $_[0] eq reftype $_[1]
 &amp;&amp;         $_[0] eq         $_[1]
}
&lt;/c&gt;
&lt;p&gt;Along the same lines, you might be interested in [id://668351], whose subtext is "gosh, it's hard to figure out what a scalar really is."
&lt;/li&gt;
&lt;li&gt;...how can I detect overloaded operators?
&lt;p&gt;Overloading applies to objects, not operators.  (In Perl&amp;nbsp;6 you can override operators and/or write your own.)  If you want to know if an object is overloading, use &lt;c&gt;overload::Overloaded( $obj )&lt;/c&gt;.  If you want to know if it's overloading &lt;c&gt;eq&lt;/c&gt; in particular, you can check &lt;c&gt;overload::Method( $obj, 'eq' )&lt;/c&gt;, but you'll also have to look for stringification (&lt;c&gt;overload::Method( $obj, q{""} )&lt;/c&gt;).
&lt;/li&gt;
&lt;li&gt;...how should I understand it?
&lt;p&gt;Just that &lt;c&gt;eq&lt;/c&gt; forces its operands to be strings in order to do its job.  A regular expression stringifies as you've found (it's a blessed reference to an [doc://undef] with regex magic added).
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You might also be interested in &lt;c&gt;overload::StrVal( $obj )&lt;/c&gt;, which gives you the string value of &lt;c&gt;$obj&lt;/c&gt; without string overloading.  For a regular expression, this is similar to "&lt;c&gt;Regexp=SCALAR(0x1d10860)&lt;/c&gt;".
&lt;p&gt;I hope this helps.
&lt;p&gt;&lt;strong&gt;Updated&lt;/strong&gt; to fix a thinko in comparifier, thanks to [AnomalousMonk].
&lt;p&gt;&lt;strong&gt;Updated&lt;/strong&gt; again thanks to [jdporter].</field>
<field name="root_node">
781447</field>
<field name="parent_node">
781447</field>
</data>
</node>
