Re: quicky perl question


Subject: Re: quicky perl question
From: Michael Fowler (michael@shoebox.net)
Date: Mon Nov 12 2001 - 15:41:02 AKST


On Mon, Nov 12, 2001 at 03:18:23PM -0900, Matthew Schumacher wrote:
> Perhaps you can suggest a better way to do what I am going. I want to
> open a log file and look for something in it then close it. The log
> file is 20MB in size. Is there anyway to do this without the perl
> program eating up 20+ MB of memory.
>
> Here is the syntax I am currently using:
>
> foreach $line (reverse <LOGFILE>){
> }

The module File::ReadBackwards allows you to read a file backwards, as
that's what, it appears, you're trying to do. The POE (Perl Object
Environment, a misnomer for what amounts to an event loop) system also has a
component for reading files backwards.

If all you're doing is looking for something in the file, and it doesn't
matter what direction you go in, then you should use the typical idiom:

    while (<LOGFILE>) {
        # do something with $_
    }

Which reads the file chunk by chunk, where a 'chunk' is delimited by $/. $/
is normally set to a newline, so a chunk is a line in that case. I mention
this because I'm not sure what you're searching for, or how you plan to get
it, so it may be benificial to change the chunk delimiter if you have odd
requirements.

Michael

--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--



This archive was generated by hypermail 2a23 : Mon Nov 12 2001 - 15:42:11 AKST