UNIVERSAL::can - Hack around people calling UNIVERSAL::can() as a function
Version 1.00
To use this module, simply:
use UNIVERSAL::can;
The UNIVERSAL class provides a few default methods so that all objects can use them. Object orientation allows programmers to override these methods in subclasses to provide more specific and appropriate behavior.
Some authors call methods in the UNIVERSAL class on potential invocants as functions, bypassing any possible overriding. This is wrong and you should not do it. Unfortunately, not everyone heeds this warning and their bad code can break your good code.
Fortunately, this module replaces UNIVERSAL::can()
with a method that checks
to see if the first argument is a valid invocant (whether an object -- a
blessed referent -- or the name of a class). If so, and if the invocant's
class has its own can()
method, it calls that as a method. Otherwise,
everything works as you might expect.
If someone attempts to call UNIVERSAL::can()
as a function, this module will
emit a lexical warning (see perllexwarn) to that effect. You can disable it
with no warnings;
or no warnings 'UNIVERSAL::isa';
, but don't do that;
fix the code instead.
By default, this module exports a can()
subroutine that works exactly as
described. It's a convenient shortcut for you.
The can()
method takes two arguments, a potential invocant and the name of a
method that that invocant may be able to call. It attempts to divine whether
the invocant is an object or a valid class name, whether there is an overridden
can()
method for it, and then calls that. Otherwise, it calls
UNIVERSAL::can()
directly, as if nothing had happened.
chromatic, <chromatic@wgz.org>
Please report any bugs or feature requests to bug-universal-can@rt.cpan.org
,
or through the web interface at
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=UNIVERSAL-can. This will
notify me, hold onto patches so I don't drop them, and will notify you of
progress on your request as I make changes.
Inspired by UNIVERSAL::isa by Yuval Kogman, Autrijus Tang, and myself.
Copyright 2005 chromatic, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.