[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

close($FH);

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

See the online Perl Cookbook and Programming Perl:

http://docstore.mik.ua/orelly/perl/



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

Send Novalug mailing list submissions to
	novalug@calypso.tux.org

To subscribe or unsubscribe via the World Wide Web, visit
	http://calypso.tux.org/mailman/listinfo/novalug
or, via email, send a message with subject or body 'help' to
	novalug-request@calypso.tux.org

You can reach the person managing the list at
	novalug-owner@calypso.tux.org

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>
Message-ID:
	<alpine.DEB.2.00.1108060036290.2763@localhost6.localdomain6>
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 
error:

"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/){
     actions---
   }

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
Novalug@calypso.tux.org
http://calypso.tux.org/mailman/listinfo/novalug


End of Novalug Digest, Vol 58, Issue 5
**************************************




More information about the Novalug mailing list