Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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

by tye (Sage)
on Mar 26, 2015 at 01:32 UTC ( #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!"); }


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?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2019-10-21 08:21 GMT
Find Nodes?
    Voting Booth?