On 9/4/07, sivasakthi <[EMAIL PROTECTED]> wrote:
snip
>  Thanks for your suggestions.. but i need to read a file from backwards...
> First time reading from backwards & set the last line position to one
> variable..next time reading backwards until reach that variable position...
> is it possible to achieve that..????
snip

Yes, just save the value of tell before reading anything (this will be
the end of the file) and then stop reading when you reach that
position in the file.  I have included a sample script that does that
very thing.  It saves the position in a growing data section at the
end (to give it something to read on the next run).  I am having a
hard time coming up with a reason why you would want to read a file
that grows in size backwards; if you don't mind my asking, why do you
need to do such an odd thing?

#!/usr/bin/perl

use strict;
use warnings;

use File::ReadBackwards;

tie *FH, 'File::ReadBackwards', $0
        or die "cannot open $0:$!";

my $newend = tell FH;
my $end = <FH>;
print $end;
$end = 0 if $end eq "__DATA__\n";

while (<FH>) {
        my $pos = tell FH;
        print;
        last if $pos == $end;
}

close FH;

open my $fh, ">>", $0
        or die "cannot open $0:$!";

print $fh "read file\nstop next time at\n$newend\n";

__DATA__

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to