Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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 } );
Results:
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


In reply to Formatting dynamic method names by clinton

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-03-19 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found