in reply to Re: ReadBackwards usage
in thread Readbackwards usage

Thanks much for the explanation Athanasius. This clears the doubt. Any suggestion on how I can use the file handle with ReadBackwards?

The reason is that I would have to work with a temporary file and I would not know the name of the file that is created for giving the actual string.

Regards,

Jay

Replies are listed 'Best First'.
Re^3: ReadBackwards usage
by Athanasius (Archbishop) on Mar 16, 2015 at 07:48 UTC

    Hello jayu_rao,

    Here are two possible solutions:

    (1) Create the temporary file using the core module File::Temp and get the filename together with the handle:

    use File::Temp 'tempfile'; ... my ($fh, $filename) = tempfile(); my $bw = File::ReadBackwards->new($filename) or die "Cannot read '$filename' backwards"; while (my $log_line = $bw->readline) { ... }

    (2) Use the File::ReadBackwards TIED HANDLE INTERFACE, which accepts a filehandle:

    my $filename = 'data.txt'; open(my $bw, '<', $filename) or die "Cannot open file '$filename' for reading"; tie *$bw, 'File::ReadBackwards', $filename or die "Cannot read '$filename' backwards"; while (my $log_line = <$bw>) { ... }

    (Note the asterisk in tie *$bw — this is required. Also note the two different ways of reading the next line from the file: readline vs. <$bw>. N.B.: they are not interchangeable!)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,