On 14/04/2011 19:36, Shlomi Fish wrote:
> 
> I should note that in C "continue", "break", and a pre-mature "return" may
> also be considered as pseudo-gotos.

Indeed. Funtionally, Perl has many keywords that transfer control to a
different place in the code (next, break, continue, return, if, elsif,
redo, last, exit, die come to mind) and so are equivalent to goto. But
they have the difference that they 'go to' an implicit place, and are
used as terms to describe a process. goto, however, has no place in
structured code, as there is no indication of its purpose apart from a
(hopefully) well-chosen label.

High-level programming languages are meant to allow us to abstract our
solutions from the machine's execution of those solutions. We write in
terms of abstract concepts, and should be largely oblivious that
underneath the veneer the computer has a program counter and is
executing instructions one by one. All of the keywords that result in a
transfer of control to a different place are carefully chosen so that
they blend as seamlessly as possible into a narrative describing an
alogorithm: when we write 'if ($condition)' we should not be seeing
something that tests a value and transfers control to a different place
in the code depending on the result of that test, we should be seeing
sequence of operations that are performed - or not - depending on the
current conditions.

Writing 'goto' suddenly makes the program 'self-aware' so that it is
suddenly part of the data it is processing. You may argue that
subroutine calls do the same thing but, written well, they fall into the
same scheme as keywords whereby the contents of the subroutine and where
its code is placed can be ignored once it is written and debugged.
Object-oriented programming is very strong on this encapsulation, but I
believe it is and should be applied to procedural programming too.

I think the most common use of gotos is in abandoning a sequence of
steps, any of which may fail and make the rest of the sequence pointless
or impossible. If the language doesn't support a try/catch mechanism
(eval/die for older Perl) or it isn't practicable within the structure
of the program, there is always the possibility of combining the steps
into a subroutine and using 'return' to perform an implicit goto to the
end of the sequence.

A final note, perldoc perlsyn says

  A loop's LABEL is not actually a valid target for a goto; it's just 
  the name of the loop.

and I am left wondering what this means, as I have had no problem
writing a 'goto' targetting such labels. I wonder if this is a mistake
in the documentation, or it is simply saying that such a goto is
deprecated?

Cheers all,

Rob

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to