in reply to Entering and exiting subs in style

In your first scenario, it's okay to call exit (that's what it's for!), as long as you document that the subroutine could exit under specific circumstances.

In the second scenario, the goto actually replaces the current entry on the call stack. If you don't know what this means or why you'd want to do it, you probably don't need to use this form.

As a side note, I much prefer calling bar() instead of &bar. Again, if you don't know the difference or why you'd want to do it, you're probably much better off with the parenthesis.

Replies are listed 'Best First'.
Re: Re: Entering and exiting subs in style
by orderthruchaos (Scribe) on May 26, 2004 at 14:18 UTC
    As stated above, goto replaces the current call on the stack... In your code, I think it might be useful:

    use strict; sub foo { goto &bar if ( $_[0] < 0 ); return "$_[0] from foo\n"; } sub bar { return "$_[0] from bar\n"; } print foo(-1);

    If you run this code, you'll get -1 from bar\n as your output. Put simply, goto will replace the current call to &foo with a call to &bar. There are several limitations to this method, though... You can't pass &bar any parameters; &bar will be passed the arguments which &foo was called with. That being said, the second limitation is that you don't mess around with @_ in the calling subroutine... you'll surely regret it later. For further information, see pages 127 and 297 of The Bible.