[EMAIL PROTECTED] wrote:

> I use the following to tail a growing file and exist on key hit. This code is 
> pretty much straight from the Perl CD Bookshelf.  I'd like to know how I 
> could possibly seek to the last line of the file ~at the time of execution~ 
> so as to not have to print the whole file each time this program is run.  
> This would be useful against very large files of course. Even the current eof 
> line pos - 100 lines at the time of execution would work.
> Thanks for any tips.
> 
> 
> 
> 
> use Term::ReadKey;
> use IO::Handle;
> 
> my $key;
> my $nohupfile="nohup.out";
> 
> open (NOHUPFILE, $nohupfile) or die "can't open $nohupfile: $!";
> while (!$key) {
>        $key=ReadKey(-1);
>        if ($key) { 
>            last;
>        }
>        while (<NOHUPFILE>) { 
>               print
>        }   
>        if (!$key) {
>            sleep 1;
>        } else { 
>            last;
>        }
>        NOHUPFILE->clearerr();           
> }

Read the perlfunc man page :

    seek FILEHANDLE,POSITION,WHENCE
            Sets FILEHANDLE's position, just like the "fseek" call of "stdio".
            FILEHANDLE may be an expression whose value gives the name of the
            filehandle. The values for WHENCE are 0 to set the new position *in
            bytes* to POSITION, 1 to set it to the current position plus
            POSITION, and 2 to set it to EOF plus POSITION (typically negative).
            For WHENCE you may use the constants "SEEK_SET", "SEEK_CUR", and
            "SEEK_END" (start of the file, current position, end of the file)
            from the Fcntl module. Returns 1 upon success, 0 otherwise.

            Note the *in bytes*: even if the filehandle has been set to operate
            on characters (for example by using the ":utf8" open layer), tell()
            will return byte offsets, not character offsets (because
            implementing that would render seek() and tell() rather slow).

            If you want to position file for "sysread" or "syswrite", don't use
            "seek"--buffering makes its effect on the file's system position
            unpredictable and non-portable. Use "sysseek" instead.

            Due to the rules and rigors of ANSI C, on some systems you have to
            do a seek whenever you switch between reading and writing. Amongst
            other things, this may have the effect of calling stdio's
            clearerr(3). A WHENCE of 1 ("SEEK_CUR") is useful for not moving the
            file position:

                seek(TEST,0,1);

            This is also useful for applications emulating "tail -f". Once you
            hit EOF on your read, and then sleep for a while, you might have to
            stick in a seek() to reset things. The "seek" doesn't change the
            current position, but it *does* clear the end-of-file condition on
            the handle, so that the next "<FILE>" makes Perl try again to read
            something. We hope.

            If that doesn't work (some IO implementations are particularly
            cantankerous), then you may need something more like this:

                for (;;) {
                    for ($curpos = tell(FILE); $_ = <FILE>;
                         $curpos = tell(FILE)) {
                        # search for some stuff and put it into files
                    }
                    sleep($for_a_while);
                    seek(FILE, $curpos, 0);
                }

-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Unix-Users mailing list
Perl-Unix-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to