Exclude Line Number from die() Message

In Perl, we sometimes catch die() using eval as an exception handling and use the string given as the parameter of die() in it, like in the example below.

$ vim die_test
#!/usr/bin/perl

eval {
    die "Exception occurred";
};
if ($@) {
    print $@;
}

This script will display the below output on console.

$ ./die_test
Exception occurred at die_test line 4.

This output is fine as long as you use it for error message. But, if you want to use the string for other process, “at die_test line 4” would be an obstacle. To exclude the line number, you can simply add line break (\n) at the end of the string.

#!/usr/bin/perl

eval {
    die "Exception occurred\n";
};
if ($@) {
    print $@;
}
$ ./die_test
Exception occurred

Leave a Reply

Required fields are marked *.


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Top