UNIVERSAL(3perl)
π UNIVERSAL β the manual page, but make it slay π
okay bestie, gather round, because we're talking about UNIVERSAL, which lives in the Perl Programmers Reference Guide, and it is literally the mother of all classes. Like, the main character of the entire inheritance tree. π
β¨ NAME β¨
UNIVERSAL β the base class for ALL classes, aka all your blessed references. Every single one. No exceptions, no cap. π
π¦ SYNOPSIS π¦
here's the vibe, copy-paste ready:
my $obj_is_io = $fd->isa("IO::Handle");
my $cls_is_io = Class->isa("IO::Handle");
my $obj_does_log = $obj->DOES("Logger");
my $cls_does_log = Class->DOES("Logger");
my $obj_sub = $obj->can("print");
my $cls_sub = Class->can("print");
my $eval_sub = eval { $ref->can("fandango") };
my $ver = $obj->VERSION;
# but never do this!
my $is_io = UNIVERSAL::isa($fd, "IO::Handle");
my $sub = UNIVERSAL::can($obj, "print");
bestie. do you see that comment at the bottom? # but never do this! β that's not a suggestion, that's the author being like "please, I'm begging you." π
πΈ DESCRIPTION πΈ
UNIVERSAL is the base class from which all blessed references inherit. (Pop over to perlobj if you want the lore.)
UNIVERSAL provides the following methods β and these are the ones you'll actually be using, so let's get into it:
π
$obj->isa( TYPE ) / CLASS->isa( TYPE ) / eval { VAL->isa( TYPE ) }
Where:
TYPEβ is a package name$objβ is a blessed reference or a package nameCLASSβ is a package nameVALβ is any of the above, or an unblessed reference
When you use it as an instance or class method ($obj->isa( TYPE )), isa returns true if $obj is blessed into package TYPE or inherits from package TYPE.
When you use it as a class method (CLASS->isa( TYPE ), sometimes called a static method), isa returns true if CLASS inherits from (or is itself) the name of the package TYPE, or inherits from package TYPE.
If you're not sure what you're holding (the VAL situation, we've all been there), wrap the method call in an eval block to catch the exception that pops off if VAL is undefined or an unblessed reference. The isa operator is an alternative that just quietly returns false in that case, so the eval isn't even needed. slay.
If you want to be sure you're calling isa on an instance and not a class, check the invocant with blessed from Scalar::Util first, like so:
use Scalar::Util 'blessed';
if ( blessed( $obj ) && $obj->isa("Some::Class") ) {
...
}
π§ $obj->DOES( ROLE ) / CLASS->DOES( ROLE )
DOES checks whether the object or class performs the role ROLE. A role is a named group of specific behavior (often methods with particular names and signatures), kind of like a class, but not necessarily a complete class on its own. For example, logging or serialization may be roles. it's giving "vibe, not identity." β¨
DOES and isa are similar in that if either one is true, you know the object or class you called the method on can perform specific behavior. But DOES is different from isa because it does not care how the invocant performs the operations β merely that it does. (isa of course mandates an inheritance relationship. Other relationships include aggregation, delegation, and mocking.)
By default, classes in Perl only perform the UNIVERSAL role, plus the role of all classes in their inheritance. In other words, by default DOES responds identically to isa. twins!
There's a relationship between roles and classes, because each class implies the existence of a role of the same name. There's also a relationship between inheritance and roles, in that a subclass that inherits from an ancestor class implicitly performs any roles its parent performs. So you can use DOES in place of isa safely β it will return true in all the places where isa returns true (provided that any overridden DOES and isa methods behave appropriately). iconic behaviour, honestly.
π«Ά $obj->can( METHOD ) / CLASS->can( METHOD ) / eval { VAL->can( METHOD ) }
can checks whether the object or class has a method called METHOD. If it does, it returns a reference to the sub. If it doesn't, it returns undef. This includes methods inherited or imported by $obj, CLASS, or VAL.
Here's the tea though: can cannot know whether an object will be able to provide a method through AUTOLOAD (unless the object's class has overridden can appropriately), so a return value of undef doesn't necessarily mean the object won't be able to handle the method call. To get around this, some module authors use a forward declaration (see perlsub) for methods they'll handle via AUTOLOAD. For those 'dummy' subs, can will still return a code reference which, when called, will fall through to the AUTOLOAD. And if no suitable AUTOLOAD is provided, calling the coderef will cause an error. π
You may call can as a class (static) method or as an object method. flexible queen.
Again, the same rule about having a valid invocant applies β use an eval block or blessed if you need to be extra paranoid.
β VERSION ( [ REQUIRE ] )
VERSION returns the value of the variable $VERSION in the package the object is blessed into. If REQUIRE is given, then it will do a comparison and die if the package version is not greater than or equal to REQUIRE, or if either $VERSION or REQUIRE is not a "lax" version number (as defined by the version module). so like, yes, it will absolutely end you if the numbers don't math.
The return from VERSION will actually be the stringified version object using the package $VERSION scalar, which is guaranteed to be equivalent but may not be precisely the contents of the $VERSION scalar. If you want the actual contents of $VERSION, use $CLASS::VERSION instead. noted. π
VERSION can be called as either a class (static) method or an object method.
π₯ WARNINGS π₯
NOTE:candirectly uses Perl's internal code for method lookup, andisauses a very similar method and cache-ing strategy. This may cause strange effects if the Perl code dynamically changes@ISAin any package. π
You may add other methods to the UNIVERSAL class via Perl or XS code. You do not need to use UNIVERSAL to make these methods available to your program β and bestie, you should not do so. don't. seriously. π
ββοΈ
π EXPORTS π
None. that's it, that's the section.
Previous versions of this documentation suggested using isa as a function to determine the type of a reference:
$yes = UNIVERSAL::isa($h, "HASH");
$yes = UNIVERSAL::isa("Foo", "Bar");
The problem? This code would never call an overridden isa method in any class. embarrassing for the old docs, honestly. Instead, use reftype from Scalar::Util for the first case:
use Scalar::Util 'reftype';
$yes = reftype( $h ) eq "HASH";
and the method form of isa for the second:
$yes = Foo->isa("Bar");
and that's the whole page, bestie. you understood it, you're thriving, you're the main character of your own inheritance tree. periodt. π
this has been UNIVERSAL(3perl) from perl v5.44.0, dated 2026-10-17, brought to you by the Perl Programmers Reference Guide. mwah πβ¨
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms