perl.org wrote:
> I can probably figure this out if I spend some time but as I was
> going through it more questions were raised.
> 
> Is it possible for a Perl script to check if a subroutine exists in a
> module without actually invoking that subroutine?

Not really, because of autoloading. See perldoc perlsub under the heading
"Autoloading".

> 
> I know there is some way to get a reference to a subroutine.  If I
> can figure out how to get this reference (which I assume would be
> undef or something if I try to get a reference to a subroutine that
> does not exist) that would probably help, but I am not sure of the
> syntax or if that's the right approach. 

You can take a reference to a subroutine that doesn't yet exist! Consider:

  $x = \&foo;             # reference to non-existant sub

  print "\$x is a reference to ", ref($x), "\n";
  eval { &$x };           # try to call the sub
  warn $@ if $@;

  eval qq[sub foo { print "Foo is here now!\n" }];   # define the sub
  &$x;                    # you can call it now

Outputs:

  $x is a reference to CODE
  Undefined subroutine &main::foo called at foo.pl line 4.
  Foo is here now!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to