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