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

I need to die from deep within a library. And I need to generate my own ' at script line n, <fh> line m.' suffix because die's idea of script/line will not be helpful to the user.

$. provides the line number of the last touched file. (or is undef is there is no such file).

Question: How can I determine the file handle name of the corresponding (last touched) file? (so I can generate ", <HISFH> line n") I haven't found this documented.

I suppose I could do an eval die "fake" and parse the result from $@, but that seems rather heavy-handed...is there a sensible alternative?

Thanks

This communication may not represent my employer's views, if any, on the matters discussed.

  • Comment on How do I replicate die's magic with $. ?

Replies are listed 'Best First'.
Re: How do I replicate die's magic with $. ?
by Corion (Patriarch) on Aug 06, 2011 at 16:58 UTC

    For providing the error location, Carp::croak is very good. For getting the location in some filehandle, the documentation of $. says that it's the line number of the last file accessed, so you might have to (re)read a line from that file to make $. work the way you intend.

    Update: s/Croak/Carp/, as spotted by toolic

      Thanks for the reply, but that doesn't answer my question. As I thought I was careful to explain, I know the error location, but I need to determine the name of the file handle associated with the value in $.. And as a library that doesn't do file IO, I have no clue what the file might be.

      There's got to be a better way than:
      my $msg = " at yourbadscript line your mistake"; if( defined $. ) { local $@ = ''; eval { die; }; if($@ =~ /^Died at .*(, <.*?> line \d+).$/ ) { $msg .= $1; } } $msg .= ".\n"; die $msg;

      This communication may not represent my employer's views, if any, on the matters discussed.

        And as a library that doesn't do file IO, I have no clue what the file might be.

        How do you know anything did IO then?

        When you can answer that question, you know the right place to handle the file/line information.

Re: How do I replicate die's magic with $. ?
by tlhackque (Beadle) on Aug 09, 2011 at 23:06 UTC
    Thank you all for your attempts to help.

    I decided to submit a ticket ( rt://96672 ) against Carp, which is missing this functionality.

    This communication may not represent my employer's views, if any, on the matters discussed.