| 1 | package IO::WrapOutput; | ||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | $IO::WrapOutput::AUTHORITY = 'cpan:HINRIK'; | |||||||||
| 4 | } | ||||||||||
| 5 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | $IO::WrapOutput::VERSION = '0.07'; | |||||||||
| 7 | } | ||||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use warnings FATAL => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Carp 'croak'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use IO::Handle; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Symbol 'gensym'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use base 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 1 | our @EXPORT = qw(wrap_output unwrap_output); | |||||||||
| 17 | 1 | our @EXPORT_OK = @EXPORT; | |||||||||
| 18 | 1 | our %EXPORT_TAGS = (ALL => [@EXPORT]); | |||||||||
| 19 | |||||||||||
| 20 | 1 | my ($orig_stderr, $orig_stdout); | |||||||||
| 21 | |||||||||||
| 22 | 1 | ▶ | sub wrap_output { | ||||||||
|
Subroutine
wrap_output: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||
| 23 | # 2-arg open() here because Perl 5.6 doesn't understand the '>&' mode | ||||||||||
| 24 | # with a 3-arg open | ||||||||||
| 25 | 1 | ▶ | open $orig_stdout, '>&'.fileno(STDOUT) or croak("Can't dup STDOUT: $!"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 1 | ▶ | open $orig_stderr, '>&'.fileno(STDERR) or croak("Can't dup STDERR: $!"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | |||||||||||
| 28 | 1 | my ($read_stdout, $write_stdout) = (gensym(), gensym()); | |||||||||
| 29 | 1 | pipe $read_stdout, $write_stdout; | |||||||||
| 30 | 1 | ▶ | open STDOUT, '>&'.fileno($write_stdout) or croak("Can't pipe STDOUT: $!"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | |||||||||||
| 32 | 1 | my ($read_stderr, $write_stderr) = (gensym(), gensym()); | |||||||||
| 33 | 1 | pipe $read_stderr, $write_stderr; | |||||||||
| 34 | 1 | ▶ | open STDERR, '>&'.fileno($write_stderr) or croak("Can't pipe STDERR: $!"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 1 | STDERR->autoflush(1); | |||||||||
| 36 | |||||||||||
| 37 | 1 | return $read_stdout, $read_stderr; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 1 | ▶ | sub unwrap_output { | ||||||||
|
Subroutine
unwrap_output: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||
| 41 | 1 | ▶ | open STDOUT, '>&'.fileno($orig_stdout) or croak("Can't dup STDOUT: $!"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 1 | ▶ | open STDERR, '>&'.fileno($orig_stderr) or croak("Can't dup STDERR: $!"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 1 | STDERR->autoflush(1); | |||||||||
| 44 | 1 | return; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 1 | 1; | |||||||||
| 48 | |||||||||||
| 49 | =encoding utf8 | ||||||||||
| 50 | |||||||||||
| 51 | =head1 NAME | ||||||||||
| 52 | |||||||||||
| 53 | IO::WrapOutput - Wrap your output filehandles with minimal fuss | ||||||||||
| 54 | |||||||||||
| 55 | =head1 SYNOPSIS | ||||||||||
| 56 | |||||||||||
| 57 | use IO::WrapOutput; | ||||||||||
| 58 | use Module::Which::Hogs::STDOUT::And::STDERR; | ||||||||||
| 59 | |||||||||||
| 60 | my $foo = Module::Which::Hogs::STDOUT::And::STDERR->new(); | ||||||||||
| 61 | my ($stdout, $stderr) = wrap_output(); | ||||||||||
| 62 | |||||||||||
| 63 | # read from $stdout and $stderr | ||||||||||
| 64 | |||||||||||
| 65 | # then, later, restore the original handles | ||||||||||
| 66 | $foo->shutdown; | ||||||||||
| 67 | unwrap_output(); | ||||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | # example using POE::Wheel::ReadLine | ||||||||||
| 71 | use strict; | ||||||||||
| 72 | use warnings; | ||||||||||
| 73 | use IO::WrapOutput; | ||||||||||
| 74 | use POE; | ||||||||||
| 75 | use POE::Wheel::ReadLine; | ||||||||||
| 76 | use POE::Wheel::ReadWrite; | ||||||||||
| 77 | |||||||||||
| 78 | POE::Session->create( | ||||||||||
| 79 | package_states => [main => [qw(_start got_output got_input)]], | ||||||||||
| 80 | ); | ||||||||||
| 81 | |||||||||||
| 82 | $poe_kernel->run(); | ||||||||||
| 83 | |||||||||||
| 84 | sub _start { | ||||||||||
| 85 | my ($heap) = $_[HEAP]; | ||||||||||
| 86 | |||||||||||
| 87 | $heap->{console} = POE::Wheel::ReadLine->new( | ||||||||||
| 88 | InputEvent => 'got_input', | ||||||||||
| 89 | ); | ||||||||||
| 90 | |||||||||||
| 91 | my ($stdout, $stderr) = wrap_output(); | ||||||||||
| 92 | $heap->{stdout_reader} = POE::Wheel::ReadWrite->new( | ||||||||||
| 93 | Handle => $stdout, | ||||||||||
| 94 | InputEvent => 'got_output', | ||||||||||
| 95 | ); | ||||||||||
| 96 | $heap->{stderr_reader} = POE::Wheel::ReadWrite->new( | ||||||||||
| 97 | Handle => $stderr, | ||||||||||
| 98 | InputEvent => 'got_output', | ||||||||||
| 99 | ); | ||||||||||
| 100 | |||||||||||
| 101 | # request the first line | ||||||||||
| 102 | $heap->{console}->get('>'); | ||||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | sub got_output { | ||||||||||
| 106 | my ($heap, $line) = @_[HEAP, ARG0]; | ||||||||||
| 107 | $heap->{console}->put($line); | ||||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | sub got_input { | ||||||||||
| 111 | my ($heap, $line, $exception) = @_[HEAP, ARG0, ARG1]; | ||||||||||
| 112 | |||||||||||
| 113 | if (defined $exception && $exception eq 'interrupt') { | ||||||||||
| 114 | # terminate the console | ||||||||||
| 115 | unwrap_output(); | ||||||||||
| 116 | delete $heap->{console}; | ||||||||||
| 117 | delete $heap->{stdout_reader}; | ||||||||||
| 118 | delete $heap->{stderr_reader}; | ||||||||||
| 119 | print "Terminated\n"; | ||||||||||
| 120 | return; | ||||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | # do something with $line ... | ||||||||||
| 124 | |||||||||||
| 125 | # request the next line | ||||||||||
| 126 | $heap->{console}->get(); | ||||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | =head1 DESCRIPTION | ||||||||||
| 130 | |||||||||||
| 131 | When you have a module (e.g. POE::Wheel::ReadLine) which needs all output | ||||||||||
| 132 | to go through a method that it provides, it can be cumbersome (or even | ||||||||||
| 133 | impossible) to change all the code in an asynchronous/event-driven program | ||||||||||
| 134 | to do that instead of printing directly to STDOUT/STDERR. That's | ||||||||||
| 135 | where C<IO::WrapOutput> comes in. | ||||||||||
| 136 | |||||||||||
| 137 | You just do the setup work for the output-hogging module in question, then | ||||||||||
| 138 | call C<wrap_output> which will return filehandles that you can read from. | ||||||||||
| 139 | Then you take what you get from those filehandles and feed it into your | ||||||||||
| 140 | output-hogging module's output method. After you stop using the | ||||||||||
| 141 | output-hogging module, you can restore your original STDOUT/STDERR handles | ||||||||||
| 142 | with C<unwrap_output>. | ||||||||||
| 143 | |||||||||||
| 144 | =head1 FUNCTIONS | ||||||||||
| 145 | |||||||||||
| 146 | =head2 C<wrap_output> | ||||||||||
| 147 | |||||||||||
| 148 | Takes no arguments. Replaces the current STDOUT and STDERR handles with | ||||||||||
| 149 | pipes, and returns the read ends of those pipes back to you. Any copies | ||||||||||
| 150 | made of the STDOUT/STDERR handles before calling this function will still | ||||||||||
| 151 | be attached to the process' terminal. | ||||||||||
| 152 | |||||||||||
| 153 | my ($stdout, $stderr) = wrap_output(); | ||||||||||
| 154 | |||||||||||
| 155 | =head2 C<unwrap_output> | ||||||||||
| 156 | |||||||||||
| 157 | Takes no arguments. Restores the original STDOUT and STDERR handles. | ||||||||||
| 158 | |||||||||||
| 159 | =head1 AUTHOR | ||||||||||
| 160 | |||||||||||
| 161 | Hinrik E<Ouml>rn SigurE<eth>sson, hinrik.sig@gmail.com | ||||||||||
| 162 | |||||||||||
| 163 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 164 | |||||||||||
| 165 | Copyright 2011 Hinrik E<Ouml>rn SigurE<eth>sson | ||||||||||
| 166 | |||||||||||
| 167 | This program is free software, you can redistribute it and/or modify | ||||||||||
| 168 | it under the same terms as Perl itself. | ||||||||||
| 169 | |||||||||||
| 170 | =cut | ||||||||||