BrowserUk has asked for the
wisdom of the Perl Monks concerning the following question:

I have a class in which I am overloading stringification. However, it would be very useful if I could disable the overloading when I want to print stuff out for debugging purposes.

Currently, whenever I try to use print, warn, die etc., on $self, overload routes me to my stringify routine, which results in deep recursion if it that routine I happen to be try to get debug output from.

It also interferes with being able to use Abigail's Inside out objects technique.

Is there a smart dodge around this?

Examine what is said, not who speaks.

"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.

To use perl's native stringification method you can just use the static method overload::Strval. As for how you specifically disable stringification for output, I don't think you can control this, so you might just want a couple of wrapper methods along the lines of

Thanks broquaint, overload::StrVal is perfect. Dunno how I missed that in the docs.

Your right on the \1, I normally use \rand if I only want a scalar. In this case, that was just a placeholder for a value derived from arguments to new().

Examine what is said, not who speaks.

"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.

As a "matter arising" from my playing with the above, but of little real import

If my object is internally implemented using a single scalar value, and I use the IOO method of using the blessed ref representing the object as a key into a my'd hash, then I need 3 things. The hash (%hash), the scalar holding my data ($data), and a blessed ref ($self);

In the above, I am using \1 just to get a unique scalar ref which I can bless. I'm then using this as the key through which to access another scalar (the hash value) which is going to hold my actual data.

Now (my twisted type of) logic says, if I already have a scalar to hold my data, then I don't need to create another just to get my blessed ref, I could just bless a reference to the data. The problem is, you can't take a reference to a hash element until you have a key, and I want to use the reference as the key. Catch-22

Can I have my chicken lay an egg? Can I generate a self-addressing stringified reference?

The "mypackage=SCALAR()" bit is constant, so I would need to vary the hex value within the brackets until the address of the scalar allocated for the hash generated matched the hex value I generated. Is that possible? In my lifetime:)

Examine what is said, not who speaks.

"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.