Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Stringify objects with overloaded stringification

by liz (Monsignor)
on Nov 30, 2003 at 20:37 UTC ( #311092=snippet: print w/ replies, xml ) Need Help??

Description: By using overload it is possible to change the standard stringification of an object. This is a very nice and wonderful feature, which can be a bit of a pain during development and debugging. It is at those times, I have found myself wanting to get the standard stringification for an object, just as a visual aid for debugging.

This little sub does just that: it gives you the standard stringification.

I wonder if it would make sense to add this to Scalar::Util as well.

use Scalar::Util qw(blessed reftype refaddr);
sub stringify ($) {sprintf "%s=%s(0x%x)",blessed $_[0],reftype $_[0],r
+efaddr $_[0]}
Comment on Stringify objects with overloaded stringification
Download Code
Re: Stringify objects with overloaded stringification
by ysth (Canon) on Nov 30, 2003 at 23:52 UTC
    overload::StrVal??
      Indeed. In hindsight, it's a logical place and name for such functionality, but (as may be clear) I didn't find it. I guess I should have phrased is as a SOPW then, rather than creating a subroutine to do this. ;-) as I could have known someone would already have made something like that.

      On the other hand, this will work even if overload is not loaded, so you can apply it to a blessed value always. So there is some merit in doing it this way.

      Learned something again today (well, actually, it's already tomorrow here ;-)

      Liz

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://311092]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2014-09-21 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (169 votes), past polls