Subject: ANN: Backwards.pm - read files backwards by lines
From: Uri Guttman <[email-masked]>
Date: 4 Feb 1999 14:09:31 GMT
Newsgroups: comp.lang.perl.announce,comp.lang.perl.modules

there has been a cry and hue for a long time for this and after a
suitable gestation period (including lots of laziness), i have finally
produced a Backwards.pm module.

it has both an object and a tied handle interface.

it is very fast. it typically is over 2 times faster than tom c's double
loop algorithm which builds an index of the lines and then seeks to each
one (that is without compile time, with compile time it depends on the
length of the file). the recipes in the cookbook are weak as they read
the file completely into memory. this module reads blocks of text from
the end of the file and returns lines from the block.

the beta version is at:

http://www.sysarch.com/perl/Backwards.pm

i am looking for testers to drive it hard before i post it to cpan. i
just applied for my cpan id so this will be my first official
submission.

I think it should go under the File:: hierarchy as
File::Backwards.pm. it doesn't use any of the IO:: modules, just Symbol,
Fcntl, Carp and integer.

BTW this code is published under my beer runtime license. every time it
is run, you have to drink a beer.

enjoy,

uri

here is the pod:

NAME
    Backwards.pm -- Read a file backwards by lines.

SYNOPSIS
            use Backwards ;

            # Object interface

            $bw = Backwards->new( 'log_file' ) ;

            while( $log_line = $bw->readline ) {
                    print $log_line ;
            }

            # Tied Handle Interface

            tie *BW, 'log_file' ;

            while( <BW> ) {
                    print ;
            }

DESCRIPTION
    This module reads a file backwards line by line. It is simple to
    use, memory efficient and fast. It supports both an object and a
    tied handle interface.

    It is intended for processing log and other similar text files
    which typically have new entries appended. It uses newline as
    the separator and not $/ since it is only meant to be used for
    text.

    It works by reading large (8kb) blocks of text from the end of
    the file, splits them on newlines and stores the other lines
    until the buffer runs out. Then it seeks to the previous block
    and splits it. When it reaches the beginning of the file, it
    stops reading more blocks. All boundary conditions are handled
    correctly. If there is a trailing partial line (no newline) it
    will be the first line returned. Lines larger than the read
    buffer size are ok.

  Object Interface

    There are only 2 methods in Backwards' object interface, new and
    readline.

  new

    New takes just a filename for an argument and it either returns
    the object on a successful open on that file or undef.

  readline

    Readline takes no arguments and it returns the previous line in
    the file or undef when there are no more lines in the file.

  Tied Handle Interface

    The only tied handle calls supported are TIEHANDLE and READLINE
    and they are typeglobbed to new and readline respectively. All
    other tied handle operations will generate an unknown method
    error. Do not seek, write or do any other operation other than
    <> on the handle.

-- 
Uri Guttman  -----------------  SYStems ARCHitecture and Software Engineering
Perl Hacker for Hire  ----------------------  Perl, Internet, UNIX Consulting
[email-masked]  ------------------------------------  http://www.sysarch.com
The Best Search Engine on the Net -------------  http://www.northernlight.com