Modes of Writing a Perl Code - GeeksforGeeks

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 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 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 resultprint "Result after addition is: $result";
Result after addition is: 35

 
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.
Perl
Perl
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Perl Tutorial - Learn Perl With Examples
Perl | Hashes
Perl | Opening and Reading a File
Perl | Basic Syntax of a Perl Program
Perl | Multidimensional Hashes
How to Install Perl on Windows?
Perl | CGI Programming
Perl | Sorting of Arrays
Perl | Operators | Set - 1
Perl | Data Types