How might this work?
Andy Armstrong
andy at hexten.net
Mon Feb 26 20:42:36 GMT 2007
(I posted this to P5P but nobody has bitten yet so I'll spam it here
too)
I'd like to implement a module that permits this:
use IO::Mark;
sub examine {
# Get the file handle. Might not be seekable
my $fh = shift;
my $mark = IO::Mark->new($fh);
my $buf;
$mark->read($buf, 1000, 0);
# Do something with buf
# On exit $fh rewinds making the fact that we've read from it
# invisible
}
IO::Mark is a clone of a filehandle that you can read from without
(apparently) consuming input from the underlying handle.
Behind the scenes it would add a dynamically sized buffer to the
filehandle and arrange for the filehandle to reread the contents of the
buffer and then carry on reading new data from the handle.
The idea is to be able to write code that peeks the contents of a non-
seekable handle transparently to the caller. A concrete example would
be doing magic filetype detection on an open handle without consuming
and data from that handle from an external perspective.
I can't immediately see anything on CPAN that does that - although it's
entirely possible that I missed it.
As far as I can see I could do it using PerlIO and pushing a new
buffering layer into the handle.
Anyone got any thoughts?
--
Andy Armstrong, hexten.net
More information about the london.pm
mailing list