Open In App

Packages in Perl

Last Updated : 11 Feb, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

A Perl package is a collection of code which resides in its own namespace. Perl module is a package defined in a file having the same name as that of the package and having extension .pm. Two different modules may contain a variable or a function of the same name. Any variable which is not contained in any package belongs to the main package. Therefore, all the variables being used, belong to the ‘main’ package. With the declaration of additional packages, it is maintained that variables in different packages do not interfere with each other.
 

Declaration of a Perl Module

Name of the module must be the same as that of the package name and has a .pm extension.

Example : Calculator.pm




package Calculator;
  
# Defining sub-routine for Addition
sub addition
{
    # Initializing Variables a & b
    $a = $_[0];
    $b = $_[1];
      
    # Performing the operation
    $a = $a + $b;
      
    # Function to print the Sum
    print "\n***Addition is $a";
}
  
# Defining sub-routine for Subtraction
sub subtraction
{
    # Initializing Variables a & b
    $a = $_[0];
    $b = $_[1];
      
    # Performing the operation
    $a = $a - $b;
      
    # Function to print the difference
    print "\n***Subtraction is $a";
}
1;


Here, the name of the file is “Calculator.pm” stored in the directory Calculator. Notice that 1; is written at the end of the code to return a true value to the interpreter. Perl accepts anything which is true instead of 1.
 

Using a Perl Module

To use this calculator module, we use require or use functions. To access a function or a variable from a module, :: is used. Here is an example demonstrating the same:

Examples: Test.pl




#!/usr/bin/perl
  
# Using the Package 'Calculator'
use Calculator;
  
print "Enter two numbers to add";
  
# Defining values to the variables
$a = 10;
$b = 20;
  
# Subroutine call
Calculator::addition($a, $b);
  
print "\nEnter two numbers to subtract";
  
# Defining values to the variables
$a = 30;
$b = 10;
  
# Subroutine call
Calculator::subtraction($a, $b);


Output:

 

Accessing a Package from a different directory

If a file accessing the package lies outside the directory then we use ‘::’ to tell the path of the module. For example, file using calculator module is outside the calculator package so we write Calculator :: Calculator to load the module, where the value on left of ‘::’ represents package name and value on the right of ‘::’ represents Perl module name. Let’s see an example to understand this:

Examples: Test_out_package.pl outside Calculator directory




#!/usr/bin/perl
  
use GFG::Calculator; # Directory_name::module_name
  
print "Enter two numbers to add";
  
# Defining values to the variables
$a = 10;
$b = 20;
  
# Subroutine call
Calculator::addition($a, $b);
  
print "\nEnter two numbers to subtract";
  
# Defining values to the variables
$a = 30;
$b = 10;
  
# Subroutine call
Calculator::subtraction($a, $b);


Output:

 

Using Variables from modules

Variables from different packages can be used by declaring them before using. Following example demonstrates this
Examples: Message.pm




#!/usr/bin/perl
  
package Message;
  
# Variable Creation
$username;
  
# Defining subroutine
sub Hello
{
  print "Hello $username\n";
}
1;


Perl file to access the module is as below
Examples: variable.pl




#!/usr/bin/perl
  
# Using Message.pm package
use Message;
  
# Defining value to variable
$Message::username = "ABC";
  
# Subroutine call
Message::Hello();


Output:

 

Begin and End Block

BEGIN and END block is used when we want to run some part of the code at the beginning and some part of the code at an end. The codes written within BEGIN{…} are executed at the beginning of the script while the codes written within END{…} are executed at the end of the script. The program below demonstrates this:
Examples: begineg.pl




#!/usr/bin/perl
  
# Predefined BEGIN block
BEGIN
{
    print "In the begin block\n";
}
  
# Predefined END block
END
{
    print "In the end block\n";
}
  
print "Hello Perl;\n";


Output:

In the begin block
Hello Perl;
In the end block


Previous Article
Next Article

Similar Reads

Perl | Basic Syntax of a Perl Program
Perl is a general purpose, high level interpreted and dynamic programming language. Perl was originally developed for the text processing like extracting the required information from a specified text file and for converting the text file into a different form. Perl supports both the procedural and Object-Oriented programming. Perl is a lot similar
10 min read
Perl | chomp() Function
The chomp() function in Perl is used to remove the last trailing newline from the input string. Syntax: chomp(String) Parameters: String : Input String whose trailing newline is to be removed Returns: the total number of trailing newlines removed from all its arguments Example 1: C/C++ Code #!/usr/bin/perl # Initialising a string $string = &quo
2 min read
Perl | Backtracking in Regular Expression
In Perl, a Regular expression(a.k.a regexes or regexps or REs) is a way of describing a set of strings without having to list all strings in your program or simply we can say that it is a sequence of characters that are used for pattern matching. In Perl, regular expressions have different uses: Firstly, they are used in conditionals to determine w
3 min read
Perl | Searching in a File using regex
Prerequisite: Perl | Regular Expressions Regular Expression (Regex or Regexp or RE) in Perl is a special text string for describing a search pattern within a given text. Regex in Perl is linked to host language and are not the same as in PHP, Python, etc. Sometimes these are termed as "Perl 5 Compatible Regular Expressions". To use the Regex, Bindi
6 min read
Perl | Operators | Set - 1
Operators are the main building block of any programming language. Operators allow the programmer to perform different kinds of operations on operands. In Perl, operators symbols will be different for different kind of operands(like scalars and string). Operators Can be categorized based upon their different functionality: Arithmetic OperatorsRelat
12 min read
Perl | lt operator
'lt' operator in Perl is one of the string comparison operators used to check for the equality of the two strings. It is used to check if the string to its left is stringwise less than the string to its right. Syntax: String1 lt String2 Returns: 1 if left argument is less than the right argument Example 1: #!/usr/local/bin/perl # Initializing Strin
1 min read
Perl | chop() Function
The chop() function in Perl is used to remove the last character from the input string. Syntax: chop(String) Parameters: String : It is the input string whose last characters are removed. Returns: the last removed character. Example 1: #!/usr/bin/perl # Initialising a string $string = "GfG is a computer science portal"; # Calling the chop
1 min read
Perl | rename() Function
rename() function in Perl renames the old name of a file to a new name as given by the user. Syntax: rename(old_file_path, new_file_path) Parameters: old_file_path: path of the old file along with its name new_file_path: path of the new file along with its name Returns 0 on failure and 1 on success Example: #!/usr/bin/perl -w # Calling the rename()
1 min read
Perl | Subroutines or Functions
A Perl function or subroutine is a group of statements that together perform a specific task. In every programming language user want to reuse the code. So the user puts the section of code in function or subroutine so that there will be no need to write code again and again. In Perl, the terms function, subroutine, and method are the same but in s
2 min read
Perl | Decision Making (if, if-else, Nested–if, if-elsif ladder, unless, unless-else, unless-elsif)
Decision Making in programming is similar to decision making in real life. In programming, a certain block of code needs to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of the program based on certain conditions. These are used to cause the flow of execution to advance
6 min read
Article Tags :