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';
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; }
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;
}
# 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
sub _filter_component {
my ( $c, $comp, @args ) = @_;
- if(ref $comp eq 'CODE') {
+ if(is_plain_coderef($comp)) {
$comp = $comp->();
}
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};
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};
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} ) {
}
my @middleware = map {
- ref $_ eq 'CODE' ?
+ is_plain_coderef($_) ?
"Inline Coderef" :
(ref($_) .' '. ($_->can('VERSION') ? $_->VERSION || '' : '')
|| '') } $class->registered_middlewares;
$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/#(.+)$//) {
}
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 '');
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) }
: ()) ];
$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);
}
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 ) );
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 );
}
);
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 );
}
}
# 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});
}
}
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->{$_});
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;
use Moose::Role;
use Moose::Util::TypeConstraints ();
+use Ref::Util ();
requires 'match', 'match_captures', 'list_extra_info';
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}]);
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';
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 = +{ @_ };
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";
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 {
%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
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 );
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';
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
@{ $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');
}
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;
$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
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+
# 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;
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));
}
}
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;
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 {
# 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) {
use Catalyst::Exception;
use Catalyst::Request::PartData;
use Moose;
+use Ref::Util qw(is_plain_arrayref is_plain_hashref);
use namespace::clean -except => 'meta';
# 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];
}
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});
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];
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];
if ( exists $self->uploads->{$field} ) {
for ( $self->uploads->{$field} ) {
- $_ = [$_] unless ref($_) eq "ARRAY";
+ $_ = [$_] unless is_plain_arrayref($_);
push( @$_, $upload );
}
}
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($_);
}
# 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 {
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;
}
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';
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;
$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;
}
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) = @_;
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;
}
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) = @_;
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 );
}
use namespace::clean;
use Devel::InnerPackage;
use Moose::Util;
+use Ref::Util qw(is_plain_hashref);
=head1 NAME
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 }
use Test::More;
use Test::Fatal;
+use Ref::Util qw(is_plain_hashref);
+
use Catalyst::Script::FastCGI;
local our $fake_handler = \42;
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';
}