Simple label question

Posted on 2000-04-10
Last Modified: 2010-03-05
I need to jump from within the middle of the statement to another part of the program.
Please remind me how to use a label to jump to another place.
Thank You.
Question by:sstouk
  • 4
  • 3
LVL 16

Expert Comment

ID: 2700833
Following is an extract from the PERL book "Learning PERL"
Labeled Blocks

What if you want to jump out of the block that contains the innermost block, or to put it another way, exit from two nested
blocks at once? In C, you'd resort to that much maligned goto to get you out. No such kludge is required in Perl; you can use
last, next, and redo on any enclosing block by giving the block a name with a label.

A label is yet another type of name from yet another namespace following the same rules as scalars, arrays, hashes, and
subroutines. As we'll see, however, a label doesn't have a special prefix punctuation character (like $ for scalars, & for
subroutines, and so on), so a label named print conflicts with the reserved word print and would not be allowed. For this
reason, you should choose labels that consist entirely of uppercase letters and digits, which will never be chosen for a reserved
word in the future. Besides, using all uppercase stands out better within the text of a mostly lowercase program.

Once you've chosen your label, place it immediately in front of the statement containing the block followed by a colon, like this:

SOMELABEL: while (condition) {
    if (nuthercondition) {
        last SOMELABEL;

We added SOMELABEL as a parameter to last. This tells Perl to exit the block named SOMELABEL, rather than just the
innermost block. In this case, we don't have anything but the innermost block. But suppose we had nested loops:

OUTER: for ($i = 1; $i <= 10; $i++) {
    INNER: for ($j = 1; $j <= 10; $j++) {
        if ($i * $j == 63) {
            print "$i times $j is 63!\n";
            last OUTER;
        if ($j >= $i) {
            next OUTER;

This set of statements tries all successive values of two small numbers multiplied together until it finds a pair whose product is
63 (7 and 9). Once the pair is found, there's no point in testing other numbers, so the first if statement exits both for loops
using last with a label. The second if ensures that the bigger of the two numbers will always be the first one by skipping to
the next iteration of the outer loop as soon as the condition would no longer hold. This means that the numbers will be tested
with ($i, $j) being (1,1), (2,1), (2,2), (3,1), (3,2), (3,3), (4,1), and so on.

Even if the innermost block is labeled, the last, next, and redo statements without the optional parameter (the label) still
operate with respect to that innermost block. Also, you can't use labels to jump into a block - just out of a block. The last,
next, or redo has to be within the block.

Author Comment

ID: 2700866
I don't know why but it doesn't jump to the label.
if (CHECK_EMAIL($USERMAIL)) # sub 19
                        if ($PASSWORD eq $PASSCHECK)

                        print "all is correct";

# ==============
# Some other code
# ==============

JUMP1_LABEL : print 'It Jumped!';

This code doesn't jump to the label.


Author Comment

ID: 2700880
# =======================
# Simple example
# =======================

print 'Before jump';


print 'after jump';

JUMP: print 'it jumped!';

# =======================
# end of "Simple example";
# =======================

The above code just exits and does print only 'Before Jump';
and nothing else.
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

LVL 16

Accepted Solution

maneshr earned 20 total points
ID: 2700898
try this...

basically i have modified your simple example.
what this does is prints Before jump and then goes to the JUMP label and prints it jumped and exits out of the program as there is nothing more to do after that


print "Before jump\n";

goto JUMP;

print "after jump\n";

JUMP: print "it jumped!\n";

Author Comment

ID: 2700901
I think it recognizes the label "JUMP" as the function which doesn't exists.

Do I have to declare the label before using it?

Author Comment

ID: 2700910
Thank You very much.

You havn't only modified it, but You have added the "goto" statement.
This is what it needed and I completely forgot about it.

Thanks alot!
LVL 16

Expert Comment

ID: 2700932
most welcome!! :-)

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now