UNIVERSAL::can(3pm) User Contributed Perl Documentation UNIVERSAL::can(3pm)
NAME
UNIVERSAL::can - Hack around people calling UNIVERSAL::can() as a function
VERSION
Version 1.01
SYNOPSIS
To use this module, simply:
use UNIVERSAL::can;
DESCRIPTION
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. Unfortu-
nately, not everyone heeds this warning and their bad code can break your good code.
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 lex-
ical 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.
Some people argue that you must call "UNIVERSAL::can()" as a function because you don't
know if your proposed invocant is a valid invocant. That's silly. Use "blessed()" from
Scalar::Util if you want to check that the potential invocant is an object or call the
method anyway in an "eval" block and check for failure.
Just don't break working code.
EXPORT
This module can optionally export a "can()" subroutine that works exactly as described.
It's a convenient shortcut for you. This actually works in version 1.11.
Also, if you pass the "-always_warn" flag on the import line, this module will warn about
all incorrect uses of "UNIVERSAL::can()". This can help you change your code to be cor-
rect.
can()
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 hap-
pened.
AUTHOR
chromatic, "<chromatic AT wgz.org>"
BUGS
Please report any bugs or feature requests to "bug-universal-can AT rt.org", or through
the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=UNIVERSAL-can>. This
will contact me, hold onto patches so I don't drop them, and will notify you of progress
on your request as I make changes.
ACKNOWLEDGEMENTS
Inspired by UNIVERSAL::isa by Yuval Kogman, Autrijus Tang, and myself.
Adam Kennedy has tirelessly made me tired by reporting potential bugs and suggesting ideas
that found actual bugs.
Mark Clements helped to track down an invalid invocant bug.
COPYRIGHT & LICENSE
Copyright (c) 2005 - 2006 chromatic. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same
terms as Perl itself.
perl v5.8.8 2006-05-08 UNIVERSAL::can(3pm)
Generated by $Id: phpMan.php,v 4.49 2006/02/26 13:18:18 chedong Exp $ Author: Che Dong
On Apache
Under GNU General Public License
2012-05-25 04:22 @38.107.179.240 Crawled by CCBot/1.0 (+http://www.commoncrawl.org/bot.html)