Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: GOTO considered (a necessary) evil?

by Abigail-II (Bishop)
on Jul 15, 2002 at 14:45 UTC ( [id://181797]=note: print w/replies, xml ) Need Help??


in reply to GOTO considered (a necessary) evil?

Well, if I had to remove the goto, I would rewrite it as:
{ eval { tie %session, 'Apache::Session::MySQL', $session, { Handle => HH::DB->db_Main, LockHandle => HH::DB->db_Main } }; if ($@) { if ($@ =~ /^Object does not exist/) { $session = undef; redo; } else { print $cgi->header; die_nice('Database Error', 'Unable to connect at this time +'); } } }
But a redo (just like next and last is just a glorified goto. People who just balk at any sight of a goto have at best heard about Dijkstra's article Go To Considered Harmful (which wasn't titles bij Dijkstra, but by Hoare), but never read the paper, or not understood it. Dijkstra just warns that goto can easily lead to unstructured programs, he doesn't say it's evil all the time.

Knuth has also written about this subject, in his Structured Programming with the Go To statement. His view is less far away from Dijkstra as that the titles suggest.

Abigail

Replies are listed 'Best First'.
Re: Re: GOTO considered (a necessary) evil?
by hossman (Prior) on Jul 16, 2002 at 01:28 UTC
    But a redo (just like next and last is just a glorified goto.

    I consider this statement blatently false and missleading.

    • goto FOO causes is designed to cause execution to jump to the lable FOO anywhere in your script (provided no initialization would be neccessary to get there)
    • redo FOO will only work if the is designed to cause execution to jump to label FOO if it exists at some surrounding loop block. (allthough it will work w/warning if the label applies to some dynamicly surrounding loop)

    This typo generates an error...

    perl -le 'BAR: print 1; BAZ: for (2..3) {print; redo BAR; }'

    This typo causes an infinite loop...

    perl -le 'BAR: print 1; BAZ: for (2..3) {print; goto BAR; }'

    (Updated to be less pedantic... the point is, goto is for arbitrary jumping, redo is for controlled jumping to the begining of a loop. Could you live w/o redo if you had goto? yes. Does that mean you should just use goto and not bother with redo? no.)

      But a redo (just like next and last is just a glorified goto.
      I consider this statement blatently false and missleading.
      • redo FOO will only work if the label FOO exists at some surrounding loop block

      I consider this statement to be inaccurate and not quite pendantic enough for flaming someone! Observe:

      sub bar { no warnings 'exiting'; redo FOO; } FOO: { print "Hey!"; bar(); }
      The redo worked and the label did not exist in a surrounding loop block.
      This typo generates an error...
      perl -le 'BAR: print 1; BAZ: for (2..3) {print; redo BAR; }'
      This typo causes an infinite loop...
      perl -le 'BAR: print 1; BAZ: for (2..3) {print; goto BAR; }'
      That's a pretty weak example, because if you omit the typos, both
      perl -le 'BAR: print 1; BAZ: for (2..3) {print; redo BAZ; }'
      and
      perl -le 'BAR: print 1; BAZ: for (2..3) {print; goto BAZ; }'
      behave identically - looping infinitely.

      Abigail

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-23 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found