SPUG: idioms of late

Andrew Sweger andrew at sweger.net
Mon Jan 18 20:10:19 PST 2010

On Mon, 18 Jan 2010, Fred Morris wrote:

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

I prefer (because of munging the original items):

s/\.$// for @{$foo->{names}};

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

I love hash slices! ...to the consternation of some coworkers (heavy
comments always provided). But does this behave correctly? Did you mean:

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

I use a similar technique of hash slices for dealing with mapping to/from
my hashes and SQL insert/update statements. I use the @FIELDS to control
the field list so it only has to be stated once. (The legacy code resorts
to explicit numeric array indices to tease out the data for each field.

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

Boo! Parameter binding is your friend, Fred. But partial points for using
the DBI/DBD quote method (as opposed to the insane DIY one I found in some
legacy code!).

Andrew B. Sweger -- The great thing about multitasking is that several
                                things can go wrong at once.

More information about the spug-list mailing list