OK, so no one really likes goto. However, I have come to a situation where goto would probably the easiest solution to a problem I have.
Before, my code looked like this (I know that next already is a form of goto, but I think it's considered "OK" to use it).
LABEL1: foreach my $a (@b) { if (...) { next LABEL1; } do_something(); if (...) { next LABEL1; } do_something_else(); if (...) { next LABEL1; } do_something_again(); }
Now however I want to add a call to a subroutine to the end of the foreach loop. This subroutine (let's call it perform_cleanup) should always be invoked, even in case we're going to the next loop iteration early. So my easiest solution with goto would be to change each "next LABEL1" to "goto LABEL1" and add the following to the end of the foreach loop:
LABEL1: perform_cleanup();
I'm asking this out of curiosity. In this case what would you say is the "cleanest" solution to do this? Do you think using goto in this case is OK or would you rewrite it?
In reply to Avoiding goto by elTriberium
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |