List Info

Thread: Re: AUTOLOAD




Re: AUTOLOAD
user name
2007-01-17 12:27:19
On 1/17/07, tilemonia.org <tilemonia.org> 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). But ->can is a method and you may extend it as you
feel
appropriate. If you what you feel appropriate is coherent,
answering
the same as UNIVERSAL::can to the predefined methods and
something
like &AUTOLOAD to the other cases handled by AUTOLOAD,
your ->can
will keep on with the abstraction of telling you what
methods can
possibly be invoked over the object in question.

>This is because
> it can't predict the future, but also because the
interfaces and
> conventions of Perl don't have anything to plug into
AUTOLOAD which also
> sorts out "can". (Well, there are things that
do that, like Class::Std,
> but they're not in very wide usage).

It is not about predicting the future. It is about complying
with the
present written in your code. But nothing obliges you. You
may write
an AUTOLOAD without a corresponding update to ->can and
get away with
murder in a very large number of applications. But to be
fully
consistent, there will be a time when the update to ->can
will be
natural and felt as bug by your code users if it is
missing.

Re: AUTOLOAD
user name
2007-01-18 09:45:57
--- Adriano Ferreira <a.r.ferreiragmail.com> wrote:

> On 1/17/07, tilemonia.org <tilemonia.org> 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 universal.pl
line 13.
  1..2
  # Looks like your test died just after 2.
  ok
  All tests successful.
  Files=1, Tests=2,  0 wallclock secs ( 0.04 cusr +  0.00
csys =  0.04
CPU)

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

Cheers,
Ovid

--

Buy the book -- http://www.or
eilly.com/catalog/perlhks/
Perl and CGI -- http://u
sers.easystreet.com/ovid/cgi_course/

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )