Skip Menu |

This queue is for tickets about the File-Slurper CPAN distribution.

Report information
The Basics
Id: 107153
Status: open
Priority: 0/
Queue: File-Slurper

People
Owner: Nobody in particular
Requestors: TTKCIAR [...] cpan.org
Cc: ttk [...] ciar.org
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 0.008
Fixed in: (no value)



CC: ttk [...] ciar.org
Subject: lacks append_* analogs to write_* functions
File::Slurp provides an append_file function, equivalent to write_file with {append => 1} option. This is tremendously convenient, and should be in File::Slurper. If you deem it worthy, please export functions: append_text append_binary
Hi,

I just want to support this feature request

Regards

--
kmx
Patches humbly offered implementing append_text() and append_binary(), and unit tests for same.
Subject: slurper-append.patch
*** 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 {
Subject: slurper-test.patch
*** 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;
Just realized I neglected documentation patches :-P sorry. Will rectify.