Modes of Writing a Perl Code
Last Updated :
23 May, 2019
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:
$var1 = 10;
$var2 = 25;
$result = $var1 + $var2 ;
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 | 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
3 min read
Perl | Sorting of Arrays
Perl has a built-in sort() function to sort an array of alphabets and numbers. When an array is passed to the sort() function it returns a sorted array. Syntax: sort @Array Returns: a sorted array Sorting of Arrays in Perl can be done in multiple ways: Use of ASCII values to sort an Array Use of Com
5 min read
Comparing content of files using Perl
In Perl, we can easily compare the content of two files by using the File::Compare module. This module provides a function called compare, which helps in comparing the content of two files specified to it as arguments. If the data present in both the files comes out to be same, the function returns
2 min read
Perl | Opening and Reading a File
A filehandle is an internal Perl structure that associates a physical file with a name. All filehandles have read/write access, so once filehandle is attached to a file reading/writing can be done. However, the mode in which file handle is opened is to be specified while associating a filehandle. Op
4 min read
Perl | Reading Excel Files
Excel sheets are one of the most commonly used methods for maintaining office records, especially to work on applications where non-developers and even managers can provide input to the systems in batches. However, the issue is to read the content from a file created by Microsoft Excel using Perl. F
4 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 variabl
4 min read
Perl | Date and Time
Perl is one of the most feature-rich programming languages. Perl is portable and cross-platform. Perl can run on over 100 platforms with highly integrated text processing ability. Perl contains the features of different languages like C, sed, awk, and sh etc. which makes the Perl more useful and pro
3 min read
Perl | Warnings and how to handle them
Warning in Perl is the most commonly used Pragma in Perl programming and is used to catch 'unsafe code'. A pragma is a specific module in Perl package which has the control over some functions of the compile time or Run time behavior of Perl, which is strict or warnings. So the first line goes like
4 min read
How to convert 0 into string in Perl?
First off, in most cases, it doesnât matter. Perl is usually smart enough to figure out if somethingâs a string or a number from context. If you have a scalar value that contains a number, you can use it interchangeably as a string or a number based on what operators you use (this is why Perl has di
1 min read
Use of print() and say() in Perl
Perl uses statements and expressions to evaluate the input provided by the user or given as Hardcoded Input in the code. This evaluated expression will not be shown to the programmer as it's been evaluated in the compiler. To display this evaluated expression, Perl uses print() function and say() fu
3 min read