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

Re: 'can' with undefined subs

Thread Previous
From:
chromatic
Date:
August 19, 2001 03:11
Subject:
Re: 'can' with undefined subs
Message ID:
20010819030832.72417.qmail@onion.perl.org
In article <20010817081640.A1868@blackstar.co.uk>, "Tony Bowden"
<tony@kasei.com> 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'))) {
	$ref->(@args);
} else {
	die "Can't call $method!"
}

-- c

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About