From: Karen Etheridge Date: Tue, 3 Dec 2013 22:31:25 +0000 (-0800) Subject: also handle undef inputs X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0b257d229cde83e86ef137501d73073e1a1a2e0e;p=p5sagit%2FSafe-Isa.git also handle undef inputs --- diff --git a/lib/Safe/Isa.pm b/lib/Safe/Isa.pm index 2d6c675..7155c84 100644 --- a/lib/Safe/Isa.pm +++ b/lib/Safe/Isa.pm @@ -17,7 +17,7 @@ our $_call_if_object = sub { our $_call_if_object_or_classname = sub { my ($thing, $method) = (shift, shift); - return unless blessed($thing) or do { + return unless blessed($thing) or defined($thing) and do { no strict 'refs'; %{"main::${thing}::"} }; diff --git a/t/safe_isa.t b/t/safe_isa.t index 0fa9d37..54e57c3 100644 --- a/t/safe_isa.t +++ b/t/safe_isa.t @@ -10,6 +10,7 @@ my $bar = Bar->new; my $blam = [ 42 ]; my $class = 'Bar'; my $blam2 = ''; +my $undef; # basic isa usage - @@ -18,12 +19,14 @@ ok($bar->isa('Foo'), 'bar isa Foo'); ok($class->isa('Foo'), 'class name isa Foo'); ok(!eval { $blam->isa('Foo'); 1 }, 'blam goes blam with isa'); ok(!eval { $blam2->isa('Foo'); 1 }, 'blam2 goes blam with isa'); +ok(!eval { $undef->isa('Foo'); 1 }, 'undef goes blam with isa'); ok(!$foo->can('bar'), 'foo !can bar'); ok($bar->can('bar'), 'bar can bar'); ok($class->can('bar'), 'our class name can bar'); ok(!eval { $blam->can('bar'); 1 }, 'blam goes blam with can'); ok(!eval { $blam2->can('bar'); 1 }, 'blam2 goes blam with can'); +ok(!eval { $undef->can('bar'); 1 }, 'undef goes blam with can'); use Safe::Isa; @@ -32,12 +35,14 @@ ok($bar->$_isa('Foo'), 'bar $_isa Foo'); ok($class->$_isa('Foo'), 'class name $_isa Foo'); ok(eval { $blam->$_isa('Foo'); 1 }, 'no boom today with isa'); ok(eval { $blam2->$_isa('Foo'); 1 }, 'no boom today with isa'); +ok(eval { $undef->$_isa('Foo'); 1 }, 'no boom today with isa'); ok(!$foo->$_can('bar'), 'foo !$_can bar'); ok($bar->$_can('bar'), 'bar $_can bar'); ok($class->$_can('bar'), 'class name $_can bar'); ok(eval { $blam->$_can('bar'); 1 }, 'no boom today with can'); ok(eval { $blam2->$_can('bar'); 1 }, 'no boom today with can'); +ok(eval { $undef->$_can('bar'); 1 }, 'no boom today with can'); ok($foo->$_call_if_object(isa => 'Foo'), 'foo $_call_if_object(isa => Foo)'); ok($bar->$_call_if_object(isa => 'Foo'), 'bar $_call_if_object(isa => Foo)'); @@ -48,3 +53,4 @@ ok($bar->$_call_if_object_or_classname(isa => 'Foo'), 'bar $_call_if_object_or_c ok($class->$_call_if_object_or_classname(isa => 'Foo'), 'class name $_call_if_object_or_classname(isa => Foo)'); ok(eval { $blam->$_call_if_object_or_classname(isa => 'Foo'); 1 }, 'no boom today with _call_if_object_or_classname'); ok(eval { $blam2->$_call_if_object_or_classname(isa => 'Foo'); 1 }, 'no boom today with _call_if_object_or_classname'); +ok(eval { $undef->$_call_if_object_or_classname(isa => 'Foo'); 1 }, 'no boom today with _call_if_object_or_classname');