April 2012 Archives

Painless RSS processing with Mojo

I wasn't looking forward to dealing with this XML feed because I hate anything that deals with XML. However, with Mojo's DOM stuff, I don't even have to know it's XML. Here's the interesting bits from a program that's not much larger than this snippet:

use Mojo::UserAgent;

my $ua = Mojo::UserAgent->new;
my $response = $ua->get( $feed_address )->res;

my @links = $response->
dom( 'item' )->
grep( sub { $_->children( 'title' ) !~ /.../ } )->
map( sub {
map( su…

Giving to TPF through Capital One

I'm not begging for money so much as begging other people to beg for money. Well, I'm not even doing that. I just noticed one way that people can give money to a charity.

Generalized single element access

chromatic explained his problem with auto-dereferencing in the hash and array operators, and I said a bit more about it. I missed the right day to post this, but I had a half baked idea about solving the problem.

Basically, the auto-dereferencing, in combination with Perl 5.12 allowing hash operators to extend to arrays, means that part of the problem doesn't care what you have and another part does. When you want to do a single element access, you still have syntax that must denote the reference type. If you have a hash, you need curlies:

foreach my $key ( keys $hash_or_array ) {
     my $value = $hash_or_array->{ $key };

But, if that's an array reference, you need square braces:

foreach my $key ( keys $hash_or_array ) {
     my $value = $hash_or_array->[ $key ];

Get the wrong thing there and your Perl program dies. This sort of thing happens when you solve the easy half of the problem without following it all the way through.

But, who cares? Why don't we have a generalized single element access that doesn't care what sort of aggregate it is? Parens are taken for code references. The remaining ASCII pair is the angle brackets:

foreach my $key ( keys $hash_or_array ) {
     my $value = $hash_or_array->< $key >;

But, we have Unicode source now, so we have lots of paired characters:

     my $value = $hash_or_array->" $key ";

     my $value = $hash_or_array->' $key ';

     my $value = $hash_or_array->« $key »;

     my $value = $hash_or_array->◀ $key ▶;

     my $value = $hash_or_array->▶ $key ◀;

     my $value = $hash_or_array->◤ $key ◢;

     my $value = $hash_or_array->☛ $key ☚;

But, with generalized quoting, we can include subroutines too:

     my $value = $hash_or_array_or_sub->« @keys »

If the subroutine is going to take multiple arguments, we can make this an automatic slice for the aggregates.

But what is the key is itself an array reference? We can auto deref that to get the slice. If it's a hash, we can take the list of keys as the slice indices.

Oh hell, isn't this half of Perl 6 now (and the half that I like)?

About brian d foy

user-pic I'm the author of Mastering Perl, and the co-author of Learning Perl (6th Edition), Intermediate Perl, Programming Perl (4th Edition) and Effective Perl Programming (2nd Edition).