Simple label question

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.
LVL 6
sstoukAsked:
Who is Participating?
 
maneshrConnect With a Mentor Commented:
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

#!/usr/local/bin/perl

print "Before jump\n";

goto JUMP;

print "after jump\n";

JUMP: print "it jumped!\n";
0
 
maneshrCommented:
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) {
    statement;
    statement;
    statement;
    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.
0
 
sstoukAuthor Commented:
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";
                        JUMP1_LABEL;
        };

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

JUMP1_LABEL : print 'It Jumped!';


This code doesn't jump to the label.

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
sstoukAuthor Commented:
# =======================
# Simple example
# =======================

print 'Before jump';

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.
0
 
sstoukAuthor Commented:
I think it recognizes the label "JUMP" as the function which doesn't exists.

Do I have to declare the label before using it?
0
 
sstoukAuthor Commented:
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!
0
 
maneshrCommented:
most welcome!! :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.