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

Allow selection of an encoding on slurp/spew in Mojo::File #2089

Merged
merged 3 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 18 additions & 5 deletions lib/Mojo/File.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use File::stat ();
use File::Temp ();
use IO::File ();
use Mojo::Collection;
use Mojo::Util qw(decode encode);

our @EXPORT_OK = ('curfile', 'path', 'tempdir', 'tempfile');

Expand Down Expand Up @@ -127,23 +128,26 @@ sub sibling {
}

sub slurp {
my $self = shift;
my ($self, $encoding) = @_;

CORE::open my $file, '<', $$self or croak qq{Can't open file "$$self": $!};
my $ret = my $content = '';
while ($ret = $file->sysread(my $buffer, 131072, 0)) { $content .= $buffer }
croak qq{Can't read from file "$$self": $!} unless defined $ret;

return $content;
return $encoding ? decode($encoding, $content) : $content;
}

sub spurt {
my ($self, $content) = (shift, join '', @_);
sub spew {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

my ($self, $content, $encoding) = @_;
$content = encode($encoding, $content) if $encoding;
CORE::open my $file, '>', $$self or croak qq{Can't open file "$$self": $!};
($file->syswrite($content) // -1) == length $content or croak qq{Can't write to file "$$self": $!};
return $self;
}

sub spurt { shift->spew(join '', @_) }

sub stat { File::stat::stat(${shift()}) }

sub tap { shift->Mojo::Base::tap(@_) }
Expand Down Expand Up @@ -471,8 +475,17 @@ Return a new L<Mojo::File> object relative to the directory part of the path.
=head2 slurp

my $bytes = $path->slurp;
my $chars = $path->slurp('UTF-8');

Read all data at once from the file. If an encoding is provided, an attempt will be made to decode the content.

=head2 spew

$path = $path->spew($bytes);
$path = $path->spew($chars, $encoding);

Read all data at once from the file.
Write all data at once to the file. If an encoding is provided, an attempt to encode the content will be made prior
to writing.

=head2 spurt

Expand Down
21 changes: 20 additions & 1 deletion t/mojo/file.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use File::Basename qw(basename dirname);
use File::Spec::Functions qw(abs2rel canonpath catfile rel2abs splitdir);
use File::Temp;
use Mojo::File qw(curfile path tempdir tempfile);
use Mojo::Util qw(encode);
use Mojo::Util qw(decode encode);

subtest 'Constructor' => sub {
is(Mojo::File->new, canonpath(getcwd), 'same path');
Expand Down Expand Up @@ -342,4 +342,23 @@ subtest 'I/O' => sub {
}
};

subtest 'I/O with encoding' => sub {
my $dir = tempdir;
my $file = $dir->child('test.txt')->spew('♥1', 'UTF-8');
is $file->slurp('UTF-8'), '♥1', 'right content';
is decode('UTF-8', $file->slurp()), '♥1', 'right content';
is $file->spew('works too!')->slurp, 'works too!', 'right content';
{
genio marked this conversation as resolved.
Show resolved Hide resolved
eval { $file->spew('♥1') };
like $@, qr/Wide character/, 'right error';
}
};

subtest 'I/O with manual encoding' => sub {
genio marked this conversation as resolved.
Show resolved Hide resolved
my $dir = tempdir;
my $file = $dir->child('test.txt')->spew(encode('UTF-8', '♥1'));
is $file->slurp('UTF-8'), '♥1', 'right content';
is decode('UTF-8', $file->slurp()), '♥1', 'right content';
};

done_testing();
Loading