http://www.perlmonks.org?node_id=1121353


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