Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re (tilly) 1: Strict, strings and subroutines

by tilly (Archbishop)
on Oct 10, 2001 at 17:24 UTC ( #117988=note: print w/ replies, xml ) Need Help??


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")->();


Comment on Re (tilly) 1: Strict, strings and subroutines
Download Code
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;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://117988]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2014-09-18 09:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (110 votes), past polls