Patches humbly offered implementing append_text() and append_binary(), and unit tests for same.
*** lib/File/Slurper.pm.orig Tue Apr 19 17:05:18 2016
--- lib/File/Slurper.pm Tue Apr 19 17:30:13 2016
*************** use warnings;
*** 5,11 ****
use Carp 'croak';
use Exporter 5.57 'import';
! our @EXPORT_OK = qw/read_binary read_text read_lines write_binary write_text read_dir/;
sub read_binary {
my $filename = shift;
--- 5,11 ----
use Carp 'croak';
use Exporter 5.57 'import';
! our @EXPORT_OK = qw/read_binary read_text read_lines write_binary write_text append_binary append_text read_dir/;
sub read_binary {
my $filename = shift;
*************** sub read_text {
*** 59,78 ****
return do { local $/; <$fh> };
}
! sub write_text {
! my ($filename, undef, $encoding, $crlf) = @_;
$encoding ||= 'utf-8';
my $layer = _text_layers($encoding, $crlf);
local $PerlIO::encoding::fallback = 1;
! open my $fh, ">$layer", $filename or croak "Couldn't open $filename: $!";
print $fh $_[1] or croak "Couldn't write to $filename: $!";
close $fh or croak "Couldn't write to $filename: $!";
return;
}
sub write_binary {
! return write_text(@_[0,1], 'latin-1');
}
sub read_lines {
--- 59,91 ----
return do { local $/; <$fh> };
}
! sub _write_text {
! my ($filename, undef, $encoding, $crlf, $append) = @_;
$encoding ||= 'utf-8';
+ my $mode = $append ? '>>' : '>';
my $layer = _text_layers($encoding, $crlf);
local $PerlIO::encoding::fallback = 1;
! open my $fh, "$mode$layer", $filename or croak "Couldn't open $filename: $!";
print $fh $_[1] or croak "Couldn't write to $filename: $!";
close $fh or croak "Couldn't write to $filename: $!";
return;
}
+ sub append_text {
+ return _write_text(@_[0,1], $_[2], $_[3], 1);
+ }
+
+ sub append_binary {
+ return _write_text(@_[0,1], 'latin-1', undef, 1);
+ }
+
+ sub write_text {
+ return _write_text(@_);
+ }
+
sub write_binary {
! return _write_text(@_[0,1], 'latin-1');
}
sub read_lines {
*** t/10-basics.t.orig Tue Apr 19 17:11:27 2016
--- t/10-basics.t Tue Apr 19 17:29:57 2016
*************** use strict;
*** 4,10 ****
use warnings;
use File::Spec::Functions qw/catfile/;
! use File::Slurper qw/read_text read_binary read_lines write_text read_dir/;
use File::Temp 'tempfile';
use Test::More;
--- 4,10 ----
use warnings;
use File::Spec::Functions qw/catfile/;
! use File::Slurper qw/read_text read_binary read_lines write_text append_text write_binary append_binary read_dir/;
use File::Temp 'tempfile';
use Test::More;
*************** my ($fh, $filename) = tempfile(UNLINK =>
*** 27,30 ****
--- 27,39 ----
ok(eval { write_text($filename, $content); 1 }, 'File has been written') or diag "Error: $@";
is(read_text($filename), $content, 'New file has correct content');
+ ok(eval { append_text($filename, "foo"); 1 }, 'File has been extended') or diag "Error: $@";
+ is(read_text($filename), $content . "foo", 'Extended file has correct content');
+
+ ok(eval { write_binary($filename, $content); 1 }, 'File has been written as binary') or diag "Error: $@";
+ is(read_binary($filename), $content, 'New file has correct binary content');
+
+ ok(eval { append_binary($filename, "foo"); 1 }, 'File has been extended as binary') or diag "Error: $@";
+ is(read_binary($filename), $content . "foo", 'Extended file has correct binary content');
+
done_testing;