Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signal success on buffer writing with PerlIO #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/Stream/Buffered/Auto.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ sub new {

sub print {
my $self = shift;
$self->{_buffer}->print(@_);
my $res = $self->{_buffer}->print(@_);

if ($self->{_max} && $self->{_buffer}->size > $self->{_max}) {
my $buf = $self->{_buffer}->{buffer};
$self->{_buffer} = Stream::Buffered->create('File'),
$self->{_buffer}->print($buf);
$res = $self->{_buffer}->print($buf);
delete $self->{_max};
}
return $res;
}

sub size {
Expand Down
1 change: 1 addition & 0 deletions lib/Stream/Buffered/PerlIO.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sub new {
sub print {
my $self = shift;
$self->{buffer} .= "@_";
return 1;
}

sub size {
Expand Down
48 changes: 48 additions & 0 deletions t/write-zero.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;

use Stream::Buffered;

{
local $Stream::Buffered::MaxMemoryBufferSize = -1;
my $b = Stream::Buffered->new;
ok $b->print(0), "Writing returns true with PerlIO";
_is_zero($b);
}

{
local $Stream::Buffered::MaxMemoryBufferSize = 0;
my $b = Stream::Buffered->new;
ok $b->print(0), "Writing returns true with temp file";
_is_zero($b);
}

{
# auto
my $b = Stream::Buffered->new;
ok $b->print(0), "Writing returns true with auto";
_is_zero($b);
}

{
# auto with max
local $Stream::Buffered::MaxMemoryBufferSize = 3;
my $b = Stream::Buffered->new;
for (1..4) {
ok $b->print(0), "Writing returns true with auto";
}
my $fh = $b->rewind;
is do { local $/; <$fh> }, '0' x 4, "Buffer content OK";
}



sub _is_zero {
my $b = shift;
my $fh = $b->rewind;
is do { local $/; <$fh> }, '0', "Buffer content is zero";
}

done_testing;