Re: Re (tilly) 1: Strict, strings and subroutines
by merlyn (Sage) on Oct 10, 2001 at 18:01 UTC
|
I thought about that one too, but it also follows @ISA. You can fix that with this:
do { local @ISA; __PACKAGE__->can("do_$type") }->();
Much smoke. Many mirrors. In fact, I'd cache the result too:
BEGIN {
my %cache;
sub do_it {
my $func = shift;
($cache{$func} ||= do { local @ISA; __PACKAGE__->can("do_$fun") })
->(@_);
}
}
...
do_it("function", $arg1, $arg2, @more_args);
-- Randal L. Schwartz, Perl hacker | [reply] [d/l] [select] |
|
|
BEGIN {
my %cache;
sub do_it {
my $func = shift;
$cache{$func} = do {
local @ISA; __PACKAGE__->can("do_$fun")
}
unless exist $cache{$func};
$cache{$func}->(@_);
}
}
Jeroen
Update: merlyn is right. Fixed that. | [reply] [d/l] |
|
|
| [reply] [d/l] |
|
|
But you still don't avoid pulling in UNIVERSAL. Which is why I left it at, "It does something different." If you don't understand what you are doing differently and don't have a reason for it, well then you don't need to do this. But then again that is true for most of these solutions... :-)
| [reply] |
|
|
Unless someone has a strange perl configuration, UNIVERSAL is statically linked.
So you don't pull in UNIVERSAL, it is just there.
| [reply] |
|
|
|
|
|
|
Changing @ISA at runtime is not always a good thing to do.
If your
application does use a lot of objects and method calls
it will cause the
method lookup cache to be cleared.
So it will have the affect of slowing your program down.
By how much, and if that is an issue really depends on the
application though.
| [reply] |
|
|
Changing @ISA at runtime is not always a good thing to do. If your application does use a lot of objects and method calls it will cause the method lookup cache to be cleared
That's the beauty of local isn't it?
Simon Flack ($code or die)
$,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
=~y'_"' ';eval"die";print $_,lc substr$@,0,3;
| [reply] |
|
|