Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Why does this work? AUTOLOAD sans sub declaration

by braswell (Sexton)
on Nov 10, 2003 at 20:43 UTC ( #305965=perlquestion: print w/ replies, xml ) Need Help??
braswell has asked for the wisdom of the Perl Monks concerning the following question:

I must confess that I was writing some code and I omitted the 'sub' declaration in front of AUTOLOAD. Since it half-way works, it was a pain to troubleshoot. But I was surprised that it worked at all without even a warning. I vaguely recall reading about something similar but I can't find the documentation on it. Can anyone say why? Example:
use strict; use warnings; package Foo; our $AUTOLOAD; sub new { return bless {}, shift } sub do_something { print "did something" } AUTOLOAD { # note it's missing the 'sub' declaration print "message from Foo: undefined method ($AUTOLOAD) called"; } package main; my $instance = Foo->new; $instance->do_something; # prints "did something" $instance->do_notimplemented; # prints "message from Foo: undefinded m +ethod (Foo::do_notimplemented) called

Comment on Why does this work? AUTOLOAD sans sub declaration
Download Code
Re: Why does this work? AUTOLOAD sans sub declaration
by batkins (Chaplain) on Nov 10, 2003 at 20:50 UTC
    That is indeed odd. Try putting a semicolon on the line that defines do_something.

    Still odd even if that does fix it.

    Are you sure it was a book? Are you sure it wasn't.....nothing?

      Named subroutine definitions aren't expressions. They don't need semicolons after the block.

        Yeah, you're absolutely right. I think I was thinking of prototypes.

        Are you sure it was a book? Are you sure it wasn't.....nothing?
Re: Why does this work? AUTOLOAD sans sub declaration
by converter (Priest) on Nov 10, 2003 at 21:06 UTC

    Since the perlsub POD mentions the AUTOLOAD subroutine in the same sentence as "BEGIN", "CHECK", "INIT", "END", "CLONE" and "DESTROY", I assume it shares some of the same characteristics as the others, the important one in this case being that the sub keyword is implied. When fed your code, B::Deparse prints the following. Notice how the sub keyword is inserted for the BEGIN subs, as well as the AUTOLOAD sub:

    package Foo; use warnings; use strict 'refs'; our $AUTOLOAD; sub main::BEGIN { package main; no strict 'refs'; require strict; do { 'strict'->import }; } sub main::BEGIN { package main; require warnings; do { 'warnings'->import }; } sub new { return bless({}, shift @_); } sub do_something { print 'did something'; } sub AUTOLOAD { print "message from Foo: undefined method ($AUTOLOAD) called"; } package main; my $instance = 'Foo'->new; $instance->do_something; $instance->do_notimplemented;
Re: Why does this work? AUTOLOAD sans sub declaration
by hardburn (Abbot) on Nov 10, 2003 at 21:12 UTC

    When I ran your code through B::Deparse, it automagically put a sub keyword before AUTOLOAD. I don't remember this being documented anywhere, though there is plenty of stuff in Perl's documentation that is easy to overlook :)

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: Why does this work? AUTOLOAD sans sub declaration
by duff (Vicar) on Nov 10, 2003 at 21:44 UTC

    What works "half-way" about it?

    I was all set to say that this is documented behavior, but then I couldn't find where it was documented. I guess it's just part of the things I've picked up over time that one of the ways that all-caps sub names are special is that the word "sub" is optional.

    Anyone know what POD this is documented in or if it's documented at all?

      Not all all-caps functions imply the sub. The tie-related ones don't.

      Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://305965]
Approved by Enlil
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2014-10-21 13:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (103 votes), past polls