[Novalug] perl exiting loop question

230therapy 230therapy@gmail.com
Sat Aug 6 22:21:39 EDT 2011

Your code needs to handle the end of file condition.  Use the angle operator to read the file.  It is guaranteed to be able to handle the end of file condition.

See also "open".

use strict;
my $file = "/path/to/file";

open $FH, "< $file" 
	or die "cannot open $file for reading:  $!\n";

while (<$FH>) {
  # do something

} # while


There are multiple ways to open a file.  Programming Perl goes over them in detail.

See the online Perl Cookbook and Programming Perl:


On Aug 6, 2011, at 12:00 PM, novalug-request@calypso.tux.org wrote:

Send Novalug mailing list submissions to

To subscribe or unsubscribe via the World Wide Web, visit
or, via email, send a message with subject or body 'help' to

You can reach the person managing the list at

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Novalug digest..."

Today's Topics:

  1. perl exiting loop question (Bonnie Dalzell)


Message: 1
Date: Sat, 6 Aug 2011 00:47:16 -0400 (EDT)
From: Bonnie Dalzell <bdalzell@qis.net>
Subject: [Novalug] perl exiting loop question
To: NOVALUG <novalug@calypso.tux.org>
Content-Type: TEXT/PLAIN; format=flowed; charset=US-ASCII

i have a loop in a program that, using a while loop,

reads data from a large file of unknown length in 256k bytes chunks and 
then within the loop does stuff to the data and saves it out to another 
open out file.

this works until I run out of input. then the program exits with an 

"substr outside of string at libs/get_record.lib line 160"

when it exits the file the data is being written into is properly 
closed by the program exiting.

however what I would like to have the program do is to exit the while loop 
when the error appears without the program closing down.

i have tried stuff like this:
   while ($@ =~m/outside of string/){

but this exits the loop immediately

enlightenment and suggestions sought.

                       Bonnie Dalzell, MA
mail:5100 Hydes Rd PO Box 60, Hydes,MD,USA 21082-0060|EMAIL:bdalzell@qis.net
Freelance anatomist, vertebrate paleontologist, writer, illustrator, dog
breeder, computer nerd & iconoclast... Borzoi info at www.borzois.com.
HOME www.batw.net    ART bdalzellart.batw.net  BUSINESS www.boardingatwedge.com


Novalug mailing list

End of Novalug Digest, Vol 58, Issue 5

More information about the Novalug mailing list