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 import
47 A pragma for ref()-enabling your class. This adds the calling class
48 name to a global list of ref()-enabled classes.
49
50 package YourClass;
51 use UNIVERSAL::ref;
52 sub ref { ... }
53
54 unimport
55 A pragma for ref()-disabling your class. This removes the calling
56 class name from a global list of ref()-enabled classes.
57
59 Currently UNIVERSAL::ref must be installed before any ref() calls that
60 are to be affected.
61
62 I think ref() always occurs in an implicit scalar context. There is no
63 accomodation for list context.
64
65 UNIVERSAL::ref probably shouldn't allow a module to lie to itself. Or
66 should it?
67
69 ambrus for the excellent idea to overload defined() to allow Perl 5 to
70 have Perl 6's "interesting values of undef."
71
72 chromatic for pointing out how utterly broken ref() is. This fix covers
73 its biggest hole.
74
76 Joshua ben Jore - jjore@cpan.org
77
79 The standard Artistic / GPL license most other perl code is typically
80 using.
81
82
83
84perl v5.28.1 2011-01-23 UNIVERSAL::ref(3)