Skip to content

Commit

Permalink
no more smartmatch
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 29, 2012
1 parent 73a3542 commit 392ac5b
Show file tree
Hide file tree
Showing 21 changed files with 71 additions and 62 deletions.
23 changes: 11 additions & 12 deletions lib/Mojo/Content.pm
Expand Up @@ -78,11 +78,11 @@ sub is_dynamic {
return $self->{dynamic} && !defined $self->headers->content_length;
}

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

sub is_multipart {undef}

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

sub leftovers { shift->{buffer} }

Expand Down Expand Up @@ -113,7 +113,7 @@ sub parse {
$self->{real_size} //= 0;
if ($self->is_chunked && $self->{state} ne 'headers') {
$self->_parse_chunked;
$self->{state} = 'finished' if $self->{chunked_state} ~~ 'finished';
$self->{state} = 'finished' if ($self->{chunk_state} // '') eq 'finished';
}

# Not chunked, pass through to second buffer
Expand Down Expand Up @@ -148,14 +148,13 @@ sub parse {
}

sub parse_body {
my $self = shift;
$self->{state} = 'body';
return $self->parse(@_);
shift->tap(sub { $_->{state} = 'body' })->parse(@_);
}

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

Expand Down Expand Up @@ -238,7 +237,7 @@ sub _parse_chunked {

# Trailing headers
return $self->_parse_chunked_trailing_headers
if $self->{chunked_state} ~~ 'trailing_headers';
if ($self->{chunk_state} // '') eq 'trailing_headers';

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

# Last chunk
if ($len == 0) {
$self->{chunked_state} = 'trailing_headers';
$self->{chunk_state} = 'trailing_headers';
last;
}

Expand All @@ -267,7 +266,7 @@ sub _parse_chunked {

# Trailing headers
$self->_parse_chunked_trailing_headers
if $self->{chunked_state} ~~ 'trailing_headers';
if ($self->{chunk_state} // '') eq 'trailing_headers';
}

sub _parse_chunked_trailing_headers {
Expand All @@ -279,7 +278,7 @@ sub _parse_chunked_trailing_headers {

# Check if we are finished
return unless $headers->is_finished;
$self->{chunked_state} = 'finished';
$self->{chunk_state} = 'finished';

# Replace Transfer-Encoding with Content-Length
my $encoding = $headers->transfer_encoding;
Expand Down Expand Up @@ -325,7 +324,7 @@ sub _parse_until_body {
}

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

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

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

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

# Body
elsif ($self->{multi_state} ~~ 'multipart_body') {
elsif (($self->{multi_state} // '') eq 'multipart_body') {
last unless $self->_parse_multipart_body($boundary);
}
}
Expand Down
10 changes: 5 additions & 5 deletions lib/Mojo/DOM/HTML.pm
Expand Up @@ -138,7 +138,7 @@ sub parse {
if (!$self->xml && $VOID{$start}) || $attr =~ m!/\s*$!;

# Relaxed "script" or "style"
if ($start ~~ [qw(script style)]) {
if (grep { $_ eq $start } qw(script style)) {
if ($html =~ m!\G(.*?)<\s*/\s*$start\s*>!gcsi) {
$self->_raw($1, \$current);
$self->_end($start, \$current);
Expand Down Expand Up @@ -338,27 +338,27 @@ sub _start {
elsif ($start eq 'option') { $self->_end('option', $current) }

# "<colgroup>", "<thead>", "tbody" and "tfoot"
elsif ($start ~~ [qw(colgroup thead tbody tfoot)]) {
elsif (grep { $_ eq $start } qw(colgroup thead tbody tfoot)) {
$self->_close($current);
}

# "<tr>"
elsif ($start eq 'tr') { $self->_close($current, {tr => 1}) }

# "<th>" and "<td>"
elsif ($start ~~ [qw(th td)]) {
elsif (grep { $_ eq $start } qw(th td)) {
$self->_close($current, {th => 1});
$self->_close($current, {td => 1});
}

# "<dt>" and "<dd>"
elsif ($start ~~ [qw(dt dd)]) {
elsif (grep { $_ eq $start } qw(dt dd)) {
$self->_end('dt', $current);
$self->_end('dd', $current);
}

# "<rt>" and "<rp>"
elsif ($start ~~ [qw(rt rp)]) {
elsif (grep { $_ eq $start } qw(rt rp)) {
$self->_end('rt', $current);
$self->_end('rp', $current);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Headers.pm
Expand Up @@ -77,7 +77,7 @@ sub header {
return @$headers;
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Home.pm
Expand Up @@ -61,7 +61,7 @@ sub list_files {
find {
wanted => sub {
my @parts = splitdir(abs2rel($File::Find::name, $dir));
push @files, join '/', @parts unless /^\./ ~~ \@parts;
push @files, join '/', @parts unless grep {/^\./} @parts;
},
no_chdir => 1
}, $dir;
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -89,10 +89,10 @@ sub _read {
unless (defined $read) {

# Retry
return if $! ~~ [EAGAIN, EINTR, EWOULDBLOCK];
return if grep { $_ == $! } EAGAIN, EINTR, EWOULDBLOCK;

# Closed
return $self->close if $! ~~ [ECONNRESET, EPIPE];
return $self->close if grep { $_ == $! } ECONNRESET, EPIPE;

# Read error
return $self->emit_safe(error => $!)->close;
Expand Down Expand Up @@ -134,10 +134,10 @@ sub _write {
unless (defined $written) {

# Retry
return if $! ~~ [EAGAIN, EINTR, EWOULDBLOCK];
return if grep { $_ == $! } EAGAIN, EINTR, EWOULDBLOCK;

# Closed
return $self->close if $! ~~ [ECONNRESET, EPIPE];
return $self->close if grep { $_ == $! } ECONNRESET, EPIPE;

# Write error
return $self->emit_safe(error => $!)->close;
Expand Down
9 changes: 6 additions & 3 deletions lib/Mojo/Message.pm
Expand Up @@ -172,9 +172,12 @@ sub headers { shift->content->headers }
sub is_chunked { shift->content->is_chunked }
sub is_dynamic { shift->content->is_dynamic }

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

sub is_limit_exceeded { (shift->error)[1] ~~ [413, 431] }
sub is_limit_exceeded {
return unless my $code = (shift->error)[1];
return !!grep { $_ eq $code } 413, 431;
}

sub is_multipart { shift->content->is_multipart }

Expand Down Expand Up @@ -215,7 +218,7 @@ sub parse {

# Content
$self->content($self->content->parse(delete $self->{buffer}))
if $self->{state} ~~ [qw(content finished)];
if grep { $_ eq ($self->{state} // '') } qw(content finished);

# Check line size
return $self->error('Maximum line size exceeded', 431)
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -137,7 +137,7 @@ sub get_start_line_chunk {

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

sub is_xhr {
Expand All @@ -159,7 +159,8 @@ sub parse {

# CGI like environment
$self->env($env)->_parse_env($env) if $env;
$self->content($self->content->parse_body(@args)) if $self->{state} ~~ 'cgi';
$self->content($self->content->parse_body(@args))
if ($self->{state} // '') eq 'cgi';

# Pass through
$self->SUPER::parse(@args);
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojo/Message/Response.pm
Expand Up @@ -132,7 +132,8 @@ sub get_start_line_chunk {

sub is_empty {
my $self = shift;
return $self->is_status_class(100) || $self->code ~~ [qw(204 304)];
return unless my $code = $self->code;
return $self->is_status_class(100) || grep { $_ eq $code } qw(204 304);
}

sub is_status_class {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Path.pm
Expand Up @@ -27,7 +27,7 @@ sub canonicalize {
}

# "."
next if $part ~~ ['.', ''];
next if grep { $_ eq $part } '.', '';

# Part
push @parts, $part;
Expand Down
7 changes: 4 additions & 3 deletions lib/Mojo/Template.pm
Expand Up @@ -72,7 +72,7 @@ sub build {
if ($type eq 'code' || $multi) { $lines[-1] .= "$value" }

# Expression
if ($type ~~ [qw(expr escp)]) {
if (grep { $_ eq $type } qw(expr escp)) {

# Start
unless ($multi) {
Expand All @@ -89,7 +89,8 @@ sub build {
}

# Multiline
$multi = !($line->[$j + 2] ~~ 'text' && $line->[$j + 3] ~~ '');
$multi = !(($line->[$j + 2] // '') eq 'text'
&& ($line->[$j + 3] // '') eq '');

# Append semicolon
$lines[-1] .= ';' if !$multi && !$cpst;
Expand Down Expand Up @@ -308,7 +309,7 @@ sub _trim {
for (my $j = @$line - 4; $j >= 0; $j -= 2) {

# Skip capture
next if $line->[$j] ~~ [qw(cpst cpen)];
next if grep { $_ eq $line->[$j] } qw(cpst cpen);

# Only trim text
return unless $line->[$j] eq 'text';
Expand Down
7 changes: 4 additions & 3 deletions lib/Mojo/Transaction.pm
Expand Up @@ -28,13 +28,14 @@ sub error {
return $res->error ? $res->error : undef;
}

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

sub is_websocket {undef}

sub is_writing {
return 1 unless my $state = shift->{state};
return $state ~~ [qw(write write_start_line write_headers write_body)];
return !!grep { $_ eq $state }
qw(write write_start_line write_headers write_body);
}

sub remote_address {
Expand All @@ -58,7 +59,7 @@ sub remote_address {

sub resume {
my $self = shift;
if ($self->{state} ~~ 'paused') { $self->{state} = 'write_body' }
if (($self->{state} // '') eq 'paused') { $self->{state} = 'write_body' }
elsif (!$self->is_writing) { $self->{state} = 'write' }
return $self->emit('resume');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -19,7 +19,7 @@ sub client_read {
$self->{state} = 'finished' if $res->is_finished;

# Unexpected 100 Continue
if ($self->{state} eq 'finished' && $res->code ~~ 100) {
if ($self->{state} eq 'finished' && ($res->code // '') eq '100') {
$self->res($res->new);
$self->{state} = $preserved;
}
Expand Down
11 changes: 6 additions & 5 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -215,7 +215,7 @@ sub _connect_proxy {
my ($self, $tx) = @_;

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

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

sub _redirect {
Expand Down Expand Up @@ -471,9 +471,10 @@ sub _upgrade {
my ($self, $id) = @_;

# Check if connection needs to be upgraded
my $c = $self->{connections}{$id};
my $old = $c->{tx};
return unless $old->req->headers->upgrade && $old->res->code ~~ 101;
my $c = $self->{connections}{$id};
my $old = $c->{tx};
my $code = $old->res->code // '';
return unless $old->req->headers->upgrade && $code eq '101';

# Check challenge and upgrade to WebSocket transaction
my $new = Mojo::Transaction::WebSocket->new(handshake => $old, masked => 1);
Expand Down
10 changes: 5 additions & 5 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -79,7 +79,7 @@ sub form {
my $req = $tx->req;
my $headers = $req->headers;
$headers->content_type('multipart/form-data') if $multipart;
if ($headers->content_type ~~ 'multipart/form-data') {
if (($headers->content_type // '') eq 'multipart/form-data') {
my $parts = $self->_multipart($encoding, $p->to_hash);
$req->content(
Mojo::Content::MultiPart->new(headers => $headers, parts => $parts));
Expand Down Expand Up @@ -132,8 +132,8 @@ sub redirect {

# Commonly used codes
my $res = $old->res;
my $code = $res->code || 0;
return unless $code ~~ [301, 302, 303, 307, 308];
my $code = $res->code // '';
return unless grep { $_ eq $code } 301, 302, 303, 307, 308;

# Fix broken location without authority and/or scheme
return unless my $location = $res->headers->location;
Expand All @@ -146,12 +146,12 @@ sub redirect {
# Clone request if necessary
my $new = Mojo::Transaction::HTTP->new;
my $method = $req->method;
if ($code ~~ [301, 307, 308]) {
if (grep { $_ eq $code } 301, 307, 308) {
return unless $req = $req->clone;
$new->req($req);
$req->headers->remove('Host')->remove('Cookie')->remove('Referer');
}
else { $method = 'GET' unless $method ~~ [qw(GET HEAD)] }
elsif ($method ne 'HEAD') { $method = 'GET' }
$new->req->method($method)->url($location);
return $new->previous($old);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojolicious/Command/get.pm
Expand Up @@ -140,7 +140,8 @@ sub _json {
my $json = Mojo::JSON->new;
return unless my $data = $json->decode(shift);
return unless defined($data = Mojo::JSON::Pointer->new->get($data, shift));
ref $data ~~ [qw(HASH ARRAY)] ? say($json->encode($data)) : _say($data);
return _say($data) unless ref $data eq 'HASH' || ref $data eq 'ARRAY';
say($json->encode($data));
}

sub _say {
Expand Down

0 comments on commit 392ac5b

Please sign in to comment.