Commit | Line | Data |
e6995dc6 |
1 | package Safe::Isa; |
2 | |
3 | use strict; |
4 | use warnings FATAL => 'all'; |
5 | use Scalar::Util qw(blessed); |
7b1885ee |
6 | use Exporter 5.57 qw(import); |
e6995dc6 |
7 | |
d994d71c |
8 | our $VERSION = '1.000005'; |
e6995dc6 |
9 | |
10 | our @EXPORT = qw($_call_if_object $_isa $_can $_does $_DOES); |
11 | |
12 | our $_call_if_object = sub { |
13 | my ($obj, $method) = (shift, shift); |
f3335392 |
14 | # This is intentionally a truth test, not a defined test, otherwise |
15 | # we gratuitously break modules like Scalar::Defer, which would be |
16 | # un-perlish. |
e6995dc6 |
17 | return unless blessed($obj); |
18 | return $obj->$method(@_); |
19 | }; |
20 | |
21 | our ($_isa, $_can, $_does, $_DOES) = map { |
22 | my $method = $_; |
23 | sub { my $obj = shift; $obj->$_call_if_object($method => @_) } |
24 | } qw(isa can does DOES); |
25 | |
b19500df |
26 | 1; |
27 | __END__ |
28 | |
29 | =pod |
30 | |
e6995dc6 |
31 | =head1 NAME |
32 | |
33 | Safe::Isa - Call isa, can, does and DOES safely on things that may not be objects |
34 | |
35 | =head1 SYNOPSIS |
36 | |
37 | use strict; |
38 | use warnings; |
39 | |
40 | { package Foo; sub new { bless({}, $_[0]) } } |
41 | { package Bar; our @ISA = qw(Foo); sub bar { 1 } } |
42 | |
43 | my $foo = Foo->new; |
44 | my $bar = Bar->new; |
45 | my $blam = [ 42 ]; |
46 | |
47 | # basic isa usage - |
48 | |
49 | $foo->isa('Foo'); # true |
50 | $bar->isa('Foo'); # true |
51 | $blam->isa('Foo'); # BOOM |
52 | |
53 | $foo->can('bar'); # false |
54 | $bar->can('bar'); # true |
55 | $blam->can('bar'); # BOOM |
56 | |
57 | # Safe::Isa usage - |
58 | |
59 | use Safe::Isa; |
60 | |
61 | $foo->$_isa('Foo'); # true |
62 | $bar->$_isa('Foo'); # true |
63 | $blam->$_isa('Foo'); # false, no boom today |
64 | |
65 | $foo->$_can('bar'); # false |
66 | $bar->$_can('bar'); # true |
67 | $blam->$_can('bar'); # false, no boom today |
68 | |
69 | Similarly: |
70 | |
71 | $maybe_an_object->$_does('RoleName'); # true or false, no boom today |
72 | $maybe_an_object->$_DOES('RoleName'); # true or false, no boom today |
73 | |
74 | And just in case we missed a method: |
75 | |
76 | $maybe_an_object->$_call_if_object(name => @args); |
77 | |
78 | Or to re-use a previous example for purposes of explication: |
79 | |
ff9518c5 |
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 |
e6995dc6 |
83 | |
84 | =head1 DESCRIPTION |
85 | |
86 | How many times have you found yourself writing: |
87 | |
88 | if ($obj->isa('Something')) { |
89 | |
90 | and then shortly afterwards cursing and changing it to: |
91 | |
92 | if (Scalar::Util::blessed($obj) and $obj->isa('Something')) { |
93 | |
94 | Right. That's why this module exists. |
95 | |
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: |
101 | |
102 | my $isa_Foo = $maybe_an_object->$_call_if_object(isa => 'Foo'); |
103 | |
104 | is equivalent to |
105 | |
106 | my $isa_Foo = do { |
107 | if (Scalar::Util::blessed($maybe_an_object)) { |
108 | $maybe_an_object->isa('Foo'); |
109 | } else { |
110 | undef; |
111 | } |
112 | }; |
113 | |
fca38ff4 |
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 |
f94f3bbb |
117 | something you might be able to call methods on if you want to do that. |
fca38ff4 |
118 | |
e6995dc6 |
119 | =head1 EXPORTS |
120 | |
121 | =head2 $_isa |
122 | |
123 | $maybe_an_object->$_isa('Foo'); |
124 | |
125 | If called on an object, calls C<isa> on it and returns the result, otherwise |
126 | returns nothing. |
127 | |
128 | =head2 $_can |
129 | |
130 | $maybe_an_object->$_can('Foo'); |
131 | |
132 | If called on an object, calls C<can> on it and returns the result, otherwise |
133 | returns nothing. |
134 | |
135 | =head2 $_does |
136 | |
137 | $maybe_an_object->$_does('Foo'); |
138 | |
139 | If called on an object, calls C<does> on it and returns the result, otherwise |
140 | returns nothing. |
141 | |
142 | =head2 $_DOES |
143 | |
144 | $maybe_an_object->$_DOES('Foo'); |
145 | |
146 | If called on an object, calls C<DOES> on it and returns the result, otherwise |
147 | returns nothing. |
148 | |
4703f64c |
149 | =head2 $_call_if_object |
e6995dc6 |
150 | |
4703f64c |
151 | $maybe_an_object->$_call_if_object(method_name => @args); |
e6995dc6 |
152 | |
153 | If called on an object, calls C<method_name> on it and returns the result, |
154 | otherwise returns nothing. |
155 | |
91dfd937 |
156 | =head1 SEE ALSO |
157 | |
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>. |
160 | |
e6995dc6 |
161 | =head1 AUTHOR |
162 | |
163 | mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk> |
164 | |
165 | =head1 CONTRIBUTORS |
166 | |
167 | None yet. Well volunteered? :) |
168 | |
169 | =head1 COPYRIGHT |
170 | |
171 | Copyright (c) 2012 the Safe::Isa L</AUTHOR> and L</CONTRIBUTORS> |
172 | as listed above. |
173 | |
174 | =head1 LICENSE |
175 | |
176 | This library is free software and may be distributed under the same terms |
177 | as perl itself. |
178 | |
179 | =cut |