1IO::Capture::Stdout(3)User Contributed Perl DocumentationIO::Capture::Stdout(3)
2
3
4

NAME

6       IO::Capture::Stdout - Capture any output sent to STDOUT
7

SYNOPSIS

9           # Generic example (Just to give the overall view)
10           use IO::Capture::Stdout;
11
12           $capture = IO::Capture::Stdout->new();
13
14           $capture->start();          # STDOUT Output captured
15           print STDOUT "Test Line One\n";
16           print STDOUT "Test Line Two\n";
17           print STDOUT "Test Line Three\n";
18           $capture->stop();           # STDOUT output sent to wherever it was before 'start'
19
20           # In 'scalar context' returns next line
21           $line = $capture->read;
22           print "$line";         # prints "Test Line One"
23
24           $line = $capture->read;
25           print "$line";         # prints "Test Line Two"
26
27           # move line pointer to line 1
28           $capture->line_pointer(1);
29
30           $line = $capture->read;
31           print "$line";         # prints "Test Line One"
32
33           # Find out current line number
34           $current_line_position = $capture->line_pointer;
35
36           # In 'List Context' return an array(list)
37           @all_lines = $capture->read;
38
39           # More useful example 1 - "Using in module tests"
40           #  Note: If you don't want to make users install
41           #        the IO::Capture module just for your tests,
42           #        you can just install in the t/lib directory
43           #        of your module and use the lib pragma in
44           #        your tests.
45
46           use lib "t/lib";
47           use IO::Capture::Stdout;
48
49           use Test::More;
50
51           my $capture =  IO::Capture::Stdout->new;
52           $capture->start
53
54           # execute with a bad parameter to make sure get
55           # an error.
56
57           ok( ! $test("Bad Parameter") );
58
59           $capture->stop();
60

DESCRIPTION

62       The module "IO::Capture::Stdout", is derived from the abstract class
63       "IO::Capture".  See IO::Capture. The purpose of the module (as the name
64       suggests) is to capture any output sent to "STDOUT".  After the capture
65       is stopped, the STDOUT filehandle will be reset to the previous
66       location. E.g., If previously redirected to a file, when
67       "IO::Capture->stop" is called, output will start going into that file
68       again.
69
70       Note:  This module won't work with the perl function, system(), or any
71       other operation
72              involving a fork().  If you want to capture the output from a
73       system command,
74              it is faster to use open() or back-ticks.
75
76              my $output = `/usr/sbin/ls -l 2>&1`;
77

METHODS

79   new
80       ·   Creates a new capture object.
81
82       ·   An object can be reused as needed, so will only need to do one of
83           these.
84
85           ·   Be aware, any data previously captured will be discarded if a
86               new capture session is started.
87
88   start
89       ·   Start capturing data into the "IO::Capture" Object.
90
91       ·   Can not be called on an object that is already capturing.
92
93       ·   Can not be called while STDOUT tied to an object.
94
95       ·   "undef" will be returned on an error.
96
97   stop
98       ·   Stop capturing data and point STDOUT back to it's previous output
99           location I.e., untie STDOUT
100
101   read
102       ·   In Scalar Context
103
104           ·   Lines are read from the buffer at the position of the
105               "line_pointer", and the pointer is incremented by one.
106
107                   $next_line = $capture->read;
108
109       ·   In List Context
110
111           ·   The array is returned.  The "line_pointer" is not affected.
112
113                   @buffer = $capture->read;
114
115       ·   Data lines are returned exactly as they were captured.  You may
116           want to use "chomp" on them if you don't want the end of line
117           character(s)
118
119               while (my $line = $capture->read) {
120                   chomp $line;
121                   $cat_line = join '', $cat_line, $line;
122               }
123
124   line_pointer
125       ·   Reads or sets the "line_pointer".
126
127               my $current_line = $capture->line_pointer;
128               $capture->line_pointer(1);
129

SUB-CLASSING

131   Adding Features
132       If you would like to sub-class this module to add a feature (method) or
133       two, here is a couple of easy steps. Also see IO::Capture::Overview.
134
135       1.  Give your package a name
136
137               package MyPackage;
138
139       2.  Use this "IO::Capture::Stdout" as your base class like this:
140
141               package MyPackage;
142
143               use base qw/IO::Capture::Stdout/;
144
145       3.  Add your new method like this
146
147               package MyPackage;
148
149               use base qw/IO::Capture::Stdout/;
150
151               sub grep {
152                   my $self = shift;
153
154                   for $line (
155               }
156

See Also

158       IO::Capture::Overview
159
160       IO::Capture
161
162       IO::Capture::Stderr
163

AUTHORS

165       Mark Reynolds reynolds@sgi.com
166
167       Jon Morgan jmorgan@sgi.com
168
170       Copyright (c) 2003, Mark Reynolds. All Rights Reserved.  This module is
171       free software. It may be used, redistributed and/or modified under the
172       same terms as Perl itself.
173
174
175
176perl v5.32.0                      2020-07-28            IO::Capture::Stdout(3)
Impressum