Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Problem w/overloading and Carp::confess

by slagel (Novice)
on Nov 14, 2001 at 04:56 UTC ( #125187=perlquestion: print w/ replies, xml ) Need Help??
slagel has asked for the wisdom of the Perl Monks concerning the following question:

We're doing something a bit strange, in that we are overloading the stringify operation on an object and finding some nastiness. The nasty is that if one calls Carp::confess in your stringify method, you get a deep recursion. Is there any way to avoid this short of not calling confess? A bit of code:
#!/usr/bin/perl print P->new(); package P; use Carp; use overload q("") => \&asString; sub new { bless( {}, shift) } sub asString { confess }
Thanks, Joe

Comment on Problem w/overloading and Carp::confess
Download Code
Re: Problem w/overloading and Carp::confess
by blakem (Monsignor) on Nov 14, 2001 at 05:36 UTC
    Adding a 'fallback' => 0 to your use statement prevents the deep recursion, but I don't think it will solve the actual problem.... (just pushes it further down the line a bit) Anyway, I could be wrong....:
    use overload '""' => \&asString, 'fallback' => 0;

    -Blake

      Thanks...Interesting results:
      Operation `.=': no method found, left argument in overloaded package P, right argument has no overloaded magic at /usr/local/geospiza/ +lib/perl5/5.6.1/Carp/Heavy.pm line 91.
      Looks as if in longmess() the string concat being done to turn the "arg" into something printable is lost... I was thinking that putting a check in longmess to see if stringify is overloaded and then calling overload::StrVal(), but I don't want to change Carp.
        Apparently the recursion is being caused by
        if (ref $_) { # ...<snip>... $_ .= '';
        in Carp.pm (or Carp/Heavy.pm).

        I recommend that you copy Carp.pm to MyCarp.pm, and hack away at it. And maybe submit a patch when you're done.

        -- Frag.
        --
        "Just remember what ol' Jack Burton does when the earth quakes, the poison arrows fall from the sky, and the pillars of Heaven shake. Yeah, Jack Burton just looks that big old storm right in the eye and says, "Give me your best shot. I can take it."

Re: Problem w/overloading and Carp::confess
by pjf (Curate) on Nov 14, 2001 at 07:36 UTC
    The problem here, as you no doubt guessed, is that somewhere in the guts of Carp we attempt to stringify your object again, kicking off the asString method (yet again) to try and do this.

    The solution here is to detect when that's happened and return a static string, rather than calling confess again. Here's an example:

    sub asString { return __PACKAGE__ if (caller)[1] =~ /Carp/; confess; }

    That nicely breaks the recursion. Discovering why Carp is trying to stringify your object is left as an exercise for the reader. ;)

    Cheers,
    Paul

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2014-12-26 15:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls