Solved

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

Posted on 2008-06-10
6
9,383 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
[X]
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
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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

696 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