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.


In reply to Re^2: GOTO or not GOTO by JavaFan
in thread GOTO or not GOTO by jflevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.