thor,
You can look at subclassing IO::File, but that is going to end up giving you a different OO interface. The other option is to tie the filehandle. I was disappointed that Tie::FileHandle::Base didn't do more in the way of giving you default methods to inherit. Here is a very rough proof of concept.
package rdw;
use Carp;
sub TIEHANDLE {
my ($class, $file) = @_;
open ( my $file , '<', $file ) or croak "Unable to open $file : $!
+";
return bless \$file , $class;
}
sub READLINE {
my $self = shift;
my ($length, $record);
read $$self, $length, 1;
return undef if eof $$self;
read $$self, $record, $length;
return $record;
}
42;
# and a script that uses it
#!/usr/bin/perl
use strict;
use warnings;
use rdw;
tie *fh, 'rdw', 'foo.rdw' or die "Unable to tie : $!";
while ( <fh> ) {
print "$_\n";
}
__END__
# foo.rdw - outputs "a\nab\nabc\nabcd\n" as desired
1a2ab3abc4abcd
See perldoc perltie for more information
|