Thanks for the replies.

And just to clarify, I *can* get what I need by correctly using croak/die/confess/warn in the appropriate places or by adding additional details to the actual exception message. This is more a question of DWIW when we mistakenly use croak() in a spot where die() would been more informative.

Here's some example code. And yes, it's completely insecure; it's just to demonstrate the issue.

#!/usr/bin/env perl { package MyModule; use strict; use warnings; use Carp; sub a { croak 'croaking' } sub b { die 'dying' } sub dispatch { my $class = shift; my $method = shift or croak 'no method name supplied'; $class->$method; } } while (<>) { chomp; next unless /\S/; eval { MyModule->dispatch($_) }; warn $@ if $@; }

If you run it and enter 'b', you'll see: "dying at ./try line 11, <> line 2." Line 11 is in fact the b() routine, so that's great.

Now enter 'a' and you get: "croaking at ./try line 25." That is not helpful, since line 25 is just the dispatch routine. We have no idea what was dispatched that raised the error.

So ideally, what I would like to see is the first two call frames of the stack, rather than only the second.

IOW, croak() would emit something like this by default:

croaking at ./try line 12, <> line 1.
	MyModule::a("MyModule") called at ./try line 19

Any thoughts on how to accomplish that?

Thanks!


In reply to Re: Getting croak to show both caller and callee? by Anonymous Monk
in thread Getting croak to show both caller and callee? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.