Use Ref::Util where appropriate ref-util
Dagfinn Ilmari Mannsåker [Mon, 23 Jan 2017 17:43:12 +0000 (17:43 +0000)]
- ref($foo) eq 'BAR' -> is_plain_barref($foo)
- $foo->$_isa('Regexp') -> is_regexpref($foo)

12 files changed:
Makefile.PL
lib/Catalyst.pm
lib/Catalyst/ActionRole/QueryMatching.pm
lib/Catalyst/Component.pm
lib/Catalyst/Controller.pm
lib/Catalyst/Dispatcher.pm
lib/Catalyst/Engine.pm
lib/Catalyst/Request.pm
lib/Catalyst/Response.pm
lib/Catalyst/Test.pm
lib/Catalyst/Utils.pm
t/aggregate/unit_core_script_fastcgi.t

index 68dd556..609bb9b 100644 (file)
@@ -86,6 +86,7 @@ requires "Plack::Middleware::HTTPExceptions";
 requires "Plack::Middleware::FixMissingBodyInRedirect" => '0.09';
 requires "Plack::Middleware::MethodOverride" => '0.12';
 requires "Plack::Middleware::RemoveRedundantBody" => '0.03';
+requires 'Ref::Util' => '0.010';
 
 test_requires 'Test::Fatal';
 test_requires 'Test::More' => '0.88';
index 178d502..7112a59 100644 (file)
@@ -52,6 +52,7 @@ use Plack::Util;
 use Class::Load 'load_class';
 use Encode 2.21 'decode_utf8', 'encode_utf8';
 use Scalar::Util;
+use Ref::Util qw(is_plain_arrayref is_plain_coderef is_plain_hashref is_plain_scalarref is_regexpref);
 
 BEGIN { require 5.008003; }
 
@@ -623,7 +624,7 @@ will be an empty arrayref.
 sub error {
     my $c = shift;
     if ( $_[0] ) {
-        my $error = ref $_[0] eq 'ARRAY' ? $_[0] : [@_];
+        my $error = is_plain_arrayref($_[0]) ? $_[0] : [@_];
         croak @$error unless ref $c;
         push @{ $c->{error} }, @$error;
     }
@@ -728,13 +729,13 @@ sub _comp_names_search_prefixes {
     # undef for a name will return all
     return keys %eligible if !defined $name;
 
-    my $query  = $name->$_isa('Regexp') ? $name : qr/^$name$/i;
+    my $query  = is_regexpref($name) ? $name : qr/^$name$/i;
     my @result = grep { $eligible{$_} =~ m{$query} } keys %eligible;
 
     return @result if @result;
 
     # if we were given a regexp to search against, we're done.
-    return if $name->$_isa('Regexp');
+    return if is_regexpref($name);
 
     # skip regexp fallback if configured
     return
@@ -795,7 +796,7 @@ sub _comp_names {
 sub _filter_component {
     my ( $c, $comp, @args ) = @_;
 
-    if(ref $comp eq 'CODE') {
+    if(is_plain_coderef($comp)) {
       $comp = $comp->();
     }
 
@@ -832,7 +833,7 @@ sub controller {
 
     my $appclass = ref($c) || $c;
     if( $name ) {
-        unless ( $name->$_isa('Regexp') ) { # Direct component hash lookup to avoid costly regexps
+        unless ( is_regexpref($name) ) { # Direct component hash lookup to avoid costly regexps
             my $comps = $c->components;
             my $check = $appclass."::Controller::".$name;
             return $c->_filter_component( $comps->{$check}, @args ) if exists $comps->{$check};
@@ -876,7 +877,7 @@ sub model {
     my ( $c, $name, @args ) = @_;
     my $appclass = ref($c) || $c;
     if( $name ) {
-        unless ( $name->$_isa('Regexp') ) { # Direct component hash lookup to avoid costly regexps
+        unless ( is_regexpref($name) ) { # Direct component hash lookup to avoid costly regexps
             my $comps = $c->components;
             my $check = $appclass."::Model::".$name;
             return $c->_filter_component( $comps->{$check}, @args ) if exists $comps->{$check};
@@ -940,7 +941,7 @@ sub view {
 
     my $appclass = ref($c) || $c;
     if( $name ) {
-        unless ( $name->$_isa('Regexp') ) { # Direct component hash lookup to avoid costly regexps
+        unless ( is_regexpref($name) ) { # Direct component hash lookup to avoid costly regexps
             my $comps = $c->components;
             my $check = $appclass."::View::".$name;
             if( exists $comps->{$check} ) {
@@ -1437,7 +1438,7 @@ EOF
         }
 
         my @middleware = map {
-          ref $_ eq 'CODE' ? 
+          is_plain_coderef($_) ?
             "Inline Coderef" : 
               (ref($_) .'  '. ($_->can('VERSION') ? $_->VERSION || '' : '') 
                 || '')  } $class->registered_middlewares;
@@ -1606,7 +1607,7 @@ sub uri_for {
         $path .= '/';
     }
 
-    my $fragment =  ((scalar(@args) && ref($args[-1]) eq 'SCALAR') ? pop @args : undef );
+    my $fragment =  ((scalar(@args) && is_plain_scalarref($args[-1])) ? pop @args : undef );
 
     unless(blessed $path) {
       if (defined($path) and $path =~ s/#(.+)$//)  {
@@ -1620,7 +1621,7 @@ sub uri_for {
     }
 
     my $params =
-      ( scalar @args && ref $args[$#args] eq 'HASH' ? pop @args : {} );
+      ( scalar @args && is_plain_hashref($args[$#args]) ? pop @args : {} );
 
     undef($path) if (defined $path && $path eq '');
 
@@ -1630,7 +1631,7 @@ sub uri_for {
     if ( $path->$_isa('Catalyst::Action') ) { # action object
         s|/|%2F|g for @args;
         my $captures = [ map { s|/|%2F|g; $_; }
-                        ( scalar @args && ref $args[0] eq 'ARRAY'
+                        ( scalar @args && is_plain_arrayref($args[0])
                          ? @{ shift(@args) }
                          : ()) ];
 
@@ -1722,7 +1723,7 @@ sub uri_for {
               $key =~ s/([^A-Za-z0-9\-_.!~*'() ])/$URI::Escape::escapes{$1}/go;
               $key =~ s/ /+/g;
 
-              "${key}=$param"; } ( ref $val eq 'ARRAY' ? @$val : $val ));
+              "${key}=$param"; } ( is_plain_arrayref($val) ? @$val : $val ));
       } @keys);
     }
 
@@ -2320,7 +2321,7 @@ sub finalize_encoding {
     if(
       ($res->encodable_response) and
       (defined($res->body)) and
-      (ref(\$res->body) eq 'SCALAR')
+      (is_plain_scalarref(\$res->body))
     ) {
         $c->res->body( $c->encoding->encode( $c->res->body, $c->_encode_check ) );
 
@@ -2743,7 +2744,7 @@ sub log_request_parameters {
         for my $key ( sort keys %$params ) {
             my $param = $params->{$key};
             my $value = defined($param) ? $param : '';
-            $t->row( $key, ref $value eq 'ARRAY' ? ( join ', ', @$value ) : $value );
+            $t->row( $key, is_plain_arrayref($value) ? ( join ', ', @$value ) : $value );
         }
         $c->log->debug( ucfirst($type) . " Parameters are:\n" . $t->draw );
     }
@@ -2771,7 +2772,7 @@ sub log_request_uploads {
         );
         for my $key ( sort keys %$uploads ) {
             my $upload = $uploads->{$key};
-            for my $u ( ref $upload eq 'ARRAY' ? @{$upload} : ($upload) ) {
+            for my $u ( is_plain_arrayref($upload) ? @{$upload} : ($upload) ) {
                 $t->row( $key, $u->filename, $u->type, $u->size );
             }
         }
@@ -3031,7 +3032,7 @@ sub setup_components {
     # All components are registered, now we need to 'init' them.
     foreach my $component_name (@comps, @injected) {
       $class->components->{$component_name} = $class->components->{$component_name}->() if
-        (ref($class->components->{$component_name}) || '') eq 'CODE';
+        is_plain_coderef($class->components->{$component_name});
     }
 }
 
@@ -3629,13 +3630,13 @@ sub _handle_unicode_decoding {
     return unless defined $value;
 
     ## I think this mess is to support the old nested
-    if ( ref $value eq 'ARRAY' ) {
+    if ( is_plain_arrayref($value) ) {
         foreach ( @$value ) {
             $_ = $self->_handle_unicode_decoding($_);
         }
         return $value;
     }
-    elsif ( ref $value eq 'HASH' ) {
+    elsif ( is_plain_hashref($value) ) {
         foreach (keys %$value) {
             my $encoded_key = $self->_handle_param_unicode_decoding($_);
             $value->{$encoded_key} = $self->_handle_unicode_decoding($value->{$_});
@@ -3968,9 +3969,9 @@ sub setup_middleware {
         if(ref $next) {
             if(Scalar::Util::blessed $next && $next->can('wrap')) {
                 push @middleware, $next;
-            } elsif(ref $next eq 'CODE') {
+            } elsif(is_plain_coderef($next)) {
                 push @middleware, $next;
-            } elsif(ref $next eq 'HASH') {
+            } elsif(is_plain_hashref($next)) {
                 my $namespace = shift @middleware_definitions;
                 my $mw = $class->Catalyst::Utils::build_middleware($namespace, %$next);
                 push @middleware, $mw;
index 955258b..83b8341 100644 (file)
@@ -2,6 +2,7 @@ package Catalyst::ActionRole::QueryMatching;
 
 use Moose::Role;
 use Moose::Util::TypeConstraints ();
+use Ref::Util ();
 
 requires 'match', 'match_captures', 'list_extra_info';
 
@@ -65,7 +66,7 @@ has query_constraints => (
 around ['match','match_captures'] => sub {
     my ($orig, $self, $c, @args) = @_;
     my $tc = $self->query_constraints;
-    if(ref $tc eq 'HASH') {
+    if(Ref::Util::is_plain_hashref($tc)) {
       # Do the key names match, unless slurpy?
       unless($self->is_slurpy) {
         return 0 unless $self->_compare_arrays([sort keys %$tc],[sort keys %{$c->req->query_parameters}]);
index 55f25c0..8fe109f 100644 (file)
@@ -11,6 +11,7 @@ use mro 'c3';
 use Scalar::Util 'blessed';
 use Class::Load 'is_class_loaded';
 use Moose::Util 'find_meta';
+use Ref::Util qw(is_plain_hashref);
 use namespace::clean -except => 'meta';
 
 with 'MooseX::Emulate::Class::Accessor::Fast';
@@ -91,12 +92,12 @@ sub BUILDARGS {
     my $args = {};
 
     if (@_ == 1) {
-        $args = $_[0] if ref($_[0]) eq 'HASH';
+        $args = $_[0] if is_plain_hashref($_[0]);
     } elsif (@_ == 2) { # is it ($app, $args) or foo => 'bar' ?
         if (blessed($_[0])) {
-            $args = $_[1] if ref($_[1]) eq 'HASH';
+            $args = $_[1] if is_plain_hashref($_[1]);
         } elsif (is_class_loaded($_[0]) &&
-                $_[0]->isa('Catalyst') && ref($_[1]) eq 'HASH') {
+                $_[0]->isa('Catalyst') && is_plain_hashref($_[1])) {
             $args = $_[1];
         } else {
             $args = +{ @_ };
@@ -112,7 +113,7 @@ sub COMPONENT {
     my ( $class, $c ) = @_;
 
     # Temporary fix, some components does not pass context to constructor
-    my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
+    my $arguments = is_plain_hashref($_[-1]) ? $_[-1] : {};
     if ( my $next = $class->next::can ) {
       my ($next_package) = Class::MOP::get_code_info($next);
       warn "There is a COMPONENT method resolving after Catalyst::Component in ${next_package}.\n";
index fbab60f..653363f 100644 (file)
@@ -7,6 +7,7 @@ use String::RewritePrefix;
 use Moose::Util qw/find_meta/;
 use List::Util qw/first/;
 use List::MoreUtils qw/uniq/;
+use Ref::Util qw(is_plain_arrayref);
 use namespace::clean -except => 'meta';
 
 BEGIN {
@@ -429,7 +430,7 @@ sub _parse_attrs {
         %raw_attributes,
         # Note we deep copy array refs here to stop crapping on config
         # when attributes are parsed. RT#65463
-        exists $actions_config->{$name} ? map { ref($_) eq 'ARRAY' ? [ @$_ ] : $_ } %{ $actions_config->{$name } } : (),
+        exists $actions_config->{$name} ? map { is_plain_arrayref($_) ? [ @$_ ] : $_ } %{ $actions_config->{$name } } : (),
     );
 
     # Private actions with additional attributes will raise a warning and then
@@ -455,7 +456,7 @@ sub _parse_attr {
     my ($self, $c, $name, $key, $values) = @_;
 
     my %final_attributes;
-    foreach my $value (ref($values) eq 'ARRAY' ? @$values : $values) {
+    foreach my $value (is_plain_arrayref($values) ? @$values : $values) {
         my $meth = "_parse_${key}_attr";
         if ( my $code = $self->can($meth) ) {
             my %new_attrs = $self->$code( $c, $name, $value );
index cf98256..a2a4e1c 100644 (file)
@@ -16,6 +16,7 @@ use Tree::Simple;
 use Tree::Simple::Visitor::FindByPath;
 use Class::Load qw(load_class try_load_class);
 use Encode 2.21 'decode_utf8';
+use Ref::Util qw(is_plain_arrayref is_plain_coderef);
 
 use namespace::clean -except => 'meta';
 
@@ -135,11 +136,11 @@ sub _command2action {
 
     my (@args, @captures);
 
-    if ( ref( $extra_params[-2] ) eq 'ARRAY' ) {
+    if ( is_plain_arrayref($extra_params[-2]) ) {
         @captures = @{ splice @extra_params, -2, 1 };
     }
 
-    if ( ref( $extra_params[-1] ) eq 'ARRAY' ) {
+    if ( is_plain_arrayref($extra_params[-1]) ) {
         @args = @{ pop @extra_params }
     } else {
         # this is a copy, it may take some abuse from
@@ -631,7 +632,7 @@ sub setup_actions {
     @{ $self->_registered_dispatch_types }{@classes} = (1) x @classes;
 
     foreach my $comp ( map @{$_}{sort keys %$_}, $c->components ) {
-        $comp = $comp->() if ref($comp) eq 'CODE';
+        $comp = $comp->() if is_plain_coderef($comp);
         $comp->register_actions($c) if $comp->can('register_actions');
     }
 
index 150c269..5c51dcb 100644 (file)
@@ -13,6 +13,7 @@ use Catalyst::EngineLoader;
 use Encode 2.21 'decode_utf8', 'encode', 'decode';
 use Plack::Request::Upload;
 use Hash::MultiValue;
+use Ref::Util qw(is_plain_arrayref is_plain_globref is_plain_hashref);
 use namespace::clean -except => 'meta';
 use utf8;
 
@@ -112,7 +113,7 @@ sub finalize_body {
                     $body = ["$body"]; 
                 }
             } elsif(ref $body) {
-                if( (ref($body) eq 'GLOB') or (ref($body) eq 'ARRAY')) {
+                if( (is_plain_globref($body)) or (is_plain_arrayref($body))) {
                   # Again, PSGI can just accept this, no transform needed.  We don't officially
                   # document the body as arrayref at this time (and there's not specific test
                   # cases.  we support it because it simplifies some plack compatibility logic
@@ -144,7 +145,7 @@ sub finalize_body {
 
         if(my $body = $res->body) {
 
-          if ( blessed($body) && $body->can('read') or ref($body) eq 'GLOB' ) {
+          if ( blessed($body) && $body->can('read') or is_plain_globref($body) ) {
 
               ## In this case we have no choice and will fall back on the old
               ## manual streaming stuff.  Not optimal.  This is deprecated as of 5.900560+
@@ -233,7 +234,7 @@ sub _dump_error_page_element {
     # This is fugly, but the metaclass is _HUGE_ and demands waaay too much
     # scrolling. Suggestions for more pleasant ways to do this welcome.
     local $val->{'__MOP__'} = "Stringified: "
-        . $val->{'__MOP__'} if ref $val eq 'HASH' && exists $val->{'__MOP__'};
+        . $val->{'__MOP__'} if is_plain_hashref($val) && exists $val->{'__MOP__'};
 
     my $text = encode_entities( dump( $val ));
     sprintf <<"EOF", $name, $text;
@@ -443,7 +444,7 @@ sub finalize_uploads {
     foreach my $key (keys %{ $request->uploads }) {
         my $upload = $request->uploads->{$key};
         unlink grep { -e $_ } map { $_->tempname }
-          (ref $upload eq 'ARRAY' ? @{$upload} : ($upload));
+          (is_plain_arrayref($upload) ? @{$upload} : ($upload));
     }
 
 }
@@ -657,7 +658,7 @@ sub prepare_uploads {
         my $files = $uploads->{$name};
         $name = $c->_handle_unicode_decoding($name) if $enc;
         my @uploads;
-        for my $upload (ref $files eq 'ARRAY' ? @$files : ($files)) {
+        for my $upload (is_plain_arrayref($files) ? @$files : ($files)) {
             my $headers = HTTP::Headers->new( %{ $upload->{headers} } );
             my $filename = $upload->{filename};
             $filename = $c->_handle_unicode_decoding($filename) if $enc;
@@ -679,7 +680,7 @@ sub prepare_uploads {
         my @filenames = map { $_->{filename} } @uploads;
         # append, if there's already params with this name
         if (exists $parameters->{$name}) {
-            if (ref $parameters->{$name} eq 'ARRAY') {
+            if (is_plain_arrayref($parameters->{$name})) {
                 push @{ $parameters->{$name} }, @filenames;
             }
             else {
@@ -764,7 +765,7 @@ sub run {
     # FIXME - we should stash the options in an attribute so that custom args
     # like Gitalist's --git_dir are possible to get from the app without stupid tricks.
     my $server = pop @args if (scalar @args && blessed $args[-1]);
-    my $options = pop @args if (scalar @args && ref($args[-1]) eq 'HASH');
+    my $options = pop @args if (scalar @args && is_plain_hashref($args[-1]));
     # Back compat hack for applications with old (non Catalyst::Script) scripts to work in FCGI.
     if (scalar @args && !ref($args[0])) {
         if (my $listen = shift @args) {
index 1306b94..57b328c 100644 (file)
@@ -14,6 +14,7 @@ use HTTP::Body;
 use Catalyst::Exception;
 use Catalyst::Request::PartData;
 use Moose;
+use Ref::Util qw(is_plain_arrayref is_plain_hashref);
 
 use namespace::clean -except => 'meta';
 
@@ -224,15 +225,15 @@ sub _build_parameters {
     # We copy, no references
     foreach my $name (keys %$query_parameters) {
         my $param = $query_parameters->{$name};
-        $parameters->{$name} = ref $param eq 'ARRAY' ? [ @$param ] : $param;
+        $parameters->{$name} = is_plain_arrayref($param) ? [ @$param ] : $param;
     }
 
     # Merge query and body parameters
     foreach my $name (keys %$body_parameters) {
         my $param = $body_parameters->{$name};
-        my @values = ref $param eq 'ARRAY' ? @$param : ($param);
+        my @values = is_plain_arrayref($param) ? @$param : ($param);
         if ( my $existing = $parameters->{$name} ) {
-          unshift(@values, (ref $existing eq 'ARRAY' ? @$existing : $existing));
+          unshift(@values, (is_plain_arrayref($existing) ? @$existing : $existing));
         }
         $parameters->{$name} = @values > 1 ? \@values : $values[0];
     }
@@ -337,7 +338,7 @@ sub prepare_body_parameters {
     if(scalar %part_data && !$c->config->{skip_complex_post_part_handling}) {
       foreach my $key (keys %part_data) {
         my $proto_value = $part_data{$key};
-        my ($val, @extra) = (ref($proto_value)||'') eq 'ARRAY' ? @$proto_value : ($proto_value);
+        my ($val, @extra) = is_plain_arrayref($proto_value) ? @$proto_value : ($proto_value);
 
         $key = $c->_handle_param_unicode_decoding($key)
           if ($c and $c->encoding and !$c->config->{skip_body_param_unicode_decoding});
@@ -783,7 +784,7 @@ sub param {
             return wantarray ? () : undef;
         }
 
-        if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
+        if ( is_plain_arrayref($self->parameters->{$param}) ) {
             return (wantarray)
               ? @{ $self->parameters->{$param} }
               : $self->parameters->{$param}->[0];
@@ -925,7 +926,7 @@ sub upload {
             return wantarray ? () : undef;
         }
 
-        if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
+        if ( is_plain_arrayref($self->uploads->{$upload}) ) {
             return (wantarray)
               ? @{ $self->uploads->{$upload} }
               : $self->uploads->{$upload}->[0];
@@ -943,7 +944,7 @@ sub upload {
 
             if ( exists $self->uploads->{$field} ) {
                 for ( $self->uploads->{$field} ) {
-                    $_ = [$_] unless ref($_) eq "ARRAY";
+                    $_ = [$_] unless is_plain_arrayref($_);
                     push( @$_, $upload );
                 }
             }
@@ -999,7 +1000,7 @@ sub mangle_params {
 
     foreach my $value ( values %$args ) {
         next unless defined $value;
-        for ( ref $value eq 'ARRAY' ? @$value : $value ) {
+        for ( is_plain_arrayref($value) ? @$value : $value ) {
             $_ = "$_";
             #      utf8::encode($_);
         }
@@ -1016,8 +1017,8 @@ sub mangle_params {
                 # an existing one regardless if the existing value is an array
                 # or not, and regardless if the new value is an array or not
                 $params{$key} = [
-                    ref($params{$key}) eq 'ARRAY' ? @{ $params{$key} } : $params{$key},
-                    ref($val) eq 'ARRAY' ? @{ $val } : $val
+                    is_plain_arrayref($params{$key}) ? @{ $params{$key} } : $params{$key},
+                    is_plain_arrayref($val) ? @{ $val } : $val
                 ];
 
             } else {
@@ -1056,7 +1057,7 @@ sub uri_with {
     carp( 'No arguments passed to uri_with()' ) unless $args;
 
     my $append = 0;
-    if((ref($behavior) eq 'HASH') && defined($behavior->{mode}) && ($behavior->{mode} eq 'append')) {
+    if(is_plain_hashref($behavior) && defined($behavior->{mode}) && ($behavior->{mode} eq 'append')) {
         $append = 1;
     }
 
index 94ee3f3..e558e4d 100644 (file)
@@ -7,6 +7,7 @@ use namespace::autoclean;
 use Scalar::Util 'blessed';
 use Catalyst::Response::Writer;
 use Catalyst::Utils ();
+use Ref::Util qw(is_plain_arrayref is_plain_coderef);
 
 with 'MooseX::Emulate::Class::Accessor::Fast';
 
@@ -170,15 +171,15 @@ sub from_psgi_response {
     if(blessed($psgi_res) && $psgi_res->can('as_psgi')) {
       $psgi_res = $psgi_res->as_psgi;
     }
-    if(ref $psgi_res eq 'ARRAY') {
+    if(is_plain_arrayref($psgi_res)) {
         my ($status, $headers, $body) = @$psgi_res;
         $self->status($status);
         $self->headers(HTTP::Headers->new(@$headers));
         # Can be arrayref or filehandle...
         if(defined $body) { # probably paranoia
-          ref $body eq 'ARRAY' ? $self->body(join('', @$body)) : $self->body($body);
+          is_plain_arrayref($body) ? $self->body(join('', @$body)) : $self->body($body);
         }
-    } elsif(ref $psgi_res eq 'CODE') {
+    } elsif(is_plain_coderef($psgi_res)) {
         $psgi_res->(sub {
             my $response = shift;
             my ($status, $headers, $maybe_body) = @$response;
@@ -186,7 +187,7 @@ sub from_psgi_response {
             $self->headers(HTTP::Headers->new(@$headers));
             if(defined $maybe_body) {
                 # Can be arrayref or filehandle...
-                ref $maybe_body eq 'ARRAY' ? $self->body(join('', @$maybe_body)) : $self->body($maybe_body);
+                is_plain_arrayref($maybe_body) ? $self->body(join('', @$maybe_body)) : $self->body($maybe_body);
             } else {
                 return $self->write_fh;
             }
index e121b35..1fd6a76 100644 (file)
@@ -11,6 +11,7 @@ use Class::Load qw(load_class is_class_loaded);
 use Sub::Exporter;
 use Moose::Util 'find_meta';
 use Carp 'croak', 'carp';
+use Ref::Util qw(is_plain_coderef is_plain_hashref);
 
 sub _build_request_export {
     my ($self, $args) = @_;
@@ -153,7 +154,7 @@ say: use Catalyst::Test (); # If you don't want to import a test app right now.
 or say: use Catalyst::Test 'MyApp'; # If you do want to import a test app.\n\n})
         unless $class;
         $import->($self, '-all' => { class => $class });
-        $opts = {} unless ref $opts eq 'HASH';
+        $opts = {} unless is_plain_hashref($opts);
         $default_host = $opts->{default_host} if exists $opts->{default_host};
         return 1;
     }
@@ -290,7 +291,7 @@ sub _local_request {
     my $class = shift;
 
     return _request({
-        app => ref($class) eq "CODE" ? $class : $class->_finalized_psgi_app,
+        app => is_plain_coderef($class) ? $class : $class->_finalized_psgi_app,
         mangle_response => sub {
             my ($resp) = @_;
 
@@ -405,7 +406,7 @@ sub _customize_request {
     my $request = shift;
     my $extra_env = shift;
     my $opts = pop(@_) || {};
-    $opts = {} unless ref($opts) eq 'HASH';
+    $opts = {} unless is_plain_hashref($opts);
     if ( my $host = exists $opts->{host} ? $opts->{host} : $default_host  ) {
         $request->header( 'Host' => $host );
     }
index 9fb1e92..9cb0eb6 100644 (file)
@@ -13,6 +13,7 @@ use Class::Load ();
 use namespace::clean;
 use Devel::InnerPackage;
 use Moose::Util;
+use Ref::Util qw(is_plain_hashref);
 
 =head1 NAME
 
@@ -331,8 +332,8 @@ sub merge_hashes {
 
     my %merged = %$lefthash;
     for my $key ( keys %$righthash ) {
-        my $right_ref = ( ref $righthash->{ $key } || '' ) eq 'HASH';
-        my $left_ref  = ( ( exists $lefthash->{ $key } && ref $lefthash->{ $key } ) || '' ) eq 'HASH';
+        my $right_ref = is_plain_hashref( $righthash->{ $key } );
+        my $left_ref  = exists $lefthash->{ $key } && is_plain_hashref( $lefthash->{ $key } );
         if( $right_ref and $left_ref ) {
             $merged{ $key } = merge_hashes(
                 $lefthash->{ $key }, $righthash->{ $key }
index 80a8068..5a2c498 100644 (file)
@@ -7,6 +7,8 @@ use lib "$Bin/../lib";
 use Test::More;
 use Test::Fatal;
 
+use Ref::Util qw(is_plain_hashref);
+
 use Catalyst::Script::FastCGI;
 
 local our $fake_handler = \42;
@@ -41,7 +43,7 @@ sub testOption {
     my $server = pop @TestAppToTestScripts::RUN_ARGS;
     is $server, $fake_handler, 'Loaded Plack handler gets passed to the app';
 
-    if (scalar(@TestAppToTestScripts::RUN_ARGS) && ref($TestAppToTestScripts::RUN_ARGS[-1]) eq "HASH") {
+    if (scalar(@TestAppToTestScripts::RUN_ARGS) && is_plain_hashref($TestAppToTestScripts::RUN_ARGS[-1])) {
         is ref(delete($TestAppToTestScripts::RUN_ARGS[-1]->{argv})), 'ARRAY';
         is ref(delete($TestAppToTestScripts::RUN_ARGS[-1]->{extra_argv})), 'ARRAY';
     }