in reply to Re: referring to packages indirectly
in thread referring to packages indirectly
Grab the CODE slot of the typeglob if you want to check if a function is defined. The existance of any of the package variables $f_preferred, @f_preferred, %f_preferred, etc will cause exists to return true, so exists is not suitable.
my $pkg = ...; my $mod_file = ...; # require always returns true or an exception. eval { require $mod_file } or warn $@; my $sym = do { no strict 'refs'; ${ "${pkg}::" }{ 'f_preferred' } }; my $ref = *$sym{CODE};
If the function hasn't been declared, or if the function has been declared (sub func;) but not defined (sub func { ... }), then $ref will be undef.
Update: Rephrased for increased clarity. Code unchanged.
|
|---|