develooper Front page | perl.perl5.porters | Postings from August 2001

Re: 'can' with undefined subs

Thread Previous
August 19, 2001 03:11
Re: 'can' with undefined subs
Message ID:
In article <>, "Tony Bowden"
<> wrote:

> So we end up with a scenario where UserA, thinking she's playing safe,
> doesn't just call Foo->$method(@args), but ensures it's there first:
>   if (my $ref = Foo->can($method)) {
>     $ref->(@args)
>   }
> And the program still blows up.
defined() can come in handy:

if (my $ref = Foo->can($method)) {
	$ref->(@args) if defined &$ref;

Of course, that doesn't work in the face of an AUTOLOADed $method.  At
some point, saying "Don't use forward declarations if you don't define
your subs somehow" is a lot easier than the alternatives:

my $ref;

if ($ref = Foo->can($method) and (defined &$ref or Foo->can('AUTOLOAD'))) {
} else {
	die "Can't call $method!"

-- c

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About