Skip to content
Snippets Groups Projects
Commit c1b7a481 authored by Matthieu Muffato's avatar Matthieu Muffato
Browse files

Added the opposite of slurp: spurt

parent 29c98e98
No related branches found
No related tags found
2 merge requests!213Added the opposite of slurp: spurt,!213Added the opposite of slurp: spurt
......@@ -131,10 +131,11 @@ our $GZIP_OK = 0;
our $BZIP2_OK = 0;
our $ZIP_OK = 0;
our @EXPORT_OK = qw/slurp slurp_to_array fh_to_array process_to_array work_with_file gz_slurp gz_slurp_to_array gz_work_with_file bz_slurp bz_slurp_to_array bz_work_with_file zip_slurp zip_slurp_to_array zip_work_with_file filter_dir iterate_file iterate_lines move_data/;
our @EXPORT_OK = qw/slurp slurp_to_array fh_to_array process_to_array work_with_file gz_slurp gz_slurp_to_array gz_work_with_file bz_slurp bz_slurp_to_array bz_work_with_file zip_slurp zip_slurp_to_array zip_work_with_file spurt filter_dir iterate_file iterate_lines move_data/;
our %EXPORT_TAGS = (
all => [@EXPORT_OK],
slurp => [qw/slurp slurp_to_array gz_slurp gz_slurp_to_array/],
spurt => [qw/spurt/],
array => [qw/fh_to_array process_to_array slurp_to_array gz_slurp_to_array/],
gz => [qw/gz_slurp gz_slurp_to_array gz_work_with_file/],
bz => [qw/bz_slurp bz_slurp_to_array bz_work_with_file/],
......@@ -201,6 +202,34 @@ sub slurp {
return ($want_ref) ? \$contents : $contents;
}
=head2 spurt()
Arg [1] : string $file
Arg [2] : string $contents
Arg [3] : boolean; $append
Arg [4] : boolean; $binary
Description : Convenient method to safely open a file and dump some content into it.
$append can be set to append to the file instead of resetting it first.
$binary can be set if the content you are printing is not plain-text.
Returntype : None
Example : spurt('/tmp/file.txt', $contents);
Exceptions : If the file could not be created or was not writable
Status : Stable
=cut
sub spurt {
my ( $file, $contents, $append, $binary ) = @_;
work_with_file(
$file,
$append ? 'a' : 'w',
sub {
my ($fh) = @_;
binmode($fh) if $binary;
syswrite( $fh, $contents );
} );
}
=head2 gz_slurp
Arg [1] : string $file
......
......@@ -77,6 +77,15 @@ my $expected_array = [qw/>X AAAAGGGTTCCC TTGGCCAAAAAA ATTC/];
dies_ok { slurp($file) } 'File no longer exists so die';
spurt($file, $contents);
my $rewritten_contents = slurp($file);
is($contents, $rewritten_contents, 'Contents should still be the same');
spurt($file, $contents, 'append');
$rewritten_contents = slurp($file);
is($contents.$contents, $rewritten_contents, 'Contents should be doubled');
unlink $file;
}
{
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment