Skip to content

Commit

Permalink
use more smartmatch
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 26, 2012
1 parent 45fb21a commit fb9a1e2
Show file tree
Hide file tree
Showing 24 changed files with 59 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

3.14 2012-07-25
3.14 2012-07-26
- Improved documentation.

3.13 2012-07-24
Expand Down
18 changes: 9 additions & 9 deletions lib/Mojo/Content.pm
Expand Up @@ -71,7 +71,7 @@ sub get_header_chunk {
$ENV{MOJO_CHUNK_SIZE} || 131072;
}

sub has_leftovers { length(shift->{buffer} || '') }
sub has_leftovers { !!length(shift->{buffer} || '') }

sub header_size { length shift->build_headers }

Expand All @@ -82,11 +82,11 @@ sub is_dynamic {
return $self->{dynamic} && !defined $self->headers->content_length;
}

sub is_finished { (shift->{state} || '') eq 'finished' }
sub is_finished { shift->{state} ~~ 'finished' }

sub is_multipart {undef}

sub is_parsing_body { (shift->{state} || '') eq 'body' }
sub is_parsing_body { shift->{state} ~~ 'body' }

sub leftovers { shift->{buffer} }

Expand All @@ -109,9 +109,9 @@ sub parse {

# Parse chunked content
$self->{real_size} = 0 unless exists $self->{real_size};
if ($self->is_chunked && ($self->{state} || '') ne 'headers') {
if ($self->is_chunked && !($self->{state} ~~ 'headers')) {
$self->_parse_chunked;
$self->{state} = 'finished' if ($self->{chunked} || '') eq 'finished';
$self->{state} = 'finished' if $self->{chunked} ~~ 'finished';
}

# Not chunked, pass through to second buffer
Expand Down Expand Up @@ -174,14 +174,14 @@ sub parse_until_body {
}

# Parse headers
$self->_parse_headers if ($self->{state} || '') eq 'headers';
$self->_parse_headers if $self->{state} ~~ 'headers';

return $self;
}

sub progress {
my $self = shift;
return 0 unless ($self->{state} || '') ~~ [qw(body finished)];
return 0 unless $self->{state} ~~ [qw(body finished)];
return $self->{raw_size} - ($self->{header_size} || 0);
}

Expand Down Expand Up @@ -273,7 +273,7 @@ sub _parse_chunked {

# Trailing headers
return $self->_parse_chunked_trailing_headers
if ($self->{chunked} || '') eq 'trailing_headers';
if $self->{chunked} ~~ 'trailing_headers';

# New chunk (ignore the chunk extension)
while ($self->{pre_buffer} =~ /^((?:\x0d?\x0a)?([\da-fA-F]+).*\x0d?\x0a)/) {
Expand Down Expand Up @@ -306,7 +306,7 @@ sub _parse_chunked {

# Trailing headers
$self->_parse_chunked_trailing_headers
if ($self->{chunked} || '') eq 'trailing_headers';
if $self->{chunked} ~~ 'trailing_headers';
}

sub _parse_chunked_trailing_headers {
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Content/MultiPart.pm
Expand Up @@ -145,17 +145,17 @@ sub _parse_multipart {
until ($self->is_finished) {

# Preamble
if (($self->{multi_state} || '') eq 'multipart_preamble') {
if ($self->{multi_state} ~~ 'multipart_preamble') {
last unless $self->_parse_multipart_preamble($boundary);
}

# Boundary
elsif (($self->{multi_state} || '') eq 'multipart_boundary') {
elsif ($self->{multi_state} ~~ 'multipart_boundary') {
last unless $self->_parse_multipart_boundary($boundary);
}

# Body
elsif (($self->{multi_state} || '') eq 'multipart_body') {
elsif ($self->{multi_state} ~~ 'multipart_body') {
last unless $self->_parse_multipart_body($boundary);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/EventEmitter.pm
Expand Up @@ -41,7 +41,7 @@ sub emit_safe {
return $self;
}

sub has_subscribers { scalar @{shift->subscribers(shift)} }
sub has_subscribers { !!@{shift->subscribers(shift)} }

sub on {
my ($self, $name, $cb) = @_;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Headers.pm
Expand Up @@ -78,9 +78,9 @@ sub header {
return @$headers;
}

sub is_finished { (shift->{state} || '') eq 'finished' }
sub is_finished { shift->{state} ~~ 'finished' }

sub is_limit_exceeded { shift->{limit} }
sub is_limit_exceeded { !!shift->{limit} }

sub leftovers { delete shift->{buffer} }

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -44,7 +44,7 @@ sub is_readable {
sub is_writing {
my $self = shift;
return unless exists $self->{handle};
return length($self->{buffer}) || $self->has_subscribers('drain');
return !!length($self->{buffer}) || $self->has_subscribers('drain');
}

sub start {
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Message.pm
Expand Up @@ -181,9 +181,9 @@ sub headers { shift->content->headers(@_) }
sub is_chunked { shift->content->is_chunked }
sub is_dynamic { shift->content->is_dynamic }

sub is_finished { (shift->{state} || '') eq 'finished' }
sub is_finished { shift->{state} ~~ 'finished' }

sub is_limit_exceeded { ((shift->error)[1] || 0) ~~ [413, 431] }
sub is_limit_exceeded { (shift->error)[1] ~~ [413, 431] }

sub is_multipart { shift->content->is_multipart }

Expand Down Expand Up @@ -327,7 +327,7 @@ sub _parse {
}

# Content
if (($self->{state} || '') ~~ [qw(body content finished)]) {
if ($self->{state} ~~ [qw(body content finished)]) {

# Until body
my $content = $self->content;
Expand Down
30 changes: 11 additions & 19 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -15,7 +15,7 @@ my $START_LINE_RE = qr|
([a-zA-Z]+) # Method
\s+
([0-9a-zA-Z\-._~:/?#[\]\@!\$&'()*+,;=\%]+) # Path
(?:\s+HTTP/(\d+\.\d+))? # Version
(?:\s+HTTP/(\d\.\d))? # Version
$
|x;

Expand Down Expand Up @@ -86,7 +86,7 @@ sub fix_headers {

sub is_secure {
my $url = shift->url;
return ($url->scheme || $url->base->scheme || '') eq 'https';
return ($url->scheme || $url->base->scheme) ~~ 'https';
}

sub is_xhr {
Expand All @@ -105,9 +105,7 @@ sub parse {
my $self = shift;

# CGI like environment
my $env;
if (@_ > 1) { $env = {@_} }
else { $env = $_[0] if ref $_[0] eq 'HASH' }
my $env = @_ > 1 ? {@_} : ref $_[0] eq 'HASH' ? $_[0] : undef;

# Parse CGI like environment
my $chunk;
Expand Down Expand Up @@ -177,11 +175,10 @@ sub _build_start_line {

# Proxy
elsif ($self->proxy) {
my $clone = $url = $url->clone;
$clone->userinfo(undef);
$path = $clone
unless lc($self->headers->upgrade || '') eq 'websocket'
|| ($url->scheme || '') eq 'https';
my $clone = $url = $url->clone->userinfo(undef);
my $upgrade = lc($self->headers->upgrade || '');
my $scheme = $url->scheme || '';
$path = $clone unless $upgrade eq 'websocket' || $scheme eq 'https';
}

return "$method $path HTTP/@{[$self->version]}\x0d\x0a";
Expand All @@ -203,9 +200,8 @@ sub _parse_env {
my $headers = $self->headers;
my $url = $self->url;
my $base = $url->base;
for my $name (keys %$env) {
while (my ($name, $value) = each %$env) {
next unless $name =~ /^HTTP_/i;
my $value = $env->{$name};
$name =~ s/^HTTP_//i;
$name =~ s/_/-/g;
$headers->header($name, $value);
Expand Down Expand Up @@ -241,9 +237,7 @@ sub _parse_env {
$base->scheme('https') if $env->{HTTPS};

# Path
my $path = $url->path;
if (my $value = $env->{PATH_INFO}) { $path->parse($value) }
else { $path->parse('') }
my $path = $url->path->parse($env->{PATH_INFO} ? $env->{PATH_INFO} : '');

# Base path
if (my $value = $env->{SCRIPT_NAME}) {
Expand All @@ -270,10 +264,8 @@ sub _parse_start_line {
my $self = shift;

# Ignore any leading empty lines
my $line = get_line \$self->{buffer};
$line = get_line \$self->{buffer}
while ((defined $line) && ($line =~ m/^\s*$/));
return unless defined $line;
$self->{buffer} =~ s/^\s+//;
return unless defined(my $line = get_line \$self->{buffer});

# We have a (hopefully) full request line
return $self->error('Bad request start line', 400)
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Message/Response.pm
Expand Up @@ -83,7 +83,7 @@ sub cookies {
# Add cookies
for my $cookie (@_) {
$cookie = Mojo::Cookie::Response->new($cookie) if ref $cookie eq 'HASH';
$headers->add('Set-Cookie', "$cookie");
$headers->add('Set-Cookie' => "$cookie");
}

return $self;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Path.pm
Expand Up @@ -58,7 +58,7 @@ sub contains {
return unless $part eq $try;
}

return @$parts ? undef : 1;
return !@$parts;
}

sub merge {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Reactor/EV.pm
Expand Up @@ -11,7 +11,7 @@ sub DESTROY { undef $EV }
# We have to fall back to Mojo::Reactor::Poll, since EV is unique
sub new { $EV++ ? Mojo::Reactor::Poll->new : shift->SUPER::new }

sub is_running {EV::depth}
sub is_running { !!EV::depth }

sub one_tick { EV::run(EV::RUN_ONCE) }

Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Reactor/Poll.pm
Expand Up @@ -17,7 +17,7 @@ sub io {
return $self->watch($handle, 1, 1);
}

sub is_running { shift->{running} }
sub is_running { !!shift->{running} }

sub one_tick {
my $self = shift;
Expand Down Expand Up @@ -74,9 +74,9 @@ sub recurring { shift->_timer(1, @_) }

sub remove {
my ($self, $remove) = @_;
return delete shift->{timers}{shift()} unless ref $remove;
return !!delete shift->{timers}{shift()} unless ref $remove;
$self->_poll->remove($remove);
return delete $self->{io}{fileno $remove};
return !!delete $self->{io}{fileno $remove};
}

sub start {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server/Morbo.pm
Expand Up @@ -21,7 +21,7 @@ sub check_file {
my $cache = $self->{cache} ||= {};
my $stats = $cache->{$file} ||= [$^T, $size];
return if $mtime <= $stats->[0] && $size == $stats->[1];
return $cache->{$file} = [$mtime, $size];
return !!($cache->{$file} = [$mtime, $size]);
}

sub run {
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/Template.pm
Expand Up @@ -90,8 +90,7 @@ sub build {
}

# Multiline
$multi = ($line->[$j + 2] || '') eq 'text'
&& ($line->[$j + 3] || '') eq '' ? 0 : 1;
$multi = $line->[$j + 2] ~~ 'text' && $line->[$j + 3] ~~ '' ? 0 : 1;

# Append semicolon
$lines[-1] .= ';' if !$multi && !$cpst;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Transaction.pm
Expand Up @@ -28,7 +28,7 @@ sub error {
return $res->error ? $res->error : undef;
}

sub is_finished { (shift->{state} || '') eq 'finished' }
sub is_finished { shift->{state} ~~ 'finished' }

sub is_websocket {undef}

Expand Down Expand Up @@ -58,7 +58,7 @@ sub remote_address {

sub resume {
my $self = shift;
if (($self->{state} || '') eq 'paused') { $self->{state} = 'write_body' }
if ($self->{state} ~~ 'paused') { $self->{state} = 'write_body' }
elsif (!$self->is_writing) { $self->{state} = 'write' }
return $self->emit('resume');
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -26,7 +26,7 @@ sub client_read {
}

# Unexpected 100 Continue
if ($self->{state} eq 'finished' && ($res->code || '') eq '100') {
if ($self->{state} eq 'finished' && $res->code ~~ 100) {
$self->res($res->new);
$self->{state} = $preserved;
}
Expand Down Expand Up @@ -81,7 +81,7 @@ sub keep_alive {
return 1 if $req_conn eq 'keep-alive' || $res_conn eq 'keep-alive';

# No keep alive for 1.0
return $req->version eq '1.0' || $res->version eq '1.0' ? undef : 1;
return !($req->version eq '1.0' || $res->version eq '1.0');
}

sub server_leftovers {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -84,7 +84,7 @@ sub build_frame {
sub client_challenge {
my $self = shift;
return $self->_challenge($self->req->headers->sec_websocket_key) eq
$self->res->headers->sec_websocket_accept ? 1 : undef;
$self->res->headers->sec_websocket_accept;
}

sub client_handshake {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/URL.pm
Expand Up @@ -79,7 +79,7 @@ sub ihost {
$host;
}

sub is_abs { shift->scheme }
sub is_abs { !!shift->scheme }

sub parse {
my ($self, $url) = @_;
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -222,7 +222,7 @@ sub _connect_proxy {
my ($self, $tx) = @_;

# CONNECT failed
unless (($tx->res->code || '') eq '200') {
unless ($tx->res->code ~~ 200) {
$old->req->error('Proxy connection failed');
return $self->_finish($old, $cb);
}
Expand Down Expand Up @@ -391,7 +391,7 @@ sub _remove {

# Keep connection alive
$self->_cache(join(':', $self->transactor->endpoint($tx)), $id)
unless $tx->req->method eq 'CONNECT' && ($tx->res->code || '') eq '200';
unless $tx->req->method eq 'CONNECT' && $tx->res->code ~~ 200;
}

sub _redirect {
Expand Down Expand Up @@ -483,7 +483,7 @@ sub _upgrade {
my $c = $self->{connections}{$id};
my $old = $c->{tx};
return unless $old->req->headers->upgrade;
return unless ($old->res->code || '') eq '101';
return unless $old->res->code ~~ 101;

# Check challenge and upgrade to WebSocket transaction
my $new = Mojo::Transaction::WebSocket->new(handshake => $old, masked => 1);
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Util.pm
Expand Up @@ -258,7 +258,7 @@ sub secure_compare {
return if length $a != length $b;
my $r = 0;
$r |= ord(substr $a, $_) ^ ord(substr $b, $_) for 0 .. length($a) - 1;
return $r == 0 ? 1 : undef;
return $r == 0;
}

sub sha1_bytes { sha1(@_) }
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Command/get.pm
Expand Up @@ -135,8 +135,8 @@ sub run {

# JSON Pointer
return unless $selector;
return _json($buffer, $selector)
if ($tx->res->headers->content_type || '') =~ /json/i;
my $type = $tx->res->headers->content_type || '';
return _json($buffer, $selector) if $type =~ /json/i;

# Selector
_select($buffer, $selector, $charset // $tx->res->content->charset);
Expand Down

0 comments on commit fb9a1e2

Please sign in to comment.