develooper Front page | perl.perl5.porters | Postings from October 2003

Re: cannot AUTOLOAD a method called VERSION

Thread Previous
From:
Graham Barr
Date:
October 21, 2003 11:25
Subject:
Re: cannot AUTOLOAD a method called VERSION
Message ID:
F5A3F75E-03F3-11D8-8802-0003938857CC@pobox.com
On 21 Oct 2003, at 13:59, Dirk Koopman wrote:
> It appears that AUTOLOAD will never see a request for a method (an
> autogenerated external data dictionary accessor in my case) called
> 'VERSION'.

That is correct. And is the same for all methods that are defined in 
UNIVERSAL because AUTOLOAD is only called if a method cannot be found 
and methods in UNIVERSAL can be found in all classes.

>  It will simply bypass it and try to return the (usually
> non-existant) scalar $FOO::VERSION (and, failed, the error message
> associated with that).

That is not strictly true. When called UNIVERSAL::VERSION looks at its 
first argument to obtain a package name. If then access $VERSION in 
that package. If there is no second argument it simply returns that 
VERSION, if there is it does a comparison and die it the VERSION is 
less than the given argument.

> This is fixed by having an explicit method called VERSION in one
> of the ancestors (ie: sub VERSION {...}).

Actually you can get AUTOLOAD to be called by doing

sub VERSION;

But I would reccomend against doing this. The VERSION method is used by 
perl itself if the module is use'd and a version is requested, eg

   use Foo 1.12;

is actually

   BEGIN {
     require Foo;
     Foo->VERSION("1.12");
     Foo->import;
   }

Graham.

> I wonder what other 'magical' names there are?
>
> Dirk
> -- 
> Please Note: Some Quantum Physics Theories Suggest That When the
> Consumer Is Not Directly Observing This Message, It May Cease to
> Exist or Will Exist Only in a Vague and Undetermined State.
>
>


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