Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Perl Best Practices - Loop Labels

by haj (Vicar)
on Apr 16, 2020 at 22:24 UTC ( [id://11115658]=note: print w/replies, xml ) Need Help??


in reply to Perl Best Practices - Loop Labels

Short version: I'm strictly following PBP on this one, and I agree with your current usage.

I'm following my own shortcut rule to this: Always use labels after next, redo, and last.. That includes: Use labels even if the loop isn't nested. Maybe it will become nested when some other guy adds some feature years later, and for that case the label adds robustness. When refactoring complex loops it helps to define the scope: Extracting an inner loop into a subroutine needs extra care if the inner loop contains a last OUTER;.

It has already been noted by others that good names for labels also make good documentation - next FILE; or last TRY; tell pretty well what the line is trying to achieve, and I don't need to scroll even if that line happens to be at the top of my screen.

I don't see these loop controls related to goto, but rather to two other mechanisms of execution control: return and die. A return is implicitly labeled with the surrounding sub (I know, special cases exist where it isn't), and die is followed by a description why you bail out. With a label, loop control keywords achieve the same level of self-explanation. Loop control, returning from a subroutine and exceptions are part of every modern programming language, and they have in common that they go strictly "upward" in the call stack or loop hierarchy. A goto LABEL; or even, horrors, goto EXPRESSION is indeed scary, not only for maintenance of the code, but also for those who write compilers and interpreters.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11115658]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-19 03:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found