http://qs321.pair.com?node_id=117988


in reply to Strict, strings and subroutines

Getting into the gross, this does something slightly different from the above solutions. Though called like a function, it will actually do a method search for it:
__PACKAGE__->can("do_$type")->();

Replies are listed 'Best First'.
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

      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

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