Solved

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

Posted on 2008-06-10
6
9,255 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
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

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

746 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

11 Experts available now in Live!

Get 1:1 Help Now