UNIVERSAL::ref(3)
πΈ NAME πΈ
Okay so bestie, welcome to UNIVERSAL::ref(3) β straight out of the User Contributed Perl Documentation corner of the universe. This module is literally called UNIVERSAL::ref, and its whole vibe is: turns ref() into a multimethod. No cap.
β¨ SYNOPSIS β¨
Hereβs the iconic little demo where Wrapper pretends to be Thing, and the ref check says true:
# True! Wrapper pretends to be Thing.
ref( Wrapper->new( Thing->new ) )
eq ref( Thing->new );
package Thing;
sub new { bless [], shift }
package Wrapper;
sub new {
my ($class,$proxy) = @_;
bless \ $proxy, $class;
}
sub ref {
my $self = shift @_;
return $$self;
}
π DESCRIPTION π
This module changes the behavior of the builtin ref(). If ref() is called on an object that has requested an overloaded ref, then the objectβs ->ref method gets called and its return value is used instead. Thatβs the whole trick, and itβs honestly iconic.
π USING π
To enable this feature for a class, put use UNIVERSAL::ref in your class. Hereβs a sample proxy module:
package Pirate;
# Pirate pretends to be a Privateer
use UNIVERSAL::ref;
sub new { bless {}, shift }
sub ref { return 'Privateer' }
Anywhere you call ref($obj) on a Pirate object, it will allow Pirate to lie and pretend to be something else. iykyk π΄ββ οΈ
π¦ METHODS π¦
π₯ import
A pragma for ref()-enabling your class. This adds the calling class name to a global list of ref()-enabled classes.
package YourClass;
use UNIVERSAL::ref;
sub ref { ... }
π€ unimport
A pragma for ref()-disabling your class. This removes the calling class name from a global list of ref()-enabled classes.
π TODO π
- Currently
UNIVERSAL::refmust be installed before anyref()calls that are to be affected. - I think
ref()always occurs in an implicit scalar context. There is no accomodation for list context. UNIVERSAL::refprobably shouldnβt allow a module to lie to itself. Or should it?
π ACKNOWLEDGEMENTS π
- ambrus for the excellent idea to overload
defined()to allow Perl 5 to have Perl 6βs "interesting values of undef." - chromatic for pointing out how utterly broken
ref()is. This fix covers its biggest hole.
π AUTHOR π
Joshua ben Jore - jjore@cpan.org
π LICENSE π
The standard Artistic / GPL license most other perl code is typically using.
Okay thatβs the whole page, bestie. Source/version: perl v5.42.0, date: 2024-03-04, manual: UNIVERSAL::ref(3). Mwah ππ«Ά
π¦ where this page comes from
- opensuse-leap 16.0: perl-UNIVERSAL-ref 0.14-bp160.1.13 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-UNIVERSAL-ref 0.14-bp160.1.13 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-UNIVERSAL-ref 0.14-bp160.1.13 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-UNIVERSAL-ref 0.14-bp160.1.13 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms