| 1 | package IO::Buffered; | ||||||||||||||||||||
| 2 | 9 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 9 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 9 | our $VERSION = '1.00'; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 9 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 9 | ▶ | use IO::Buffered::Split; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 9 | ▶ | use IO::Buffered::Regexp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 9 | ▶ | use IO::Buffered::Size; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 9 | ▶ | use IO::Buffered::FixedSize; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 9 | ▶ | use IO::Buffered::Last; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 9 | ▶ | use IO::Buffered::HTTP; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | IO::Buffered - A simple buffer class for dealing with different data types | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | my $buf = new IO::Buffered(Split => qr/,/); | ||||||||||||||||||||
| 23 | $buf->write("record1,reco") | ||||||||||||||||||||
| 24 | $buf->write("rd2,record3"); | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | my @records = $buf->read(); # @records is now ("record1", "record2") | ||||||||||||||||||||
| 27 | @records = $buf->read_last(); # @records is now ("record3") | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | IO::Buffered provides a simple unified way of dealing with buffering. This is | ||||||||||||||||||||
| 33 | done by providing a set of buffering types each with an understanding of what | ||||||||||||||||||||
| 34 | they are buffering. All buffering types share a common set of function for | ||||||||||||||||||||
| 35 | working with the buffer. | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | =over 4 | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | =item B<write($str,..)> | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | C<write()> appends more data to the buffer if the buffer type allows it. | ||||||||||||||||||||
| 42 | Different types might have rules that prohibit the buffer for growing over a | ||||||||||||||||||||
| 43 | certain limit or mandates that only certain types of data be written to the | ||||||||||||||||||||
| 44 | buffer. | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | In case of error the number of bytes written to the buffer is returned and the | ||||||||||||||||||||
| 47 | function croaks. | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | =item B<read($alt_size)> | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | C<read()> returns the number of ready records as defined by the buffer type or | ||||||||||||||||||||
| 52 | returns an empty array when no records are available. Read records will be | ||||||||||||||||||||
| 53 | cleared from the buffer. $alt_size defines alternative size of the next record | ||||||||||||||||||||
| 54 | in the buffer if the buffer type does not know how much data to buffer before | ||||||||||||||||||||
| 55 | returning the record. This is currently used by the HTTP buffer type when it is | ||||||||||||||||||||
| 56 | in HeaderOnly mode and needs to return the data part of a http request. | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | =item B<read_last()> | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | C<read_last()> returns the number of ready records as defined by the buffer type and | ||||||||||||||||||||
| 61 | the rest of the buffer as the last record. Or returns an empty array when no records | ||||||||||||||||||||
| 62 | are available. After C<read_last> is called the buffer will be empty. | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | =item B<flush($str, ...)> | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | C<flush()> flushes the buffer if no input or replace the buffer with the input. | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | =item B<buffer()> | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | C<buffer()> returns a copy of the buffer. | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =item B<returns_last()> | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | C<returns_last()> tells if the buffer type knows if it's dealing with a | ||||||||||||||||||||
| 75 | complete record or not. Or a call to <read_last()> is need to get all valid | ||||||||||||||||||||
| 76 | records. An example of this is the "Split" buffer type where record delimiter | ||||||||||||||||||||
| 77 | does not have to be at the end of every record: | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | my $buffer = new IO::Buffered::Split(qr/\n/); | ||||||||||||||||||||
| 80 | $buffer->write("Hello\nHello"); | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | if($buffer->returns_last) { | ||||||||||||||||||||
| 83 | my @records = $buffer->read(); # @records would be ('Hello') | ||||||||||||||||||||
| 84 | } else { | ||||||||||||||||||||
| 85 | my @records = $buffer->read_last(); # @records is ('Hello', 'Hello') | ||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =back | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | =head1 BUFFER TYPES | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =head2 Regexp | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | The Regexp buffer type takes a regular expression as input and splits records | ||||||||||||||||||||
| 97 | based on that. Only the match defined in the () is returned and not the | ||||||||||||||||||||
| 98 | complete match. | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | An example would be C<qr/^(.+)\n/> that would work as line buffing: | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | my $buf = IO::Buffered(Regexp => qr/^(.+)\n/); | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | Read more here: L<IO::Buffered::Regexp> | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | =head2 Split | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | Split is special case of the Regexp buffer type and is in essence just | ||||||||||||||||||||
| 109 | C</(.*?)$split/>. Here only the non matching part of $split is returned. | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | An example would be C<qr/\n/> that also works as line buffering or C<qr/\0/> | ||||||||||||||||||||
| 112 | for C strings. | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | my $buf = IO::Buffered(Split => qr/\n/); | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | Read more here: L<IO::Buffered::Split> | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =head2 Size | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | The Size buffering type reads the size from the data to determine where record | ||||||||||||||||||||
| 121 | boundaries are. Only the data is returned not the bytes that hold the length | ||||||||||||||||||||
| 122 | information. Size buffering takes two arguments, a L<pack> template and a offset | ||||||||||||||||||||
| 123 | for the numbers of bytes to add to the length that was unpacked with the | ||||||||||||||||||||
| 124 | template. | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | An example would be a template of "n" and a offset of 0 that could be used to | ||||||||||||||||||||
| 127 | handle DNS tcp requests. Offset defaults to 0 if not set. | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | my $buf = IO::Buffered(Size => ["n", 0]); | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | Read more here: L<IO::Buffered::Size> | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | =head2 FixedSize | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | FixedSize buffering returns records in fixed size chunks. | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | An example would to return 100 bytes at a time: | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | my $buf = IO::Buffered(FixedSize => 100); | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | Read more here: L<IO::Buffered::FixedSize> | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | =head2 Last | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | Last buffering simple only returns one record when read_last is called. All | ||||||||||||||||||||
| 146 | calls to read will return an empty array. | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | An example would be: | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | my $buf = IO::Buffered(Last => 1); | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | Read more here: L<IO::Buffered::Last> | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =head2 HTTP | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | HTTP buffering provides a simple buffering for HTTP traffic by looking for | ||||||||||||||||||||
| 157 | "Content-Length:" in the HTTP header. If one is found this will be used to | ||||||||||||||||||||
| 158 | split records. If not only the header will be returned. | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | An example would be: | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | my $buf = IO::Buffered(HTTP => 1); | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | Read more here: L<IO::Buffered::HTTP> | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | =head1 GENERIC OPTIONS | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | =head2 MaxSize | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | MaxSize provides a limit on how big a buffer can grow, when the limit is hit an | ||||||||||||||||||||
| 171 | exception is thrown. | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | The default value for MaxSize is 0, meaning that there is no size limit on the | ||||||||||||||||||||
| 174 | buffer. | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =head1 METHODS | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =over | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 9 | ▶ | use base "Exporter"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 9 | our @EXPORT_OK = qw(recroak); | |||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =item new() | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | IO::Buffered simple provides a wrapper for the different buffering types. The | ||||||||||||||||||||
| 189 | argument given to the buffer type is simply given as first argument to the | ||||||||||||||||||||
| 190 | constructor of the buffer type, as show below: | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | $buf = Buffered::IO(Split => qr/\n/); | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | # is the same as | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | $buf = Buffered::IO::Split(qr/\n/); | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | Extra options are passed along as an array after first argument, as show below: | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | $buf = Buffered::IO(Split => qr/\n/, MaxSize => 1000_000); | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | # is the same as | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | $buf = Buffered::IO::Split(qr/\n/, MaxSize => 1000_000); | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | Buffered::IO recasts exceptions so there is no differences in using either | ||||||||||||||||||||
| 207 | interface. | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | =cut | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | 25 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||
| 212 | 25 | my ($class, %args) = @_; | |||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | 25 | my ($type) = grep { exists $args{$_} } | |||||||||||||||||||
| 215 | qw(Split Regexp Size FixedSize Last HTTP); # Find buffer type | ||||||||||||||||||||
| 216 | 25 | ▶ | croak "Unknown or no buffer type defined" if !defined $type; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | 2 | my %opts = map { $_ => $args{$_} } | |||||||||||||||||||
| 219 | 25 | grep { $_ ne $type } keys %args; # Get options for buffer type | |||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 25 | my $package = "IO::Buffered::$type"; | |||||||||||||||||||
| 222 | 25 | ▶ | if($type =~ /HTTP|Last/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 223 | 3 | ▶ | return (eval { new $package(%opts); } | ||||||||||||||||||
|
Condition33%1/3
eval {
do {
$package->new(%opts)
}
} || recroak($@)
MC/DC0%0/2
eval {
do {
$package->new(%opts)
}
} || recroak($@)
eval {
do {
$package->new(%opts)
}
}
recroak($@)
Truth table33%1/3
A || B
A= eval {
do {
$package->new(%opts)
}
}B= recroak($@)
| |||||||||||||||||||||
| 224 | or recroak($@)); | ||||||||||||||||||||
| 225 | } else { | ||||||||||||||||||||
| 226 | 22 | ▶ | return (eval { new $package($args{$type}, %opts); } | ||||||||||||||||||
|
Condition66%2/3
eval {
do {
$package->new($args{$type}, %opts)
}
} || recroak($@)
MC/DC0%0/2
eval {
do {
$package->new($args{$type}, %opts)
}
} || recroak($@)
eval {
do {
$package->new($args{$type}, %opts)
}
}
recroak($@)
Truth table33%1/3
A || B
A= eval {
do {
$package->new($args{$type}, %opts)
}
}B= recroak($@)
| |||||||||||||||||||||
| 227 | or recroak($@)); | ||||||||||||||||||||
| 228 | } | ||||||||||||||||||||
| 229 | } | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | =item recroak() | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | Helper function to rethrow croaks | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | =cut | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | 18 | ▶ | sub recroak { | ||||||||||||||||||
|
Subroutine
recroak: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 238 | 18 | $_[0] =~ s/ at \S+ line \d+.*$//s; | |||||||||||||||||||
| 239 | 18 | croak $_[0]; | |||||||||||||||||||
| 240 | } | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | =back | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | =head1 AUTHOR | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | Troels Liebe Bentsen <tlb@rapanden.dk> | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | =head1 COPYRIGHT | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | Copyright(C) 2008 Troels Liebe Bentsen | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||
| 253 | it under the same terms as Perl itself. | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | =cut | ||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | 9 | 1; | |||||||||||||||||||