File Coverage

blib/lib/CGI/Parse/PSGI/Streaming/Handle.pm
Criterion Covered Total %
statement 33 34 97.0
branch 1 2 50.0
condition n/a
subroutine 9 9 100.0
pod n/a
total 43 45 95.5


line stmt bran cond sub pod time code
1             package CGI::Parse::PSGI::Streaming::Handle;
2 2     2   7 use strict;
  2         2  
  2         44  
3 2     2   6 use warnings;
  2         2  
  2         62  
4             our $VERSION = '1.0.0'; # VERSION
5 2     2   463 use POSIX 'SEEK_SET';
  2         4327  
  2         9  
6 2     2   888 use parent 'Tie::Handle';
  2         2  
  2         10  
7              
8             # ABSTRACT: internal class for the tied handle
9              
10              
11             sub TIEHANDLE {
12 4     4   6 my ($class,$callback) = @_;
13              
14             # our state: the callback, a filehandle, and the buffer it writes
15             # to
16 4         11 my $self = { cb => $callback, buffer => '' };
17 1     1   6 open $self->{fh},'>',\($self->{buffer});
  1         1  
  1         6  
  4         57  
18 4         898 return bless $self, $class;
19             }
20              
21             sub BINMODE {
22 1     1   7 my ($self, $layer) = @_;
23             # this is why we have a filehandle, instead of just passing data
24             # through: emulating all the binmode combinations is a nightmare;
25             # much better to get Perl to handle the mess
26 1 50       4 if (@_==2) {
27 1         13 binmode $self->{fh},$layer;
28             }
29             else {
30 0         0 binmode $self->{fh};
31             }
32             }
33              
34             sub WRITE {
35 7     7   1000380 my ($self,$buf,$len,$offset) = @_;
36             # clear the buffer, make the fh print to the beginning of it
37 7         26 seek( $self->{fh}, 0, SEEK_SET );
38 7         14 $self->{buffer}='';
39             # print! this goes through all the PerlIO layers, so encodings&c
40             # just work
41 7         9 print {$self->{fh}} substr($buf, $offset, $len);
  7         37  
42              
43             # invoke the callback with the data
44 7         23 $self->{cb}->($self->{buffer});
45 7         3010 return $len;
46             }
47              
48             sub CLOSE {
49 1     1   4 my ($self) = @_;
50 1         6 close $self->{fh};
51             # invoke the callback without data to signal the closing
52 1         6 $self->{cb}->();
53 1         921 return 1;
54             }
55              
56             1;
57              
58             __END__