in reply to Re^2: How to determine if a package method is defined
in thread How to determine if a package method is defined

it's quite likely that the OP should have been using exists and that can will do the trick by itself.

Well, depends on whether this is the desired result...

#!/usr/bin/perl $TEST = "foo"; if ( $TEST->can("test_sub") ) { $TEST->test_sub; } else { print "Method doesn't exist"; } package foo; sub test_sub; __END__ Undefined subroutine &foo::test_sub called at ./798474.pl line 6.

And if the method was defined (likely, in real life, if it's there at all), the difference between defined and exists wouldn't matter (which is why I skipped that part and focused on the inheritance aspect).  In other words, I'd say in practical terms the difference between defined and exists is mostly irrelevant (after all, why would people declare bodiless subs in their packages), but if you want to be sure a method can actually be called, then do test whether it's defined...

Replies are listed 'Best First'.
Re^4: How to determine if a package method is defined
by ikegami (Patriarch) on Oct 01, 2009 at 04:00 UTC
    Yeah, it'll cause an error, but that's good since it is an error. Something promised a sub and it didn't deliver. Silently ignoring the sub you were asked to use is not the right course.