1UNIVERSAL::can(3) User Contributed Perl Documentation UNIVERSAL::can(3)
2
3
4
6 UNIVERSAL::can - Hack around people calling UNIVERSAL::can() as a func‐
7 tion
8
10 Version 1.01
11
13 To use this module, simply:
14
15 use UNIVERSAL::can;
16
18 The UNIVERSAL class provides a few default methods so that all objects
19 can use them. Object orientation allows programmers to override these
20 methods in subclasses to provide more specific and appropriate behav‐
21 ior.
22
23 Some authors call methods in the UNIVERSAL class on potential invocants
24 as functions, bypassing any possible overriding. This is wrong and you
25 should not do it. Unfortunately, not everyone heeds this warning and
26 their bad code can break your good code.
27
28 This module replaces "UNIVERSAL::can()" with a method that checks to
29 see if the first argument is a valid invocant (whether an object -- a
30 blessed referent -- or the name of a class). If so, and if the invo‐
31 cant's class has its own "can()" method, it calls that as a method.
32 Otherwise, everything works as you might expect.
33
34 If someone attempts to call "UNIVERSAL::can()" as a function, this mod‐
35 ule will emit a lexical warning (see perllexwarn) to that effect. You
36 can disable it with "no warnings;" or "no warnings 'UNIVERSAL::isa';",
37 but don't do that; fix the code instead.
38
39 Some people argue that you must call "UNIVERSAL::can()" as a function
40 because you don't know if your proposed invocant is a valid invocant.
41 That's silly. Use "blessed()" from Scalar::Util if you want to check
42 that the potential invocant is an object or call the method anyway in
43 an "eval" block and check for failure.
44
45 Just don't break working code.
46
48 This module can optionally export a "can()" subroutine that works
49 exactly as described. It's a convenient shortcut for you. This actu‐
50 ally works in version 1.11.
51
52 Also, if you pass the "-always_warn" flag on the import line, this mod‐
53 ule will warn about all incorrect uses of "UNIVERSAL::can()". This can
54 help you change your code to be correct.
55
56 can()
57
58 The "can()" method takes two arguments, a potential invocant and the
59 name of a method that that invocant may be able to call. It attempts
60 to divine whether the invocant is an object or a valid class name,
61 whether there is an overridden "can()" method for it, and then calls
62 that. Otherwise, it calls "UNIVERSAL::can()" directly, as if nothing
63 had happened.
64
66 chromatic, "<chromatic@wgz.org>"
67
69 Please report any bugs or feature requests to "bug-univer‐
70 sal-can@rt.cpan.org", or through the web interface at
71 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=UNIVERSAL-can>. This
72 will contact me, hold onto patches so I don't drop them, and will
73 notify you of progress on your request as I make changes.
74
76 Inspired by UNIVERSAL::isa by Yuval Kogman, Autrijus Tang, and myself.
77
78 Adam Kennedy has tirelessly made me tired by reporting potential bugs
79 and suggesting ideas that found actual bugs.
80
81 Mark Clements helped to track down an invalid invocant bug.
82
84 Copyright (c) 2005 - 2006 chromatic. All rights reserved.
85
86 This program is free software; you can redistribute it and/or modify it
87 under the same terms as Perl itself.
88
89
90
91perl v5.8.8 2006-04-05 UNIVERSAL::can(3)