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

Is there any module or possible ways to strip the file name and error line number from the error message ? Example : Sending Mail Failed : SMTP Server not set at check_mail.pl line 6 I need the result only "Sending Mail Failed : SMTP Server not set".
  • Comment on Is there any module to strip error filename ?

Replies are listed 'Best First'.
Re: Is there any module to strip error filename ?
by Corion (Patriarch) on Oct 20, 2008 at 19:08 UTC

    Read die, or post some code that causes this.

Re: Is there any module to strip error filename ?
by billward (Initiate) on Oct 20, 2008 at 23:38 UTC
    If the die message ends in \n then you won't see the file/line information.

    If it's code that you don't have control over generating the error, such as in a CPAN module, then you might need to run it in an eval{} block, and modify the error message before re-throwing it.

    eval { Some::Module::send_mail() }; if ($@) { (my $error = $@) =~ s/ at .* line \d+$//; die $error; }
Re: Is there any module to strip error filename ?
by ikegami (Patriarch) on Oct 20, 2008 at 19:09 UTC

    Just don't print it in the first place. Compare

    >perl -e"die qq{foo}" foo at -e line 1. >perl -e"die qq{foo\n}" foo

    See die.

      Thanks. This is for the script. Actually, in some of the model classes also i need. Like it's a huge application, whenever i used croak i am getting the line number and i want to avoid ?

        That's the point of Carp::croak. If you don't want that, use die.