How might this work?

Dirk Koopman djk at tobit.co.uk
Mon Feb 26 21:24:46 GMT 2007


Andy Armstrong wrote:
> (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.
> 

Isn't this just dup()? The problem you have with that is that on 
non-seekable (or at least non-rewindable) things (like sockets), reading 
anywhere from it will IIRC consume the data.


More information about the london.pm mailing list