Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

At $dayjob I'm working on a project where the home-grown error reporting module occasionally warns Use of "goto" to jump into a construct is deprecated

So now I'm trying to find out how exactly goto LABEL works, in order to assess whether the whole error reporting module needs to be rewritten. And I found no documentation.

The closest I found was "goto only searches the immediate call stack for labels", but this alone doesn't explain how "jump into a construct" can happen at all -- if it only searched the caller's scope and the caller's caller's scope etc. for the label, it would never find label from inside a construct that's not in dyanmic call chain.

Some curious experiments:

$ perl -wE 'if (0) { label: say 1 }; goto label' Can't find label label at -e line 1. $ perl -wE 'if (1) { label: say 1;}; goto label' Use of "goto" to jump into a construct is deprecated at -e line 1. 1 Use of "goto" to jump into a construct is deprecated at -e line 1. 1 ^C # aborted, because it loops

So apparently, the construct containing the label has to have been executed once.

This works:

#!/usr/bin/perl use strict; use warnings; use 5.014; sub f { my $arg = shift; if ($arg == 0) { label: say 'here'; } elsif ($arg == 1) { # don't execute a branch with 'label:' } else { goto label } } f($_) for 0..2; __END__ here here

Whereas this one dies:

$ perl -wE 'sub g() { label: say "here" }; g; goto label' here Can't find label label at -e line 1.

I have trouble finding a clear mental model that can help me to explain all of this behavior. Who can help?

In reply to How does 'goto LABEL' search for its label? by moritz

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (8)
    As of 2018-03-20 20:32 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (259 votes). Check out past polls.