Alternative to AUTOLOAD

Discussion in 'Perl Misc' started by J Krugman, Apr 8, 2004.

  1. J Krugman

    J Krugman Guest

    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.
     
    J Krugman, Apr 8, 2004
    #1
    1. Advertising

  2. 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\\
     
    Brian McCauley, Apr 8, 2004
    #2
    1. Advertising

  3. J Krugman

    J Krugman Guest

    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.
     
    J Krugman, Apr 8, 2004
    #3
  4. J Krugman

    Joe Smith Guest

    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.
     
    Joe Smith, Apr 8, 2004
    #4
  5. J Krugman

    Anno Siegel Guest

    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
     
    Anno Siegel, Apr 9, 2004
    #5
  6. J Krugman

    J Krugman Guest

    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.
     
    J Krugman, Apr 9, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Schueler

    Tricky AUTOLOAD behavior

    Jim Schueler, Aug 25, 2004, in forum: Perl
    Replies:
    1
    Views:
    459
  2. mdudlik

    Autoload to a different frame

    mdudlik, Jun 6, 2004, in forum: HTML
    Replies:
    0
    Views:
    424
    mdudlik
    Jun 6, 2004
  3. Thomas Schneider

    Autoload Page outside of Frameset

    Thomas Schneider, Jun 2, 2005, in forum: HTML
    Replies:
    6
    Views:
    2,917
    Thomas Schneider
    Jun 8, 2005
  4. Wybo Dekker

    tk/autoload not found

    Wybo Dekker, Jul 12, 2004, in forum: Ruby
    Replies:
    4
    Views:
    171
    Hidetoshi NAGAI
    Jul 13, 2004
  5. J Krugman

    *{$AUTOLOAD} vs *$AUTOLOAD

    J Krugman, Jul 13, 2005, in forum: Perl Misc
    Replies:
    1
    Views:
    104
    Tassilo v. Parseval
    Jul 14, 2005
Loading...

Share This Page