4 use warnings FATAL => 'all';
6 use Exporter 5.57 qw(import);
8 our $VERSION = '1.000007';
10 our @EXPORT = qw($_call_if_object $_isa $_can $_does $_DOES $_call_if_can);
12 our $_call_if_object = sub {
13 my ($obj, $method) = (shift, shift);
14 # This is intentionally a truth test, not a defined test, otherwise
15 # we gratuitously break modules like Scalar::Defer, which would be
17 return unless Scalar::Util::blessed($obj);
18 return $obj->isa(@_) if lc($method) eq 'does' and not $obj->can($method);
19 return $obj->$method(@_);
22 our ($_isa, $_can, $_does, $_DOES) = map {
24 sub { my $obj = shift; $obj->$_call_if_object($method => @_) }
25 } qw(isa can does DOES);
27 our $_call_if_can = sub {
28 my ($obj, $method) = (shift, shift);
29 return unless $obj->$_call_if_object(can => $method);
30 return $obj->$method(@_);
40 Safe::Isa - Call isa, can, does and DOES safely on things that may not be objects
47 { package Foo; sub new { bless({}, $_[0]) } }
48 { package Bar; our @ISA = qw(Foo); sub bar { 1 } }
56 $foo->isa('Foo'); # true
57 $bar->isa('Foo'); # true
58 $blam->isa('Foo'); # BOOM
60 $foo->can('bar'); # false
61 $bar->can('bar'); # true
62 $blam->can('bar'); # BOOM
68 $foo->$_isa('Foo'); # true
69 $bar->$_isa('Foo'); # true
70 $blam->$_isa('Foo'); # false, no boom today
72 $foo->$_can('bar'); # false
73 $bar->$_can('bar'); # true
74 $blam->$_can('bar'); # false, no boom today
78 $maybe_an_object->$_does('RoleName'); # true or false, no boom today
79 $maybe_an_object->$_DOES('RoleName'); # true or false, no boom today
81 And just in case we missed a method or two:
83 $maybe_an_object->$_call_if_object(name => @args);
84 $maybe_an_object->$_call_if_can(name => @args);
86 Or to re-use a previous example for purposes of explication:
88 $foo->$_call_if_object(isa => 'Foo'); # true
89 $bar->$_call_if_object(isa => 'Foo'); # true
90 $blam->$_call_if_object(isa => 'Foo'); # false, no boom today
94 How many times have you found yourself writing:
96 if ($obj->isa('Something')) {
98 and then shortly afterwards cursing and changing it to:
100 if (Scalar::Util::blessed($obj) and $obj->isa('Something')) {
102 Right. That's why this module exists.
104 Since perl allows us to provide a subroutine reference or a method name to
105 the -> operator when used as a method call, and a subroutine doesn't require
106 the invocant to actually be an object, we can create safe versions of isa,
107 can and friends by using a subroutine reference that only tries to call the
108 method if it's used on an object. So:
110 my $isa_Foo = $maybe_an_object->$_call_if_object(isa => 'Foo');
115 if (Scalar::Util::blessed($maybe_an_object)) {
116 $maybe_an_object->isa('Foo');
122 Note that we don't handle trying class names, because many things are valid
123 class names that you might not want to treat as one (like say "Matt") - the
124 C<is_module_name> function from L<Module::Runtime> is a good way to check for
125 something you might be able to call methods on if you want to do that.
127 We are careful to make sure that scalar/list context is preserved for the
128 method that is eventually called.
134 $maybe_an_object->$_isa('Foo');
136 If called on an object, calls C<isa> on it and returns the result, otherwise
141 $maybe_an_object->$_can('Foo');
143 If called on an object, calls C<can> on it and returns the result, otherwise
148 $maybe_an_object->$_does('Foo');
150 If called on an object, calls C<does> on it and returns the result, otherwise
155 $maybe_an_object->$_DOES('Foo');
157 If called on an object, calls C<DOES> on it and returns the result, otherwise
160 =head2 $_call_if_object
162 $maybe_an_object->$_call_if_object(method_name => @args);
164 If called on an object, calls C<method_name> on it and returns the result,
165 otherwise returns nothing.
169 $maybe_an_object->$_call_if_can(name => @args);
171 If called on an object, calls C<can> on it; if that returns true, then
172 calls C<method_name> on it and returns the result; if any condition is false
177 I gave a lightning talk on this module (and L<curry> and L<Import::Into>) at
178 L<YAPC::NA 2013|https://www.youtube.com/watch?v=wFXWV2yY7gE&t=46m05s>.
182 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
186 None yet. Well volunteered? :)
190 Copyright (c) 2012 the Safe::Isa L</AUTHOR> and L</CONTRIBUTORS>
195 This library is free software and may be distributed under the same terms