tobold.org

correct • elegant • free

△ comp.lang.perl △

◅ Quotes, ", ' and more ' " 's

tricky i/o with files ▻

HELP:

In article <3r28a8$kpo@tron.ppp.de>, Gerrit Griebel <gg@tron.ppp.de> wrote:
>leaks memory:          print <INPUT> while not eof(INPUT);
>
>I need the second version for simulating a 'tail'-like behavior.

[ Others have mentioned that it isn't strictly leaking memory. ]

You'll need something a little trickier than this if you want an
effect similar to `tail -f'.  In particular, you need to clear the eof
condition on the filehandle, and you probably want to throw a sleep in
there, too.

Here's a simple `tail -f' in Perl.

    #! /usr/local/bin/perl5 -w

    $0 =~ s!.*/!!;
    die "Usage: $0 file\n" unless @ARGV == 1;
    die "$0: can't open $ARGV[0] for input: $!\n" unless open(IN, $ARGV[0]);

    seek(IN, 0, 2); # Seek to end of file.

    for (;;) {
            print <IN>;
            seek(IN, 0, 1); # Clear eof condition.
            sleep 1;
    }
--
Tim Goodwin        | "If you're used to paint-by-number, a blank canvas
Public IP Exchange | can be unsettling at first." -- Larry Wall

Original headers:

From: tim@pipex.net (Tim Goodwin)
Newsgroups: comp.lang.perl
Subject: Re: HELP: (please help, wizards) Memory problems
Date: 13 Jun 1995 16:41:04 +0100
Organization: PIPEX, 216 Science Park, Cambridge, England
Message-ID: <3rkbig$co3@pipe.pipex.net>
References: <3r28a8$kpo@tron.ppp.de>

△ comp.lang.perl △

◅ Quotes, ", ' and more ' " 's

tricky i/o with files ▻