Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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

Replies are listed 'Best First'.
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;


      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/ 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 (or Carp/

        I recommend that you copy to, 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. ;)


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://125187]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2017-02-25 12:44 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (365 votes). Check out past polls.