|
NAMEIO::NestedCapture - module for performing nested STD* handle capturesSYNOPSISuse IO::NestedCapture qw/ :subroutines /; my $in = IO::NestedCapture->get_next_in; print $in "Harry\n"; print $in "Ron\n"; print $in "Hermione\n"; capture_in_out { my @profs = qw( Dumbledore Flitwick McGonagall ); while (<STDIN>) { my $prof = shift @prof; print STDOUT "$_ favors $prof"; } }; my $out = IO::NestedCapture->get_last_out; while (<$out>) { print; } # This program will output: # Harry favors Dumbledore # Ron favors Flitwick # Hermione favors McGonagall DESCRIPTIONThis module was partially inspired by IO::Capture, but is intended for a very different purpose and is not otherwise related to that package. In particular, I have a need for some pretty aggressive output/input redirection in a web project I'm working on. I'd like to be able to pipe input into a subroutine and then capture that subroutines output to be used as input on the next.I was using a fairly clumsy, fragile, and brute force method for doing this. If you're interested, you can take a look at the code on PerlMonks.org: http://perlmonks.org/?node_id=459275 This module implements a much saner approach that involves only a single tie per file handle (regardless of what you want to tie it to). It works by tying the STDIN, STDOUT, and STDERR file handles. Then, uses internal tied class logic to handle any nested use or other work. With this module you can capture any combination of STDIN, STDOUT, and STDERR. In the case of STDIN, you may feed any input into capture you want (or even set it to use another file handle). For STDOUT and STDERR you may review the full output of these or prior to capture set a file handle that will receive all the data during the capture. As of version 1.02 of this library, there are two different interfaces to the library. The object-oriented version was first, but the new subroutine interface is a little less verbose and a little safer. OBJECT-ORIENTED INTERFACEThe object-oriented interface is available either through the "IO::NestedCapture" class directly or through a single instance of the class available through the "instance" method.my $capture = IO::NestedCapture->instance; $capture->start(CAPTURE_STDOUT); # Is the same as... IO::NestedCapture->start(CAPTURE_STDOUT); It doesn't really make much difference. You will probably want to important one, several, or all of the capture constants to use this interface. SUBROUTINE INTERFACEThis interface is available via the import of one of the capture subroutines (or not if you want to fully qualify the names):use IO::NestedCapture 'capture_out'; capture_out { # your code to print to STDOUT here... }; # Is similar to... IO::NestedCapture::capture_err { # your code to print to STDERR here... }; This interface has the advantage of being a little more concise and automatically starts and stops the capture before and after running the code block. This will help avoid typos and other mistakes in your code, such as forgetting to call "stop" when you are done. NESTED CAPTURE SUBROUTINESThese subroutines are used with the subroutine interface. (See "SUBROUTINE INTERFACE".) These subroutines actually use the object-oriented interface internally, so they merely provide a convenient set of shortcuts to it that may help save you some trouble.For each subroutine, the subroutine captures one or more file handles before running the given code block and uncaptures them after. In case of an exception, the file handles will still be uncaptured properly. Make sure to put a semi-colon after each method call. To manipulate the input, output, and error handles before or after the capture, you will still need to use parts of the object-oriented interface. You will want to import the subroutines you want to use when you load the "IO::NestedCapture" object: use IO::NestedCapture qw/ capture_in capture_out /; or you can import all of the capture subroutines with the ":subroutines" mnemonic: use IO::NestedCapture ':subroutines'; In place of a block, you may also give a code reference as the argument to any of these calls: sub foo { print "bah\n" } capture_all \&foo; This will run the subroutine foo (with no arguments) and capture the streams it reads/writes. Also, each of the capture subroutines return the return value of the block or rethrow the exceptions raised in the block after stopping the capture.
NESTED CAPTURE CONSTANTSThese constants are used with the object-oriented interface. (See "OBJECT-ORIENTED INTERFACE".)You will want to import the constants you want when you load the "IO::NestedCapture" module: use IO::NestedCapture qw/ CAPTURE_STDIN CAPTURE_STDOUT /; or you may import all of them with the ":constants" mnemonic.: use IO::NestedCapture ':constants';
OBJECT-ORIENTED CAPTURE METHODSThese are the methods used for the object-oriented interface. (See "OBJECT-ORIENTED INTERFACE".)
EXPORTSThis module exports all of the constants used with the object-oriented interface and the subroutines used with the subroutine interface.See "NESTED CAPTURE CONSTANTS" for the specific constant names or use ":constants" to import all the constants. See "NESTED CAPTURE SUBROUTINES" for the specific subroutine names or use ":subroutines" to import all the subroutines. SEE ALSOIO::CaptureAUTHORAndrew Sterling Hanenkamp, <hanenkamp@cpan.org>COPYRIGHT AND LICENSECopyright 2005 Andrew Sterling Hanenkamp.This code is licensed and distributed under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |