bump version to 1.000005
[p5sagit/Safe-Isa.git] / lib / Safe / Isa.pm
CommitLineData
e6995dc6 1package Safe::Isa;
2
3use strict;
4use warnings FATAL => 'all';
5use Scalar::Util qw(blessed);
7b1885ee 6use Exporter 5.57 qw(import);
e6995dc6 7
d994d71c 8our $VERSION = '1.000005';
e6995dc6 9
10our @EXPORT = qw($_call_if_object $_isa $_can $_does $_DOES);
11
12our $_call_if_object = sub {
13 my ($obj, $method) = (shift, shift);
14 return unless blessed($obj);
15 return $obj->$method(@_);
16};
17
18our ($_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
25Safe::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
61Similarly:
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
66And just in case we missed a method:
67
68 $maybe_an_object->$_call_if_object(name => @args);
69
70Or 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
78How many times have you found yourself writing:
79
80 if ($obj->isa('Something')) {
81
82and then shortly afterwards cursing and changing it to:
83
84 if (Scalar::Util::blessed($obj) and $obj->isa('Something')) {
85
86Right. That's why this module exists.
87
88Since perl allows us to provide a subroutine reference or a method name to
89the -> operator when used as a method call, and a subroutine doesn't require
90the invocant to actually be an object, we can create safe versions of isa,
91can and friends by using a subroutine reference that only tries to call the
92method if it's used on an object. So:
93
94 my $isa_Foo = $maybe_an_object->$_call_if_object(isa => 'Foo');
95
96is 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 106Note that we don't handle trying class names, because many things are valid
107class names that you might not want to treat as one (like say "Matt") - the
108C<is_module_name> function from L<Module::Runtime> is a good way to check for
f94f3bbb 109something you might be able to call methods on if you want to do that.
fca38ff4 110
e6995dc6 111=head1 EXPORTS
112
113=head2 $_isa
114
115 $maybe_an_object->$_isa('Foo');
116
117If called on an object, calls C<isa> on it and returns the result, otherwise
118returns nothing.
119
120=head2 $_can
121
122 $maybe_an_object->$_can('Foo');
123
124If called on an object, calls C<can> on it and returns the result, otherwise
125returns nothing.
126
127=head2 $_does
128
129 $maybe_an_object->$_does('Foo');
130
131If called on an object, calls C<does> on it and returns the result, otherwise
132returns nothing.
133
134=head2 $_DOES
135
136 $maybe_an_object->$_DOES('Foo');
137
138If called on an object, calls C<DOES> on it and returns the result, otherwise
139returns nothing.
140
4703f64c 141=head2 $_call_if_object
e6995dc6 142
4703f64c 143 $maybe_an_object->$_call_if_object(method_name => @args);
e6995dc6 144
145If called on an object, calls C<method_name> on it and returns the result,
146otherwise returns nothing.
147
148=head1 AUTHOR
149
150mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
151
152=head1 CONTRIBUTORS
153
154None yet. Well volunteered? :)
155
156=head1 COPYRIGHT
157
158Copyright (c) 2012 the Safe::Isa L</AUTHOR> and L</CONTRIBUTORS>
159as listed above.
160
161=head1 LICENSE
162
163This library is free software and may be distributed under the same terms
164as perl itself.
165
166=cut