Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: GOTO or not GOTO

by monarch (Priest)
on Jan 27, 2009 at 12:24 UTC ( #739168=note: print w/ replies, xml ) Need Help??


in reply to Re^2: GOTO or not GOTO
in thread GOTO or not GOTO

The big difference between last and next is that those functions give deterministic answers to the compiler as to code flow. On the other hand goto may not offer as clear an indication of the target; although given a label the compiler may be able to work out whether the destination is outside the given closure anyway.

So here's the next example, what does the following do:

for ( my $i = 0; $i < 10; $i++ ) { my $j = $i; if ( $i == 5 ) { goto nextpoint; } } for ( my $k = 0; $k < 10; $k++ ) { my $m = $k; nextpoint: print( "$m\n" ); }

Well I tried it, but the point is I didn't know what it would do before hand. If that is "bragging one's ignorance" then so be it. Rightly or wrongly I came from a C background where if you tried risky behaviour like this you got into real trouble. Perl is far more forgiving.

I do prefer to program in a manner that has a degree of portability between languages; as I said before, goto is risky: if you consider yourself an expert in the language at the time and the compiler behaviour and implications (if any) to the stack then by all means go ahead. I'll stick to what I know is safe.


Comment on Re^3: GOTO or not GOTO
Download Code
Re^4: GOTO or not GOTO
by JavaFan (Canon) on Jan 27, 2009 at 12:37 UTC
    So here's the next example, what does the following do:
    Well, that's answered by the same second line of the goto documentation: It may not be used to go into any construct that requires initialization, such as a subroutine or a foreach loop.

    For the third example, will to be trying to goto in a while block?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2014-12-22 23:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (133 votes), past polls