goto &NAME is a way to call a sub NAME() while autoloading and transparently skipping the actual frame for sub AUTOLOAD() on the call stack.
Hence caller wouldn't show AUTOLOAD but the line which triggered it.
That's also of practical importance when using carp() et al from Carp , because you don't wanna get the error shown inside the AUTOLOAD's code.
But what if I want to delegate to a method from an autoloaded sub?
My best guess is to call it as a normal sub and to prepend the object to @_
sub AUTOLOAD { #warn pp "$AUTOLOAD $OBJ=>",\@_; (my $meth = $AUTOLOAD) =~ s/^.*://; if (my $c_ref = $OBJ->can($meth)) { unshift @_,$OBJ; goto &$c_ref; } }
And - to my big surprise - this is even respecting inheritance, i.e. if the method belongs to a super-class of OBJ, it's properly delegated.
Questions:
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
PS: Hey Mike can't wait to hear about your huge "expertise" from JAVA, bread-trucks and millions of lines of PHP ...
2 monks msged me Where does $OBJ come from?
In this simplified example you can consider it a global, like
our $OBJ = SomeClass->new();
The real use case is a more complicated implementation of a with construct (like in with), where $OBJ is a closure var and AUTOLOAD is localized to a blocks context.
In reply to goto &method and skipping call frame by LanX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |