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

Hi,

I want to know is there a way to get rid of the location messages from things like die/croak... etc. I know - if you add \n at the end of the message it's okay. But I have something like that to deal with

eval {#something croaks from outside module ; }; if(...){ die "$@\n"}

So we take the result message from $@. Is there some way to get rid of the location at the end? Like specific regular exp, or doing something before the eval, or taking the clear error from somewhere, or something smart I have never even heard of ... :D

Replies are listed 'Best First'.
Re: Remove the location of error from error messages from croak
by LanX (Saint) on Jul 01, 2014 at 17:20 UTC
    yes you can transform the messages in $SIG{__DIE__} or $SIG{__WARN__} (see %SIG ) with a regular expression, since editors like emacs rely on a format ending with ...

    at ... line ...

    ... to be able to offer "jump to error" on click.

    But you need to take care of different things after at like (eval ... ) or -e

    > perl -e 'warn "bla\n"' bla > perl -e 'warn "bla"' bla at -e line 1. > perl -e 'eval q{warn "bla"}' bla at (eval 1) line 1.

    Reading caller should help figuring out such stuff.

    Cheers Rolf

    (addicted to the Perl Programming Language)

    PS: Next time please use proper formatting instead of <pre> tags

Re: Remove the location of error from error messages from croak
by Anonymous Monk on Jul 01, 2014 at 17:18 UTC
      eval{ die 1; }; warn $@; $SIG{__DIE__} = sub{ my( $err ) = @_; $err=~ s/ at .*? line \d+.$/\n/; die $err; }; die 2; __END__ 1 at - line 1. 2
        let's hope no filename will ever contain the substring " at " ! ;-)

        Cheers Rolf

        (addicted to the Perl Programming Language)