1 package UNIVERSAL::isa;
4 use vars qw( $VERSION $recursing );
8 use Scalar::Util 'blessed';
9 use warnings::register;
13 my ( $orig, $verbose_warning );
15 BEGIN { $orig = \&UNIVERSAL::isa }
17 no warnings 'redefine';
26 *{ caller() . '::isa' } = \&UNIVERSAL::isa if $arg eq 'isa';
27 $verbose_warning = 1 if $arg eq 'verbose';
33 goto &$orig if $recursing;
34 my $type = invocant_type(@_);
41 return \&nonsense unless defined($invocant);
42 return \&object_or_class if blessed($invocant);
43 return \&reference if ref($invocant);
44 return \&nonsense unless $invocant;
45 return \&object_or_class;
50 report_warning('on invalid invocant') if $verbose_warning;
60 if ( my $override = eval { $_[0]->can('isa') } )
62 unless ( $override == \&UNIVERSAL::isa )
66 return $obj->$override(@_);
70 report_warning() if $verbose_warning;
76 report_warning('Did you mean to use Scalar::Util::reftype() instead?')
84 $extra = $extra ? " ($extra)" : '';
86 if ( warnings::enabled() )
88 my $calling_sub = ( caller(3) )[3] || '';
89 return if $calling_sub =~ /::isa$/;
91 "Called UNIVERSAL::isa() as a function, not a method$extra" );
103 UNIVERSAL::isa - Attempt to recover from people calling UNIVERSAL::isa as a
109 echo 'export PERL5OPT=-MUNIVERSAL::isa' >> /etc/profile
111 # within your program
114 # enable warnings for all dodgy uses of UNIVERSAL::isa
115 use UNIVERSAL::isa 'verbose';
119 Whenever you use L<UNIVERSAL/isa> as a function, a kitten using
120 L<Test::MockObject> dies. Normally, the kittens would be helpless, but if they
121 use L<UNIVERSAL::isa> (the module whose docs you are reading), the kittens can
122 live long and prosper.
124 This module replaces C<UNIVERSAL::isa> with a version that makes sure that,
125 when called as a function on objects which override C<isa>, C<isa> will call
126 the appropriate method on those objects
128 In all other cases, the real C<UNIVERSAL::isa> gets called directly.
132 If the lexical warnings pragma is available, this module will emit a warning
133 for each naughty invocation of C<UNIVERSAL::isa>. Silence these warnings by
136 no warnings 'UNIVERSAL::isa';
138 in the lexical scope of the naughty code.
140 After version 1.00, warnings only appear when naughty code calls
141 UNIVERSAL::isa() as a function on an invocant for which there is an overridden
142 isa(). These are really truly I<active> bugs, and you should fix them rather
143 than relying on this module to find them.
145 To get warnings for all potentially dangerous uses of UNIVERSAL::isa() as a
146 function, not a method (that is, for I<all> uses of the method as a function,
147 which are latent bugs, if not bugs that will break your code as it exists now),
148 pass the C<verbose> flag when using the module. This can generate many extra
149 warnings, but they're more specific as to the actual wrong practice and they
150 usually suggest proper fixes.
154 L<UNIVERSAL::can> for another discussion of the problem at hand.
156 L<Test::MockObject> for one example of a module that really needs to override
159 Any decent explanation of OO to understand why calling methods as functions is
160 a staggeringly bad idea.
164 Audrey Tang <cpan@audreyt.org>
166 chromatic <chromatic@wgz.org>
168 Yuval Kogman <nothingmuch@woobling.org>
170 =head1 COPYRIGHT & LICENSE
172 Artistic Licence 2.0, (c) 2005 - 2009.