Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
let Mojo::Base change the encoding of STDIN, STDOUT and STDERR to UTF…
…-8 the first time it is loaded
  • Loading branch information
kraih committed Jun 3, 2013
1 parent d922b12 commit 51f5dea
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lib/Mojo/Asset/File.pm
Expand Up @@ -184,7 +184,7 @@ Delete file automatically once it's not used anymore.
my $handle = $file->handle;
$file = $file->handle(IO::File->new);
File handle, created on demand.
Filehandle, created on demand.
=head2 path
Expand Down
6 changes: 6 additions & 0 deletions lib/Mojo/Base.pm
Expand Up @@ -11,6 +11,9 @@ use Carp ();
# Only Perl 5.14+ requires it on demand
use IO::Handle ();

# Default to UTF-8
binmode $_, ':encoding(UTF-8)' for *STDIN, *STDOUT, *STDERR;

sub import {
my $class = shift;
return unless my $flag = shift;
Expand Down Expand Up @@ -167,6 +170,9 @@ All three forms save a lot of typing.
use Mojo::Base;
sub has { Mojo::Base::attr(__PACKAGE__, @_) }
The default encoding of the filehandles C<STDIN>, C<STDOUT> and C<STDERR> will
also be set C<UTF-8> the first time L<Mojo::Base> is loaded.
=head1 FUNCTIONS
L<Mojo::Base> exports the following functions if imported with the C<-base>
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojo/Log.pm
Expand Up @@ -3,14 +3,13 @@ use Mojo::Base 'Mojo::EventEmitter';

use Carp 'croak';
use Fcntl ':flock';
use Mojo::Util 'encode';

has handle => sub {

# File
if (my $path = shift->path) {
croak qq{Can't open log file "$path": $!}
unless open my $file, '>>', $path;
unless open my $file, '>>:encoding(:UTF-8)', $path;
return $file;
}

Expand Down Expand Up @@ -63,7 +62,7 @@ sub _message {

flock $handle, LOCK_EX;
croak "Can't write to log: $!"
unless $handle->print(encode 'UTF-8', $self->format($level, @lines));
unless $handle->print($self->format($level, @lines));
flock $handle, LOCK_UN;
}

Expand Down Expand Up @@ -123,7 +122,7 @@ L<Mojo::Log> implements the following attributes.
my $handle = $log->handle;
$log = $log->handle(IO::Handle->new);
Log file handle used by default C<message> event, defaults to opening C<path>
Log filehandle used by default C<message> event, defaults to opening C<path>
or C<STDERR>.
=head2 level
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -56,7 +56,7 @@ sub run {
exit 0 if $pid;
setsid or die "Can't start a new session: $!";

# Close file handles
# Close filehandles
open STDIN, '</dev/null';
open STDOUT, '>/dev/null';
open STDERR, '>&STDOUT';
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Template.pm
Expand Up @@ -4,7 +4,7 @@ use Mojo::Base -base;
use Carp 'croak';
use Mojo::ByteStream;
use Mojo::Exception;
use Mojo::Util qw(decode encode monkey_patch slurp);
use Mojo::Util qw(decode monkey_patch slurp);

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

Expand Down Expand Up @@ -311,7 +311,7 @@ sub _wrap {
$lines->[-1] .= "@{[$self->append]}; \$_M } };";

my $code = join "\n", @$lines;
warn "-- Code for @{[$self->name]}\n@{[encode 'UTF-8', $code]}\n\n" if DEBUG;
warn "-- Code for @{[$self->name]}\n$code\n\n" if DEBUG;
return $code;
}

Expand Down
8 changes: 3 additions & 5 deletions lib/Mojolicious/Command/get.pm
Expand Up @@ -7,7 +7,7 @@ use Mojo::IOLoop;
use Mojo::JSON;
use Mojo::JSON::Pointer;
use Mojo::UserAgent;
use Mojo::Util qw(decode encode);
use Mojo::Util 'decode';
use Scalar::Util 'weaken';

has description => "Perform HTTP request.\n";
Expand Down Expand Up @@ -47,7 +47,6 @@ sub run {
'r|redirect' => \my $redirect,
'v|verbose' => \my $verbose;

@args = map { decode 'UTF-8', $_ } @args;
die $self->usage unless my $url = shift @args;
my $selector = shift @args;

Expand Down Expand Up @@ -89,7 +88,6 @@ sub run {
STDOUT->autoflush(1);
my $tx = $ua->start($ua->build_tx($method, $url, \%headers, $content));
my ($err, $code) = $tx->error;
$url = encode 'UTF-8', $url;
warn qq{Problem loading URL "$url". ($err)\n} if $err && !$code;

# JSON Pointer
Expand All @@ -106,10 +104,10 @@ sub _json {
return unless my $data = $json->decode(shift);
return unless defined($data = Mojo::JSON::Pointer->new->get($data, shift));
return _say($data) unless ref $data eq 'HASH' || ref $data eq 'ARRAY';
say $json->encode($data);
say decode('UTF-8', $json->encode($data));
}

sub _say { say encode('UTF-8', $_[0]) if length $_[0] }
sub _say { say $_[0] if length $_[0] }

sub _select {
my ($buffer, $selector, $charset, @args) = @_;
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojolicious/Command/routes.pm
Expand Up @@ -3,7 +3,6 @@ use Mojo::Base 'Mojolicious::Command';

use re 'regexp_pattern';
use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
use Mojo::Util 'encode';

has description => "Show available routes.\n";
has usage => <<"EOF";
Expand Down Expand Up @@ -54,7 +53,7 @@ sub _draw {
$regex .= $optional ? "(?:$format)?" : $format if $format;
push @parts, $regex if $verbose;

say encode('UTF-8', join(' ', @parts));
say join(' ', @parts);
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/Mojolicious/Commands.pm
Expand Up @@ -4,6 +4,7 @@ use Mojo::Base 'Mojolicious::Command';
use Getopt::Long 'GetOptions';
use List::Util 'max';
use Mojo::Server;
use Mojo::Util 'decode';

has hint => <<"EOF";
Expand Down Expand Up @@ -60,6 +61,7 @@ sub run {
$name = $self->detect($name) unless $ENV{MOJO_NO_DETECT};

# Run command
@args = map { decode('UTF-8', $_) // $_ } @args;
if ($name && $name =~ /^\w+$/ && ($name ne 'help' || $args[0])) {

# Help
Expand Down
19 changes: 9 additions & 10 deletions lib/Test/Mojo.pm
Expand Up @@ -13,7 +13,7 @@ use Mojo::JSON;
use Mojo::JSON::Pointer;
use Mojo::Server;
use Mojo::UserAgent;
use Mojo::Util qw(decode encode);
use Mojo::Util 'decode';
use Test::More ();

has [qw(message tx)];
Expand Down Expand Up @@ -91,13 +91,13 @@ sub delete_ok { shift->_request_ok(delete => @_) }

sub element_exists {
my ($self, $selector, $desc) = @_;
$desc ||= encode 'UTF-8', qq{element for selector "$selector" exists};
$desc ||= qq{element for selector "$selector" exists};
return $self->_test('ok', $self->tx->res->dom->at($selector), $desc);
}

sub element_exists_not {
my ($self, $selector, $desc) = @_;
$desc ||= encode 'UTF-8', qq{no element for selector "$selector"};
$desc ||= qq{no element for selector "$selector"};
return $self->_test('ok', !$self->tx->res->dom->at($selector), $desc);
}

Expand Down Expand Up @@ -257,25 +257,25 @@ sub status_isnt {

sub text_is {
my ($self, $selector, $value, $desc) = @_;
$desc ||= encode 'UTF-8', qq{exact match for selector "$selector"};
$desc ||= qq{exact match for selector "$selector"};
return $self->_test('is', $self->_text($selector), $value, $desc);
}

sub text_isnt {
my ($self, $selector, $value, $desc) = @_;
$desc ||= encode 'UTF-8', qq{no match for selector "$selector"};
$desc ||= qq{no match for selector "$selector"};
return $self->_test('isnt', $self->_text($selector), $value, $desc);
}

sub text_like {
my ($self, $selector, $regex, $desc) = @_;
$desc ||= encode 'UTF-8', qq{similar match for selector "$selector"};
$desc ||= qq{similar match for selector "$selector"};
return $self->_test('like', $self->_text($selector), $regex, $desc);
}

sub text_unlike {
my ($self, $selector, $regex, $desc) = @_;
$desc ||= encode 'UTF-8', qq{no similar match for selector "$selector"};
$desc ||= qq{no similar match for selector "$selector"};
return $self->_test('unlike', $self->_text($selector), $regex, $desc);
}

Expand All @@ -297,8 +297,7 @@ sub websocket_ok {
);
Mojo::IOLoop->start;

my $desc = encode 'UTF-8', "WebSocket $url";
return $self->_test('ok', $self->tx->res->code eq 101, $desc);
return $self->_test('ok', $self->tx->res->code eq 101, "WebSocket $url");
}

sub _get_content {
Expand Down Expand Up @@ -340,7 +339,7 @@ sub _request_ok {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ($err, $code) = $self->tx->error;
Test::More::diag $err if !(my $ok = !$err || $code) && $err;
return $self->_test('ok', $ok, encode('UTF-8', "@{[uc $method]} $url"));
return $self->_test('ok', $ok, "@{[uc $method]} $url");
}

sub _test {
Expand Down

0 comments on commit 51f5dea

Please sign in to comment.