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:
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:
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.) 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. ------
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
|
|