Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Alternative to AUTOLOAD

Reply
Thread Tools

Alternative to AUTOLOAD

 
 
J Krugman
Guest
Posts: n/a
 
      04-08-2004




I've just come across this sort of code (from the CPAN module
SOAP::Lite):

sub BEGIN {
no strict 'refs';
for my $method (qw(ids hrefs parts parser base xmlschemas xmlschema)) {
my $field = '_' . $method;
*$method = sub {
my $self = shift->new;
@_ ? ($self->{$field} = shift, return $self) : return $self->{$field};
}
}
}

(SOAP::Lite comprises many packages; practically every one of them
has a BEGIN block in which methods are defined using this technique.)

This seems to me like an alternative to AUTOLOAD for defining
generic accessor methods. Any ideas about why this technique would
be preferable to using AUTOLOAD?

Thanks!

jill

--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      04-08-2004
J Krugman <> writes:

> I've just come across this sort of code (from the CPAN module
> SOAP::Lite):
>
> sub BEGIN {
> no strict 'refs';
> for my $method (qw(ids hrefs parts parser base xmlschemas xmlschema)) {
> my $field = '_' . $method;
> *$method = sub {
> my $self = shift->new;
> @_ ? ($self->{$field} = shift, return $self) : return $self->{$field};
> }
> }
> }
>
> (SOAP::Lite comprises many packages; practically every one of them
> has a BEGIN block in which methods are defined using this technique.)
>
> This seems to me like an alternative to AUTOLOAD for defining
> generic accessor methods. Any ideas about why this technique would
> be preferable to using AUTOLOAD?


If a package can be subclassed then all it's autoloadable methods have
to be stubbed so that Perl knows to call the AUTOLOAD in your class
not in the subclass.

The choice beween using stubs and AUTOLOAD or just defining the whole
lot in a loop is largely a matter of personal preference.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
Reply With Quote
 
 
 
 
J Krugman
Guest
Posts: n/a
 
      04-08-2004
In <> Brian McCauley <> writes:

>J Krugman <> writes:


>> I've just come across this sort of code (from the CPAN module
>> SOAP::Lite):
>>
>> sub BEGIN {
>> no strict 'refs';
>> for my $method (qw(ids hrefs parts parser base xmlschemas xmlschema)) {
>> my $field = '_' . $method;
>> *$method = sub {
>> my $self = shift->new;
>> @_ ? ($self->{$field} = shift, return $self) : return $self->{$field};
>> }
>> }
>> }
>>
>> (SOAP::Lite comprises many packages; practically every one of them
>> has a BEGIN block in which methods are defined using this technique.)
>>
>> This seems to me like an alternative to AUTOLOAD for defining
>> generic accessor methods. Any ideas about why this technique would
>> be preferable to using AUTOLOAD?


>If a package can be subclassed then all it's autoloadable methods have
>to be stubbed so that Perl knows to call the AUTOLOAD in your class
>not in the subclass.


>The choice beween using stubs and AUTOLOAD or just defining the whole
>lot in a loop is largely a matter of personal preference.


Pardon my ignorance, but what are stubs? Where can I find
documentation on this? I couldn't find a definition of "stub" in
any of the usual places (Programming Perl, perltoot, FAQ, etc.)

Thanks,

jill


>--
> \\ ( )
> . _\\__[oo
> .__/ \\ /\@
> . l___\\
> # ll l\\
> ###LL LL\\

--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      04-08-2004
J Krugman wrote:

> Pardon my ignorance, but what are stubs? Where can I find
> documentation on this? I couldn't find a definition of "stub" in
> any of the usual places (Programming Perl, perltoot, FAQ, etc.)


perldoc Devel::SelfStubber

Check out all the *.al files in your perl installation.
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      04-09-2004
J Krugman <> wrote in comp.lang.perl.misc:
> In <> Brian McCauley <> writes:
>
> >J Krugman <> writes:

>
> >> I've just come across this sort of code (from the CPAN module
> >> SOAP::Lite):
> >>
> >> sub BEGIN {
> >> no strict 'refs';
> >> for my $method (qw(ids hrefs parts parser base xmlschemas xmlschema)) {
> >> my $field = '_' . $method;
> >> *$method = sub {
> >> my $self = shift->new;
> >> @_ ? ($self->{$field} = shift, return $self) : return $self->{$field};
> >> }
> >> }
> >> }
> >>
> >> (SOAP::Lite comprises many packages; practically every one of them
> >> has a BEGIN block in which methods are defined using this technique.)
> >>
> >> This seems to me like an alternative to AUTOLOAD for defining
> >> generic accessor methods. Any ideas about why this technique would
> >> be preferable to using AUTOLOAD?

>
> >If a package can be subclassed then all it's autoloadable methods have
> >to be stubbed so that Perl knows to call the AUTOLOAD in your class
> >not in the subclass.

>
> >The choice beween using stubs and AUTOLOAD or just defining the whole
> >lot in a loop is largely a matter of personal preference.

>
> Pardon my ignorance, but what are stubs? Where can I find
> documentation on this? I couldn't find a definition of "stub" in
> any of the usual places (Programming Perl, perltoot, FAQ, etc.)


It's another name for a "forward" declaration, which looks like

sub foo;

This declares "foo" as a subroutine, but leaves the definition for
later. One way of completing the definition is through AUTOLOADER.

The effect of a forward declaration is that "foo" is known to be
a subroutine from then on. So ...->can( "foo") will find the method
"foo" if it is declared (it doesn't have to be defined). Even
"\ &foo" can be used and will do the right thing if the definition
is supplied later.

Anno
 
Reply With Quote
 
J Krugman
Guest
Posts: n/a
 
      04-09-2004


Joe, Anno: Thanks!

jill

--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
*{$AUTOLOAD} vs *$AUTOLOAD J Krugman Perl Misc 1 07-14-2005 06:09 AM
Autoload Page outside of Frameset Thomas Schneider HTML 6 06-08-2005 02:58 PM
Tricky AUTOLOAD behavior Jim Schueler Perl 1 08-25-2004 08:00 AM
Autoload to a different frame mdudlik HTML 0 06-06-2004 01:51 AM
CD Autoload IHSRC Computer Support 0 01-08-2004 02:46 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57