Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The context of your problem is still not entirely clear. Still, this example does what you ask for:

use strict; use warnings; my @methodcalls = qw (getExeName getTempName); my @getDataMethods = qw (getNameType); my $obj = Foo->new(); my %hash; my $counter=0; for my $call (@methodcalls) { $hash{"temp".$counter} = $obj->$call; $counter++; } for my $call (@getDataMethods) { $hash{"temp".$counter} = $obj->getData->$call; $counter++; } for (keys %hash) { print "$_\t $hash{$_}\n"; } package Foo; sub new { return bless \{}, shift; } sub getExeName { return "exename"; } sub getTempName { return "tempname"; } sub getData { return shift; } sub getNameType { return "nametype"; }

Note that the getData->getNameType call has to be treated separately from the simple method calls. If you have other (possibly more complex) chained methods, you'll have to adapt the code to handle those as well (unless someone can think of a better way?)

Update: As BrowserUK has said, the chained methods can be done with a string eval. It's a slow and ugly way of doing it, but it works:

use strict; use warnings; my $obj = Foo->new(); my %hash; my $counter=0; my @methodcalls = qw (getExeName getTempName getData->getNameType); for my $call (@methodcalls) { my $evalstring = "\$obj->$call"; $hash{"temp".$counter} = eval($evalstring); $counter++; } for (keys %hash) { print "$_\t $hash{$_}\n"; }

--------------------------------------------------------------

"If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."

John Brunner, "The Shockwave Rider".


In reply to Re: elegant way of handling hash? by g0n
in thread elegant way of handling hash? by Anonymous Monk

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 imbibing at the Monastery: (4)
As of 2024-04-19 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found