File Coverage

blib/lib/TaskForest/StringHandle.pm
Criterion Covered Total %
statement 18 27 66.6
branch n/a
condition n/a
subroutine 7 9 77.7
pod 0 3 0.0
total 25 39 64.1


line stmt bran cond sub pod time code
1             # ##############################################################################
2             #
3             # $Id: StringHandle.pm 211 2009-05-25 06:05:50Z aijaz $
4             #
5             # ##############################################################################
6              
7             =head1 NAME
8              
9             StringHandle - intercept text sent to stdout or stderr
10              
11             =head1 SYNOPSIS
12              
13             $text1 = "This should go to STDOUT\n";
14             $text2 = "This is the second line\n";
15              
16             $sh = TaskForest::StringHandle->start(*STDOUT);
17             # stdout is being captured as a string
18             print $text1; # nothing is printed to stdout
19             $stdout1 = $sh->read(); # $stdout1 eq $text1
20             $stdout2 = $sh->read(); # $stdout2 eq ''
21             print $text2; # nothing is printed now either
22             $stdout3 = $sh->stop(); # $stdout3 eq $text2, capture stopped
23             print "Hello, world!\n"; # this is printed to stdout
24              
25             =head1 DESCRIPTION
26              
27             This is a simple class that you can use to intercept any text that
28             would have been written to stdout or stderr or any file handle and
29             saves it instead locally. You can then retrieve the text and use it
30             to examine what would have been sent to stdout (or stderr). It was
31             developed primarily to help with the test cases.
32              
33             =cut
34              
35             package TaskForest::StringHandle;
36 22     22   83691 use strict;
  22         56  
  22         1184  
37 22     22   176 use warnings;
  22         41  
  22         890  
38 22     22   21413 use TaskForest::StringHandleTier;
  22         77  
  22         1344  
39 22     22   139 use Carp;
  22         45  
  22         1966  
40            
41             BEGIN {
42 22     22   121 use vars qw($VERSION);
  22         54  
  22         819  
43 22     22   3543 $VERSION = '1.30';
44             }
45              
46              
47             # this is a constructor
48             sub start {
49 21     21 0 45329 my ($class, $handle) = @_;
50 21         7649 my $obj = tie($handle, 'TaskForest::StringHandleTier');
51 0           my $self = { obj => $obj, handle => $handle};
52 0           bless $self, $class;
53             }
54              
55             sub read {
56 0     0 0   my $self = shift;
57 0           return $self->{obj}->getData();
58            
59             }
60              
61             sub stop {
62 0     0 0   my $self = shift;
63              
64 0           my $d = $self->read();
65 0           undef $self->{obj};
66 0           untie($self->{handle});
67 0           return $d;
68             }
69              
70             1;
71              
72             __END__