LanX has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: goto &method and skipping call frame
by jcb (Parson) on Mar 05, 2021 at 04:02 UTC | |
by LanX (Saint) on Mar 05, 2021 at 19:43 UTC | |
Re: goto &method and skipping call frame
by shmem (Chancellor) on Mar 04, 2021 at 22:15 UTC | |
by LanX (Saint) on Mar 04, 2021 at 22:20 UTC | |
by shmem (Chancellor) on Mar 05, 2021 at 11:57 UTC | |
by LanX (Saint) on Mar 05, 2021 at 19:08 UTC | |
by shmem (Chancellor) on Mar 04, 2021 at 22:23 UTC | |
Re: goto &method and skipping call frame (SSCCE)
by LanX (Saint) on Mar 05, 2021 at 14:53 UTC |