in reply to "Is it a hashref" vs "Can I use it like a hashref?"
sub isa { my $self = shift; my ($type) = @_; my %is_builtin = map { $_ => 1 } qw( HASH ARRAY SCALAR CODE GLOB ); return 0 if $is_builtin{$type}; return $self->SUPER::isa($type); }
Both your method and mine have their advantages and disadvantages. I like yours for modules that need to care. I prefer mine when dealing with modules that care how stuff is implemented, like H::T.
Note about yours - it would be better if you didn't do explicitly dereferencing as the only option. It doesn't deal with stringification as provided by overload. I would prefer to see can() expanded to handle overload. For example, instead of asking if it overloads scalar dereferencing (which my object doesn't and shouldn't), you should be able to ask if it provides a stingification method. (The standard stringification wouldn't count. If you wanted to allow that, you shouldn't be asking the question.) Something along the lines of:
sub can_stringify { my ($thingy) = @_; return 1 unless ref $thingy; return 0 unless blessed $thingy; return $thingy->can('STRINGIFY'); }
That kind of code could be provided as part of can(). So, you could do something like if (UNIVERSAL::can($thingy, 'STRINGIFY')) { ... }, similar to how one tests an unknown scalar if it's a certain type, using UNIVERSAL::isa().
In other words, I think the problem can be solved by extending overload.
... does some source-diving ...
Hmmm ... Apparently, there is a way to test for these things using can. The stringification method is called ("". So, doing something like will work:
(I've tested it.)if (UNIVERSAL::can($x, 'can') && $x->can('(""')) { # Do stuff here }
There're also methods called OverloadedStringify() and mycan(), but I couldn't make it work in 5 seconds. I think the can() method probably works best. Ideally, overload would provide constants that can be used for determining overloaded-ness, instead of '(@{}' (which is the overloaded method for array dereference).
There are other neato gems in overload. For example, there's an Overloaded() method, which tells you if the class has been overloaded.
------
We are the carpenters and bricklayers of the Information Age.
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
|
|---|