aaaaaaargh I'm an idiot (typo)
[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);
6use base qw(Exporter);
7
08146a57 8our $VERSION = '1.000001';
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
106=head1 EXPORTS
107
108=head2 $_isa
109
110 $maybe_an_object->$_isa('Foo');
111
112If called on an object, calls C<isa> on it and returns the result, otherwise
113returns nothing.
114
115=head2 $_can
116
117 $maybe_an_object->$_can('Foo');
118
119If called on an object, calls C<can> on it and returns the result, otherwise
120returns nothing.
121
122=head2 $_does
123
124 $maybe_an_object->$_does('Foo');
125
126If called on an object, calls C<does> on it and returns the result, otherwise
127returns nothing.
128
129=head2 $_DOES
130
131 $maybe_an_object->$_DOES('Foo');
132
133If called on an object, calls C<DOES> on it and returns the result, otherwise
134returns nothing.
135
4703f64c 136=head2 $_call_if_object
e6995dc6 137
4703f64c 138 $maybe_an_object->$_call_if_object(method_name => @args);
e6995dc6 139
140If called on an object, calls C<method_name> on it and returns the result,
141otherwise returns nothing.
142
143=head1 AUTHOR
144
145mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
146
147=head1 CONTRIBUTORS
148
149None yet. Well volunteered? :)
150
151=head1 COPYRIGHT
152
153Copyright (c) 2012 the Safe::Isa L</AUTHOR> and L</CONTRIBUTORS>
154as listed above.
155
156=head1 LICENSE
157
158This library is free software and may be distributed under the same terms
159as perl itself.
160
161=cut