Skip to content

Commit

Permalink
slurp and spurt methods are no longer needed in Mojo::ByteStream
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 7, 2017
1 parent a60872d commit d7d1656
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 49 deletions.
42 changes: 22 additions & 20 deletions lib/Mojo/ByteStream.pm
Expand Up @@ -12,8 +12,8 @@ our @EXPORT_OK = ('b');
my @UTILS = (
qw(b64_decode b64_encode camelize decamelize hmac_sha1_sum html_unescape),
qw(md5_bytes md5_sum punycode_decode punycode_encode quote sha1_bytes),
qw(sha1_sum slurp spurt term_escape trim unindent unquote url_escape),
qw(url_unescape xml_escape xor_encode)
qw(sha1_sum term_escape trim unindent unquote url_escape url_unescape),
qw(xml_escape xor_encode)
);
for my $name (@UTILS) {
my $sub = Mojo::Util->can($name);
Expand Down Expand Up @@ -47,11 +47,31 @@ sub secure_compare { Mojo::Util::secure_compare ${shift()}, shift }

sub size { length ${$_[0]} }

# DEPRECATED!
sub slurp {
Mojo::Util::deprecated 'Mojo::ByteStream::slurp is DEPRECATED'
. ' in favor of Mojo::File::slurp';
require Mojo::File;
my $self = shift;
$$self = Mojo::File::path($$self)->slurp;
return $self;
}

sub split {
my ($self, $pattern) = @_;
return Mojo::Collection->new(map { $self->new($_) } split $pattern, $$self);
}

# DEPRECATED!
sub spurt {
Mojo::Util::deprecated 'Mojo::ByteStream::spurt is DEPRECATED'
. ' in favor of Mojo::File::spurt';
require Mojo::File;
my $self = shift;
Mojo::File::path(shift)->spurt($$self);
return $self;
}

sub tap { shift->Mojo::Base::tap(@_) }

sub to_string { ${$_[0]} }
Expand Down Expand Up @@ -250,24 +270,6 @@ Generate SHA1 checksum for bytestream with L<Mojo::Util/"sha1_sum">.
Size of bytestream.
=head2 slurp
$stream = $stream->slurp;
Read all data at once from file into bytestream with L<Mojo::Util/"slurp">.
# Read file and print lines in random order
b('/home/sri/myapp.pl')->slurp->split("\n")->shuffle->join("\n")->say;
=head2 spurt
$stream = $stream->spurt('/home/sri/myapp.pl');
Write all data from bytestream at once to file with L<Mojo::Util/"spurt">.
# Remove unnecessary whitespace from file
b('/home/sri/foo.txt')->slurp->trim->spurt('/home/sri/bar.txt');
=head2 split
my $collection = $stream->split(',');
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/File.pm
Expand Up @@ -212,7 +212,7 @@ Create the given directories if they don't exist already with L<File::Path>.
=head2 move_to
my $path = $path->move_to('/home/sri/.vimrc.backup');
my $destination = $path->move_to('/home/sri/.vimrc.backup');
Move a file with L<File::Copy> and return a new L<Mojo::File> object for the
destination path.
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojo/Template.pm
Expand Up @@ -4,7 +4,8 @@ use Mojo::Base -base;
use Carp 'croak';
use Mojo::ByteStream;
use Mojo::Exception;
use Mojo::Util qw(decode encode monkey_patch slurp);
use Mojo::File 'path';
use Mojo::Util qw(decode encode monkey_patch);

use constant DEBUG => $ENV{MOJO_TEMPLATE_DEBUG} || 0;

Expand Down Expand Up @@ -168,7 +169,7 @@ sub render_file {
my ($self, $path) = (shift, shift);

$self->name($path) unless defined $self->{name};
my $template = slurp $path;
my $template = path($path)->slurp;
my $encoding = $self->encoding;
croak qq{Template "$path" has invalid encoding}
if $encoding && !defined($template = decode $encoding, $template);
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojolicious/Plugin/Config.pm
Expand Up @@ -2,9 +2,10 @@ package Mojolicious::Plugin::Config;
use Mojo::Base 'Mojolicious::Plugin';

use File::Spec::Functions 'file_name_is_absolute';
use Mojo::Util qw(decode slurp);
use Mojo::File 'path';
use Mojo::Util 'decode';

sub load { $_[0]->parse(decode('UTF-8', slurp $_[1]), @_[1, 2, 3]) }
sub load { $_[0]->parse(decode('UTF-8', path($_[1])->slurp), @_[1, 2, 3]) }

sub parse {
my ($self, $content, $file, $conf, $app) = @_;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Plugin/PODRenderer.pm
Expand Up @@ -4,8 +4,8 @@ use Mojo::Base 'Mojolicious::Plugin';
use Mojo::Asset::File;
use Mojo::ByteStream;
use Mojo::DOM;
use Mojo::File 'path';
use Mojo::URL;
use Mojo::Util 'slurp';
use Pod::Simple::XHTML;
use Pod::Simple::Search;

Expand Down Expand Up @@ -85,7 +85,7 @@ sub _perldoc {
return $c->redirect_to("https://metacpan.org/pod/$module")
unless $path && -r $path;

my $src = slurp $path;
my $src = path($path)->slurp;
$c->respond_to(txt => {data => $src}, html => sub { _html($c, $src) });
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Renderer.pm
Expand Up @@ -6,7 +6,7 @@ use Mojo::File 'path';
use Mojo::JSON 'encode_json';
use Mojo::Home;
use Mojo::Loader 'data_section';
use Mojo::Util qw(decamelize encode md5_sum monkey_patch slurp);
use Mojo::Util qw(decamelize encode md5_sum monkey_patch);

has cache => sub { Mojo::Cache->new };
has classes => sub { ['main'] };
Expand Down
14 changes: 0 additions & 14 deletions t/mojo/bytestream.t
Expand Up @@ -3,7 +3,6 @@ use Mojo::Base -strict;
use Test::More;
use FindBin;
use Mojo::ByteStream 'b';
use Mojo::File qw(path tempdir);

# Tap into method chain
is b('test')->tap(sub { $$_ .= '1' })->camelize, 'Test1', 'right result';
Expand Down Expand Up @@ -133,19 +132,6 @@ b('te', 'st')->say($handle);
}
is $buffer, "test\n123\n\"123\"\n", 'right output';

# slurp
my $file = path(__FILE__)->dirname->child('templates', 'exception.mt');
$stream = b($file)->slurp;
is $stream, "test\n% die;\n123\n", 'right content';
$stream = b($file)->slurp->split("\n")->grep(qr/die/)->join;
is $stream, '% die;', 'right content';

# spurt
my $dir = tempdir;
$file = $dir->child('test.txt');
is b("just\nworks!")->spurt($file)->quote, qq{"just\nworks!"}, 'right result';
is b($file)->slurp, "just\nworks!", 'successful roundtrip';

# term_escape
is b("\t\b\r\n\f")->term_escape, "\\x09\\x08\\x0d\n\\x0c", 'right result';

Expand Down
6 changes: 3 additions & 3 deletions t/mojo/log.t
@@ -1,9 +1,9 @@
use Mojo::Base -strict;

use Test::More;
use Mojo::File 'tempdir';
use Mojo::File qw(path tempdir);
use Mojo::Log;
use Mojo::Util qw(decode slurp);
use Mojo::Util 'decode';

# Logging to file
my $dir = tempdir;
Expand All @@ -13,7 +13,7 @@ $log->error('Just works');
$log->fatal('I ♥ Mojolicious');
$log->debug('Does not work');
undef $log;
my $content = decode 'UTF-8', slurp($path);
my $content = decode 'UTF-8', path($path)->slurp;
like $content, qr/\[.*\] \[error\] Just works/, 'right error message';
like $content, qr/\[.*\] \[fatal\] I ♥ Mojolicious/, 'right fatal message';
unlike $content, qr/\[.*\] \[debug\] Does not work/, 'no debug message';
Expand Down
5 changes: 2 additions & 3 deletions t/mojo/prefork.t
Expand Up @@ -11,20 +11,19 @@ use Mojo::File 'path';
use Mojo::IOLoop::Server;
use Mojo::Server::Prefork;
use Mojo::UserAgent;
use Mojo::Util 'slurp';

# Manage and clean up PID file
my $prefork = Mojo::Server::Prefork->new;
my $file = $prefork->pid_file;
ok !$prefork->check_pid, 'no process id';
$prefork->ensure_pid_file(-23);
ok -e $file, 'file exists';
is slurp($file), "-23\n", 'right process id';
is path($file)->slurp, "-23\n", 'right process id';
ok !$prefork->check_pid, 'no process id';
ok !-e $file, 'file has been cleaned up';
$prefork->ensure_pid_file($$);
ok -e $file, 'file exists';
is slurp($file), "$$\n", 'right process id';
is path($file)->slurp, "$$\n", 'right process id';
is $prefork->check_pid, $$, 'right process id';
undef $prefork;
ok !-e $file, 'file has been cleaned up';
Expand Down
3 changes: 2 additions & 1 deletion t/pod_coverage.t
Expand Up @@ -8,4 +8,5 @@ plan skip_all => 'Test::Pod::Coverage 1.04+ required for this test!'
unless eval 'use Test::Pod::Coverage 1.04; 1';

# DEPRECATED!
all_pod_coverage_ok({also_private => [qw(files rel_dir is_status_class)]});
all_pod_coverage_ok(
{also_private => [qw(files is_status_class rel_dir slurp spurt)]});

0 comments on commit d7d1656

Please sign in to comment.