SPUG: idioms of late

Fred Morris m3047 at m3047.net
Mon Jan 18 12:04:07 PST 2010

Lately I've found myself doing a lot of processing of lists... usually
either listrefs, or a list of keys which can be looked up in a hashref.
I've found myself using a few idioms in place of for/while loops
repeatedly, and I thought I'd share.

$foo->{names} = [ map { my $x = $_; $x =~ s/\.$//o; $x; }

That one munges a listref, stripping trailing dots off of all of the
elements. The initial "$x = $_;" seems to be necessary. That one seems
rather pythonic to me. Generally speaking they prefer that I use perl.

printf "%s\n", join( "\t", @{$foo}{@FIELDS} );


printf "%s\n", join( "\t", @{$foo}{qw/ fee fie foe /});

That's kind of an interesting one because $foo is actually a hashref.

Another variant of that which is useful is as part of, for instance,
enforcing a "no quotes in SQL literals" rule:

my ($name, $address, $city) =
    map $dbh->quote($_), @{$self}{qw/ name address city /};

my $sql .= "VALUES ($name, $address, $city)";

Nothing major, maybe food for thought.


Fred Morris, internet plumber

More information about the spug-list mailing list