our $VERSION = '1.000004';
-our @EXPORT = qw($_call_if_object $_isa $_can $_does $_DOES);
+our @EXPORT = qw($_call_if_object $_call_if_object_or_classname $_isa $_can $_does $_DOES);
our $_call_if_object = sub {
my ($obj, $method) = (shift, shift);
return $obj->$method(@_);
};
+our $_call_if_object_or_classname = sub {
+ my ($thing, $method) = (shift, shift);
+ return unless blessed($thing) or do {
+ no strict 'refs';
+ %{"main::${thing}::"}
+ };
+ return $thing->$method(@_);
+};
+
our ($_isa, $_can, $_does, $_DOES) = map {
my $method = $_;
- sub { my $obj = shift; $obj->$_call_if_object($method => @_) }
+ sub { my $obj = shift; $obj->$_call_if_object_or_classname($method => @_) }
} qw(isa can does DOES);
=head1 NAME
-Safe::Isa - Call isa, can, does and DOES safely on things that may not be objects
+Safe::Isa - Call isa, can, does and DOES safely on arbitrary things
=head1 SYNOPSIS
my $foo = Foo->new;
my $bar = Bar->new;
+ my $class = 'Bar';
my $blam = [ 42 ];
# basic isa usage -
$foo->isa('Foo'); # true
$bar->isa('Foo'); # true
+ $class->isa('Foo') # true
$blam->isa('Foo'); # BOOM
$foo->can('bar'); # false
$bar->can('bar'); # true
+ $class->can('bar); # true
$blam->can('bar'); # BOOM
# Safe::Isa usage -
$foo->$_isa('Foo'); # true
$bar->$_isa('Foo'); # true
+ $class->isa('Foo') # true
$blam->$_isa('Foo'); # false, no boom today
$foo->$_can('bar'); # false
$bar->$_can('bar'); # true
+ $class->can('bar); # true
$blam->$_can('bar'); # false, no boom today
Similarly:
$maybe_an_object->$_call_if_object(name => @args);
+or this might be closer to what you want:
+
+ $maybe_a_referent->$_call_if_object_or_class(name => @args);
+
Or to re-use a previous example for purposes of explication:
$foo->$_call_if_object(isa => 'Foo'); # true
use strict;
use warnings FATAL => 'all';
-use Test::More tests => 15;
+use Test::More tests => 28;
{ package Foo; sub new { bless({}, $_[0]) } }
{ package Bar; our @ISA = qw(Foo); sub bar { 1 } }
my $foo = Foo->new;
my $bar = Bar->new;
my $blam = [ 42 ];
+my $class = 'Bar';
+my $blam2 = '';
# basic isa usage -
ok($foo->isa('Foo'), 'foo isa Foo');
ok($bar->isa('Foo'), 'bar isa Foo');
-ok(!eval { $blam->isa('Foo'); 1 }, 'blam goes blam');
+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(!$foo->can('bar'), 'foo !can bar');
ok($bar->can('bar'), 'bar can bar');
-ok(!eval { $blam->can('bar'); 1 }, 'blam goes blam');
+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');
use Safe::Isa;
ok($foo->$_isa('Foo'), 'foo $_isa Foo');
ok($bar->$_isa('Foo'), 'bar $_isa Foo');
-ok(eval { $blam->$_isa('Foo'); 1 }, 'no boom today');
+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(!$foo->$_can('bar'), 'foo !$_can bar');
ok($bar->$_can('bar'), 'bar $_can bar');
-ok(eval { $blam->$_can('bar'); 1 }, 'no boom today');
+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($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)');
-ok(eval { $blam->$_call_if_object(isa => 'Foo'); 1 }, 'no boom today');
+ok(eval { $blam->$_call_if_object(isa => 'Foo'); 1 }, 'no boom today with _call_if_object');
+
+ok($foo->$_call_if_object_or_classname(isa => 'Foo'), 'foo $_call_if_object_or_classname(isa => Foo)');
+ok($bar->$_call_if_object_or_classname(isa => 'Foo'), 'bar $_call_if_object_or_classname(isa => Foo)');
+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');