Documentation fixes
Clarified some limitations; added a link to CPAN Testers Matrix; removed
redundant BUGS section; standardized terminology
David Golden
12 years ago
5 | 5 | |
6 | 6 | - Added a workaround for failing t/08-stdin-closed.t under blead |
7 | 7 | perl / 5.15.8 [rt.perl.org #111070] |
8 | ||
9 | Documented: | |
10 | ||
11 | - Clarified some limitations; added a link to CPAN Testers Matrix; | |
12 | removed redundant BUGS section; standardized terminology | |
8 | 13 | |
9 | 14 | Tested: |
10 | 15 |
32 | 32 | Capture::Tiny provides a simple, portable way to capture almost anything sent |
33 | 33 | to STDOUT or STDERR, regardless of whether it comes from Perl, from XS code or |
34 | 34 | from an external program. Optionally, output can be teed so that it is |
35 | captured while being passed through to the original handles. Yes, it even | |
35 | captured while being passed through to the original filehandles. Yes, it even | |
36 | 36 | works on Windows (usually). Stop guessing which of a dozen capturing modules |
37 | 37 | to use in any particular situation and just use this one. |
38 | 38 | |
48 | 48 | The C<<< capture >>> function takes a code reference and returns what is sent to |
49 | 49 | STDOUT and STDERR as well as any return values from the code reference. In |
50 | 50 | scalar context, it returns only STDOUT. If no output was received for a |
51 | handle, it returns an empty string for that handle. Regardless of calling | |
52 | context, all output is captured -- nothing is passed to the existing handles. | |
51 | filehandle, it returns an empty string for that filehandle. Regardless of calling | |
52 | context, all output is captured -- nothing is passed to the existing filehandles. | |
53 | 53 | |
54 | 54 | It is prototyped to take a subroutine reference as an argument. Thus, it |
55 | 55 | can be called in block form: |
147 | 147 | |
148 | 148 | Portability is a goal, not a guarantee. C<<< tee >>> requires fork, except on |
149 | 149 | Windows where C<<< system(1, @cmd) >>> is used instead. Not tested on any |
150 | particularly esoteric platforms yet. | |
150 | particularly esoteric platforms yet. See the | |
151 | L<CPAN Testers Matrix|http://matrix.cpantesters.org/?dist=Capture-Tiny> | |
152 | for test result by platform. | |
151 | 153 | |
152 | 154 | =head2 PerlIO layers |
153 | 155 | |
154 | 156 | Capture::Tiny does it's best to preserve PerlIO layers such as ':utf8' or |
155 | 157 | ':crlf' when capturing. Layers should be applied to STDOUT or STDERR I<before> |
156 | the call to C<<< capture >>> or C<<< tee >>>. This may not work for tied handles (see below). | |
158 | the call to C<<< capture >>> or C<<< tee >>>. This may not work for tied filehandles (see below). | |
157 | 159 | |
158 | 160 | =head2 Modifying filehandles before capturing |
159 | 161 | |
160 | 162 | Generally speaking, you should do little or no manipulation of the standard IO |
161 | handles prior to using Capture::Tiny. In particular, closing, reopening, | |
162 | localizing or tying standard handles prior to capture may cause a variety of | |
163 | filehandles prior to using Capture::Tiny. In particular, closing, reopening, | |
164 | localizing or tying standard filehandles prior to capture may cause a variety of | |
163 | 165 | unexpected, undesirable andE<sol>or unreliable behaviors, as described below. |
164 | 166 | Capture::Tiny does its best to compensate for these situations, but the |
165 | 167 | results may not be what you desire. |
172 | 174 | course, not find them to be closed. If they started closed, Capture::Tiny will |
173 | 175 | close them again when the capture block finishes. |
174 | 176 | |
175 | Note that this reopening will happen even for STDIN or a handle not being | |
177 | Note that this reopening will happen even for STDIN or a filehandle not being | |
176 | 178 | captured to ensure that the filehandle used for capture is not opened to file |
177 | 179 | descriptor 0, as this causes problems on various platforms. |
178 | 180 | |
179 | 181 | B<Localized filehandles> |
180 | 182 | |
181 | If code localizes any of Perl's standard handles before capturing, the capture | |
182 | will affect the localized handles and not the original ones. External system | |
183 | calls are not affected by localizing a handle in Perl and will continue | |
184 | to send output to the original handles (which will thus not be captured). | |
183 | If code localizes any of Perl's standard filehandles before capturing, the capture | |
184 | will affect the localized filehandles and not the original ones. External system | |
185 | calls are not affected by localizing a filehandle in Perl and will continue | |
186 | to send output to the original filehandles (which will thus not be captured). | |
185 | 187 | |
186 | 188 | B<Scalar filehandles> |
187 | 189 | |
188 | 190 | If STDOUT or STDERR are reopened to scalar filehandles prior to the call to |
189 | C<<< capture >>> or C<<< tee >>>, then Capture::Tiny will override the output handle for the | |
191 | C<<< capture >>> or C<<< tee >>>, then Capture::Tiny will override the output filehandle for the | |
190 | 192 | duration of the C<<< capture >>> or C<<< tee >>> call and then send captured output to the |
191 | output handle after the capture is complete. (Requires Perl 5.8) | |
193 | output filehandle after the capture is complete. (Requires Perl 5.8) | |
192 | 194 | |
193 | 195 | Capture::Tiny attempts to preserve the semantics of STDIN opened to a scalar |
194 | 196 | reference. |
195 | 197 | |
196 | B<Tied output handles> | |
198 | B<Tied output filehandles> | |
197 | 199 | |
198 | 200 | If STDOUT or STDERR are tied prior to the call to C<<< capture >>> or C<<< tee >>>, then |
199 | 201 | Capture::Tiny will attempt to override the tie for the duration of the |
200 | C<<< capture >>> or C<<< tee >>> call and then send captured output to the tied handle after | |
202 | C<<< capture >>> or C<<< tee >>> call and then send captured output to the tied filehandle after | |
201 | 203 | the capture is complete. (Requires Perl 5.8) |
202 | 204 | |
203 | 205 | Capture::Tiny may not succeed resending UTF-8 encoded data to a tied |
204 | STDOUT or STDERR handle. Characters may appear as bytes. If the tied handle | |
206 | STDOUT or STDERR filehandle. Characters may appear as bytes. If the tied filehandle | |
205 | 207 | is based on L<Tie::StdHandle>, then Capture::Tiny will attempt to determine |
206 | appropriate layers like C<<< :utf8 >>> from the underlying handle and do the right | |
208 | appropriate layers like C<<< :utf8 >>> from the underlying filehandle and do the right | |
207 | 209 | thing. |
208 | 210 | |
209 | B<Tied input handle> | |
211 | B<Tied input filehandle> | |
210 | 212 | |
211 | 213 | Capture::Tiny attempts to preserve the semantics of tied STDIN, but this is not |
212 | 214 | entirely stable or portable. For example: |
219 | 221 | |
220 | 222 | =item * |
221 | 223 | |
222 | L<FCGI> has been reported as having a pathological tied handle implementation | |
224 | L<FCGI> has been reported as having a pathological tied filehandle implementation | |
223 | 225 | that causes fatal (and hard to diagnose) errors |
224 | 226 | |
225 | 227 | =back |
229 | 231 | |
230 | 232 | my ($out, $err) = do { local *STDIN; capture { ... } }; |
231 | 233 | |
232 | =head2 Modifying handles during a capture | |
234 | =head2 Modifying filehandles during a capture | |
233 | 235 | |
234 | 236 | Attempting to modify STDIN, STDOUT or STDERR I<during> C<<< capture >>> or C<<< tee >>> is |
235 | 237 | almost certainly going to cause problems. Don't do that. |
248 | 250 | alternate timeout may be specified by setting the C<<< PERL_CAPTURE_TINY_TIMEOUT >>> |
249 | 251 | environment variable. Setting it to zero will disable timeouts. |
250 | 252 | |
251 | =head1 BUGS | |
252 | ||
253 | Please report any bugs or feature requests using the CPAN Request Tracker. | |
254 | Bugs can be submitted through the web interface at | |
255 | L<http://rt.cpan.org/Dist/Display.html?Queue=Capture-Tiny> | |
256 | ||
257 | When submitting a bug or request, please include a test-file or a patch to an | |
258 | existing test-file that illustrates the bug or desired feature. | |
259 | ||
260 | 253 | =head1 SEE ALSO |
261 | 254 | |
262 | 255 | This module was, inspired by L<IO::CaptureOutput>, which provides |
422 | 422 | Capture::Tiny provides a simple, portable way to capture almost anything sent |
423 | 423 | to STDOUT or STDERR, regardless of whether it comes from Perl, from XS code or |
424 | 424 | from an external program. Optionally, output can be teed so that it is |
425 | captured while being passed through to the original handles. Yes, it even | |
425 | captured while being passed through to the original filehandles. Yes, it even | |
426 | 426 | works on Windows (usually). Stop guessing which of a dozen capturing modules |
427 | 427 | to use in any particular situation and just use this one. |
428 | 428 | |
438 | 438 | The {capture} function takes a code reference and returns what is sent to |
439 | 439 | STDOUT and STDERR as well as any return values from the code reference. In |
440 | 440 | scalar context, it returns only STDOUT. If no output was received for a |
441 | handle, it returns an empty string for that handle. Regardless of calling | |
442 | context, all output is captured -- nothing is passed to the existing handles. | |
441 | filehandle, it returns an empty string for that filehandle. Regardless of calling | |
442 | context, all output is captured -- nothing is passed to the existing filehandles. | |
443 | 443 | |
444 | 444 | It is prototyped to take a subroutine reference as an argument. Thus, it |
445 | 445 | can be called in block form: |
537 | 537 | |
538 | 538 | Portability is a goal, not a guarantee. {tee} requires fork, except on |
539 | 539 | Windows where {system(1, @cmd)} is used instead. Not tested on any |
540 | particularly esoteric platforms yet. | |
540 | particularly esoteric platforms yet. See the | |
541 | [CPAN Testers Matrix|http://matrix.cpantesters.org/?dist=Capture-Tiny] | |
542 | for test result by platform. | |
541 | 543 | |
542 | 544 | == PerlIO layers |
543 | 545 | |
544 | 546 | Capture::Tiny does it's best to preserve PerlIO layers such as ':utf8' or |
545 | 547 | ':crlf' when capturing. Layers should be applied to STDOUT or STDERR ~before~ |
546 | the call to {capture} or {tee}. This may not work for tied handles (see below). | |
548 | the call to {capture} or {tee}. This may not work for tied filehandles (see below). | |
547 | 549 | |
548 | 550 | == Modifying filehandles before capturing |
549 | 551 | |
550 | 552 | Generally speaking, you should do little or no manipulation of the standard IO |
551 | handles prior to using Capture::Tiny. In particular, closing, reopening, | |
552 | localizing or tying standard handles prior to capture may cause a variety of | |
553 | filehandles prior to using Capture::Tiny. In particular, closing, reopening, | |
554 | localizing or tying standard filehandles prior to capture may cause a variety of | |
553 | 555 | unexpected, undesirable and/or unreliable behaviors, as described below. |
554 | 556 | Capture::Tiny does its best to compensate for these situations, but the |
555 | 557 | results may not be what you desire. |
562 | 564 | course, not find them to be closed. If they started closed, Capture::Tiny will |
563 | 565 | close them again when the capture block finishes. |
564 | 566 | |
565 | Note that this reopening will happen even for STDIN or a handle not being | |
567 | Note that this reopening will happen even for STDIN or a filehandle not being | |
566 | 568 | captured to ensure that the filehandle used for capture is not opened to file |
567 | 569 | descriptor 0, as this causes problems on various platforms. |
568 | 570 | |
569 | 571 | *Localized filehandles* |
570 | 572 | |
571 | If code localizes any of Perl's standard handles before capturing, the capture | |
572 | will affect the localized handles and not the original ones. External system | |
573 | calls are not affected by localizing a handle in Perl and will continue | |
574 | to send output to the original handles (which will thus not be captured). | |
573 | If code localizes any of Perl's standard filehandles before capturing, the capture | |
574 | will affect the localized filehandles and not the original ones. External system | |
575 | calls are not affected by localizing a filehandle in Perl and will continue | |
576 | to send output to the original filehandles (which will thus not be captured). | |
575 | 577 | |
576 | 578 | *Scalar filehandles* |
577 | 579 | |
578 | 580 | If STDOUT or STDERR are reopened to scalar filehandles prior to the call to |
579 | {capture} or {tee}, then Capture::Tiny will override the output handle for the | |
581 | {capture} or {tee}, then Capture::Tiny will override the output filehandle for the | |
580 | 582 | duration of the {capture} or {tee} call and then send captured output to the |
581 | output handle after the capture is complete. (Requires Perl 5.8) | |
583 | output filehandle after the capture is complete. (Requires Perl 5.8) | |
582 | 584 | |
583 | 585 | Capture::Tiny attempts to preserve the semantics of STDIN opened to a scalar |
584 | 586 | reference. |
585 | 587 | |
586 | *Tied output handles* | |
588 | *Tied output filehandles* | |
587 | 589 | |
588 | 590 | If STDOUT or STDERR are tied prior to the call to {capture} or {tee}, then |
589 | 591 | Capture::Tiny will attempt to override the tie for the duration of the |
590 | {capture} or {tee} call and then send captured output to the tied handle after | |
592 | {capture} or {tee} call and then send captured output to the tied filehandle after | |
591 | 593 | the capture is complete. (Requires Perl 5.8) |
592 | 594 | |
593 | 595 | Capture::Tiny may not succeed resending UTF-8 encoded data to a tied |
594 | STDOUT or STDERR handle. Characters may appear as bytes. If the tied handle | |
596 | STDOUT or STDERR filehandle. Characters may appear as bytes. If the tied filehandle | |
595 | 597 | is based on [Tie::StdHandle], then Capture::Tiny will attempt to determine |
596 | appropriate layers like {:utf8} from the underlying handle and do the right | |
598 | appropriate layers like {:utf8} from the underlying filehandle and do the right | |
597 | 599 | thing. |
598 | 600 | |
599 | *Tied input handle* | |
601 | *Tied input filehandle* | |
600 | 602 | |
601 | 603 | Capture::Tiny attempts to preserve the semantics of tied STDIN, but this is not |
602 | 604 | entirely stable or portable. For example: |
603 | 605 | |
604 | 606 | * Capturing or teeing with STDIN tied is broken on Windows |
605 | * [FCGI] has been reported as having a pathological tied handle implementation | |
607 | * [FCGI] has been reported as having a pathological tied filehandle implementation | |
606 | 608 | that causes fatal (and hard to diagnose) errors |
607 | 609 | |
608 | 610 | Unless having STDIN tied is crucial, it may be safest to localize STDIN when |
610 | 612 | |
611 | 613 | my ($out, $err) = do { local *STDIN; capture { ... } }; |
612 | 614 | |
613 | == Modifying handles during a capture | |
615 | == Modifying filehandles during a capture | |
614 | 616 | |
615 | 617 | Attempting to modify STDIN, STDOUT or STDERR ~during~ {capture} or {tee} is |
616 | 618 | almost certainly going to cause problems. Don't do that. |
628 | 630 | 30 seconds (or whatever is the value of {$Capture::Tiny::TIMEOUT}). An |
629 | 631 | alternate timeout may be specified by setting the {PERL_CAPTURE_TINY_TIMEOUT} |
630 | 632 | environment variable. Setting it to zero will disable timeouts. |
631 | ||
632 | = BUGS | |
633 | ||
634 | Please report any bugs or feature requests using the CPAN Request Tracker. | |
635 | Bugs can be submitted through the web interface at | |
636 | [http://rt.cpan.org/Dist/Display.html?Queue=Capture-Tiny] | |
637 | ||
638 | When submitting a bug or request, please include a test-file or a patch to an | |
639 | existing test-file that illustrates the bug or desired feature. | |
640 | 633 | |
641 | 634 | = SEE ALSO |
642 | 635 |