loading...

Carp::Always sometimes

jplindstrom profile image Johan Lindstrom Updated on ・1 min read

Troubleshooting

Your program just died. Great, now what?

If the exception string ended with a \n, it didn't give you a line
number. But even if it did, a line number is often not enough to debug the
problem. That's where the problem occurred, but how did we get there?

Carp::Always to the rescue.

use Carp::Always;

Put this anywhere and all exceptions (and warnings) will have a full
stack trace, including subroutine arguments.

But wait...

You probably don't want to turn this on permanently for the entire program. Using Carp::Always is a global, process-wide change, and the stack trace will be added to all exceptions, even inside an eval BLOCK. That's probably not what you wanted.

So since it's a temporary debugging tool, this is a pretty bad thing
to forget in the code base since it changes global behaviour.

Do you know that nothing will go weird in the entire rest of your app
now that exceptions will look different? No you do not. So put a
reminder comment you can 'grep' for on the same line:

use Carp::Always; ### TODO: MyInitials: Remove before committing

Yes, it needs to have your initials on it, it's your personal
responsibility
to remove this.

Preventing mistakes

This low-key way of reminding yourself to not screw up may not be enough. After all, programmers are people, and people forget things and screw up all the time. You may even be one of them.

If this turns out to be a problem for you there are several approaches you can take, e.g.

  • Commit hooks in your source control system that prevents them to be checked in
  • Automated test that looks through the source tree for any use Carp::Always; statements

Ad-hoc Carp

Still worried about leaving in the use statements? You can also load the module on the command line with:

perl -MCarp::Always script.pl
prove -MCarp::Always -r t

just for that script invocation. Or, to make it semi permanent, set the PERL5OPT variable with this option:

export PERL5OPT=-MCarp::Always
perl script.pl

Remember: Carp::Always, but not all of the time!

Posted on Nov 5 '18 by:

Discussion

markdown guide