"sub", when used as a declaration and not an expression, "happens"
(i.e., modifies the package) immediately when the parser sees your
code.
If you want to install the code block late, use an assignment into the
symbol table:
sub AUTOLOAD {
*{"funny"} = sub {
# ...
};
}
Note that since this is a statement, it should (generally) be followed
by a semicolon.
On Thu, Jul 15, 2010 at 10:13 AM, Chanan Berler <[email protected]> wrote:
> Hi All,
>
> Me going back on working with perl made me go over and look at the books.
> Got a question for you all:
>
> when invoking a none declared subroutine, I was told it will invoke
> the AUTOLOAD sub with same arguments
> sent to my origin (none declared) sub.
>
> So I wrote the following:
> and the output was:
> Before:
> AUTOLOAD | *TestMe::AUTOLOAD
> funny | *TestMe::funny
> single | *TestMe::single
> Found: 12
> Running funny from Autoload
> Before:
> AUTOLOAD | *TestMe::AUTOLOAD
> funny | *TestMe::funny
> single | *TestMe::single
>
> Q: why am I seeing the funny sub before declaring it ? should I not
> see it after invoking the sub ?
>
>
> Here is what I wrote:
> ==================
> #!C:\Perl\bin\perl.exe
> use warnings;
> use strict;
>
> package TestMe;
> our $single = 1;
> sub AUTOLOAD
> {
> sub funny{
> my $arg = shift;
>
> print "Found: $arg\n";
> if ($single)
> {
> print "Running funny from Autoload\n";
> $single--;
> } else {
>
> print "Running the sub after declaring.\n";
> }
>
> }
> }
>
> print "Before:\n";
> &::dispSymbols(\%TestMe::);
> &funny(12);
> print "Before:\n";
> &::dispSymbols(\%TestMe::);
>
>
> sub ::dispSymbols {
> my($hashRef) = @_;
> my(%symbols);
> my(@symbols);
>
> %symbols = %{$hashRef};
> �...@symbols = sort(keys(%symbols));
>
> foreach (@symbols)
> {
> printf("%-10.10s| %s\n", $_, $symbols{$_});
> }
> }
>
> --
> ===================
> ---- Chanan Berler ----
> ===================
> _______________________________________________
> Perl mailing list
> [email protected]
> http://mail.perl.org.il/mailman/listinfo/perl
--
Gaal Yahas <[email protected]>
http://gaal.livejournal.com/
_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl