diff --git a/lib/Mo.pm b/lib/Mo.pm index cbc5764e..ba96a512 100644 --- a/lib/Mo.pm +++ b/lib/Mo.pm @@ -41,19 +41,19 @@ use strict; use warnings qw( FATAL all ); use Carp (); -use Scalar::Util (); +use Scalar::Util qw(looks_like_number blessed); # Basic types for isa. If you want a new type, either add it here, # or give isa a coderef. + our %TYPES = ( - Bool => sub { !$_[0] || (defined $_[0] && &Scalar::Util::looks_like_number && $_[0] == 1) }, - Num => sub { defined $_[0] && &Scalar::Util::looks_like_number }, - Int => sub { defined $_[0] && &Scalar::Util::looks_like_number && $_[0] == int $_[0] }, + Bool => sub { !$_[0] || (defined $_[0] && looks_like_number($_[0]) && $_[0] == 1) }, + Num => sub { defined $_[0] && looks_like_number($_[0]) }, + Int => sub { defined $_[0] && looks_like_number($_[0]) && $_[0] == int($_[0]) }, Str => sub { defined $_[0] }, - Object => sub { defined $_[0] && &Scalar::Util::blessed }, + Object => sub { defined $_[0] && blessed($_[0]) }, FileHandle => sub { local $@; require IO::Handle; fileno($_[0]) && $_[0]->opened }, - # Ref types: map { my $type = /R/ ? $_ : uc $_; $_ . "Ref" => sub { ref $_[0] eq $type } @@ -245,7 +245,7 @@ sub Mo::import { $method = sub { if ( $#_ ) { Carp::confess(qq) - unless blessed($_[1]) && $_[1]->does($role) + unless Scalar::Util::blessed($_[1]) && eval { $_[1]->does($role) } } goto &$original_method }; diff --git a/t/lib/Mo/isa.t b/t/lib/Mo/isa.t index 39e4e19c..9d73c75f 100644 --- a/t/lib/Mo/isa.t +++ b/t/lib/Mo/isa.t @@ -9,6 +9,7 @@ BEGIN { use strict; use warnings FATAL => 'all'; use English qw(-no_match_vars); +use PerconaTest (); use Test::More; sub dies_ok (&;$) { @@ -182,4 +183,19 @@ for my $type (@types[1..$#types]) { my $method = "my$type"; dies_ok { $foo->$method(undef) } "$type attr set to undef dies" } + +use Config; +use File::Spec; +use IPC::Cmd (); +my $thisperl = $^X; +if ($^O ne 'VMS') + {$thisperl .= $Config{_exe} unless $thisperl =~ m/$Config{_exe}$/i;} + +my $pm_test = File::Spec->catfile($PerconaTest::trunk, qw(t lib Mo isa_subtest.pm)); + +ok( + scalar(IPC::Cmd::run(command => [$thisperl, $pm_test])), + "Mo types work with Scalar::Util::PP", +); + done_testing; diff --git a/t/lib/Mo/isa_subtest.pm b/t/lib/Mo/isa_subtest.pm new file mode 100644 index 00000000..1f331b93 --- /dev/null +++ b/t/lib/Mo/isa_subtest.pm @@ -0,0 +1,28 @@ +BEGIN { + require Scalar::Util::PP; + *Scalar::Util:: = \*Scalar::Util::PP::; + $INC{"Scalar/Util.pm"} = __FILE__; +}; + +BEGIN { + die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n" + unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH}; + unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib"; +}; + +use strict; +use warnings; + +{ + package isa_subtest; + use Mo; + + has attr => ( + is => 'rw', + isa => 'Int', + ); + + 1; +} + +isa_subtest->new(attr => 100);