| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #line 1 | 
| 2 |  |  |  |  |  |  | package IO::Capture; | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 1 |  |  | 1 |  | 6 | $VERSION = 0.05; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 38 |  | 
| 5 | 1 |  |  | 1 |  | 5 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 865 |  | 
| 6 |  |  |  |  |  |  | use Carp; | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | #line 270 | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub new { | 
| 12 |  |  |  |  |  |  | my $class = shift; | 
| 13 |  |  |  |  |  |  | if (ref $class) { | 
| 14 |  |  |  |  |  |  | carp "WARNING: " . __PACKAGE__ . "::new cannot be called from existing object. (cloned)"; | 
| 15 |  |  |  |  |  |  | return; | 
| 16 |  |  |  |  |  |  | } | 
| 17 |  |  |  |  |  |  | my $object = shift || {}; | 
| 18 |  |  |  |  |  |  | bless $object, $class; | 
| 19 |  |  |  |  |  |  | $object->_initialize; | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | sub _check_pre_conditions { | 
| 23 |  |  |  |  |  |  | my $self = shift; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | if( $self->{'IO::Capture::status'} ne "Ready") { | 
| 26 |  |  |  |  |  |  | carp "Start issued on an in progress capture ". ref($self); | 
| 27 |  |  |  |  |  |  | return; | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | return 1; | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | sub _initialize { | 
| 34 |  |  |  |  |  |  | my $self = shift; | 
| 35 |  |  |  |  |  |  | if (!ref $self) { | 
| 36 |  |  |  |  |  |  | carp "WARNING: _initialize was called, but not called from a valid object"; | 
| 37 |  |  |  |  |  |  | return; | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | $self->{'IO::Capture::messages'} = []; | 
| 41 |  |  |  |  |  |  | $self->{'IO::Capture::line_pointer'} = 1; | 
| 42 |  |  |  |  |  |  | $self->{'IO::Capture::status'} = "Ready"; | 
| 43 |  |  |  |  |  |  | return $self; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub start { | 
| 47 |  |  |  |  |  |  | my $self = shift; | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | if (! $self->_check_pre_conditions) { | 
| 50 |  |  |  |  |  |  | carp "Error: failed _check_pre_confitions in ". ref($self); | 
| 51 |  |  |  |  |  |  | return; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | if (! $self->_save_current_configuration ) { | 
| 55 |  |  |  |  |  |  | carp "Error saving configuration in " . ref($self); | 
| 56 |  |  |  |  |  |  | return; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | $self->{'IO::Capture::status'} = "Busy"; | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | if (! $self->_start(@_)) { | 
| 62 |  |  |  |  |  |  | carp "Error starting capture in " . ref($self); | 
| 63 |  |  |  |  |  |  | return; | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  | return 1; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub stop { | 
| 69 |  |  |  |  |  |  | my $self = shift; | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | if( $self->{'IO::Capture::status'} ne "Busy") { | 
| 72 |  |  |  |  |  |  | carp "Stop issued on an unstarted capture ". ref($self); | 
| 73 |  |  |  |  |  |  | return; | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | if (! $self->_retrieve_captured_text() ) { | 
| 77 |  |  |  |  |  |  | carp "Error retreaving captured text in " . ref($self); | 
| 78 |  |  |  |  |  |  | return; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | if (!$self->_stop() ) { | 
| 82 |  |  |  |  |  |  | carp "Error return from _stop() " . ref($self) . "\n"; | 
| 83 |  |  |  |  |  |  | return; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | $self->{'IO::Capture::status'} = "Ready"; | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | return 1; | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | sub read { | 
| 92 |  |  |  |  |  |  | my $self = shift; | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | $self->_read; | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | # | 
| 98 |  |  |  |  |  |  | #  Internal start routine.  This needs to be overriden with instance | 
| 99 |  |  |  |  |  |  | #  method | 
| 100 |  |  |  |  |  |  | # | 
| 101 |  |  |  |  |  |  | sub _start { | 
| 102 |  |  |  |  |  |  | my $self = shift; | 
| 103 |  |  |  |  |  |  | return 1; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | sub _read { | 
| 107 |  |  |  |  |  |  | my $self = shift; | 
| 108 |  |  |  |  |  |  | my $messages = \@{$self->{'IO::Capture::messages'}}; | 
| 109 |  |  |  |  |  |  | my $line_pointer = \$self->{'IO::Capture::line_pointer'}; | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | if ($self->{'IO::Capture::status'} ne "Ready") { | 
| 112 |  |  |  |  |  |  | carp "Read cannot be done while capture is in progress". ref($self); | 
| 113 |  |  |  |  |  |  | return; | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | return if $$line_pointer > @$messages; | 
| 117 |  |  |  |  |  |  | return wantarray ? @$messages :  $messages->[($$line_pointer++)-1]; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | sub _retrieve_captured_text { | 
| 121 |  |  |  |  |  |  | return 1; | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | sub _save_current_configuration { | 
| 126 |  |  |  |  |  |  | my $self = shift; | 
| 127 |  |  |  |  |  |  | $self->{'IO::Capture::handler_save'} = $SIG{__WARN__}; | 
| 128 |  |  |  |  |  |  | open STDOUT_SAVE, ">&STDOUT"; | 
| 129 |  |  |  |  |  |  | $self->{'IO::Capture::stdout_save'} = *STDOUT_SAVE; | 
| 130 |  |  |  |  |  |  | open STDERR_SAVE, ">&STDOUT"; | 
| 131 |  |  |  |  |  |  | $self->{'IO::Capture::stderr_save'} = *STDERR_SAVE; | 
| 132 |  |  |  |  |  |  | return $self; | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | sub _stop { | 
| 136 |  |  |  |  |  |  | my $self = shift; | 
| 137 |  |  |  |  |  |  | return 1; | 
| 138 |  |  |  |  |  |  | } | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | sub line_pointer { | 
| 141 |  |  |  |  |  |  | my $self = shift; | 
| 142 |  |  |  |  |  |  | my $new_number = shift; | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | $self->{'IO::Capture::line_pointer'} = $new_number if $new_number; | 
| 145 |  |  |  |  |  |  | return $self->{'IO::Capture::line_pointer'}; | 
| 146 |  |  |  |  |  |  | } | 
| 147 |  |  |  |  |  |  | 1; |