4 use warnings FATAL => 'all';
6 use Exporter 5.57 qw(import);
8 our $VERSION = '1.000009';
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->$method(@_);
21 our ($_isa, $_can) = map {
23 sub { my $obj = shift; $obj->$_call_if_object($method => @_) }
26 our $_call_if_can = sub {
27 my ($obj, $method) = (shift, shift);
28 return unless $obj->$_call_if_object(can => $method);
29 return $obj->$method(@_);
34 $obj->$_call_if_can(does => @_);
39 return unless Scalar::Util::blessed($obj);
52 Safe::Isa - Call isa, can, does and DOES safely on things that may not be objects
59 { package Foo; sub new { bless({}, $_[0]) } }
60 { package Bar; our @ISA = qw(Foo); sub bar { 1 } }
68 $foo->isa('Foo'); # true
69 $bar->isa('Foo'); # true
70 $blam->isa('Foo'); # BOOM
72 $foo->can('bar'); # false
73 $bar->can('bar'); # true
74 $blam->can('bar'); # BOOM
80 $foo->$_isa('Foo'); # true
81 $bar->$_isa('Foo'); # true
82 $blam->$_isa('Foo'); # false, no boom today
84 $foo->$_can('bar'); # false
85 $bar->$_can('bar'); # true
86 $blam->$_can('bar'); # false, no boom today
90 $maybe_an_object->$_does('RoleName'); # true or false, no boom today
91 $maybe_an_object->$_DOES('RoleName'); # true or false, no boom today
93 And just in case we missed a method or two:
95 $maybe_an_object->$_call_if_object(name => @args);
96 $maybe_an_object->$_call_if_can(name => @args);
98 Or to re-use a previous example for purposes of explication:
100 $foo->$_call_if_object(isa => 'Foo'); # true
101 $bar->$_call_if_object(isa => 'Foo'); # true
102 $blam->$_call_if_object(isa => 'Foo'); # false, no boom today
106 How many times have you found yourself writing:
108 if ($obj->isa('Something')) {
110 and then shortly afterwards cursing and changing it to:
112 if (Scalar::Util::blessed($obj) and $obj->isa('Something')) {
114 Right. That's why this module exists.
116 Since perl allows us to provide a subroutine reference or a method name to
117 the -> operator when used as a method call, and a subroutine doesn't require
118 the invocant to actually be an object, we can create safe versions of isa,
119 can and friends by using a subroutine reference that only tries to call the
120 method if it's used on an object. So:
122 my $isa_Foo = $maybe_an_object->$_call_if_object(isa => 'Foo');
127 if (Scalar::Util::blessed($maybe_an_object)) {
128 $maybe_an_object->isa('Foo');
134 Note that we don't handle trying class names, because many things are valid
135 class names that you might not want to treat as one (like say "Matt") - the
136 C<is_module_name> function from L<Module::Runtime> is a good way to check for
137 something you might be able to call methods on if you want to do that.
139 We are careful to make sure that scalar/list context is preserved for the
140 method that is eventually called.
146 $maybe_an_object->$_isa('Foo');
148 If called on an object, calls C<isa> on it and returns the result, otherwise
153 $maybe_an_object->$_can('Foo');
155 If called on an object, calls C<can> on it and returns the result, otherwise
160 $maybe_an_object->$_does('Foo');
162 If called on an object, calls C<does> on it and returns the result, otherwise
163 returns nothing. If the C<does> method does not exist, returns nothing rather
168 $maybe_an_object->$_DOES('Foo');
170 If called on an object, calls C<DOES> on it and returns the result, otherwise
171 returns nothing. On perl versions prior to 5.10.0, the built in core C<DOES>
172 method doesn't exist. If the method doesn't exist, this will fall back to
173 calling C<isa> just like the core C<DOES> method.
175 =head2 $_call_if_object
177 $maybe_an_object->$_call_if_object(method_name => @args);
179 If called on an object, calls C<method_name> on it and returns the result,
180 otherwise returns nothing.
184 $maybe_an_object->$_call_if_can(name => @args);
186 If called on an object, calls C<can> on it; if that returns true, then
187 calls C<method_name> on it and returns the result; if any condition is false
192 I gave a lightning talk on this module (and L<curry> and L<Import::Into>) at
193 L<YAPC::NA 2013|https://www.youtube.com/watch?v=wFXWV2yY7gE&t=46m05s>.
197 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
201 None yet. Well volunteered? :)
205 Copyright (c) 2012 the Safe::Isa L</AUTHOR> and L</CONTRIBUTORS>
210 This library is free software and may be distributed under the same terms