http://www.perlmonks.org?node_id=117994


in reply to Re (tilly) 1: Strict, strings and subroutines
in thread Strict, strings and subroutines

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

Replies are listed 'Best First'.
Re:{3} Strict, strings and subroutines
by jeroenes (Priest) on Oct 10, 2001 at 18:44 UTC
    Great.

    A pity you don't cache the possibility that it doesn't exist:

    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.

      BEGIN { my %cache; sub do_it { my $func = shift; $cache{$func} = do { local @ISA; __PACKAGE__->can("do_$fun") } unless defined $cache{$func}; $cache{$func}->(@_); } }
      This does precisely what mine does. Did you mean "exists" there instead of "defined"?

      -- Randal L. Schwartz, Perl hacker

Re (tilly) 3: Strict, strings and subroutines
by tilly (Archbishop) on Oct 10, 2001 at 18:57 UTC
    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... :-)
      Unless someone has a strange perl configuration, UNIVERSAL is statically linked. So you don't pull in UNIVERSAL, it is just there.
        I possibly was unclear.

        I meant pulling in UNIVERSAL in that functions which exist in UNIVERSAL but not in the current package would be found by merlyn's can invocation, even though you might not want them to be. In other words his local @ISA trick doesn't quite succeed in making a method lookup into a function lookup because it still finds things in UNIVERSAL.

Re: Re: Re (tilly) 1: Strict, strings and subroutines
by gbarr (Monk) on Oct 10, 2001 at 19:16 UTC
    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.
      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;
        What good does that do?

        Graham's point is that when you start dynamically changing inheritance, Perl throws away its memoized method lookup results. Even if you change the inheritance right back, your cache is still gone and it has to work out results again the long way.