UNIVERSAL::can.3pm

Langue: en

Version: 2009-02-27 (fedora - 05/07/09)

Section: 3 (Bibliothèques de fonctions)

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. Unfortunately, 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 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.

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 correct.

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 happened.

AUTHOR

chromatic, "<chromatic@wgz.org>"

BUGS

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 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 (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.