clinton has asked for the wisdom of the Perl Monks concerning the following question:
Every now and again, I want to set the value of a number of properties on an object from a hash, where the method name is the same as the key name, but with a prefix, eg
$object->set_date( $hash{ date } );
But how to do this for all the keys in a hash? I've thought of two methods:
Method 1: --------- for ( keys %hash ) { my $method = "set_$_"; $object->$method( $hash{ $_ } ); } Method 2: --------- for ( keys %hash ) { $object->${ \"set_$_" }( $hash{ $_ } ); }
I prefer the second method, because it is shorter, but (1) it is less readable and (2) Perl::Tidy reformats it with a space before the arrow, which still works, but looks rather odd:
$object ->${ \"set_$_" }( $hash{$_} );
Which method would you prefer? One of the above, or some other syntax?
(Note: I benchmarked the difference between these two, and while the temp var is slightly faster, the difference is negligible.)
#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); package foo; { sub set_var1 { } sub set_var2 { } sub set_var3 { } sub set_var4 { } sub set_var5 { } } package main; my $object = bless {}, 'foo'; my %hash = map { ( "var$_" => $_ ) } ( 1 .. 5 ); sub temp_var { for ( keys %hash ) { my $method = "set_$_"; $object->$method( $hash{$_} ); } } sub deref { for ( keys %hash ) { $object ->${ \"set_$_" }( $hash{$_} ); } } cmpthese( 1_000_000, { temp_var => \&temp_var, deref => \&deref } );
Rate deref temp_var deref 176991/s -- -18% temp_var 215054/s 22% --
Update: corrected a typo in Method 2, thanks to zwon for pointing it out
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Formatting dynamic method names
by zwon (Abbot) on Oct 10, 2009 at 13:43 UTC | |
by clinton (Priest) on Oct 10, 2009 at 13:45 UTC | |
Re: Formatting dynamic method names
by AnomalousMonk (Archbishop) on Oct 10, 2009 at 20:15 UTC | |
by clinton (Priest) on Oct 11, 2009 at 10:24 UTC |