in reply to Re^3: how to goto &sysread ?
in thread how to goto &sysread ?
Simply put, you do not "goto" a function or a subroutine. You "call" the function or subroutine, which means: save my current place in the code and go do something and when that is done resume my code after this "call". If you "goto" or "jump" directly into some function's or subroutine's code, it will cause a crash because the preamble of "where to go back to when I am finished" was not done.
I'm not sure, but it could be that you don't know what goto(&func); does. goto(&func); is basically return &func;, except it tears down the current stack frame first.
>perl -MCarp=confess -e"sub h { confess; } sub g { h(); } sub f { g(); + } f();" at -e line 1 main::h() called at -e line 1 main::g() called at -e line 1 main::f() called at -e line 1 >perl -MCarp=confess -e"sub h { confess; } sub g { goto &h; } sub f { +g(); } f();" at -e line 1 main::h() called at -e line 1 main::f() called at -e line 1
It's perfectly safe.
IIRC, goto(&func); is slower than return &func;, but it can save memory in deeply recursive functions. The main purpose is too fool Carp.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: how to goto &sysread ?
by Marshall (Canon) on Jul 25, 2011 at 06:39 UTC | |
|
Re^5: how to goto &sysread ?
by Tanktalus (Canon) on Jul 26, 2011 at 14:06 UTC |