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

Hello Monks, I have a (hopefully) simple question:

I'm trying to verify if a module has an AUTOLOAD subroutine or not. I can successfully determine whether other subroutines exist using code like this:
require Test::Pkg::Thing; my $func_name = "Test::Pkg::Thing::do_something"; if( defined &$func_name ) { print "defined!\n"; }
Using the same method if I try to detect if AUTOLOAD is defined, it actually tries to call Test::Pkg::Thing::AUTOLOAD even if it doesn't exist (which of course is a problem and causes the program to exit with an error.)
require Test::Pkg::Thing; my $func_name = "Test::Pkg::Thing::AUTOLOAD"; if( defined &$func_name ) # not what I expect { print "defined!\n"; }
Does any monk have a nugget of wisdom to share?

Replies are listed 'Best First'.
Re: Does AUTOLOAD exist?
by rinceWind (Monsignor) on Oct 12, 2005 at 16:01 UTC

    You can use 'can', even if the interface is not OO. I use this in Module::Optional

    require Test::Pkg::Thing; if( Test::Pkg::Thing->can('AUTOLOAD') ) { print "can AUTOLOAD!\n"; }

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

Re: Does AUTOLOAD exist?
by Perl Mouse (Chaplain) on Oct 12, 2005 at 15:41 UTC
    Are you sure it doesn't have an AUTOLOAD?
    $ mkdir -p Test/Pkg $ echo 'package Thing; 1' > Test/Pkg/Thing.pm $ perl -w require Test::Pkg::Thing; my $func_name = "Test::Pkg::Thing::AUTOLOAD"; if( defined &$func_name ) # not what I expect { print "defined!\n"; } __END__ $
    No output, just as expected.
    Perl --((8:>*
      Actually it wasn't an issue with this code, it works. *sheepish grin*

      What was causing the issue was Carp.. I was using it to croak if the method didn't exist.. it seems it's not friendly with our code and was trying to execute the nonexistant AUTOLOAD method.

      Thanks :)