Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7a65bd7

Browse files
committed
not sure why i didn't add these before
1 parent 4231cc7 commit 7a65bd7

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

lib/Stream/Buffered/Auto.pm

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package Stream::Buffered::Auto;
2+
use strict;
3+
use parent 'Stream::Buffered';
4+
5+
sub new {
6+
my($class, undef, $max_memory_size) = @_;
7+
bless {
8+
_buffer => Stream::Buffered->create('PerlIO'),
9+
_max => $max_memory_size,
10+
}, $class;
11+
}
12+
13+
sub print {
14+
my $self = shift;
15+
$self->{_buffer}->print(@_);
16+
17+
if ($self->{_max} && $self->{_buffer}->size > $self->{_max}) {
18+
my $buf = $self->{_buffer}->{buffer};
19+
$self->{_buffer} = Stream::Buffered->create('File'),
20+
$self->{_buffer}->print($buf);
21+
delete $self->{_max};
22+
}
23+
}
24+
25+
sub size {
26+
my $self = shift;
27+
$self->{_buffer}->size;
28+
}
29+
30+
sub rewind {
31+
my $self = shift;
32+
$self->{_buffer}->rewind;
33+
}
34+
35+
1;

lib/Stream/Buffered/File.pm

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Stream::Buffered::File;
2+
use strict;
3+
use parent 'Stream::Buffered';
4+
5+
use IO::File;
6+
7+
sub new {
8+
my $class = shift;
9+
10+
my $fh = IO::File->new_tmpfile;
11+
$fh->binmode;
12+
13+
bless { fh => $fh }, $class;
14+
}
15+
16+
sub print {
17+
my $self = shift;
18+
$self->{fh}->print(@_);
19+
}
20+
21+
sub size {
22+
my $self = shift;
23+
$self->{fh}->flush;
24+
-s $self->{fh};
25+
}
26+
27+
sub rewind {
28+
my $self = shift;
29+
$self->{fh}->seek(0, 0);
30+
$self->{fh};
31+
}
32+
33+
1;

lib/Stream/Buffered/PerlIO.pm

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package Stream::Buffered::PerlIO;
2+
use strict;
3+
use parent 'Stream::Buffered';
4+
5+
sub new {
6+
my $class = shift;
7+
bless { buffer => '' }, $class;
8+
}
9+
10+
sub print {
11+
my $self = shift;
12+
$self->{buffer} .= "@_";
13+
}
14+
15+
sub size {
16+
my $self = shift;
17+
length $self->{buffer};
18+
}
19+
20+
sub rewind {
21+
my $self = shift;
22+
my $buffer = $self->{buffer};
23+
open my $io, "<", \$buffer;
24+
bless $io, 'FileHandle'; # This makes $io work as FileHandle under 5.8, .10 and .11 :/
25+
return $io;
26+
}
27+
28+
1;

t/print.t

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env perl
2+
use strict;
3+
use warnings;
4+
use Test::More;
5+
6+
use Stream::Buffered;
7+
8+
{
9+
my $b = Stream::Buffered->new(-1);
10+
$b->print("foo");
11+
is $b->size, 3;
12+
my $fh = $b->rewind;
13+
is do { local $/; <$fh> }, 'foo';
14+
$fh->seek(0, 0);
15+
}
16+
17+
{
18+
local $Stream::Buffered::MaxMemoryBufferSize = 12;
19+
my $b = Stream::Buffered->new;
20+
is $b->size, 0;
21+
$b->print("foo") for 1..5;
22+
is $b->size, 15;
23+
my $fh = $b->rewind;
24+
isa_ok $fh, 'IO::File';
25+
is do { local $/; <$fh> }, ('foo' x 5);
26+
}
27+
28+
{
29+
local $Stream::Buffered::MaxMemoryBufferSize = 0;
30+
my $b = Stream::Buffered->new(3);
31+
$b->print("foo\n");
32+
is $b->size, 4;
33+
my $fh = $b->rewind;
34+
isa_ok $fh, 'IO::File';
35+
is do { local $/; <$fh> }, "foo\n";
36+
}
37+
38+
done_testing;

0 commit comments

Comments
 (0)