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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |