Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^4: Perl Best Practices - Loop Labels

by jcb (Parson)
on Apr 20, 2020 at 03:34 UTC ( [id://11115817]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Perl Best Practices - Loop Labels
in thread Perl Best Practices - Loop Labels

Yes, and that was the pattern I used before finding the "goto out" pattern in the Linux kernel sources.

For simple cases, there is little difference, but even with two allocations, a problem begins to appear: compare the indentation depth between the two solutions and consider what happens when they are extended to include a third or fourth allocation. The "goto out" pattern also takes up far less vertical space for error handling in the main code path, like the common open ... or die ... idiom in Perl.

When the error path is also branchless, simply releasing some subset of allocated objects and returning NULL, the "goto out" pattern has the advantage of cleanly grouping the error path into one block (with many entry points) after the main code path, allowing both to be examined separately.

I should probably include a reminder here that this pattern is useful in C, but Perl has better ways of handling error exits because the runtime manages memory, unlike in C. I am unsure if I have ever used goto LABEL in Perl.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-19 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found