in reply to elegant way of handling hash?
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".
|
|---|