I Didn't Want to See That

Ovid on 2008-06-23T07:45:39

One thing I found unsettling was running this over our CPAN install:

ack '^\s*exit\b' lib/perl5/

Admittedly, many of those were false positives, but not all of them were. This puzzles me. Even if it's clearly documented, this is a very dangerous practice. How do I cleanly write code for something which might call exit()? Do I have to remember to override exit() every time? Yuck. What this means is that the module author has taken it upon themselves to deny me control over my use of the module.

That being said, exit() isn't always evil. Just mostly evil. Sometimes you need it, but programmers reach for it more than I would like. Even if you're writing a standalone program, you usually don't need to call exit() because Perl's nice enough to pick up after itself. If you do call exit(), Perl has no choice and I, as the person writing tests, am now forced to deal with that. Yuck.

On another annoying note, I realized that my grepl software doesn't have a means of only searching code (it can search POD, heredocs, quoted strings and comments). How did I manage to overlook that?


P::C

ChrisDolan on 2008-06-24T04:57:33

I just added a TODO for Perl::Critic to forbid exit() from all files that lack a shebang line. A similar policy exists in the Java FindBugs tool, and I've found that policy useful to increase trust in library code.

Would anyone like to implement that policy? I think it would be quite easy to do, especially given the Perl::Critic::Utils::is_script() helper method and existing example policies in Perl::Critic::Policy::BuiltinFunctions::*