Search⌘ K
AI Features

The UNIVERSAL Package

Explore Perl's UNIVERSAL package, the base class for all Perl packages. Learn to use its core methods like VERSION, DOES, can, and isa to manage class roles, check method availability, and handle inheritance, enabling you to write more maintainable and idiomatic Perl code.

Perl’s built-in UNIVERSAL package is the ancestor of all other packages—it’s the ultimate parent class in an object-oriented sense. UNIVERSAL provides a few methods for its children to use, inherit, or override.

The VERSION() method

The VERSION() method returns the value of the $VERSION variable of the invoking package or class. If we provide a version number as an optional parameter, the method will throw an exception if the queried $VERSION is not equal to or greater than the parameter.

Given a HowlerMonkey module of version 1.23, its VERSION() method behaves as follows:

Perl
package HowlerMonkey {
our $VERSION = 1.23;
use Moose
#...
}
my $hm = HowlerMonkey->new;
say HowlerMonkey->VERSION; # prints 1.23
say $hm->VERSION; # prints 1.23
say $hm->VERSION( 0.0 ); # prints 1.23
say $hm->VERSION( 1.23 ); # prints 1.23
say $hm->VERSION( 2.0 ); # exception!

There’s little reason to override VERSION().

The DOES() method

The DOES() method supports the use of roles in programs. Pass it an invocant and the name of a role, and the method will return true if the ...