Berkeley CSUA MOTD:Entry 35129
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/07/09 [General] UID:1000 Activity:popular
7/9     

2004/11/30-12/1 [Computer/SW/Languages/Perl] UID:35129 Activity:nil
11/30   Perl question, I want to make a sub do_these which when called as
        do_these(('foo','bar')); will call foo() and bar()
        will something like
        foreach(@_) { &$_(); }
        work?
        \_ Yup.  If you get an error about "strict refs", add the line
           "no strict 'refs';" at the beginning of sub do_these.  --mconst
        \_ I'd suggest looking through hoserchat source code (locate hoserchat
           on soda) and look at how the function pointers are done there.
           \_ mconst says I'm good to go, but the way hoserchat does it is to
              use function references, not dereferenced fuction names.  Is
              this a TWTOWTDI situation, or is it illegal to dereference a
              function name, since $_ will after all be a string?
              \_ TMTOWTDI.  Real references are sometimes safer: they work
                 regardless of what package you're in, and they keep working
                 even if the original function gets renamed or undefined.
                 (That's why "use strict" disables the ability to use a
                 function or variable name as a reference.)  It's usually
                 not a big deal, though.  The cool thing is that your code
                 will actually work either way -- if you want to use real
                 references, just call do_these(\&foo, \&bar) instead. --mconst
        \_ OK, because I'm calling specific instances of a class function
           (probably should have mentioned that)  I actually needed to use
           $self->$_ in do_each(), as &$_() just gave calls to member function
           of an uninstantiated class.  All in all, thanks a bunch to both of
           you. -op