| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package IO::Select::Trap; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 6132 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 42 |  | 
| 4 | 1 |  |  | 1 |  | 1127 | use IO::Select; | 
|  | 1 |  |  |  |  | 1867 |  | 
|  | 1 |  |  |  |  | 48 |  | 
| 5 | 1 |  |  | 1 |  | 6 | use Carp; | 
|  | 1 |  |  |  |  | 6 |  | 
|  | 1 |  |  |  |  | 59 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 1 |  |  | 1 |  | 5 | use vars qw/$VERSION/; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 1223 |  | 
| 8 |  |  |  |  |  |  | $VERSION = '0.032'; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | sub new { | 
| 11 | 0 |  |  | 0 | 0 |  | my $pkg = shift; | 
| 12 | 0 | 0 |  |  |  |  | my $opts = (ref $_[0] eq 'HASH') ? shift : {}; | 
| 13 | 0 | 0 | 0 |  |  |  | my $self = bless { | 
|  |  |  | 0 |  |  |  |  | 
| 14 |  |  |  |  |  |  | ioselect => new IO::Select(), | 
| 15 |  |  |  |  |  |  | handles => {}, | 
| 16 |  |  |  |  |  |  | traps => ($opts->{traps} or 'String|Scalar'), | 
| 17 |  |  |  |  |  |  | debug => (exists $opts->{debug} ? $opts->{debug} : 1), | 
| 18 |  |  |  |  |  |  | }, (ref $pkg || $pkg); | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 0 | 0 |  |  |  |  | $self->add(@_) if @_; | 
| 21 | 0 |  |  |  |  |  | $self; | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub _update { | 
| 25 | 0 |  |  | 0 |  |  | my ($self) = shift; | 
| 26 | 0 |  |  |  |  |  | my $add = shift eq 'add'; | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 0 |  |  |  |  |  | my @pthru; | 
| 29 | 0 |  |  |  |  |  | foreach my $h (@_) { | 
| 30 | 0 | 0 |  |  |  |  | next unless defined $h; | 
| 31 | 0 | 0 |  |  |  |  | unless ($self->_trapped($h)) { | 
| 32 | 0 |  |  |  |  |  | push @pthru, $h; | 
| 33 | 0 |  |  |  |  |  | next; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 0 | 0 |  |  |  |  | if ($add) { | 
| 37 | 0 |  |  |  |  |  | $self->{handles}->{\*{$h}} = $h; | 
|  | 0 |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | } else { | 
| 39 | 0 |  |  |  |  |  | delete $_[0]->{handles}->{\*{$h}}; | 
|  | 0 |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  | } | 
| 42 | 0 |  |  |  |  |  | return \@pthru; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub _trapped { | 
| 46 | 0 |  |  | 0 |  |  | my ($self, $h) = @_; | 
| 47 | 0 | 0 |  |  |  |  | if ((ref $h) =~ /$self->{traps}/i) { | 
| 48 | 0 |  |  |  |  |  | carp (ref $h)." is trapped."; | 
| 49 | 0 |  |  |  |  |  | return 1; | 
| 50 |  |  |  |  |  |  | } else { | 
| 51 | 0 |  |  |  |  |  | carp (ref $h)." is NOT trapped."; | 
| 52 | 0 |  |  |  |  |  | return 0; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | sub _count { | 
| 57 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 58 | 0 |  |  |  |  |  | return scalar keys %{$self->{handles}}; | 
|  | 0 |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | sub _can_read { | 
| 62 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 63 | 0 | 0 |  |  |  |  | return unless $self->_count; | 
| 64 | 0 |  |  |  |  |  | my @result; | 
| 65 | 0 |  |  |  |  |  | while (my ($k, $h) = each %{$self->{handles}}) { | 
|  | 0 |  |  |  |  |  |  | 
| 66 | 0 | 0 |  |  |  |  | push @result, $h if (length ${$h->sref}); | 
|  | 0 |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | } | 
| 68 | 0 | 0 |  |  |  |  | return wantarray ? @result : \@result; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | sub _can_write { | 
| 72 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 73 | 0 | 0 |  |  |  |  | return unless $self->_count; | 
| 74 | 0 |  |  |  |  |  | my @result; | 
| 75 | 0 |  |  |  |  |  | while (my ($k, $h) = each %{$self->{handles}}) { | 
|  | 0 |  |  |  |  |  |  | 
| 76 | 0 | 0 |  |  |  |  | push @result, $h if ($h->opened); | 
| 77 |  |  |  |  |  |  | } | 
| 78 | 0 | 0 |  |  |  |  | return wantarray ? @result : \@result; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 0 |  |  | 0 |  |  | sub _has_exception {} | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | sub add { | 
| 84 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 85 | 0 |  |  |  |  |  | my $pthru = $self->_update('add', @_); | 
| 86 | 0 | 0 |  |  |  |  | $self->{ioselect}->add(@$pthru) if @$pthru; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | sub remove { | 
| 90 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 91 | 0 |  |  |  |  |  | my $pthru = $self->_update('remove', @_); | 
| 92 | 0 | 0 |  |  |  |  | $self->{ioselect}->remove(@$pthru) if @$pthru; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | sub select { | 
| 96 | 0 | 0 | 0 | 0 | 0 |  | shift if defined $_[0] && !ref($_[0]); | 
| 97 | 0 |  |  |  |  |  | my ($r, $w, $e, $t) = @_; | 
| 98 | 0 |  |  |  |  |  | my (@RR, @WR, @ER); | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 0 | 0 |  |  |  |  | my $rready = defined $r ? $r->_can_read : undef; | 
| 101 | 0 | 0 |  |  |  |  | my $wready = defined $w ? $w->_can_write : undef; | 
| 102 | 0 | 0 |  |  |  |  | my $eready = defined $e ? $e->_has_exception : undef; | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 0 | 0 |  |  |  |  | push @RR, @$rready if defined $rready; | 
| 105 | 0 | 0 |  |  |  |  | push @WR, @$wready if defined $wready; | 
| 106 | 0 | 0 |  |  |  |  | push @ER, @$eready if defined $eready; | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 0 | 0 |  |  |  |  | my ($ir) = defined $r ? $r->{ioselect} : undef; | 
| 109 | 0 | 0 |  |  |  |  | my ($iw) = defined $w ? $w->{ioselect} : undef; | 
| 110 | 0 | 0 |  |  |  |  | my ($ie) = defined $e ? $e->{ioselect} : undef; | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 0 | 0 | 0 |  |  |  | if (@RR || @WR || @ER) { | 
|  |  |  | 0 |  |  |  |  | 
| 113 | 0 | 0 |  |  |  |  | $t = 0 unless (defined $t); # Force non-blocking select() | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 | 0 |  |  |  |  |  | ($rready, $wready, $eready) = IO::Select->select($ir, $iw, $ie, $t); | 
| 117 | 0 | 0 |  |  |  |  | push @RR, @$rready if defined $rready; | 
| 118 | 0 | 0 |  |  |  |  | push @WR, @$wready if defined $wready; | 
| 119 | 0 | 0 |  |  |  |  | push @ER, @$eready if defined $eready; | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 0 |  |  |  |  |  | return (\@RR, \@WR, \@ER); | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | sub exists { | 
| 126 | 0 | 0 |  | 0 | 0 |  | return unless defined $_[1]; | 
| 127 | 0 |  |  |  |  |  | exists $_[0]->{handles}->{\*{$_[1]}}; | 
|  | 0 |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | sub can_read { | 
| 131 | 0 |  |  | 0 | 0 |  | my ($self, $t) = @_; | 
| 132 | 0 |  |  |  |  |  | my @hready = $self->_can_read(); | 
| 133 | 0 | 0 | 0 |  |  |  | $t = 0 if (! defined $t && @hready); | 
| 134 | 0 |  |  |  |  |  | my @iready = $self->{ioselect}->can_read($t); | 
| 135 |  |  |  |  |  |  | return | 
| 136 | 0 | 0 |  |  |  |  | @iready ? | 
|  |  | 0 |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | @hready ? (@iready, @hready) : @iready | 
| 138 |  |  |  |  |  |  | : @hready; | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | sub can_write { | 
| 142 | 0 |  |  | 0 | 0 |  | my ($self, $t) = @_; | 
| 143 | 0 |  |  |  |  |  | my @hready = $self->_can_write(); | 
| 144 | 0 | 0 | 0 |  |  |  | $t = 0 if (! defined $t && @hready); | 
| 145 | 0 |  |  |  |  |  | my @iready = $self->{ioselect}->can_write($t); | 
| 146 |  |  |  |  |  |  | return | 
| 147 | 0 | 0 |  |  |  |  | @iready ? | 
|  |  | 0 |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | @hready ? (@iready, @hready) : @iready | 
| 149 |  |  |  |  |  |  | : @hready; | 
| 150 |  |  |  |  |  |  | } | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | sub has_exception { | 
| 153 | 0 |  |  | 0 | 0 |  | my ($self, $t) = @_; | 
| 154 | 0 |  |  |  |  |  | my @hready = $self->_has_exception(); | 
| 155 | 0 | 0 | 0 |  |  |  | $t = 0 if (! defined $t && @hready); | 
| 156 | 0 |  |  |  |  |  | my @iready = $self->{ioselect}->has_exception($t); | 
| 157 |  |  |  |  |  |  | return | 
| 158 | 0 | 0 |  |  |  |  | @iready ? | 
|  |  | 0 |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | @hready ? (@iready, @hready) : @iready | 
| 160 |  |  |  |  |  |  | : @hready; | 
| 161 |  |  |  |  |  |  | } | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | sub count { | 
| 164 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 165 | 0 |  |  |  |  |  | return $self->{ioselect}->count | 
| 166 | 0 |  |  |  |  |  | + scalar keys %{$self->{handles}}; | 
| 167 |  |  |  |  |  |  | } | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | sub _debug { | 
| 170 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 171 | 0 | 0 |  |  |  |  | print STDERR "$self: ", @_ if $self->{debug}; | 
| 172 |  |  |  |  |  |  | } | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | 1; | 
| 176 |  |  |  |  |  |  | __END__ |