Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to create a reference to a subroutine from a package being used? For instance, if I had a package called Subs, which had a subroutine called new, I try to do this:

use Subs; my %menu = ( n => \&Subs->new; ); . . . $menu{n}->();


This doesn't work, is there a way to pull it off?

Replies are listed 'Best First'.
Re: references to subroutines
by Anonymous Monk on Mar 14, 2002 at 00:44 UTC
    Nevermind I figured it out:

    my %menu = ( n => \&Subs::new; );


    That was dumb.
      Except if that's a method call, that won't work, because you're not passing the name of the class as the first parameter. Try this:
      my %menu = ( n => sub { Subs->new }, );

      -- Randal L. Schwartz, Perl hacker