Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Simple label question

Posted on 2000-04-10
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 16

Accepted Solution

maneshr earned 80 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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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 …
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…
Six Sigma Control Plans

719 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