20
loading...
This website collects cookies to deliver better user experience
*new_method = \&old_method;
\
character) to the old method. Methods are just subroutines in Perl, and although you don’t need the &
character when calling one, you do need it if you’re passing a subroutine as an argument or creating a reference, as we’re doing above.caller
or Carp.sub old_method {
warn 'old_method is deprecated';
no warnings 'redefine';
*old_method = \&new_method;
goto &new_method;
}
sub new_method {
# code from old_method goes here
}
The goto &NAME
form is quite different from the other forms of goto
. In fact, it isn’t a goto in the normal sense at all, and doesn’t have the stigma associated with other gotos. Instead, it exits the current subroutine (losing any changes set by local
) and immediately calls in its place the named subroutine using the current value of @_
. […] After the goto
, not even caller
will be able to tell that this routine was called first.
perlfunc manual page
old_method
to point to new_method
while we’re still inside old_method
. (Yes, you can do this.) If you’re running under use warnings
(and we are, and you should), you first need to disable that warning. Later calls to old_method
will go straight to new_method
without logging anything.20