Name: Paul Fenwick
Member since: 2000-03-29 03:29:11
Last Login: 2008-07-04 03:54:49
Homepage: http://perltraining.com.au/
Notes:
Personal homepage: http://pjf.id.au/
E-mail: pjf at cpan dot org
AIM: miyuki3k
ICQ:18669287
Yahoo: miyuki3k
Jabber:pjf@jabber.org
autodie 1.11_01 "Aristotle" released - Awesome inheritance
autodie version 1.11_01, codename "Aristotle", is now making its way through the CPAN. The new version contains quite a few exciting changes. You should be able to make and install the new version just like any other module, without having to muck around with @INC. Do be warned it will overpower your existing Fatal.pm, and while it should be completely backwards compatible, there's always the small risk of bugs that I've missed.
In this post, I want to show you why autodie is so cool, particularly with this new release.
Because autodie is smarter than Fatal, a whole bunch of things which are normally bugs start being correct if you're using autodie. As an example, take the following code:
if ( fork() ) {
# This is the parent process.
} else {
# This is the child process.
}
Normally, that code involving fork contains a bug, because fork could return an undefined value to indicate the fork failed. Since we're not checking for undef, our parent would act as a child process, and things are likely to go very badly from there.
However, with autodie we can write:
use autodie qw(fork);
if ( fork() ) {
# This is the parent process.
} else {
# This is the child process.
}
And now the code is correct! autodie knows that fork is allowed to return zero to the child, but returning undef is an error. This is much easier than checking the return values manually.
However in my opinion the best thing about the new release is that we can now cleanly subclass both autodie and autodie::exception, which means we now have a very easy way to slot in our own exceptions frameworks.
Let's start with a trivial example, and pretend we think the old name lethal was a better name for the module. We can just write inside a new lethal.pm:
package lethal; use base qw(autodie); 1;
And now, we can use lethal the same way we would use autodie.
As a more complex example, let's say that we want to do a bit of localisation. I want an Australian version of autodie that caters to our strong cultural identity. I can write inside bludging.pm:
package bludging;
use base qw(autodie);
sub throw {
my ($class, @args) = @_;
return bludging::exception->new(@args);
}
package bludging::exception;
use base qw(autodie::exception);
# Mate, it's always a good time for a beer in Australia!
sub time_for_a_beer {
return "Now's a good time for a beer.";
}
sub stringify {
my ($this) = @_;
my $original_string = $this->SUPER::stringify;
return "$original_string\n" . $this->time_for_a_beer;
}
1;
I can now write:
use bludging qw(open); open(my $fh, "<", "fair_dinkum.txt");
If fair_dinkum.txt doesn't exist, I get the Australian-friendly error message:
Can't open 'fair_dinkum.txt' for reading: "No such file or directory" at australia.pl line 1 Now's a good time for a beer.
Of course, one could instead extend the exceptions to use your favourite object framework, wake your sysadmins up in the wee hours of the morning, provide extra diagnostics and stack back-traces, or do anything else you consider worthwhile.
So, what are you waiting for? Grab yourself autodie 1.11_01 from the CPAN now, or (if your mirror is a little behind) you can download the release directly from my server.
autodie release 1.10_07 - Codename ikegami
Don't know what autodie is? Check out the 5 minute video and find out.
It's Sunday, and that means autodie release day! The new version of autodie is codenamed "ikegami" in honour of the wonderful perlmonk who assisted me in solving one of the trickiest problems in getting autodie running under its new architecture.
In fact, the architecture is the big change for this release. In older versions of autodie, different code was used when running under Perl 5.8 than under Perl 5.10, and each code had its own set of bugs and own maintenance that was required. That meant twice as much work for me.
The new code employs a single unified architecture regardless of the version of Perl used. In fact, in the ikegami release the total number of lines of code dropped, rather than increased. That's a good thing for maintenance, and goes quite some way to paying off a lot of the technical debt I've been accumulating in the project.
There's also been the regular swag of bugfixes, improvements and tests. You can check out the changes file for full information.
You can help autodie!
I've still got a big list of things to complete in my TODO list. Some of them are large, and some of them are small. I also need a bunch more test cases, both working and failing.
If you do wish to help, then feel free to grab the code from CPAN, or take a copy of the autodie repository using git, and feel free to send me patches or even ask for a commit bit. I won't bite.
Also feel free to say "I want to help, but I don't know where to start", and optionally a bit about your skills. I'm sure I can put you to good use. ;)
Even if you don't want to help with the project per se, I am very interested in hearing about if you've been using autodie, are planning on using autodie once it's completely stable, or just think it's a great idea. Likewise, if you think autodie sucks (or needs improvement), I'm also interested in hearing from you, so I can make it suck less. Drop me a note at pjf@cpan.org.
Be cool and join ohloh
Ohloh is a website that tracks open source projects. It looks at source code, prepares metrics, and draws lots of pretty graphs. It's great if you want to compare your favourite languages, or see how a codebase changes over time, or see how often you commit to various projects. Like anything web 2.0 these days, it has ways of making friends and rating people and projects.
However, like most things I write about, ohloh has a serious problem. Put simply, it has me listed as the seventh highest committer in the open source world for projects written in Perl this quarter.
This, of course, is complete bollocks. Sure, I write a lot of Perl, but only when I'm at work, travelling, at a Perl conference, at home, or waiting for a bus. There are lots of people out there who write way more code than I do, but they're not listed on ohloh.
So, how can you correct this terrible state of affairs? Well, firstly you can get yourself an ohloh account. Go poking around and claim all the commits you've made on various projects; you may be surprised how many you have, and instead of ohloh thinking of you has half-a-dozen cool people, it'll think of you as one amazing person. Once you've done that, you won't be happy with your score, so go and add any projects to which you've done anything at all to ohloh. Claim those commits. Rinse, repeat.
Before long, you'll have a massive kudos score, and you can show off to all the kids on #moose about how amazing you are. The Moose folk are really impressed by ohloh statistics; that's why so many Moose contributors are in the Perl top contributors list.
And yes, I've already got an account.
autodie release 1.10_06 - Codename chocolateboy
I'm very pleased to say that the next release of autodie is making its way to CPAN. The release's codename is "chocolateboy", after the wonderful author of autobox who spent many hours discussing the inner workings of pragmas, provided thousands of words of advice, and who allowed me to use him as a sounding-board when I had far too many ideas and not enough implementation.
The new version provides massive improvements in Perl 5.8 support, including:
I've still got a huge list of things left to do, but this release is a major stepping stone since it contains code that can lexicalise arbitrary subroutines, with many thanks to ikegami for help in debugging.
My next big tasks are:
If you're interested in learning more about the project, you can read my previous blog posts, or track the project on github or ohloh. I also welcome any questions, feedback, and especially patches at pjf@cpan.org.
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.
pjf certified others as follows:
Others have certified pjf as follows:
[ Certification disabled because you're not logged in. ]
FOAF updates: Trust rankings are now exported, making the data available to other users and websites. An external FOAF URI has been added, allowing users to link to an additional FOAF file.
Keep up with the latest Advogato features by reading the Advogato status blog.
If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!