Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
This very concept was discussed in this thread, with this very concept brought up by me here, as it pertains to ref and ARRAY. I have an object that is implemented as an blessed arrayref, but overloads stringification (and the other -ifications, but that's beside the point). I want HTML::Template to treat it as a scalar, cause it quacks like a scalar. But, H::T says "You have implemented this thing as an ARRAY, so you must be wanting it in a TMPL_LOOP, not a TMPL_VAR". So, my solution was to overload isa() in my class, as so:
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:

if (UNIVERSAL::can($x, 'can') && $x->can('(""')) { # Do stuff here }
(I've tested it.)

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.


In reply to Re: "Is it a hashref" vs "Can I use it like a hashref?" by dragonchild
in thread "Is it a hashref" vs "Can I use it like a hashref?" by blokhead

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-18 03:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found