Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Confusion about properly using Carp

by steves (Curate)
on Mar 02, 2002 at 16:35 UTC ( [id://148833]=note: print w/replies, xml ) Need Help??


in reply to Confusion about properly using Carp

Have you fully checked the Carp code? It's been a while but I seem to rememember a short and long message format, the short being the default; the long giving a full stack trace. The code counts all sub-classes in a calling path as one "level", so if you were to derive your module from DBI in this instance you'd get what you want. Then there's a $CarpLevel scalar you can set to push it back further in cases like yours. I've used that successfully; always by setting a local value before calling carp(). The trick is knowing how far back your client is. In the few cases I had to do this I either knew or could figure it out using caller().

  • Comment on Re: Confusion about properly using Carp

Replies are listed 'Best First'.
Re (tilly) 2: Confusion about properly using Carp
by tilly (Archbishop) on Mar 02, 2002 at 19:46 UTC
    Please don't use $Carp::CarpLevel. Ever.

    I have almost never seen it used anything like correctly, even when it was used inside of the core Perl distribution.

    If you think you have used it correctly, then you are almost definitely wrong unless you can tell me that you know that it works differently on a croak and a confess and can tell me what the difference is. (Hint: It works differently on croak between 5.005 and 5.6. Are you still sure you used it correctly?)

      I disagree. If you know where you are in a calling stack, this has worked for me numerous times:

      { local $Carp::CarpLevel = 2; carp "Some error"; }

      No need to worry about croak or confess here that I can see. What am I missing?

        What is wrong with this?

        What if someone calling your code wants to do what you are doing? They can't because no matter what they do with $Carp::CarpLevel, you are overriding them.

        What if someone wants to later modify your code? They don't realize that somewhere in the guts you have hardcoded an assumption about the shape of the call-stack. So your code is fragile.

        What if you are wrapping a module which might croak? If so then your callstack calculation is almost surely wrong. Read the guts of Carp to find out why.

        What if you are wrapping a module which might croak and might confess? If so then there is no single way to get the right desired result.

        And on and on. I gave this topic a lot of thought, and I concluded that what you are trying to do is a bad idea. The fact that many people (like you) think they know what they are doing and get it wrong consistently just convinces me of that even more.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-19 18:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found