To coin a phrase, a snippet is worth a thousand words...
package Foo;
use overload qw("" stringify fallback 1 + zero);
sub stringify {
"overload.pm just ruined your day!"
}
sub zero { 0 }
package main;
my $foo1=bless {},'Foo';
my $foo2=bless {},'Foo';
sub same { "'$_[0]' is ".($_[0] eq $_[1] ? "the same as" : "different
+to" )." '$_[1]'\n" };
print same(0+$foo1,0+$foo2);
print same($foo1,$foo2);
print same("$foo1","$foo2");
print same(overload::StrVal($foo1),overload::StrVal($foo2));
__END__
'0' is the same as '0'
'overload.pm just ruined your day!' is the same as 'overload.pm just r
+uined your day!'
'overload.pm just ruined your day!' is the same as 'overload.pm just r
+uined your day!'
'Foo=HASH(0x1acef84)' is different to 'Foo=HASH(0x1acf038)'
:-)
AFAIK, the _only_ non-xs way to reliably determine the underlying variable type and class of an overloaded object is to parse the results of overload::StrVal. Even then you get nowhere with reblessed qr// objects. (Which still act as regexes at the same time.)
Frankly the fact that perl completely lacks any reliable native perl way of doing type introspection is IMO one of its few serious failings. (And no, at least some of the problems do not go away with Scalar::Utils and List::Utils.)
--- demerphq
my friends call me, usually because I'm late....
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|