Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

pjf (2464)

pjf
  (email not shown publicly)
http://pjf.id.au/
AOL IM: miyuki3k (Add Buddy, Send Message)
Jabber: pjf@jabber.org

I run Perl Training Australia [perltraining.com.au].

I help with Melbourne Perl Mongers.

I spend an awful lot of time talking about Perl, and have had my picture in the Australian newspapers with a camel. That's rather scary.

Journal of pjf (2464)

Sunday June 15, 2008
06:50 AM

autodie works under Perl 5.8

[ #36692 ]

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.