File: Isa.pm

package info (click to toggle)
libsafe-isa-perl 1.000010-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 100 kB
  • sloc: perl: 33; makefile: 2
file content (213 lines) | stat: -rw-r--r-- 5,398 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package Safe::Isa;

use strict;
use warnings FATAL => 'all';
use Scalar::Util ();
use Exporter 5.57 qw(import);

our $VERSION = '1.000010';

our @EXPORT = qw($_call_if_object $_isa $_can $_does $_DOES $_call_if_can);

our $_call_if_object = sub {
  my ($obj, $method) = (shift, shift);
  # This is intentionally a truth test, not a defined test, otherwise
  # we gratuitously break modules like Scalar::Defer, which would be
  # un-perlish.
  return unless Scalar::Util::blessed($obj);
  return $obj->$method(@_);
};

our ($_isa, $_can) = map {
  my $method = $_;
  sub { my $obj = shift; $obj->$_call_if_object($method => @_) }
} qw(isa can);

our $_call_if_can = sub {
  my ($obj, $method) = (shift, shift);
  return unless $obj->$_call_if_object(can => $method);
  return $obj->$method(@_);
};

our $_does = sub {
  my $obj = shift;
  $obj->$_call_if_can(does => @_);
};

our $_DOES = sub {
  my $obj = shift;
  return unless Scalar::Util::blessed($obj);
  return $obj->DOES(@_)
    if $obj->can('DOES');
  return $obj->isa(@_);
};

1;
__END__

=pod

=head1 NAME

Safe::Isa - Call isa, can, does and DOES safely on things that may not be objects

=head1 SYNOPSIS

  use strict;
  use warnings;
  
  { package Foo; sub new { bless({}, $_[0]) } }
  { package Bar; our @ISA = qw(Foo); sub bar { 1 } }
  
  my $foo = Foo->new;
  my $bar = Bar->new;
  my $blam = [ 42 ];
  
  # basic isa usage -
  
  $foo->isa('Foo');  # true
  $bar->isa('Foo');  # true
  $blam->isa('Foo'); # BOOM
  
  $foo->can('bar');  # false
  $bar->can('bar');  # true
  $blam->can('bar'); # BOOM
  
  # Safe::Isa usage -
  
  use Safe::Isa;
  
  $foo->$_isa('Foo');  # true
  $bar->$_isa('Foo');  # true
  $blam->$_isa('Foo'); # false, no boom today
  
  $foo->$_can('bar');  # false
  $bar->$_can('bar');  # true
  $blam->$_can('bar'); # false, no boom today

Similarly:

  $maybe_an_object->$_does('RoleName'); # true or false, no boom today
  $maybe_an_object->$_DOES('RoleName'); # true or false, no boom today

And just in case we missed a method or two:

  $maybe_an_object->$_call_if_object(name => @args);
  $maybe_an_object->$_call_if_can(name => @args);

Or to re-use a previous example for purposes of explication:

  $foo->$_call_if_object(isa => 'Foo');  # true
  $bar->$_call_if_object(isa => 'Foo');  # true
  $blam->$_call_if_object(isa => 'Foo'); # false, no boom today

=head1 DESCRIPTION

How many times have you found yourself writing:

  if ($obj->isa('Something')) {

and then shortly afterwards cursing and changing it to:

  if (Scalar::Util::blessed($obj) and $obj->isa('Something')) {

Right. That's why this module exists.

Since perl allows us to provide a subroutine reference or a method name to
the -> operator when used as a method call, and a subroutine doesn't require
the invocant to actually be an object, we can create safe versions of isa,
can and friends by using a subroutine reference that only tries to call the
method if it's used on an object. So:

  my $isa_Foo = $maybe_an_object->$_call_if_object(isa => 'Foo');

is equivalent to

  my $isa_Foo = do {
    if (Scalar::Util::blessed($maybe_an_object)) {
      $maybe_an_object->isa('Foo');
    } else {
      undef;
    }
  };

Note that we don't handle trying class names, because many things are valid
class names that you might not want to treat as one (like say "Matt") - the
C<is_module_name> function from L<Module::Runtime> is a good way to check for
something you might be able to call methods on if you want to do that.

We are careful to make sure that scalar/list context is preserved for the
method that is eventually called.

=head1 EXPORTS

=head2 $_isa

  $maybe_an_object->$_isa('Foo');

If called on an object, calls C<isa> on it and returns the result, otherwise
returns nothing.

=head2 $_can

  $maybe_an_object->$_can('Foo');

If called on an object, calls C<can> on it and returns the result, otherwise
returns nothing.

=head2 $_does

  $maybe_an_object->$_does('Foo');

If called on an object, calls C<does> on it and returns the result, otherwise
returns nothing. If the C<does> method does not exist, returns nothing rather
than failing.

=head2 $_DOES

  $maybe_an_object->$_DOES('Foo');

If called on an object, calls C<DOES> on it and returns the result, otherwise
returns nothing. On perl versions prior to 5.10.0, the built in core C<DOES>
method doesn't exist. If the method doesn't exist, this will fall back to
calling C<isa> just like the core C<DOES> method.

=head2 $_call_if_object

  $maybe_an_object->$_call_if_object(method_name => @args);

If called on an object, calls C<method_name> on it and returns the result,
otherwise returns nothing.

=head2 $_call_if_can

  $maybe_an_object->$_call_if_can(name => @args);

If called on an object, calls C<can> on it; if that returns true, then
calls C<method_name> on it and returns the result; if any condition is false
returns nothing.

=head1 SEE ALSO

I gave a lightning talk on this module (and L<curry> and L<Import::Into>) at
L<YAPC::NA 2013|https://www.youtube.com/watch?v=wFXWV2yY7gE&t=46m05s>.

=head1 AUTHOR

mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>

=head1 CONTRIBUTORS

None yet. Well volunteered? :)

=head1 COPYRIGHT

Copyright (c) 2012 the Safe::Isa L</AUTHOR> and L</CONTRIBUTORS>
as listed above.

=head1 LICENSE

This library is free software and may be distributed under the same terms
as perl itself.

=cut