[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Workaround for Missing tail



Howdy!

  A couple of days ago, someone posted a request for help in porting a Unix
perl script to Mac that used the Unix tail program.

  In case this is still of interest, I am appending an announcement from
comp.lang.perl.announce for "Backwards.pm", a perl module by Uri Guttman
that looks like it might help in working around this.  When I asked Uri for
permission to repost, he sagely made the following warning:

>remember that Backwards.pm is not a tail. tail keeps the lines
>in the forward order. my module returns them in reverse order. to make a
>tail you would have to store them in an array with unshift.

-David-

----===== The announcement =====-----
From: Uri Guttman <uri@sysarch.com>
Newsgroups: comp.lang.perl.announce,comp.lang.perl.modules
Subject: ANN: Backwards.pm - read files backwards by lines

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
uri@sysarch.com  ------------------------------------  http://www.sysarch.com
The Best Search Engine on the Net -------------  http://www.northernlight.com
-----===== End Announcement =====-----

David Steffen, Ph.D.
President, Biomedical Computing, Inc. <http://www.biomedcomp.com/>
Phone: (713) 610-9770 FAX: (713) 610-9769 E-mail: steffen@biomedcomp.com



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch