Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Temporarily disabling overloaded operations.

by BrowserUk (Patriarch)
on Sep 15, 2003 at 06:04 UTC ( [id://291493]=perlquestion: print w/replies, xml ) Need Help??

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.

  • Comment on Temporarily disabling overloaded operations.

Replies are listed 'Best First'.
Re: Temporarily disabling overloaded operations.
by broquaint (Abbot) on Sep 15, 2003 at 07:45 UTC
    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
    sub foo::print { my $obj = overload::Strval($_[0]); print "$obj: @_[1 .. $#_]"; } my $o = bless [] => 'foo'; $o->print('some output'); __output__ foo=ARRAY(0x1012116c): some output
    Also be careful about what you're blessing as you can't bless constants e.g
    perl -e 'bless \1' Modification of a read-only value attempted at -e line 1.

    HTH

    _________
    broquaint

      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.

Re: Temporarily disabling overloaded operations.
by BrowserUk (Patriarch) on Sep 15, 2003 at 06:45 UTC

    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);

    package mypackage; my %hash; sub new { my $self = bless \1, $_[0]; $hash{ $self } = 'Initial data'; return $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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://291493]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-23 12:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found