Skip Menu |

This queue is for tickets about the File-ReadBackwards CPAN distribution.

Report information
The Basics
Id: 72319
Status: new
Priority: 0/
Queue: File-ReadBackwards

People
Owner: Nobody in particular
Requestors: matt [...] cpanel.net
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: Add the ability for File::ReadBackwards to take in an OpenFilehandle
I have a project I'm working on where I need to read the first line of a file for meta data, then begin reading it backwards. I've already patch File::ReadBackwards for my own purposes, however I believe this functionality may come in handy to someone else in the community (and not having to maintain patches for my own use would be nice). So here's the patch (attached) also available on github: https://github.com/MattDees/File- ReadBackwards/commit/685648c036f45d28c191d0b8f45472a7e8145c06
Subject: File-ReadBackward-1.06_01.patch
diff --git a/ReadBackwards.pm b/ReadBackwards.pm index 7bb8994..7d9ebae 100644 --- a/ReadBackwards.pm +++ b/ReadBackwards.pm @@ -9,11 +9,12 @@ use strict ; use vars qw( $VERSION ) ; -$VERSION = '1.05' ; +$VERSION = '1.06_01' ; use Symbol ; use Fcntl qw( :seek O_RDONLY ) ; use Carp ; +use Scalar::Util 'openhandle'; my $max_read_size = 1 << 13 ; @@ -71,11 +72,18 @@ sub new { $rec_sep ||= $default_rec_sep ; my $is_crlf = $rec_sep eq "\015\012" ; -# get a handle and open the file +# get a handle and open the file or assign $filename to handle if +# it is an already open filehandle - my $handle = gensym ; - sysopen( $handle, $filename, O_RDONLY ) || return ; - binmode $handle ; + my $handle; + if (openhandle( $filename ) ) { + $handle = $filename; + } + else { + $handle = gensym; + sysopen( $handle, $filename, O_RDONLY ) || return; + } + binmode $handle; # seek to the end of the file and get its size diff --git a/t/bw.t b/t/bw.t index 07796bb..c7e81e6 100755 --- a/t/bw.t +++ b/t/bw.t @@ -14,7 +14,7 @@ my $is_crlf = ( $^O =~ /win32/i || $^O =~ /vms/i ) ; print "nl\n" ; my @nl_data = init_data( "\n" ) ; -plan( tests => 10 * @nl_data + 1 ) ; +plan( tests => 20 * @nl_data + 1 ) ; test_read_backwards( \@nl_data ) ; print "crlf\n" ; @@ -71,15 +71,34 @@ sub test_read_backwards { } - test_data( $rec_sep ) ; - - test_tell_handle( $rec_sep ) ; + # Test File::ReadBackwards with a filename + + my $bw = File::ReadBackwards->new( $file, $rec_sep ) or + die "can't open $file: $!" ; + test_data( $bw, $rec_sep ) ; + + # Test File::ReadBackwards with a filehandle + open my $open_fh, '<', $file or + die "can't open $file: $!"; + my $bw_open = File::ReadBackwards->new( $open_fh, $rec_sep ); + test_data( $bw_open, $rec_sep ) ; + + + # open the file backwards again to test tell and get_handle methods + $bw = File::ReadBackwards->new( $file, $rec_sep ) or + die "can't open $file: $!" ; + test_tell_handle( $bw, $rec_sep ) ; + + open $open_fh, '<', $file or + die "can't open $file: $!"; + $bw_open = File::ReadBackwards->new( $open_fh, $rec_sep ); + test_tell_handle( $bw_open, $rec_sep ) ; } } sub test_data { - my( $rec_sep ) = @_ ; + my( $bw, $rec_sep ) = @_ ; # slurp in the file and reverse the list of lines to get golden data @@ -93,9 +112,6 @@ sub test_data { # open the file with backwards and read in the lines - my $bw = File::ReadBackwards->new( $file, $rec_sep ) or - die "can't open $file: $!" ; - my( @bw_file_lines ) ; while ( 1 ) { @@ -141,13 +157,10 @@ sub test_data { sub test_tell_handle { - my( $rec_sep ) = @_ ; + my( $bw, $rec_sep ) = @_ ; # open the file backwards again to test tell and get_handle methods - my $bw = File::ReadBackwards->new( $file, $rec_sep ) or - die "can't open $file: $!" ; - # read the last line in my $bw_line = $bw->readline() ;