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

Re: 'can' with undefined subs

Thread Previous | Thread Next
From:
Hugo van der Sanden
Date:
August 16, 2001 17:19
Subject:
Re: 'can' with undefined subs
Message ID:
200108170018.f7H0Iqt22571@crypt.compulink.co.uk
Tony Bowden <tony@kasei.com> wrote:
:A weird question came up on the ScriptScape that seems to have surprised
:quite a few people: if you have a sub that has been forwards declared,
:but never actually defined, what does 'can' return?

A stub, which is a dummy coderef.

:Is this behaviour 'correct'?

It is deliberate, which is almost as good.

:It's somewhat useful from an end-user's point of view, as it enables 
:  if (my $ref = Foo->can('bar')) {
:    $ref->();
:  }
:to work as expected, if there's an AUTOLOAD that does the right thing

That's why we do it.

:but it will equally trip the user up if there isn't an AUTOLOAD and the
:method they thought they were safe in calling explodes.

I think that's really down to the user not to declare a sub without
providing an implementation - "Undefined subroutine &Foo::bar called"
is about as helpful as we can be at that point.

Part of the problem here, of course, is that AUTOLOAD may actually
implement the subroutine without ever generating an isolated coderef
for it; we have talked about splitting AUTOLOAD into two functions
(one to locate the routine and the other to call it), but I don't
think it removes the usefulness of the stub for such cases.

:It also doesn't seem to be documented anywhere.

Patches welcome, as always. You may well find that we are deficient
on tests for this behaviour as well. :)

Hugo

Thread Previous | Thread Next


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