4 use warnings FATAL => 'all';
5 use Scalar::Util qw(blessed);
6 use Exporter 5.57 qw(import);
8 our $VERSION = '1.000005';
10 our @EXPORT = qw($_call_if_object $_isa $_can $_does $_DOES);
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 blessed($obj);
18 return $obj->$method(@_);
21 our ($_isa, $_can, $_does, $_DOES) = map {
23 sub { my $obj = shift; $obj->$_call_if_object($method => @_) }
24 } qw(isa can does DOES);
33 Safe::Isa - Call isa, can, does and DOES safely on things that may not be objects
40 { package Foo; sub new { bless({}, $_[0]) } }
41 { package Bar; our @ISA = qw(Foo); sub bar { 1 } }
49 $foo->isa('Foo'); # true
50 $bar->isa('Foo'); # true
51 $blam->isa('Foo'); # BOOM
53 $foo->can('bar'); # false
54 $bar->can('bar'); # true
55 $blam->can('bar'); # BOOM
61 $foo->$_isa('Foo'); # true
62 $bar->$_isa('Foo'); # true
63 $blam->$_isa('Foo'); # false, no boom today
65 $foo->$_can('bar'); # false
66 $bar->$_can('bar'); # true
67 $blam->$_can('bar'); # false, no boom today
71 $maybe_an_object->$_does('RoleName'); # true or false, no boom today
72 $maybe_an_object->$_DOES('RoleName'); # true or false, no boom today
74 And just in case we missed a method:
76 $maybe_an_object->$_call_if_object(name => @args);
78 Or to re-use a previous example for purposes of explication:
80 $foo->$_call_if_object(isa => 'Foo'); # true
81 $bar->$_call_if_object(isa => 'Foo'); # true
82 $blam->$_call_if_object(isa => 'Foo'); # false, no boom today
86 How many times have you found yourself writing:
88 if ($obj->isa('Something')) {
90 and then shortly afterwards cursing and changing it to:
92 if (Scalar::Util::blessed($obj) and $obj->isa('Something')) {
94 Right. That's why this module exists.
96 Since perl allows us to provide a subroutine reference or a method name to
97 the -> operator when used as a method call, and a subroutine doesn't require
98 the invocant to actually be an object, we can create safe versions of isa,
99 can and friends by using a subroutine reference that only tries to call the
100 method if it's used on an object. So:
102 my $isa_Foo = $maybe_an_object->$_call_if_object(isa => 'Foo');
107 if (Scalar::Util::blessed($maybe_an_object)) {
108 $maybe_an_object->isa('Foo');
114 Note that we don't handle trying class names, because many things are valid
115 class names that you might not want to treat as one (like say "Matt") - the
116 C<is_module_name> function from L<Module::Runtime> is a good way to check for
117 something you might be able to call methods on if you want to do that.
123 $maybe_an_object->$_isa('Foo');
125 If called on an object, calls C<isa> on it and returns the result, otherwise
130 $maybe_an_object->$_can('Foo');
132 If called on an object, calls C<can> on it and returns the result, otherwise
137 $maybe_an_object->$_does('Foo');
139 If called on an object, calls C<does> on it and returns the result, otherwise
144 $maybe_an_object->$_DOES('Foo');
146 If called on an object, calls C<DOES> on it and returns the result, otherwise
149 =head2 $_call_if_object
151 $maybe_an_object->$_call_if_object(method_name => @args);
153 If called on an object, calls C<method_name> on it and returns the result,
154 otherwise returns nothing.
158 I gave a lightning talk on this module (and L<curry> and L<Import::Into>) at
159 L<YAPC::NA 2013|https://www.youtube.com/watch?v=wFXWV2yY7gE&t=46m05s>.
163 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
167 None yet. Well volunteered? :)
171 Copyright (c) 2012 the Safe::Isa L</AUTHOR> and L</CONTRIBUTORS>
176 This library is free software and may be distributed under the same terms