Many people have suggested I use autodie. I have never been interested in the functionality it provides, and don’t use it. I think there’s primarily two reasons why that is. I might know a way to address one of them, but it’s tricky.

It’s Invisible

The first reason is that I feel it changes the language a lot, and in ways that make it really hard for a new reader to follow. If you’re reading code that has no error checking in it, as a new or even mid-level Perl author, you’ll perhaps not realize that’s bad, or that it does have error checking. That checking being hidden in “use autodie” can be somewhat difficult to see.

As part of that difficulty, using autodie makes changes to all the Perl builtins. When I need to use one, I look it up in perldoc. perldoc tells me it returns false or undef or whatever in case of an error. Except… that’s not true with autodie. While I’m a good enough programmer to remember this about my own programs, it makes it a lot harder to tell what a particular piece of Perl code will do.

I’m really unhappy with the changes to the second edition of the Modern Perl book, because it’s assuming autodie everywhere. I don’t want to give it to new Perl programmers any more because they’ll be confused, and the differences are subtle and almost entirely undocumented. I can’t recommend the new version of this otherwise useful book to anyone who isn’t an expert Perl programmer and that’s a shame.

I don’t like making large-scale, undocumented changes to the language libraries any more. At what point is it no longer Perl, but something new and different?

As an aside, I think a lot of expert Perl programmers forget that many people using Perl don’t know it has exceptions at all. I often find people who are shocked to discover it does. Some of them have even been using die for errors, and didn’t know how the whole thing works together. Professional programmers, not script kiddies. Happens all the time.

This concern of mine with autodie is actually a solvable problem. The Perl core needs to incorporate autodie, and update al the perldocs to explain it. If the perldoc for “unlink” said, “returns the number of files deleted. With autodie enabled, will die on error deleting files.” it would mean that a new author would have a chance to see what’s happening. I don’t expect this to happen, but until it does, I consider autodie too sweeping a set of changes for a module to be making.

I probably wouldn’t use it anyway, because of this second reason:

I Don’t Like Exceptions

I’ve worked in a ton of languages that use exceptions. I understand them, and I can use them. I don’t like them. I find it hard to write robust, resilient code with them. Many people like them because you can put off or ignore error handling and capture it all in one place. Your code is free of that distracting noise of error handling.

However, this means your code notices the errors in the wrong place to do anything about them, and has to pretty much fall over dead and crash. It can’t try and fix things very well. Or you can handle the exceptions very close to the place they might occur, littering your code with even more distracting and hard to read exception handling stanzas.

The most reliable and robust code I’ve ever written was about 70% error handling code. In many cases it could recover from errors or try an alternative to keep running. I find this much harder to do with exceptions, and exceptions in general much more difficult to manage.

If programmers were clamoring for a giant “ON ERROR GOTO” statement, while telling me that gotos were evil, I’d be really confused. Yet, that’s what exceptions are. Exceptions that many programmers never handle, and simply want the program to crash the first time something goes wrong. I don’t want my program to crash every time something goes wrong; I want it to log the error and keep going, or ask again or try an alternative.

If “Go To Considered Harmful” is still valid – and it seems to me to be – why is ON ERROR GOTO allowed? Isn’t that all an exception is?

I don’t know a way to change this preference of mine, and how to convince myself that it’s okay to just let things crash or to write horribly tangled code to pander to the needs of the exception handlers. So, I don’t.

Tags: Perl