4 use warnings FATAL => 'all';
5 use Scalar::Util qw(blessed);
8 our $VERSION = '1.000003';
10 our @EXPORT = qw($_call_if_object $_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 ($_isa, $_can, $_does, $_DOES) = map {
20 sub { my $obj = shift; $obj->$_call_if_object($method => @_) }
21 } qw(isa can does DOES);
25 Safe::Isa - Call isa, can, does and DOES safely on things that may not be objects
32 { package Foo; sub new { bless({}, $_[0]) } }
33 { package Bar; our @ISA = qw(Foo); sub bar { 1 } }
41 $foo->isa('Foo'); # true
42 $bar->isa('Foo'); # true
43 $blam->isa('Foo'); # BOOM
45 $foo->can('bar'); # false
46 $bar->can('bar'); # true
47 $blam->can('bar'); # BOOM
53 $foo->$_isa('Foo'); # true
54 $bar->$_isa('Foo'); # true
55 $blam->$_isa('Foo'); # false, no boom today
57 $foo->$_can('bar'); # false
58 $bar->$_can('bar'); # true
59 $blam->$_can('bar'); # false, no boom today
63 $maybe_an_object->$_does('RoleName'); # true or false, no boom today
64 $maybe_an_object->$_DOES('RoleName'); # true or false, no boom today
66 And just in case we missed a method:
68 $maybe_an_object->$_call_if_object(name => @args);
70 Or to re-use a previous example for purposes of explication:
72 $foo->$_call_if_object(isa => 'Foo'); # true
73 $bar->$_call_if_object(isa => 'Foo'); # true
74 $blam->$_call_if_object(isa => 'Foo'); # false, no boom today
78 How many times have you found yourself writing:
80 if ($obj->isa('Something')) {
82 and then shortly afterwards cursing and changing it to:
84 if (Scalar::Util::blessed($obj) and $obj->isa('Something')) {
86 Right. That's why this module exists.
88 Since perl allows us to provide a subroutine reference or a method name to
89 the -> operator when used as a method call, and a subroutine doesn't require
90 the invocant to actually be an object, we can create safe versions of isa,
91 can and friends by using a subroutine reference that only tries to call the
92 method if it's used on an object. So:
94 my $isa_Foo = $maybe_an_object->$_call_if_object(isa => 'Foo');
99 if (Scalar::Util::blessed($maybe_an_object)) {
100 $maybe_an_object->isa('Foo');
106 Note that we don't handle trying class names, because many things are valid
107 class names that you might not want to treat as one (like say "Matt") - the
108 C<is_module_name> function from L<Module::Runtime> is a good way to check for
109 somthing you might be able to call methods on if you want to do that.
115 $maybe_an_object->$_isa('Foo');
117 If called on an object, calls C<isa> on it and returns the result, otherwise
122 $maybe_an_object->$_can('Foo');
124 If called on an object, calls C<can> on it and returns the result, otherwise
129 $maybe_an_object->$_does('Foo');
131 If called on an object, calls C<does> on it and returns the result, otherwise
136 $maybe_an_object->$_DOES('Foo');
138 If called on an object, calls C<DOES> on it and returns the result, otherwise
141 =head2 $_call_if_object
143 $maybe_an_object->$_call_if_object(method_name => @args);
145 If called on an object, calls C<method_name> on it and returns the result,
146 otherwise returns nothing.
150 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
154 None yet. Well volunteered? :)
158 Copyright (c) 2012 the Safe::Isa L</AUTHOR> and L</CONTRIBUTORS>
163 This library is free software and may be distributed under the same terms