Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
small optimizations
  • Loading branch information
kraih committed Apr 14, 2012
1 parent 9645cf4 commit 50d6d56
Show file tree
Hide file tree
Showing 32 changed files with 331 additions and 331 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -6,6 +6,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Improved documentation.
- Improved tests.
- Fixed format detection bugs in Mojolicious::Routes::Pattern.
- Fixed format handling in routes command.

2.80 2012-04-10
- Added support for alternative MIME types to Mojolicious::Types.
Expand Down
16 changes: 8 additions & 8 deletions examples/connect-proxy.pl
Expand Up @@ -15,13 +15,13 @@
$stream->on(
read => sub {
my ($stream, $chunk) = @_;
if (my $server = $buffer{$client}->{connection}) {
if (my $server = $buffer{$client}{connection}) {
return Mojo::IOLoop->stream($server)->write($chunk);
}
$buffer{$client}->{client} .= $chunk;
if ($buffer{$client}->{client} =~ /\x0d?\x0a\x0d?\x0a$/) {
my $buffer = $buffer{$client}->{client};
$buffer{$client}->{client} = '';
$buffer{$client}{client} .= $chunk;
if ($buffer{$client}{client} =~ /\x0d?\x0a\x0d?\x0a$/) {
my $buffer = $buffer{$client}{client};
$buffer{$client}{client} = '';
if ($buffer =~ /CONNECT (\S+):(\d+)?/) {
my $address = $1;
my $port = $2 || 80;
Expand All @@ -35,7 +35,7 @@
return delete $buffer{$client};
}
say "Forwarding to $address:$port.";
$buffer{$client}->{connection} = $server;
$buffer{$client}{connection} = $server;
$stream->on(
read => sub {
my ($stream, $chunk) = @_;
Expand All @@ -60,8 +60,8 @@
);
$stream->on(
close => sub {
Mojo::IOLoop->remove($buffer{$client}->{connection})
if $buffer{$client}->{connection};
Mojo::IOLoop->remove($buffer{$client}{connection})
if $buffer{$client}{connection};
delete $buffer{$client};
}
);
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo.pm
Expand Up @@ -51,7 +51,7 @@ sub _dict {
return $self->{$name} unless @_;

# Get
return $self->{$name}->{$_[0]} unless @_ > 1 || ref $_[0];
return $self->{$name}{$_[0]} unless @_ > 1 || ref $_[0];

# Set
my $values = ref $_[0] ? $_[0] : {@_};
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/Base.pm
Expand Up @@ -73,21 +73,21 @@ sub attr {
my $code = "sub {\n if (\@_ == 1) {\n";

# No default value (return value)
unless (defined $default) { $code .= " return \$_[0]->{'$attr'};" }
unless (defined $default) { $code .= " return \$_[0]{'$attr'};" }

# Default value
else {

# Return value
$code .= " return \$_[0]->{'$attr'} if exists \$_[0]->{'$attr'};\n";
$code .= " return \$_[0]{'$attr'} if exists \$_[0]{'$attr'};\n";

# Return default value
$code .= " return \$_[0]->{'$attr'} = ";
$code .= " return \$_[0]{'$attr'} = ";
$code .= ref $default eq 'CODE' ? '$default->($_[0]);' : '$default;';
}

# Store value
$code .= "\n }\n \$_[0]->{'$attr'} = \$_[1];\n";
$code .= "\n }\n \$_[0]{'$attr'} = \$_[1];\n";

# Footer (return invocant)
$code .= " \$_[0];\n};";
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/CookieJar.pm
Expand Up @@ -32,7 +32,7 @@ sub add {

# Replace cookie
$domain =~ s/^\.//;
my $jar = $self->{jar}->{$domain} ||= [];
my $jar = $self->{jar}{$domain} ||= [];
@$jar = (grep({$_->path ne $path || $_->name ne $name} @$jar), $cookie);
}

Expand Down Expand Up @@ -63,7 +63,7 @@ sub find {
my $path = $url->path->to_string || '/';
my @found;
while ($domain =~ /[^\.]+\.[^\.]+|localhost$/) {
next unless my $jar = $self->{jar}->{$domain};
next unless my $jar = $self->{jar}{$domain};

# Grab cookies
my @new;
Expand All @@ -86,7 +86,7 @@ sub find {
push @found, $result;
}

$self->{jar}->{$domain} = \@new;
$self->{jar}{$domain} = \@new;
}

# Remove another part
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/DOM.pm
Expand Up @@ -572,7 +572,7 @@ Find elements with CSS3 selectors and return a L<Mojo::Collection> object.
All selectors from L<Mojo::DOM::CSS> are supported.
# Find a specific element and extract information
my $id = $dom->find('div')->[23]->{id};
my $id = $dom->find('div')->[23]{id};
# Extract information from multiple elements
my @headers = $dom->find('h1, h2, h3')->map(sub { shift->text })->each;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/DOM/HTML.pm
Expand Up @@ -292,7 +292,7 @@ sub _render {
# Attributes
my @attrs;
for my $key (sort keys %{$tree->[2]}) {
my $value = $tree->[2]->{$key};
my $value = $tree->[2]{$key};

# No value
push @attrs, $key and next unless defined $value;
Expand Down
12 changes: 6 additions & 6 deletions lib/Mojo/EventEmitter.pm
Expand Up @@ -14,7 +14,7 @@ use constant DEBUG => $ENV{MOJO_EVENTEMITTER_DEBUG} || 0;
# ...Where are we going?"
sub emit {
my ($self, $name) = (shift, shift);
if (my $s = $self->{events}->{$name}) {
if (my $s = $self->{events}{$name}) {
warn "-- Emit @{[blessed($self)]} $name (@{[scalar(@$s)]})\n" if DEBUG;
for my $cb (@$s) { $self->$cb(@_) }
}
Expand All @@ -25,7 +25,7 @@ sub emit {
sub emit_safe {
my ($self, $name) = (shift, shift);

if (my $s = $self->{events}->{$name}) {
if (my $s = $self->{events}{$name}) {
warn "-- Safe @{[blessed($self)]} $name (@{[scalar(@$s)]})\n" if DEBUG;
for my $cb (@$s) {
if (!eval { $self->$cb(@_); 1 } && $name ne 'error') {
Expand All @@ -44,7 +44,7 @@ sub has_subscribers { scalar @{shift->subscribers(shift)} }

sub on {
my ($self, $name, $cb) = @_;
push @{$self->{events}->{$name} ||= []}, $cb;
push @{$self->{events}{$name} ||= []}, $cb;
return $cb;
}

Expand All @@ -63,7 +63,7 @@ sub once {
return $wrapper;
}

sub subscribers { shift->{events}->{shift()} || [] }
sub subscribers { shift->{events}{shift()} || [] }

# "Back you robots!
# Nobody ruins my family vacation but me!
Expand All @@ -73,12 +73,12 @@ sub unsubscribe {

# All
unless ($cb) {
delete $self->{events}->{$name};
delete $self->{events}{$name};
return $self;
}

# One
$self->{events}->{$name} = [grep { $cb ne $_ } @{$self->{events}->{$name}}];
$self->{events}{$name} = [grep { $cb ne $_ } @{$self->{events}{$name}}];

return $self;
}
Expand Down
9 changes: 4 additions & 5 deletions lib/Mojo/Headers.pm
Expand Up @@ -36,16 +36,15 @@ sub add {
$NORMALCASE{$lcname} //= $name;

# Add lines
push @{$self->{headers}->{$lcname}},
map { ref $_ eq 'ARRAY' ? $_ : [$_] } @_;
push @{$self->{headers}{$lcname}}, map { ref $_ eq 'ARRAY' ? $_ : [$_] } @_;

return $self;
}

sub clone {
my $self = shift;
my $clone = $self->new;
$clone->{headers}->{$_} = [@{$self->{headers}->{$_}}]
$clone->{headers}{$_} = [@{$self->{headers}{$_}}]
for keys %{$self->{headers}};
return $clone;
}
Expand All @@ -72,7 +71,7 @@ sub header {
return $self->remove($name)->add($name, @_) if @_;

# String
return unless my $headers = $self->{headers}->{lc $name};
return unless my $headers = $self->{headers}{lc $name};
return join ', ', map { join ', ', @$_ } @$headers unless wantarray;

# Array
Expand Down Expand Up @@ -131,7 +130,7 @@ sub referrer { scalar shift->header(Referer => @_) }

sub remove {
my ($self, $name) = @_;
delete $self->{headers}->{lc $name};
delete $self->{headers}{lc $name};
return $self;
}

Expand Down
24 changes: 12 additions & 12 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -42,7 +42,7 @@ sub client {
# New client
my $client = $self->client_class->new;
my $id = $self->_id;
my $c = $self->{connections}->{$id} ||= {};
my $c = $self->{connections}{$id} ||= {};
$c->{client} = $client;
weaken $client->reactor($self->reactor)->{reactor};

Expand All @@ -53,7 +53,7 @@ sub client {
my $handle = pop;

# New stream
my $c = $self->{connections}->{$id};
my $c = $self->{connections}{$id};
delete $c->{client};
my $stream = $c->{stream} = $self->stream_class->new($handle);
$self->_stream($stream => $id);
Expand All @@ -64,7 +64,7 @@ sub client {
);
$client->on(
error => sub {
delete $self->{connections}->{$id};
delete $self->{connections}{$id};
$self->$cb(pop, undef);
}
);
Expand Down Expand Up @@ -113,7 +113,7 @@ sub recurring {
sub remove {
my ($self, $id) = @_;
$self = $self->singleton unless ref $self;
if (my $c = $self->{connections}->{$id}) { return $c->{finish} = 1 }
if (my $c = $self->{connections}{$id}) { return $c->{finish} = 1 }
$self->_remove($id);
}

Expand All @@ -130,7 +130,7 @@ sub server {
# New server
my $server = $self->server_class->new;
my $id = $self->_id;
$self->{servers}->{$id} = $server;
$self->{servers}{$id} = $server;
weaken $server->reactor($self->reactor)->{reactor};

# Events
Expand Down Expand Up @@ -182,7 +182,7 @@ sub stream {
return $self->_stream($stream, $self->_id) if blessed $stream;

# Find stream for id
return unless my $c = $self->{connections}->{$stream};
return unless my $c = $self->{connections}{$stream};
return $c->{stream};
}

Expand Down Expand Up @@ -219,7 +219,7 @@ sub _id {
my $self = shift;
my $id;
do { $id = md5_sum('c' . time . rand 999) }
while $self->{connections}->{$id} || $self->{servers}->{$id};
while $self->{connections}{$id} || $self->{servers}{$id};
return $id;
}

Expand Down Expand Up @@ -260,12 +260,12 @@ sub _remove {
return if $reactor->remove($id);

# Listen socket
if (delete $self->{servers}->{$id}) { delete $self->{listening} }
if (delete $self->{servers}{$id}) { delete $self->{listening} }

# Connection (stream needs to be deleted first)
else {
delete(($self->{connections}->{$id} || {})->{stream});
delete $self->{connections}->{$id};
delete(($self->{connections}{$id} || {})->{stream});
delete $self->{connections}{$id};
}
}

Expand All @@ -276,12 +276,12 @@ sub _stream {
$self->_cleaner;

# Connect stream with reactor
$self->{connections}->{$id}->{stream} = $stream;
$self->{connections}{$id}{stream} = $stream;
weaken $stream->reactor($self->reactor)->{reactor};

# Events
weaken $self;
$stream->on(close => sub { $self->{connections}->{$id}->{finish} = 1 });
$stream->on(close => sub { $self->{connections}{$id}{finish} = 1 });
$stream->start;

return $id;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/IOLoop/Delay.pm
Expand Up @@ -24,7 +24,7 @@ sub wait {
my $self = shift;
$self->once(finish => sub { shift->ioloop->stop });
$self->ioloop->start;
return wantarray ? @{$self->{args}} : $self->{args}->[0];
return wantarray ? @{$self->{args}} : $self->{args}[0];
}

1;
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/IOLoop/Server.pm
Expand Up @@ -178,13 +178,13 @@ sub _accept {
return $self->emit_safe(accept => $handle) unless my $tls = $self->{tls};
weaken $self;
$tls->{SSL_error_trap} = sub {
return unless my $handle = delete $self->{handles}->{shift()};
return unless my $handle = delete $self->{handles}{shift()};
$self->reactor->remove($handle);
close $handle;
};
return unless $handle = IO::Socket::SSL->start_SSL($handle, %$tls);
$self->reactor->io($handle => sub { $self->_tls($handle) });
$self->{handles}->{$handle} = $handle;
$self->{handles}{$handle} = $handle;
}

sub _cert_file {
Expand All @@ -211,7 +211,7 @@ sub _tls {
# Accepted
if ($handle->accept_SSL) {
$self->reactor->remove($handle);
delete $self->{handles}->{$handle};
delete $self->{handles}{$handle};
return $self->emit_safe(accept => $handle);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/JSON.pm
Expand Up @@ -328,8 +328,8 @@ sub _exception {
package Mojo::JSON::_Bool;
use Mojo::Base -base;
use overload
'0+' => sub { $_[0]->{value} },
'""' => sub { $_[0]->{value} },
'0+' => sub { $_[0]{value} },
'""' => sub { $_[0]{value} },
fallback => 1;

sub new { shift->SUPER::new(value => shift) }
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Message.pm
Expand Up @@ -127,7 +127,7 @@ sub cookie {
}

# Multiple
my $cookies = $self->{cookies}->{$name};
my $cookies = $self->{cookies}{$name};
my @cookies;
@cookies = ref $cookies eq 'ARRAY' ? @$cookies : ($cookies) if $cookies;

Expand Down Expand Up @@ -271,7 +271,7 @@ sub upload {
}

# Multiple
my $uploads = $self->{uploads}->{$name};
my $uploads = $self->{uploads}{$name};
my @uploads;
@uploads = ref $uploads eq 'ARRAY' ? @$uploads : ($uploads) if $uploads;

Expand Down Expand Up @@ -740,7 +740,7 @@ Decode JSON message body directly using L<Mojo::JSON> if possible, returns
C<undef> otherwise. An optional JSON Pointer can be used to extract a
specific value with L<Mojo::JSON::Pointer>.
say $message->json->{foo}->{bar}->[23];
say $message->json->{foo}{bar}[23];
say $message->json('/foo/bar/23');
=head2 C<leftovers>
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Parameters.pm
Expand Up @@ -311,7 +311,7 @@ Remove parameters.
Turn parameters into a hash reference.
# "baz"
Mojo::Parameters->new('foo=bar&foo=baz')->to_hash->{foo}->[1];
Mojo::Parameters->new('foo=bar&foo=baz')->to_hash->{foo}[1];
=head2 C<to_string>
Expand Down

0 comments on commit 50d6d56

Please sign in to comment.