1 package UNIVERSAL::can;
6 use vars qw( $VERSION $recursing );
9 use Scalar::Util 'blessed';
10 use warnings::register;
13 use vars '$always_warn';
17 $orig = \&UNIVERSAL::can;
19 no warnings 'redefine';
20 *UNIVERSAL::can = \&can;
28 $always_warn = 1 if $import eq '-always_warn';
30 *{ caller() . '::can' } = \&can if $import eq 'can';
36 my $caller = caller();
39 # don't get into a loop here
40 goto &$orig if $recursing
43 && eval { $caller->isa( $_[0] ); } );
45 # call an overridden can() if it exists
46 my $can = eval { $_[0]->$orig('can') || 0 };
48 # but only if it's a real class
49 goto &$orig unless $can;
51 # but not if it inherited this one
52 goto &$orig if $can == \&UNIVERSAL::can;
54 # redirect to an overridden can, making sure not to recurse and warning
59 return $invocant->can(@_);
64 if ( $always_warn || warnings::enabled() )
66 my $calling_sub = ( caller(2) )[3] || '';
67 warnings::warn("Called UNIVERSAL::can() as a function, not a method")
68 if $calling_sub !~ /::can$/;
79 UNIVERSAL::can - Hack around people calling UNIVERSAL::can() as a function
87 To use this module, simply:
93 The UNIVERSAL class provides a few default methods so that all objects can use
94 them. Object orientation allows programmers to override these methods in
95 subclasses to provide more specific and appropriate behavior.
97 Some authors call methods in the UNIVERSAL class on potential invocants as
98 functions, bypassing any possible overriding. This is wrong and you should not
99 do it. Unfortunately, not everyone heeds this warning and their bad code can
100 break your good code.
102 This module replaces C<UNIVERSAL::can()> with a method that checks to see if
103 the first argument is a valid invocant has its own C<can()> method. If so, it
104 gives a warning and calls the overridden method, working around buggy code.
105 Otherwise, everything works as you might expect.
107 Some people argue that you must call C<UNIVERSAL::can()> as a function because
108 you don't know if your proposed invocant is a valid invocant. That's silly.
109 Use C<blessed()> from L<Scalar::Util> if you want to check that the potential
110 invocant is an object or call the method anyway in an C<eval> block and check
111 for failure (though check the exception I<returned>, as a poorly-written
112 C<can()> method could break Liskov and throw an exception other than "You can't
113 call a method on this type of invocant").
115 Just don't break working code.
119 chromatic, C<< <chromatic@wgz.org> >>
123 Please report any bugs or feature requests to C<bug-universal-can@rt.cpan.org>,
124 or through the web interface at
125 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=UNIVERSAL-can>. This will
126 contact me, hold onto patches so I don't drop them, and will notify you of
127 progress on your request as I make changes.
129 =head1 ACKNOWLEDGEMENTS
131 Inspired by L<UNIVERSAL::isa> by Yuval Kogman, Autrijus Tang, and myself.
133 Adam Kennedy has tirelessly made me tired by reporting potential bugs and
134 suggesting ideas that found actual bugs.
136 Mark Clements helped to track down an invalid invocant bug.
138 Curtis "Ovid" Poe finally provided the inspiration I needed to clean up the
141 Peter du Marchie van Voorthuysen identified and fixed a problem with calling
144 The Perl QA list had a huge... discussion... which inspired my realization that
145 this module needed to do what it does now.
147 =head1 COPYRIGHT & LICENSE
149 Artistic License 2.0, copyright (c) 2005 - 2009 chromatic. Some rights