Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
slurp and spurt functions are no longer needed in Mojo::Util
  • Loading branch information
kraih committed Jan 7, 2017
1 parent d7d1656 commit c3cd577
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 63 deletions.
8 changes: 2 additions & 6 deletions lib/Mojo/Asset/Memory.pm
Expand Up @@ -2,7 +2,7 @@ package Mojo::Asset::Memory;
use Mojo::Base 'Mojo::Asset';

use Mojo::Asset::File;
use Mojo::Util 'spurt';
use Mojo::File 'path';

has 'auto_upgrade';
has max_memory_size => sub { $ENV{MOJO_MAX_MEMORY_SIZE} || 262144 };
Expand Down Expand Up @@ -41,11 +41,7 @@ sub get_chunk {
return substr shift->{content} // '', $offset, $max;
}

sub move_to {
my ($self, $to) = @_;
spurt $self->{content} // '', $to;
return $self;
}
sub move_to { path($_[1])->spurt($_[0]{content} // '') and return $_[0] }

sub size { length(shift->{content} // '') }

Expand Down
18 changes: 14 additions & 4 deletions lib/Mojo/File.pm
Expand Up @@ -16,7 +16,6 @@ use File::Path ();
use File::Spec;
use File::Temp ();
use Mojo::Collection;
use Mojo::Util;
use Scalar::Util 'blessed';

our @EXPORT_OK = ('path', 'tempdir');
Expand Down Expand Up @@ -77,11 +76,22 @@ sub new {

sub path { __PACKAGE__->new(@_) }

sub slurp { Mojo::Util::slurp(${shift()}) }
sub slurp {
my $self = shift;

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;
}

sub spurt {
my $self = shift;
Mojo::Util::spurt(shift, $$self);
my ($self, $content) = @_;
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;
}

Expand Down
44 changes: 14 additions & 30 deletions lib/Mojo/Util.pm
Expand Up @@ -57,13 +57,13 @@ our @EXPORT_OK = (
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode deprecated dumper encode getopt hmac_sha1_sum html_unescape),
qw(md5_bytes md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp split_cookie_header),
qw(split_header spurt steady_time tablify term_escape trim unindent unquote),
qw(url_escape url_unescape xml_escape xor_encode)
qw(secure_compare sha1_bytes sha1_sum split_cookie_header split_header),
qw(steady_time tablify term_escape trim unindent unquote url_escape),
qw(url_unescape xml_escape xor_encode)
);

# DEPRECATED!
push @EXPORT_OK, 'files';
push @EXPORT_OK, qw(files slurp spurt);

# Aliases
monkey_patch(__PACKAGE__, 'b64_decode', \&decode_base64);
Expand Down Expand Up @@ -129,7 +129,8 @@ sub encode { _encoding($_[0])->encode("$_[1]") }

# DEPRECATED!
sub files {
deprecated 'Mojo::Util::files is DEPRECATED in favor of Mojo::File';
deprecated
'Mojo::Util::files is DEPRECATED in favor of Mojo::File::list_tree';
require Mojo::File;
Mojo::File::path(shift)->list_tree(@_)->map('to_string')->each;
}
Expand Down Expand Up @@ -247,26 +248,21 @@ sub secure_compare {
return $r == 0;
}

# DEPRECATED!
sub slurp {
my $path = shift;

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

return $content;
deprecated 'Mojo::Util::slurp is DEPRECATED in favor of Mojo::File::slurp';
require Mojo::File;
Mojo::File::path(shift)->slurp;
}

sub split_cookie_header { _header(shift, 1) }
sub split_header { _header(shift, 0) }

# DEPRECATED!
sub spurt {
my ($content, $path) = @_;
open my $file, '>', $path or croak qq{Can't open file "$path": $!};
($file->syswrite($content) // -1) == length $content
or croak qq{Can't write to file "$path": $!};
return $content;
deprecated 'Mojo::Util::spurt is DEPRECATED in favor of Mojo::File::spurt';
require Mojo::File;
Mojo::File::path($_[1])->spurt($_[0]) and return $_[0];
}

sub tablify {
Expand Down Expand Up @@ -685,12 +681,6 @@ Generate SHA1 checksum for bytes.
# "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"
sha1_sum 'foo';
=head2 slurp
my $bytes = slurp '/etc/passwd';
Read all data at once from file.
=head2 split_cookie_header
my $tree = split_cookie_header 'a=b; expires=Thu, 07 Aug 2008 07:07:59 GMT';
Expand Down Expand Up @@ -720,12 +710,6 @@ its own array reference, and keys without a value get C<undef> assigned.
# "six"
split_header('one; two="three four", five=six')->[1][1];
=head2 spurt
$bytes = spurt $bytes, '/etc/passwd';
Write all data at once to file.
=head2 steady_time
my $time = steady_time;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Command.pm
Expand Up @@ -6,7 +6,7 @@ use Mojo::File 'path';
use Mojo::Loader 'data_section';
use Mojo::Server;
use Mojo::Template;
use Mojo::Util qw(deprecated spurt unindent);
use Mojo::Util qw(deprecated unindent);
use Pod::Usage 'pod2usage';

has app => sub { Mojo::Server->new->build_app('Mojo::HelloWorld') };
Expand Down Expand Up @@ -75,7 +75,7 @@ sub write_file {
my ($self, $path, $data) = @_;
return $self->_loud(" [exist] $path") if -f $path;
$self->create_dir(path($path)->dirname);
spurt $data, $path;
path($path)->spurt($data);
return $self->_loud(" [write] $path");
}

Expand Down
6 changes: 6 additions & 0 deletions t/mojo/file.t
Expand Up @@ -72,5 +72,11 @@ is_deeply path($lib)->list_tree({hidden => 1})->map('to_string')->to_array,
$dir = tempdir;
my $file = $dir->child('test.txt')->spurt('just works!');
is $file->slurp, 'just works!', 'right content';
{
no warnings 'redefine';
local *IO::Handle::syswrite = sub { $! = 0; 5 };
eval { $file->spurt("just\nworks!") };
like $@, qr/Can't write to file ".*/, 'right error';
}

done_testing();
24 changes: 3 additions & 21 deletions t/mojo/util.t
Expand Up @@ -14,9 +14,9 @@ use Mojo::Util
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode dumper encode getopt hmac_sha1_sum html_unescape md5_bytes md5_sum),
qw(monkey_patch punycode_decode punycode_encode quote secure_compare),
qw(sha1_bytes sha1_sum slurp split_cookie_header split_header spurt),
qw(steady_time tablify term_escape trim unindent unquote url_escape),
qw(url_unescape xml_escape xor_encode);
qw(sha1_bytes sha1_sum split_cookie_header split_header steady_time tablify),
qw(term_escape trim unindent unquote url_escape url_unescape xml_escape),
qw(xor_encode);

# camelize
is camelize('foo_bar_baz'), 'FooBarBaz', 'right camelized result';
Expand Down Expand Up @@ -412,24 +412,6 @@ is xor_encode("\x10\x1d\x14\x14\x17\x58\x0f\x17\x0a\x14\x1c", 'x'),
is xor_encode('hello', '123456789'), "\x59\x57\x5f\x58\x5a", 'right result';
is xor_encode("\x59\x57\x5f\x58\x5a", '123456789'), 'hello', 'right result';

# slurp
is slurp(catfile(dirname(__FILE__), 'templates', 'exception.mt')),
"test\n% die;\n123\n", 'right content';

# spurt
my $dir = tempdir CLEANUP => 1;
my $file = catfile $dir, 'test.txt';
spurt "just\nworks!", $file;
is slurp($file), "just\nworks!", 'successful roundtrip';

# spurt (incomplete write)
{
no warnings 'redefine';
local *IO::Handle::syswrite = sub { $! = 0; 5 };
eval { spurt "just\nworks!", $file };
like $@, qr/Can't write to file ".*/, 'right error';
}

# steady_time
like steady_time, qr/^[\d.]+$/, 'high resolution time';

Expand Down

0 comments on commit c3cd577

Please sign in to comment.