Solved

How can I break a loop in Perl if using 'use strict'?

Posted on 2008-06-10
6
9,296 Views
Last Modified: 2013-12-25
I've a Perl script working perfectly and now I must add:

use strict;

at the beginning of the script in order to run it correctly in a new server.

I get an error:

Bareword "break" not allowed while "strict subs" in use at form_all_perl.pl line 137

I'm using the 'break' statement to exit a loop and it was working fine until I added the 'use strict'.

What does that error means? How can I exit a loop when using 'use strict'?
0
Comment
Question by:bebosoft
6 Comments
 
LVL 1

Accepted Solution

by:
easyvbapps earned 200 total points
ID: 21751908
Not quite sure about this since I haven't done perl in a long time, but try
using 'last' instead of break...
0
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 200 total points
ID: 21751912
perldoc -f last
    last LABEL
    last    The "last" command is like the "break" statement in C (as used
            in loops); it immediately exits the loop in question. If the
            LABEL is omitted, the command refers to the innermost enclosing
            loop. The "continue" block, if any, is not executed:

                LINE: while (<STDIN>) {
                    last LINE if /^$/;      # exit when done with header
                    #...
                }

            "last" cannot be used to exit a block which returns a value such
            as "eval {}", "sub {}" or "do {}", and should not be used to
            exit a grep() or map() operation.

            Note that a block by itself is semantically identical to a loop
            that executes once. Thus "last" can be used to effect an early
            exit out of such a block.

            See also "continue" for an illustration of how "last", "next",
            and "redo" work.
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 100 total points
ID: 21757568
Your Perl script obviously isn't working perfectly as 'break' isn't a Perl function (It's used in shell scripts and C).  As already mentioned, 'last' is what you are looking for.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Closing Comment

by:bebosoft
ID: 31465757
I'm curious why the 'break' statements works in Perl scripts if not using the 'use strict' command.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 21766144
Can you please provide an example of a Perl script where break "works"?
0
 
LVL 84

Expert Comment

by:ozo
ID: 21766473
If the script really was working perfectly, then you can use "break" in quotes to mean the same thing that the bareword break means under no strict subs


splain
/usr/bin/splain: Reading from STDIN
Bareword "break" not allowed while "strict subs" in use at form_all_perl.pl line 137
Bareword "break" not allowed while "strict subs" in use at form_all_perl.pl
        line 137 (#1)
    (F) With "strict subs" in use, a bareword is only allowed as a
    subroutine identifier, in curly brackets or to the left of the "=>"
    symbol.  Perhaps you need to predeclare a subroutine?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

914 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

14 Experts available now in Live!

Get 1:1 Help Now