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".
-
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.
|