thpfft has asked for the wisdom of the Perl Monks concerning the following question:

I've offloaded all the file operations in my (increasingly rococo) content-management system into a separate module with an ad-hoc collection of other utilities. It's a nice little thing that caches and flocks, and all that, but i'd like to make it die more usefully.

At the moment i get messages along the lines of 'failed to append to /path/to/file: permission denied', or whatever. Which is fine, but it doesn't tell me much about what was going on at the time.

I'd really like the error messages to tell me which subroutine in which module was responsible for this foolish instruction. I can imagine various brute-force ways to do that, but they'd be horrible to maintain and wouldn't iterate properly.

The least-worst thing i've come up with is a sort of audit trail in which the start of every subroutine or other landmark pushes a note into a global variable (ick) recording the name of the sub and the values passed to it, if the necessary debug flag is present. Then a die_noisily() call could be used to reel off the concatenation if something goes wrong, or perhaps just the last three entries.

Is there a more elegant way to learn that i was in read_file() because get_record() was triggered by parse_template()?

Braces self for humiliating three-word answer.

btw, i know the debugger can do a lot of this, but i'd like something quick and generic for troubleshooting, and a lot of my clients have lobotomised virtual servers without command-line access.

Replies are listed 'Best First'.
Re: dying() more informatively
by japhy (Canon) on May 09, 2001 at 03:50 UTC
Re: dying() more informatively
by frag (Hermit) on May 09, 2001 at 05:14 UTC

    Like japhy said, look into Carp. However, it sounds like that isn't what you're looking for...are you saying that none of your die messages include any line/filename info? In that case, this suggests that your die() message strings all end in '\n'. If you remove those newlines, you'll get that information. Alternatively, if you replace each die() with croak(), the '\n' won't prevent a line-number from appearing (although it will push that information onto the next line). As an alternative, rather than going line-by-line through your code, just adding this should give you what you want:

    BEGIN { $SIG{__DIE__} = sub { my $string = shift; chomp($string); my ($package, $file, $line) = caller; die "$string [at $file, line $line]\n" unless ($package eq 'Carp'); } }

    Modifying $SIG{__DIE__} like this changes how all of your die() calls behave (including croak(); that last bit with the 'unless' is in there to keep the output from getting too ugly). If you want the same sort of thing for warn() and carp(), you can modify $SIG{__WARN__}.

    Update: I had croak() and carp() switched in the last paragraph there (thanks premchai21).

    Update++:Also, tilly's post above this one (about checking $^S) applies to what I'm doing here. Here's how I think this ought to be taken into account (with confess thrown in for a complete trace of the call-stack (thanks again tilly)):

    BEGIN { $SIG{__DIE__} = sub { my $string = shift; chomp($string); my ($package, $file, $line) = caller; confess "$string\n" if ((defined ($^S)) and not $^S); } }
    Note that I'm going on what I just read in perlvar more than on experience with $^S. Also, using confess in $SIG{__DIE__} like this produces a little weirdness in the output when you use croak in the main code, that I don't know how to fix.

    -- Frag.

      If you read the question closely, what the person wants is a call-stack. The die is happening from read_file. But that information is useless because everything has to read files. What you really want to know is where you were 2 calls back, in parse_template. For that you really wanted to use Carp's confess.
      I think you're a bit mixed up here. croak() calls die() and carp() calls warn(), not vice versa.
      This looks better than the first two suggestions;
      alternatively, you can replace the CORE::die function with
      your own; to start see:

      Bekman, Stas. mod_perl guide, chapter 3.12

      humbly,
      novitiate
Re: dying() more informatively
by mr.nick (Chaplain) on May 09, 2001 at 05:02 UTC
    I believe what you are looking for is caller. For deep debugging, I use the following die replacement:
    sub DIE { my $i=0; while (my @info=caller($i++)) { print STDERR "$info[3]($info[2]) "; } print STDERR ": "; printf STDERR @_; print STDERR "\n"; exit 1; }
    which when used, produces output like
    main::DIE(16) main::foo(19) main::ack(22) main::bar(25) : HI
    when run via
    sub foo { DIE "HI"; } sub ack { foo; } sub bar { ack; } bar();

    Update: I should make it clear, I don't intend for this to be a direct replacement for die in any way. I meant to imply that I stick it into code (and a comparible WARN routine) for debugging purposes only.

    If you wanted a caller output and then a normal die, I would suggest something like (untested):

    sub DIE { my $i=0; my $buff; while (my @info=caller($i++)) { $buff.="$info[3]($info[2]) "; } die $buff; }
    or using one of the other approaches suggested here :)
      Please, please, please check $^S if you are going to change the behaviour of die like this. You have no idea what libraries you will mutilate that use eval/die to catch normal exceptions in program flow (eg alarm timeouts) that don't want to be turned into an untrappable fatal error.

      Please build up a string and just redie with that string. Even better, use eval { ... } around the heart of your code instead. __DIE__ handlers have some bad design flaws and your code is exercising most of them.

      Sorry, I think tilly's reply fooled me. You aren't using a __DIE__ handler nor overriding die.

      <Emily Latela>Never mind</Emily Latela>

              - tye (but my friends call me "Tye")