IO::Mark

Read unseekable filehandles non-destructively
Download

IO::Mark Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Publisher Name:
  • Andy Armstrong
  • Publisher web site:
  • http://search.cpan.org/~andya/

IO::Mark Tags


IO::Mark Description

Read unseekable filehandles non-destructively Imagine you've got a function get_image_size. You pass it a filehandle that's open on an image file and it returns the dimensions of the image.Imagine also that you have an open socket on which you are expecting to receive an image. You'd like to know the dimensions of that image and also capture its data.If you pass the socket handle to get_image_size it'll consume some data from that socket - enough to read the image header and work out its dimensions. Unfortunately any data that get_image_size reads is lost; you know the dimensions of the image but you've lost some of its data and you can't rewind the socket to go back to the start of the image; sockets aren't seekable. sub send_image { my $socket = shift; # This works fine... my ($width, $height) = get_image_size( $socket ); # ...but the data we send here will be missing whatever header # bytes get_image_size consumed. send_image_data( $width, $height, $socket ); }You could buffer the entire image in a file, open the file and pass that handle to get_image_size. That works but means that we can't compute the image size until we have the whole image. If instead of an image file we were dealing with streaming audio the input stream might be effectively infinite - which would make caching it in a file inconvenient.We could rewrite get_image_size to cache whatever data it reads from the socket. Then we could send that data before sending the remainder of the data from the socket. That probably means digging around inside a function we didn't write and coupling its interface tightly to our application. It'd be good to avoid that.Here's the solution: use IO::Mark; sub send_image { my $socket = shift; my $mark = IO::Mark->new( $socket ); # This works fine... my ($width, $height) = get_image_size( $mark ); $mark->close; # ... and so does this! send_image_data( $width, $height, $socket ); }An IO::Mark is an IO::Handle that returns data from the handle from which it was created without consuming that data from the point of view of the original handle.Note the explicit call to close once we're done with $mark. As long as the cloned IO::Mark handle is in scope and open any data read from the original handle will be buffered in memory in case it needs to be read from the cloned handle too. To prevent this either explicitly close the cloned handle or allow it to go out of scope.SYNOPSIS use IO::Mark; sub examine { my $fh = shift; my $mark = IO::Mark->new( $fh ); my $buf; # Reads from $fh via $mark $mark->read( $buf, 1000, 0 ); # Do something with $buf # When $mark goes out of scope $fh no data will appear to have # been consumed from $fh } Requirements: · Perl


IO::Mark Related Software