package IO::Remembers;
sub new {
my ($pack, $filename) = @_;
my $fh = \do { local *FH }; # Incantation
open $fh, $filename or return;
bless $fh => $pack;
}
People can use this object just like a filehandle:
my $fh = new IO::Remembers ('input');
my $line = <$fh>;
read $fh, $bytes, 1024;
print $fh "Actually this filehandle was opened for reading only...\n";
close $fh;
It closes automatically when it is destroyed.