autodie works under Perl 5.8

(You can suggest changes to this post.)

autodie works under Perl 5.8
After applying a very different paradigm to the code, I have autodie working under Perl 5.8! For anyone who's missed my earlier posts on the subject, autodie allows Perl's built-ins (and your own code, if you like) to 'succeed-or-die' with lexical scope. This means you can write code like this:


if ($filename) {
    use autodie;  # Turns on all common built-ins by default.

    open(my $fh, '<', $filename);  # This opens or dies

    # Do things with my file.

    close($fh);                    # This closes or dies
}

# This open merely returns false on failure.
open(my $fh2, '<', $file2);

It gets even better, because unlike the old Fatal.pm, autodie also allows you to enable succeed-or-die semantics with system(), by hooking into IPC::System::Simple under the hood:


eval {
    use autodie qw(system);

    # Perform the commands below in order, but
    # if any fails, we automatically skip to
    # the end of the 'eval' block.

    system($mount_tape);
    system($check_tape_label);
    system($backup_files);
    system($unmount_tape);
    system($delete_old_files);
};

if (my $error = $@) {
    # Something went wrong.  Recover/handle it here.

    system($unmount_tape);  # autodie not in effect, this fails silently.

    wake_sysadmin_from_slumber();

    # $error / $@ stringifies into a helpful error message.  What
    # command failed, which script, which line, what did it return,
    # what signal name/number killed it, etc.

    die "Backup failed - $error";

}

If an autodying built-in fails, it doesn't just die with an ugly error (like Fatal does), it throws a well-formed exception object. You can catch that, inspect it to discover where the error occurred, what called the code that caused the error, what the arguments were, and almost all the things you'd want to discover during exception handling. If you use it as a string, it becomes a helpful error, which can depend upon the function that threw the exception. You can even register your own message handlers, making localisation easier.

Of course, the code isn't complete, I've got a big TODO list that's far from complete, but the hardest part of getting it working under 5.8 is done.

You can grab the latest version of autodie from the CPAN. I'd also like to say a special thank-you to all the people who have contributed so far, especially to Matt Trout for pointing me at namespace::clean, and Robert 'phaylon' Sedlacek for writing it; the heart of namespace::clean was twisted to my dark will to make autodie work under Perl 5.8.

If you want to play with the bleeding edge code, you can grab the code from git (or use the download button on that page for a tarball); let me know if you'd like a commit-bit. If you have bugs, comments, suggestions, praise or encouragement, feel free to e-mail me until the autodie bugtracker finds its feet.

Bitcoin QR code This site is ad-free, and all text, style, and code may be re-used under a Creative Commons Attribution 3.0 license. If like what I do, please consider supporting me on Patreon, or donating via Bitcoin (1P9iGHMiQwRrnZuA6USp5PNSuJrEcH411f).

comments powered by Disqus