Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^4: Number of times I've used goto in Perl (early return)

by tye (Sage)
on Mar 26, 2015 at 01:32 UTC ( [id://1121353]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Number of times I've used goto in Perl (sub, return++)
in thread Number of times I've used goto in Perl

Yeah, I saw several things in that node. There was the topic of this thread (goto), the intro paragraph that said that this can conflict with 2 other things, and a prominent code comment suggesting that a label could be inserted if goto were used. And none of that seems to have anything to do (as far as I could figure out) with the alternating-&&= part of the code that you commented on. I agree that the &&= part of the code is interesting. I also fail to see where a goto would help any of the code shown.

So I was only addressing the topic that matched the thread, the opening paragraph, and the prominent comment in the code.

Perhaps it was actually desirable for the &&= short-circuiting to apply to all of the code so the better alternative was a lot of "goto failed if ! ...;" lines? That's my best guess, anyway.

For such a case, the better approach I would do would be to change:

sub do_lots_and_log { ... goto failed if ! method1(...); ... log("yay!"); return; failed: log("boo!"); }

to:

sub do_lots_and_log { if( do_lots(...) ) { log("yay!"); } else { log("boo!"); } } sub do_lots { ... method1(...) or return 0; ... return 1; }

Sometimes "throw exception" is an even better solution than "early return", of course.

- tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-28 23:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found