1UNIVERSAL::ref(3) User Contributed Perl Documentation UNIVERSAL::ref(3)
2
3
4
6 UNIVERSAL::ref - Turns ref() into a multimethod
7
9 # True! Wrapper pretends to be Thing.
10 ref( Wrapper->new( Thing->new ) )
11 eq ref( Thing->new );
12
13 package Thing;
14 sub new { bless [], shift }
15
16 package Wrapper;
17 sub new {
18 my ($class,$proxy) = @_;
19 bless \ $proxy, $class;
20 }
21 sub ref {
22 my $self = shift @_;
23 return $$self;
24 }
25
27 This module changes the behavior of the builtin function ref(). If
28 ref() is called on an object that has requested an overloaded ref, the
29 object's "->ref" method will be called and its return value used
30 instead.
31
33 To enable this feature for a class, "use UNIVERSAL::ref" in your class.
34 Here is a sample proxy module.
35
36 package Pirate;
37 # Pirate pretends to be a Privateer
38 use UNIVERSAL::ref;
39 sub new { bless {}, shift }
40 sub ref { return 'Privateer' }
41
42 Anywhere you call "ref($obj)" on a "Pirate" object, it will allow
43 "Pirate" to lie and pretend to be something else.
44
46 Currently UNIVERSAL::ref must be installed before any ref() calls that
47 are to be affected.
48
49 I think ref() always occurs in an implicit scalar context. There is no
50 accomodation for list context.
51
52 UNIVERSAL::ref probably shouldn't allow a module to lie to itself. Or
53 should it?
54
56 ambrus for the excellent idea to overload defined() to allow Perl 5 to
57 have Perl 6's "interesting values of undef."
58
59 chromatic for pointing out how utterly broken ref() is. This fix covers
60 its biggest hole.
61
63 Joshua ben Jore - jjore@cpan.org
64
66 The standard Artistic / GPL license most other perl code is typically
67 using.
68
69
70
71perl v5.12.0 2007-11-20 UNIVERSAL::ref(3)