PrePAN

Sign in to PrePAN

IO::ReadHandle::Chain Chain read handles

Author
LouisStrous@github
Date
URL
Status
In Review
Good

Synopsis

use IO::ReadHandle::Chain;

$text = 'any text';
open $fh, '<', 'somefile.txt';
$cfh = IO::ReadHandle::Chain->new(\$text, 'otherfile.txt', $fh);
# now read lines first from $text, then from file 'otherfile.txt', and
# finally from $fh
print <$cfh>;        # line-based reading, honors sources' $/
print $cfh->getline; # same as previous

$bytecount = $cfh->read($buffer, 20);  # byte-based reading

Description

This module provides a mechanism to chain any number of data sources together for reading through a single file handle. This is convenient if you have multiple sources of which some are very large and you need to pretend that they are all inside a single source.

One application is to pretend to wrap a huge file of XML elements in a root element so that it becomes acceptable to XML parsers such as XML::Twig, without actually having to create a modified copy of the huge file:

use IO::ReadHandle::Chain;
use XML::Twig;

$prefix = '&lt;wrapper&gt;';
$suffix = '&lt;/wrapper&gt;';
$cfh = IO::ReadHandle::Chain->new(\$prefix, 'huge.xml', \$suffix);
$twig = XML::Twig->new( twig_handlers => { ... } );
$twig->parse($cfh);

NOTE: Read < for &lt; and > for &gt; above. I cannot figure out the Markdown to make them show up correctly.

Comments

Cool - I like it.

Please sign up to post a review.