Ovid publiustemp-londonpm at
Thu Jan 18 15:45:57 GMT 2007

--- Adriano Ferreira <a.r.ferreira at> wrote:

> On 1/17/07, ti at <ti at> wrote:
> > UNIVERSAL::can tells you what methods are *in the hash*. It doesn't
> tell
> > you what AUTOLOAD would do, because, as you say, it can't.
> ->can is a method. Its base implementation (the one provided by
> UNIVERSAL package) "tells you what methods are *in the hash*" (as you
> said).

To be fair, it doesn't quite do that (depending upon how you look at
it).  If you merely have a forward declaration and no method,
UNIVERSAL->can($method) succeeds, but then invoking the method fails.

  package Foo;
  sub bar;
  package main;

  use Test::More 'no_plan';
  can_ok 'Foo', 'bar';
  ok my $method = Foo->can('bar'), '... can("bar") should succeed';
  diag +Foo->$method;

Results in the rather confusing:

  ok 1 - Foo->can('bar')
  ok 2 - ... can("bar") should succeed
  Undefined subroutine &Foo::bar called at line 13.
  # Looks like your test died just after 2.
  All tests successful.
  Files=1, Tests=2,  0 wallclock secs ( 0.04 cusr +  0.00 csys =  0.04

Basically, a forward declaration appears to fill the *Foo::bar{CODE}
slot.  I personally think this is bug in 'can', but I'm sure opinions
will differ on this.



Buy the book --
Perl and CGI --

More information about the mailing list