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

Re^2: GOTO or not GOTO

by JavaFan (Canon)
on Jan 27, 2009 at 08:17 UTC ( #739123=note: print w/replies, xml ) Need Help??

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

for ( my $i = 0; $i < 10; $i++ ) { my $j = $i; if ( $i == 5 ) { goto newlocation; } } newlocation: print( "jumped out" );
Now my question is what happened to $j there? Is the compiler "smart enough" to know that I exited the loop and therefore the memory allocated to $j can now be reclaimed?
Imagine you had written:
for ( my $i = 0; $i < 10; $i++ ) { my $j = $i; if ( $i == 5 ) { last; } } print( "jumped out" );
you have code that does exactly the same. I presume you have the same worries about $j, and will hence decide to never use 'last'?

Or else, could you indicate why you have worries about the first construct, but not the last?

Even worse, what happens when I use goto to jump between subroutines?
Had you actually bothered to read past the first sentence of the document you link to, you would have known the answer. Here's the second sentence of the document: It may not be used to go into any construct that requires initialization, such as a subroutine or a foreach loop.
It is because I don't know the answers to these questions that I do my best to avoid the use of goto
Since I've now answered the questions, are you now going to start using goto?

I have to admit, you weren't arguing against the use of goto in particular. You were arguing against using constructs you don't know the details about. That's a smart thing. The wise thing would be to actually learn about them instead of bragging ones ignorance.

Replies are listed 'Best First'.
Re^3: GOTO or not GOTO
by monarch (Priest) on Jan 27, 2009 at 12:24 UTC
    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.

      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://739123]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2017-11-21 14:47 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (302 votes). Check out past polls.