{ package MyDate; use strict; use warnings; sub AUTOLOAD { our $AUTOLOAD; say "Going into AUTOLOAD"; # Why doesn't this print? my $method = $AUTOLOAD; $method =~ s/.*:://; grep {$method eq $_} qw/yak day month/ or Carp::croak "Invalid + method: $method."; if ($method eq "day") { eval { sub day {(localtime)[3];} }; } elsif ($method eq "month") { eval { sub month {(localtime)[4] + 1;} }; } Carp::croak $@ if $@; goto &$method; } } ###################################################################### +################## package main; use strict; use warnings; say MyDate->day; say MyDate->month;
String found where operator expected at 879141.pl line 6, near "say "G +oing into AUTOLOAD"" (Do you need to predeclare say?) String found where operator expected at 879141.pl line 9, near "Carp:: +croak "Invalid method: $method."" (Do you need to predeclare Carp::croak?) syntax error at 879141.pl line 6, near "say "Going into AUTOLOAD"" syntax error at 879141.pl line 9, near "Carp::croak "Invalid method: $ +method."" BEGIN not safe after errors--compilation aborted at 879141.pl line 26.
The first error tells me that say needs to be imported, the second that Carp isn't loaded. If nothing was printed for you, then that package didn't have warnings enabled.
2: Your code looked like this: if (foo) { eval { sub bar {} } }. You were defining a sub inside the AUTOLOAD sub, and then executing that code by name, instead of assigning to another variable a coderef, like $code = sub { ... }; and then later calling it or using goto on it.
3: Notice that the "was called" statement is executed unconditionally, after it's determined that DESTROY wasn't called.
Actually I get the impression that AUTOLOAD isn't even being called.It looks like you're trying to juggle the differences between the code I provided and the code your provided, and then trying to map them to the actual code you're running. This isn't working.
In reply to Re^3: say statements within 'sub AUTOLOAD'
by Anonymous Monk
in thread say statements within 'sub AUTOLOAD'
by ianyappy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |