Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 6, 2012
1 parent 30e5d74 commit aeb813b
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 27 deletions.
5 changes: 2 additions & 3 deletions lib/Mojo/Asset/Memory.pm
Expand Up @@ -2,7 +2,7 @@ package Mojo::Asset::Memory;
use Mojo::Base 'Mojo::Asset';

use Carp 'croak';
use IO::File;
use IO::Handle;
use Mojo::Asset::File;

has 'auto_upgrade';
Expand Down Expand Up @@ -48,8 +48,7 @@ sub get_chunk {

sub move_to {
my ($self, $path) = @_;
croak qq/Can't open file "$path": $!/
unless my $file = IO::File->new("> $path");
croak qq/Can't open file "$path": $!/ unless open my $file, '>', $path;
croak qq/Can't write to file "$path": $!/
unless defined $file->syswrite($self->{content});
return $self;
Expand Down
5 changes: 2 additions & 3 deletions lib/Mojo/Command.pm
Expand Up @@ -5,7 +5,7 @@ use Carp 'croak';
use Cwd 'getcwd';
use File::Path 'mkpath';
use File::Spec::Functions qw/catdir catfile splitdir/;
use IO::File;
use IO::Handle;
use Mojo::Server;
use Mojo::Template;
use Mojo::Util qw/b64_decode decamelize/;
Expand Down Expand Up @@ -136,8 +136,7 @@ sub write_file {
$self->create_dir($dir);

# Write unbuffered
croak qq/Can't open file "$path": $!/
unless my $file = IO::File->new("> $path");
croak qq/Can't open file "$path": $!/ unless open my $file, '>', $path;
croak qq/Can't write to file "$path": $!/
unless defined $file->syswrite($data);
say " [write] $path" unless $self->quiet;
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/Exception.pm
Expand Up @@ -5,7 +5,6 @@ use overload
'""' => sub { shift->to_string },
fallback => 1;

use IO::File;
use Scalar::Util 'blessed';

has [qw/frames line lines_before lines_after/] => sub { [] };
Expand Down Expand Up @@ -71,7 +70,7 @@ sub _detect {
# Search for context
for my $frame (reverse @trace) {
next unless -r $frame->[0];
my $handle = IO::File->new($frame->[0], '<:utf8');
open my $handle, '<:utf8', $frame->[0];
$self->_parse_context($frame->[1], [[<$handle>]]);
return $self;
}
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojo/Log.pm
Expand Up @@ -3,16 +3,15 @@ use Mojo::Base 'Mojo::EventEmitter';

use Carp 'croak';
use Fcntl ':flock';
use IO::File;
use IO::Handle;

has handle => sub {
my $self = shift;

# File
if (my $path = $self->path) {
croak qq/Can't open log file "$path": $!/
unless my $file = IO::File->new(">> $path");
binmode $file, ':utf8';
unless open my $file, '>>:utf8', $path;
return $file;
}

Expand Down Expand Up @@ -134,7 +133,7 @@ L<Mojo::Log> implements the following attributes.
=head2 C<handle>
my $handle = $log->handle;
$log = $log->handle(IO::File->new);
$log = $log->handle(IO::Handle->new);
Log file handle used by default C<message> event, defaults to opening C<path>
or C<STDERR>.
Expand Down
9 changes: 4 additions & 5 deletions lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -5,7 +5,6 @@ use Cwd 'abs_path';
use Fcntl ':flock';
use File::Basename 'dirname';
use File::Spec::Functions qw/catfile tmpdir/;
use IO::File;
use IO::Poll 'POLLIN';
use List::Util 'shuffle';
use Mojo::Server::Daemon;
Expand Down Expand Up @@ -256,7 +255,7 @@ sub _manage {
}

sub _pid {
return unless my $file = IO::File->new(shift->{config}->{pid_file}, '<');
return unless open my $file, '<', shift->{config}->{pid_file};
my $pid = <$file>;
chomp $pid;
return $pid;
Expand All @@ -274,7 +273,8 @@ sub _pid_file {
# Create PID file
$self->{log}->info(qq/Creating process id file "$file"./);
die qq/Can't create process id file "$file": $!/
unless my $pid = IO::File->new($file, '>', 0644);
unless open my $pid, '>', $file;
chmod 0644, $pid;
print $pid $$;
}

Expand Down Expand Up @@ -309,8 +309,7 @@ sub _spawn {
# Prepare lock file
my $c = $self->{config};
my $file = $c->{lock_file};
my $lock = IO::File->new("> $file")
or die qq/Can't open lock file "$file": $!/;
die qq/Can't open lock file "$file": $!/ unless open my $lock, '>', $file;

# Change user/group
my $loop = $self->{daemon}->setuidgid->ioloop;
Expand Down
8 changes: 3 additions & 5 deletions lib/Mojo/Template.pm
Expand Up @@ -2,7 +2,7 @@ package Mojo::Template;
use Mojo::Base -base;

use Carp 'croak';
use IO::File;
use IO::Handle;
use Mojo::ByteStream;
use Mojo::Exception;
use Mojo::Util qw/decode encode/;
Expand Down Expand Up @@ -309,8 +309,7 @@ sub render_file {

# Slurp file
$self->name($path) unless defined $self->{name};
croak qq/Can't open template "$path": $!/
unless my $file = IO::File->new("< $path");
croak qq/Can't open template "$path": $!/ unless open my $file, '<', $path;
my $tmpl = '';
while ($file->sysread(my $buffer, CHUNK_SIZE, 0)) { $tmpl .= $buffer }

Expand Down Expand Up @@ -361,8 +360,7 @@ sub _write_file {
my ($self, $path, $output) = @_;

# Encode and write to file
croak qq/Can't open file "$path": $!/
unless my $file = IO::File->new("> $path");
croak qq/Can't open file "$path": $!/ unless open my $file, '>', $path;
$output = encode $self->encoding, $output if $self->encoding;
croak qq/Can't write to file "$path": $!/
unless defined $file->syswrite($output);
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojolicious/Plugin/PODRenderer.pm
@@ -1,7 +1,6 @@
package Mojolicious::Plugin::PODRenderer;
use Mojo::Base 'Mojolicious::Plugin';

use IO::File;
use Mojo::Asset::File;
use Mojo::ByteStream 'b';
use Mojo::DOM;
Expand Down Expand Up @@ -54,7 +53,7 @@ sub register {
unless $path && -r $path;

# Turn POD into HTML
my $file = IO::File->new("< $path");
open my $file, '<', $path;
my $html = _pod_to_html(join '', <$file>);

# Rewrite links
Expand Down
4 changes: 1 addition & 3 deletions t/mojo/hypnotoad.t
Expand Up @@ -11,7 +11,6 @@ use Test::More;
use Cwd 'cwd';
use File::Temp;
use FindBin;
use IO::File;
use IO::Socket::INET;
use Mojo::Command;
use Mojo::IOLoop;
Expand Down Expand Up @@ -189,8 +188,7 @@ sleep 1
chdir $cwd;

sub _pid {
return
unless my $file = IO::File->new($command->rel_file('hypnotoad.pid'), '<');
return unless open my $file, '<', $command->rel_file('hypnotoad.pid');
my $pid = <$file>;
chomp $pid;
return $pid;
Expand Down

0 comments on commit aeb813b

Please sign in to comment.