4 use warnings FATAL => 'all';
5 use Scalar::Util qw(blessed);
6 use Exporter 5.57 qw(import);
8 our $VERSION = '1.000004';
10 our @EXPORT = qw($_call_if_object $_call_if_object_or_classname $_isa $_can $_does $_DOES);
12 our $_call_if_object = sub {
13 my ($obj, $method) = (shift, shift);
14 return unless blessed($obj);
15 return $obj->$method(@_);
18 our $_call_if_object_or_classname = sub {
19 my ($thing, $method) = (shift, shift);
20 return unless blessed($thing) or defined($thing) and do {
24 return $thing->$method(@_);
27 our ($_isa, $_can, $_does, $_DOES) = map {
29 sub { my $obj = shift; $obj->$_call_if_object_or_classname($method => @_) }
30 } qw(isa can does DOES);
34 Safe::Isa - Call isa, can, does and DOES safely on arbitrary things
41 { package Foo; sub new { bless({}, $_[0]) } }
42 { package Bar; our @ISA = qw(Foo); sub bar { 1 } }
51 $foo->isa('Foo'); # true
52 $bar->isa('Foo'); # true
53 $class->isa('Foo') # true
54 $blam->isa('Foo'); # BOOM
56 $foo->can('bar'); # false
57 $bar->can('bar'); # true
58 $class->can('bar); # true
59 $blam->can('bar'); # BOOM
65 $foo->$_isa('Foo'); # true
66 $bar->$_isa('Foo'); # true
67 $class->isa('Foo') # true
68 $blam->$_isa('Foo'); # false, no boom today
70 $foo->$_can('bar'); # false
71 $bar->$_can('bar'); # true
72 $class->can('bar); # true
73 $blam->$_can('bar'); # false, no boom today
77 $maybe_an_object->$_does('RoleName'); # true or false, no boom today
78 $maybe_an_object->$_DOES('RoleName'); # true or false, no boom today
80 And just in case we missed a method:
82 $maybe_an_object->$_call_if_object(name => @args);
84 or this might be closer to what you want:
86 $maybe_a_referent->$_call_if_object_or_class(name => @args);
88 Or to re-use a previous example for purposes of explication:
90 $foo->$_call_if_object(isa => 'Foo'); # true
91 $bar->$_call_if_object(isa => 'Foo'); # true
92 $blam->$_call_if_object(isa => 'Foo'); # false, no boom today
96 How many times have you found yourself writing:
98 if ($obj->isa('Something')) {
100 and then shortly afterwards cursing and changing it to:
102 if (Scalar::Util::blessed($obj) and $obj->isa('Something')) {
104 Right. That's why this module exists.
106 Since perl allows us to provide a subroutine reference or a method name to
107 the -> operator when used as a method call, and a subroutine doesn't require
108 the invocant to actually be an object, we can create safe versions of isa,
109 can and friends by using a subroutine reference that only tries to call the
110 method if it's used on an object. So:
112 my $isa_Foo = $maybe_an_object->$_call_if_object(isa => 'Foo');
117 if (Scalar::Util::blessed($maybe_an_object)) {
118 $maybe_an_object->isa('Foo');
124 Note that we don't handle trying class names, because many things are valid
125 class names that you might not want to treat as one (like say "Matt") - the
126 C<is_module_name> function from L<Module::Runtime> is a good way to check for
127 something you might be able to call methods on if you want to do that.
133 $maybe_an_object->$_isa('Foo');
135 If called on an object, calls C<isa> on it and returns the result, otherwise
140 $maybe_an_object->$_can('Foo');
142 If called on an object, calls C<can> on it and returns the result, otherwise
147 $maybe_an_object->$_does('Foo');
149 If called on an object, calls C<does> on it and returns the result, otherwise
154 $maybe_an_object->$_DOES('Foo');
156 If called on an object, calls C<DOES> on it and returns the result, otherwise
159 =head2 $_call_if_object
161 $maybe_an_object->$_call_if_object(method_name => @args);
163 If called on an object, calls C<method_name> on it and returns the result,
164 otherwise returns nothing.
168 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
172 None yet. Well volunteered? :)
176 Copyright (c) 2012 the Safe::Isa L</AUTHOR> and L</CONTRIBUTORS>
181 This library is free software and may be distributed under the same terms