Solved

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

Posted on 2008-06-10
6
9,332 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to count occurrences of each item in an array.

803 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