in reply to Re: Perl Best Practices - Loop Labels
in thread Perl Best Practices - Loop Labels
G'day Marshall,
"The normal way (in my opinion) to exit completely from an inner and outer loop in C or Perl is to use a return statement."
Sorry, but that's completely wrong. The return function is for exiting a subroutine, not a loop. The first sentence of the last documentation starts (my emphasis):
"The last command is like the break statement in C ..."
Consider this code which uses last:
$ perl -E 'X(); sub X { for (0..2) { last if $_ > 1; say; } say 42; }' 0 1 42
Now this code, which is identical in all respects, except last has been replaced by return:
$ perl -E 'X(); sub X { for (0..2) { return if $_ > 1; say; } say 42; +}' 0 1
Note how the say 42; is not executed in that second example.
"Yes, there are some folks who advocate for adding a conditional flag like: while(...and !$end_flag){}, where inside the loop the code sets $end_flag=1 to end the loop. The theory behind that is that the code should only one way in and only one way out."
I addressed "structured programming techniques" in my OP.
"In your pseudo code, there appears to be some assumption that going back to the outer loop somehow maintains the inner loop vars."
No, I have made no such assumption.
Although I do have some other issues with what you've written, I'll leave it there for now.
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Perl Best Practices - Loop Labels
by Marshall (Canon) on Apr 18, 2020 at 04:52 UTC |