ianyappy:

One thing you have to understand about the statement  sub name { ... } is that it always executes at compile time if it is not in a string eval (i.e., is in a block  eval or is a nested subroutine definition in another subroutine). So your impression that "AUTOLOAD isn't even being called" is correct. See example below.

>perl -wMstrict -le "{ package Foo; use warnings; use strict; our $AUTOLOAD; sub AUTOLOAD { eval { sub foo { return 'FOO'; } }; sub bar { return 'BAR'; } print qq{AUTOLOADing '$AUTOLOAD'}; no strict 'refs'; *$AUTOLOAD = sub { return uc $AUTOLOAD }; goto &$AUTOLOAD; } } ;; print 'foo? ', exists &Foo::foo; print Foo->foo; print 'bar? ', exists &Foo::bar; print Foo->bar; print 'baz? ', exists &Foo::baz; print Foo->baz; print 'baz? ', exists &Foo::baz; print Foo->baz; " foo? 1 FOO bar? 1 BAR baz? AUTOLOADing 'Foo::baz' FOO::BAZ baz? 1 FOO::BAZ

In this example,  Foo::foo and  Foo::bar already exist at runtime, so  AUTOLOAD is never called for them. The first time  Foo::baz is called,  AUTOLOAD is called and defines the subroutine and also executes the just-defined subroutine. When  Foo::baz is called subsequently, it exists.

Update: To pound the point about nested subroutine definitions even further into the ground, remove the  sub bar { return 'BAR'; } statement from the  AUTOLOAD function in the example above and replace the  eval statement with
    eval { sub foo { sub bar { return 'BAR'; }  return 'FOO'; } };
or the even more pathological
    eval { sub foo { return 'FOO';  sub bar { return 'BAR'; } } };
The results of execution are the same.


In reply to Re^3: say statements within 'sub AUTOLOAD' by AnomalousMonk
in thread say statements within 'sub AUTOLOAD' by ianyappy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.