UNIVERSAL Package in Perl Programming

UNIVERSAL is a built-in package in Perl 5. It can also be thought of as a base class in which the derived classes can implicitly inherit or override. It offers several default methods like isa(), can(), VERSION() and DOES(). The UNIVERSAL class cannot show in the package’s @ISA variable. Other methods can also be added to the UNIVERSAL class through Perl or XS code. There is no need to include ‘use UNIVERSAL‘ statements in code to make use of the above-mentioned methods in the program.

Version() Method

It returns the value of the $VERSION variable of the suitable class or package. This method can take a version number as an optional parameter. In case, the queried $VERSION is not greater than or equal to the parameter, this method throws an exception.

Example:
Let’s consider a module Dog of version 2.0

filter_none

edit
close

play_arrow

link
brightness_4
code

use 5.010;
package Dog 2.0;
use strict;
use warnings;
  
# constructor
sub new
{
    # the package name 'Dog' is in the default array @_
    # shift will take package name 'Dog' 
    # and assign it to variable 'class'
    my $class = shift;
      
    # object
    my $self = {
        'name' => shift,
        'breed' => shift
    };
      
    # blessing self to be object in class
    bless $self, $class;
      
    # returning object from constructor
    return $self;
}
  
my $d = new Dog('Sammy','Pug');
  
say Dog->VERSION();
say $d->VERSION();
say $d->VERSION(0.0);
say $d->VERSION(1.5);
say $d->VERSION(3.0);    # EXCEPTION

chevron_right


Output:

2.0
2.0
2.0
2.0
Dog version 3 required--this is only version 2.0 at /home/4c09f6973dc56e8a558b8be499a040bb.pl line 32.

DOES() Method

The DOES() method checks whether a class or an object performs a particular role or not. When the name of the role is passed to this method and if it performs the specified role, then it returns true(1) otherwise it returns false(0). The role can be thought of as a collection of behavior shown by a class.



Example:
Let’s consider modules/classes Dog and animal

filter_none

edit
close

play_arrow

link
brightness_4
code

use 5.010;
package Animal;  
  
sub new
{
    my $type = shift;           
    my $self = {};               
    return bless $self, $type;    
}
  
sub MyMethod 
{
   print "Animal::MyMethod called!\n";
}
  
# class Dog    
package Dog;   
  
# class Dog inherits from class Animal
@ISA = qw(Animal);    
  
sub new
{
    my $type = shift;            
    my $self = Animal->new;     
    return bless $self, $type;  
}
sub MyMethod
{
    my $self = shift;
    
    $self->SUPER::MyMethod();
}
   
# Driver Code
package main;      
$myObject = Animal->new();
$myObject2 = Dog->new();
  
# Basic DOES() usage
say Dog->DOES('Animal');
say $myObject->DOES('Animal');
say $myObject2->DOES('Dog');

chevron_right


Output:

1
1
1

can() Method

The can() method accepts the method’s name as a string. It returns a reference to the existing function that implements this method or else it returns a false value. This method can be called on a class, object, or a package.If can($classname) returns true, it means that a class having name $classname exists. This method only checks the existence of a class and does not comment on its usability.

Example:
Let’s consider a class Dog with a method named bark, a reference to this method can be made as follows:

filter_none

edit
close

play_arrow

link
brightness_4
code

use 5.010;
use strict;
use warnings;
  
    package Animal; 
    sub new 
    
        bless({}, 
        $_[0]) 
          
    }
}
  
    package Dog; 
    our @ISA = qw(Animal)
    sub dog { 1 } 
}
  
# objects
my $dog = Dog->new;
my $animal = Animal->new;
  
# Basic can() usage
say $animal->can('dog');  # false
say $dog->can('dog');  # true

chevron_right


Output:

Use of uninitialized value in say at /home/fdfdd52de0b7348de191b3d9be3cb44f.pl line 11.
CODE(0x1fc3168)

can() can also be used to check if a package has successfully implemented a specific function or method or not.

isa() Method

The isa() method accepts the name of a class or the name of a built-in type as a string. It can be called on a class method, an instance method, or an object. It returns a true if a specified class or object is derived from the package, or if it is a blessed reference to the given type.

Example:
Suppose $doggy is an object(a hash reference blessed into the Dog class, which inherits from the Animal class):

filter_none

edit
close

play_arrow

link
brightness_4
code

use 5.010;
use strict;
use warnings;
  
    package Animal; 
    sub new
    
        bless({},
        $_[0]) 
    
}
  
    package Dog; 
    our @ISA = qw(Animal)
    sub dog { 1 } 
}
  
# objects
my $animal = Animal->new;
my $dog = Dog->new;
  
# basic isa() usage 
say $animal->isa('Animal');  # true
say $dog->isa('Animal');  # true

chevron_right


Output:

1
1



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.