On 30.09.2013 05:30, Linda Walsh via RT wrote: > On Sun Sep 29 18:06:51 2013, aristotle wrote: >> * Linda Walsh via RT <perlbug-followup@perl.org> [2013-09-30 02:20]: >>> if I have: > >>> sub reader() { >>> my save=$_"; >>> open(my $fh, "<myfile") or die "open:$!"; >>> while (<$fh>) { >>> ... >>> } >>> $_=$save; >>> } >> >> Yes if you have that, you change the `my $save = $_` to `local $_` >> and you drop the last line. > > So when I split the routine in two with a goto &end, how will the $_ > being transported to the end of the routine? It won't, so don't do that. Why would you even consider doing that to a subroutine, chopping it in half and stitching the parts together with goto? Also, if you do split up a subroutine, why would you pass information in global variables (such as $_) instead of using function parameters? Besides, other (local) variables won't magically teleport over either. This is a bizarre argument. > It won't because local's are not popped when the call stack pops, they > are popped on branches as well. > > That's why my first approach was something perhaps more general about > having local vars survive over a goto, as it isn't at all intuitive that > a lateral branch would really be a context pop that one would expect to > come when one returns. "One would expect"? Speak for yourself. The point of goto &foo is to run in constant space, i.e. to reuse the current stack frame. This is only possible if the current contents of the frame are cleared off to make room for the new sub. If you want local variables to survive, you don't want a tail-optimized call; you want a normal function call. -- Lukas Mai <plokinom@gmail.com>Thread Previous | Thread Next