| IO-Capture documentation | Contained in the IO-Capture distribution. |
IO::Capture::Stdout - Capture any output sent to STDOUT
# Generic example (Just to give the overall view)
use IO::Capture::Stdout;
$capture = IO::Capture::Stdout->new();
$capture->start(); # STDOUT Output captured
print STDOUT "Test Line One\n";
print STDOUT "Test Line Two\n";
print STDOUT "Test Line Three\n";
$capture->stop(); # STDOUT output sent to wherever it was before 'start'
# In 'scalar context' returns next line
$line = $capture->read;
print "$line"; # prints "Test Line One"
$line = $capture->read;
print "$line"; # prints "Test Line Two"
# move line pointer to line 1
$capture->line_pointer(1);
$line = $capture->read;
print "$line"; # prints "Test Line One"
# Find out current line number
$current_line_position = $capture->line_pointer;
# In 'List Context' return an array(list)
@all_lines = $capture->read;
# More useful example 1 - "Using in module tests"
# Note: If you don't want to make users install
# the IO::Capture module just for your tests,
# you can just install in the t/lib directory
# of your module and use the lib pragma in
# your tests.
use lib "t/lib";
use IO::Capture::Stdout;
use Test::More;
my $capture = IO::Capture::Stdout->new;
$capture->start
# execute with a bad parameter to make sure get
# an error.
ok( ! $test("Bad Parameter") );
$capture->stop();
The module IO::Capture::Stdout, is derived from the abstract class IO::Capture.
See IO::Capture. The purpose of the module (as the name suggests) is to capture
any output sent to STDOUT. After the capture is stopped, the STDOUT filehandle
will be reset to the previous location. E.g., If previously redirected to a file, when
IO::Capture->stop is called, output will start going into that file again.
Note: This module won't work with the perl function, system(), or any other operation involving a fork(). If you want to capture the output from a system command, it is faster to use open() or back-ticks.
my $output = `/usr/sbin/ls -l 2>&1`;
IO::Capture Object. undef will be returned on an error.line_pointer,
and the pointer is incremented by one.
$next_line = $capture->read;
line_pointer is not affected.
@buffer = $capture->read;
chomp on them if you don't want the end of line character(s)
while (my $line = $capture->read) {
chomp $line;
$cat_line = join '', $cat_line, $line;
}
line_pointer.
my $current_line = $capture->line_pointer;
$capture->line_pointer(1);
If you would like to sub-class this module to add a feature (method) or two, here is a couple of easy steps. Also see IO::Capture::Overview.
Give your package a name
package MyPackage;
Use this IO::Capture::Stdout as your base class like this:
package MyPackage;
use base qw/IO::Capture::Stdout/;
Add your new method like this
package MyPackage;
use base qw/IO::Capture::Stdout/;
sub grep {
my $self = shift;
for $line (
}
IO::Capture::Overview
IO::Capture
IO::Capture::Stderr
Mark Reynolds reynolds@sgi.com
Jon Morgan jmorgan@sgi.com
Copyright (c) 2003, Mark Reynolds. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
| IO-Capture documentation | Contained in the IO-Capture distribution. |
package IO::Capture::Stdout; use Carp; use base qw/IO::Capture/; use IO::Capture::Tie_STDx; sub _start { my $self = shift; $self->line_pointer(1); tie *STDOUT, "IO::Capture::Tie_STDx"; } sub _retrieve_captured_text { my $self = shift; my $messages = \@{$self->{'IO::Capture::messages'}}; @$messages = <STDOUT>; #$self->line_pointer(1); return 1; } sub _check_pre_conditions { my $self = shift; return unless $self->SUPER::_check_pre_conditions; if (tied *STDOUT) { carp "WARNING: STDOUT already tied, unable to capture"; return; } return 1; } sub _stop { untie *STDOUT; } 1;