Perl

Use of Perl Modules

The module in Perl is an important feature of Perl programming. Perl has many built-in modules to perform different types of operations and the Perl users can also create their module with the “.pm” extension. The “package” keyword is used to create a user-defined module in Perl. Any module can be imported in any Perl file using the “use” function. The uses of the built-in and user-defined modules in Perl are shown in this tutorial.

Different Examples of Perl Modules

The methods of using the built-in Perl modules and user-defined modules are shown in this part of the tutorial.

Example 1: Using the Built-In Module

In this example, the uses of three built-in modules of Perl are shown. These are “strict”, “warnings”, and “5.34.0”. The “strict” module is used to enable the different types of restrictions to write the Perl scripts. For example, no variable can be declared without the “my” keyword if this module is used in the script. The “warnings” module is used to display the different types of warning messages after executing the script that helps the coder understand the error. The “5.34.0” module is used to use the different features of this module in the script. The “say” function that is used to print the messages with newline is the feature of this module.

#!/usr/bin/perl

#Enable restriction for coding
use strict;
#Display warning message for error
use warnings;
#Enable different services
use 5.34.0;

#Use of 'my' keyword is mandatory for 'strict' module
my $language = 'Perl';
#Enable the use of the 'say' feature
say "Learn $language programming.";

Output:

The following output appears after executing the script:

p1

Example 2: Using the User-Defined Module

The Perl users can create their module for a particular purpose by creating a file with the “.pm” extension. In this example, a user-defined module is created and it is used in another Perl script using the “use” keyword.

Create a User-Defined Module:

Create a file named “Bonus.pm” with the following script. Here, the “package” keyword is used to define that it is a module. This module contains a subroutine that calculates the bonus of an employee based on the salary and the sales amount. Two argument values are passed from the Perl script in which this module is used. The first argument contains the salary and the second argument contains the sales amount. The bonus is 15% of the sales amount if the salary is more than 10000. The bonus is 10% of the sales amount if the salary is more than 7000. The bonus is 5% of the sales amount if the salary is less than 10000. The “1;” is used at the end of the module to return true. Otherwise, an error is printed.

Bonus.pm

#!/usr/bin/perl

use strict;
use warnings;
use 5.34.0;

package Bonus;

#Declare subroutine to calculate bonus
sub calculate_bonus
{
    # Initialize the variables
    my $salary = $_[0];
    my $sales_amount = $_[1];
    my $bonus = 0.0;

    #Calculate bonus
    if ($salary > 10000)
    {
        $bonus = $sales_amount*0.15;
    }
    elsif ($salary > 7000)
    {
        $bonus = $sales_amount*0.10;
    }
    else
    {
        $bonus = $sales_amount*0.05;
    }
    #Return the calculated bonus
    return $bonus;
}

1;

Import a Module in a Perl Script:

Create a Perl file with the following script that imports the “Bonus” module that is created earlier to calculate the bonus amount of an employee based on the salary and sales amount that are assigned in the script.

#!/usr/bin/perl

use strict;
use warnings;
use 5.34.0;

use Bonus;

#Initialize variables
my $name = "Mir Sabbir";
my $salary = 60000;
my $sal_amount = 9700;

#Call subroutine from the module
my $bonus = Bonus::calculate_bonus($salary, $sal_amount);
my $salary_with_bonus = $salary + $bonus;

#Print the employee information based on sales
say "Employee Details:\n";
say "Name: $name";
say "Salary: $salary";
say "Salary(with bonus): $salary_with_bonus";

Output:

The following output appears after executing the script. Here, the “-I.” option has to be used at the time of executing the script to find out the user-defined imported module:

p2

Example 3: Using the User-Defined Module Using “Require”

The “require” function is another way of importing the modules in the Perl script and it is shown in this example. The previously created “Bonus” module is imported into the script using the “require” function. The salary and sales amount values are taken from the user in this script. The other part of the script is similar to the previous example.

#!/usr/bin/perl

use strict;
use warnings;
use 5.34.0;

require Bonus;

#Take input from the user
say "Enter the employee name:";
chomp(my $name = <>);
say "Enter the salary:";
chomp(my $salary = <>);
say "Enter the sales amount:";
chomp(my $sal_amount = <>);


#Call subroutine from the module
my $bonus = Bonus::calculate_bonus($salary, $sal_amount);
my $salary_with_bonus = $salary + $bonus;

#Print the employee information based on sales
say "Employee Details:\n";
say "Name: $name";
say "Salary: $salary";
say "Salary(with bonus): $salary_with_bonus";

Output:

The following output appears after executing the script:

p3

Conclusion

The uses of both built-in and user-defined modules in Perl are shown in this tutorial using simple examples.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.