Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-06-10
6
Medium Priority
?
9,559 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 800 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 800 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 400 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
Technology Partners: 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!

 

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

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!

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

618 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