The Wayback Machine - https://web.archive.org/web/20241005052336/https://www.geeksforgeeks.org/modes-of-writing-a-perl-code/
Open In App

Modes of Writing a Perl Code

Last Updated : 23 May, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Perl is a general purpose, high level interpreted and dynamic programming language. Perl supports both the procedural and Object-Oriented programming. At the beginning level, Perl was developed only for the system management and text handling but in later versions, Perl got the ability to handle regular expressions, and network sockets, etc. Since Perl is a lot similar to other widely used languages syntactically, it is easier to code and learn in Perl.

Perl is a free-form language which means it can be written, formatted and indented as per user’s requirement. A Perl program consists of a sequence of statements, loops, subroutines, etc. that allows moving around within the code easily. Every statement in a Perl code must end with a semicolon(;). Like other languages, Perl also supports multiple modes of writing and executing a Perl Code. These modes can be categorized on the basis of their writing compatibility and mode of execution in the following ways:

  • Interactive Mode
  • Script Mode
  • One-Liner Mode

These modes can be Run on the command line with the use of perl keyword or on the Online IDEs in the form of a block of code. Perl also provides an inbuilt IDE of its own along with the installation package.
 

Interactive Mode

Interactive mode of writing a Perl code means the direct interaction with the interpreter. Interactive mode is a good way to get started as it helps to check the flow of code line by line and makes the debugging process easier. Interactive Mode in Perl can be used on the Command line with the use of Perl Debugger. This interpreter is commonly known as REPL– Read, Evaluate, Print, Loop.
Interactive mode provides an instant development and execution of code without the need to create a temporary file to store the source code. Perl’s inbuilt command line or the Windows command prompt can be used as a REPL with the help of Perl Debugger. This debugger can be used on a Perl program with the help of the following command:

perl -de1

This command will open the debugger mode in the Perl command line as shown below:

In the Interactive mode of Writing a Perl Code, the user has to write the code line by line and it gets executed at the same time.
Example: If we need to add two numbers and display the result then that can be done in the Interactive mode in the following manner:

Interactive mode in Perl can be Run on the command line itself without the use of Perl debugger. This can be done with the use of the following command:

perl -e Code_statement;

This statement uses the -e flag to avoid the creation of a script and allows the code to Run on the command line without the debugger.
Example:

This method of writing in an Interactive mode will not allow the user to write a Multiline code as in the debugger. If a program is long then this mode will not be preferred.

Interactive mode is good for the beginner programmers to learn the basics of programming, but if you are working with more than a few lines of code then this mode can become clumsy and tedious.
 

Script Mode

Script Mode in Perl is used to write Perl programs which have more than a few lines of code and are a bit complex for the Interactive mode. Script mode in Perl can be used by the help of a text editor to write the Perl program and to save it in a file called a script and then execute the saved file by using the command line. This file must be saved with a .pl extension and should be placed in the same folder of which the directory path is given to the command line. This script is further run in the command line using the command:

perl File_Name.pl

Example: Code is written in a text editor(notepad, etc.) and is saved as Perl_program.pl script.

Now, run the following command in the Command line to execute the Script saved as Perl_program.pl

perl Perl_program.pl

Output:

Script Mode in Perl, unlike the Interactive mode, cannot produce output for the expression individually. In the Interactive mode, the expression gets evaluated and the value is displayed by itself but in the Script mode, the expression will be evaluated but it will not display any result until asked to do so.

Script mode is also implemented in the online IDEs, which are used to write and execute the perl code without manually storing them in a File. In these IDEs, the code when compiled gets stored into the memory in the form of a temporary file which is of use only till the code is being executed and the IDE is open in the browser. Once refreshed, this temporary file is deleted and the space occupied in the memory is freed.
Online IDEs have made the execution of Codes easier as they need less effort as compared to the Script mode in which the files are to be stored in the system’s memory. This makes the code compilation and execution faster. These online IDEs in spite of being an ease to the programmer also come with certain limitations, like, these IDEs can’t perform the File Handling operations until and unless the file is uploaded on their server which might be a risk to some Important data. Such kind of File handling operations can be easily done on the command line compilers.
Following is an example of a Perl code of addition of two numbers running on an Online IDE:




#!/usr/bin/perl
# Program to add two numbers
  
# Assigning values to variables
$var1 = 10;
$var2 = 25;
  
# Evaluating the result
$result = $var1 + $var2;
  
# Printing the result
print "Result after addition is: $result";


Output:

Result after addition is: 35

 

One-liner Mode

Perl also provides a one-liner mode, which allows to type and execute a very short script of code directly on the command line. This is done to avoid the creation of Files to store the script for codes which are not very lengthy. These codes can be typed on a single line in the command line mode with the help of the following command:

perl -e

This command is used to write and execute the one-liner code in the command line by writing it in the double quotes. The -e flag in the upgiven command tells the compiler that the script of the code is not stored in any kind of file but is written in the double codes immediately after this flag.
Example:

In Linux/Unix, these double quotes are replaced with single quotes and vice versa.

These one-liners can be very useful for making changes very quickly like finding information, changing file contents, etc. Some programmers avoid using one-liners because they might become clumsy when script is a bit lengthy. While some programmers enjoy doing this because one-liners are faster than the scripts because there is no need to store them into files.



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 | Writing to a File
A filehandle is a variable that is used to read and write to a file. This filehandle gets associated with the file. In order to write to the file, it is opened in write mode as shown below: open (FH, ‘>’, “filename.txt”); If the file is existing then it truncates the old content of file with the new content. Otherwise a new file will be created
3 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
Perl | Loops (for, foreach, while, do...while, until, Nested loops)
Looping in programming languages is a feature which facilitates the execution of a set of instructions or functions repeatedly while some condition evaluates to true. Loops make the programmers task simpler. Perl provides the different types of loop to handle the condition based situation in the program. The loops in Perl are : for Loop "for" loop
7 min read
Perl | Comparing Scalars
Prerequisite: Scalars in Perl Perl has two types of comparison operator sets. Just like other mathematical operators, instead of performing operations, these operators compare scalars. There are two types of sets of Perl comparison operators. One is for numeric scalar values and one is for string scalar values. Both the types are illustrated in bel
6 min read
Perl | String functions (length, lc, uc, index, rindex)
String in Perl is a sequence of character enclosed within some kinds of quotation marks. Perl string can contain UNICODE, ASCII and escape sequence characters. Perl provides the various function to manipulate the string like any other programming language. Some string functions of Perl are as follows: length() lc() uc() index() rindex() length(): T
4 min read
Perl | Arrays
In Perl, array is a special type of variable. The array is used to store the list of values and each object of the list is termed as an element. Elements can either be a number, string, or any type of scalar data including another variable. Example: @number = (50, 70, 46); @names = ("Geeks", "For", "Geeks"); Array Creation: In Perl programming ever
6 min read
Perl | Data Types
Data types specify the type of data that a valid Perl variable can hold. Perl is a loosely typed language. There is no need to specify a type for the data while using in the Perl program. The Perl interpreter will choose the type based on the context of the data itself. There are 3 data types in Perl as follows: ScalarsArraysHashes(Associative Arra
3 min read
Perl | String Operators
Operators are the foundation of any programming language. Thus, the functionality of Perl programming language is incomplete without the use of operators. A user can define operators as symbols that help to perform specific mathematical and logical computations on operands. String are scalar variables and start with ($) sign in Perl. The String is
4 min read
Perl | Hashes
Set of key/value pair is called a Hash. Each key in a hash structure are unique and of type strings. The values associated with these keys are scalar. These values can either be a number, string or a reference. A Hash is declared using my keyword. The variable name is preceded by the dollar sign($)followed by key under curly braces and the value as
5 min read
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 the host language and is not the same as in PHP, Python, etc. Sometimes it is termed as “Perl 5 Compatible Regular Expressions“. To use the Regex, Binding operators like ‘=~‘(Regex Operator) and
2 min read
Perl | Regex Cheat Sheet
Regex or Regular Expressions are an important part of Perl Programming. It is used for searching the specified text pattern. In this, set of characters together form the search pattern. It is also known as regexp. When user learns regular expression then there might be a need for quick look of those concepts which he didn't use often. So to provide
6 min read
Introduction to Perl
Perl is a general-purpose, high level interpreted and dynamic programming language. It was developed by Larry Wall, in 1987. There is no official Full form of the Perl, but still, the most used expansion is "Practical Extraction and Reporting Language". Some of the programmers also refer Perl as the "Pathologically Eclectic Rubbish Lister" Or "Prac
9 min read
Perl | Regex Character Classes
Character classes are used to match the string of characters. These classes let the user match any range of characters, which user don’t know in advance. Set of characters that to be matched is always written between the square bracket []. A character class will always match exactly for one character. If match not found then the whole regex matchin
3 min read
Perl | Special Character Classes in Regular Expressions
There are many different character classes implemented in Perl and some of them used so frequently that a special sequence is created for them. The aim of creating a special sequence is to make the code more readable and shorter. The Special Character Classes in Perl are as follows: Digit \d[0-9]: The \d is used to match any digit character and its
4 min read
Perl | Subroutines or Functions | Set - 2
Prerequisite: Subroutines or Functions in Perl 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 this article we will di
4 min read
Perl | Operators | Set - 2
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). Some of the operators already discussed in Perl Operators Set - 1. Remaining operators will be d
7 min read
Perl | qw Operator
The qw operator in Perl is used to extract each element of the given string as it is in an array of elements in single-quote ( ' ' ). This function stands for quote word because it considers each word of the given string as it is quoted like qw(Geeks for Geeks) is equivalent to ('Geeks', 'for', 'Geeks'). This qw() uses parentheses so it seems like
2 min read
Perl | Reverse an array
Reverse an array or string in Perl. Iterative Way: Iterate over the array from 0 to mid of array. Swap the arr[i] element with arr[size-i] element. #Perl code to reverse an array iteratively #declaring an array of integers @arr = (2, 3, 4, 5, 6, 7); # Store length on array in $n variable $n = $#arr; #Print the original array print "The origina
2 min read
Packages in Perl
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. There
4 min read
Perl | Scope of Variables
The scope of a variable is the part of the program where the variable is accessible. A scope is also termed as the visibility of the variables in a program. In Perl, we can declare either Global variables or Private variables. Private variables are also known as lexical variables. Scope of Global Variables Global variables can be used inside any fu
6 min read
Perl | References
In Perl, we use variables to access data stored in a memory location(all data and functions are stored in memory). Variables are assigned with data values that are used in various operations. Perl Reference is a way to access the same data but with a different variable. A reference in Perl is a scalar data type that holds the location of another va
4 min read
Article Tags :