Re: Dynamically constructed function calls
by ikegami (Patriarch) on Nov 03, 2004 at 23:50 UTC
|
my $method = "prefix_$key";
$f->$method($atr);
| [reply] [d/l] |
Re: Dynamically constructed function calls
by itub (Priest) on Nov 03, 2004 at 23:53 UTC
|
From perldoc perlop
Otherwise, the right side is a method name or a simple scalar variable
containing either the method name or a subroutine reference, and the
left side must be either an object (a blessed reference) or a class
name (that is, a package name). See perlobj.
This means that you can't have a string or an expression as a method name. It has to be a simple scalar. So if you do the following, everything should work fine:
my $method = "prefix_$key";
$f->$method($atr);
| [reply] [d/l] [select] |
|
It has to be a scalar, but it doesn't need to be a variable. You can say
$f->${\("prefix_$key")}($atr);
However, using a variable is probably clearer.
Update: added the missing '$' pointed out by ikegami
--Dave Opinions my own; statements of fact may be in error.
| [reply] [d/l] |
|
sub foo { print "in foo" }
sub do {
my $self = shift;
my $meth = shift;
$self->$meth(@_)
}
$object->do("foo", @args);
| [reply] [d/l] |
|
|
|
|
| [reply] [d/l] [select] |
|
Nice trick, I had never thought about that! :-)
| [reply] |
Re: Dynamically constructed function calls
by BrowserUk (Patriarch) on Nov 03, 2004 at 23:59 UTC
|
while ( (my ($key,$atr)) = (each %vars))
{
print "$key, $atr\n";
my $method = "prefix_$key";
$f->$method( $atr );
}
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
| [reply] [d/l] |
Re: Dynamically constructed function calls
by Anonymous Monk on Nov 04, 2004 at 12:56 UTC
|
Just check if you can call it
$f->can("prefix_$key")->( $atr );
anon-broquaint | [reply] [d/l] |
|
That doesn't work. $f->can returns a false value, and then you get the message:
Can't use string ("") as a subroutine ref while "strict refs" in use a
+t autol.pl line 21.
Update:In the pod for UNIVERSAL.pm, it says:
"can" cannot know whether an object will be able to provide a
method through AUTOLOAD, so a return value of undef does not necessarily mean the object will not be able to handle the method call.
| [reply] [d/l] |
|
{
package Foo;
my %meth = (
foo => sub { print "Foo?\n" },
bar => sub { print "Bar!\n" },
);
sub baz {
print "Baz.\n";
}
sub can {
$meth{$_[1]} || __PACKAGE__->UNIVERSAL::can($_[1])
}
sub AUTOLOAD {
use vars '$AUTOLOAD';
(my $methname = $AUTOLOAD) =~ s/.*:://;
$meth{$methname}->(@_);
}
}
for (qw(foo bar baz qux)) {
if (my $cr = Foo->can($_)) {
$cr->();
} else {
print "Cannot $_\n";
}
}
| [reply] [d/l] |
|
$f->can("prefix_$key")->( $f, $atr );
Being right, does not endow the right to be rude; politeness costs nothing. Being unknowing, is not the same as being stupid. Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence. Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.
| [reply] [d/l] |
|
anon-broquaint
Why?
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
| [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in. |