Perl Syntax Overview - Perl Scripting

what is Perl?

Perl lends syntax and concepts from many languages: awk, sed, C, Bourne Shell, Smalltalk, Lisp and even English. However, there are some slight differences between the languages. This chapter deals with speed on the syntax in Perl where the user gets quick response.
A Perl program usually consists of a sequence of declarations and statements, which run from the top to the bottom. Loops, subroutines, and other control structures are allowed to move around within the code. Every simple statement should end with a semicolon (;).
Perl is a free-form language: you can format and indent it however you like. Whitespace are used mostly to separate tokens, unlike languages like Python where it is an important part of the syntax, or Fortran where it is immaterial. Perl also has syntax which is very important in its usage.

First Perl Program

Name the two programming modes in Perl Program?

Interactive Mode Programming

Perl interpreter can be used by the user with the help of e-option at command line, which helps in executing the Perl statements from the command line. Let's try something at $ prompt as follows −
This execution will produce the following result −

Script Mode Programming

Imagine you are already on $ prompt, let's open a text file hello.pl using vi or vim editor and put the following lines inside your file.
Here /usr/bin/perl is called the perl interpreter binary. Before executing your script, be sure that you change the mode of the script file and give execution priviledge, generally a setting of 0755 works perfectly and finally you execute the above script as follows −
This execution will display the following result −
You can use parentheses for functions arguments or omit them according to your wish. They are only required whenever they need to clarify the issues of precedence. Following two statements produce the same result.

Perl File Extension

A Perl script can be embedded in any normal simple-text editor program. There are numerous programs available for every type of platform. There are many programs designed for programmers available for download on the web.
As a Perl convention, a Perl file must be saved with a .pl or .PL file extension in order to recognize as a functioning Perl script. File names can addressed with numbers, symbols, and letters but must not contain a space. Underscore (_) can be used in places of spaces.

Comments in Perl

Comments in any programming language are friends of developers. They are used to make program user friendly and they are simply skipped by the interpreter without getting disturbed the code functionality. For example, in the above program, a line starting with hash # is a comment.
Comments in Perl start with a hash symbol and run to the end of the line −
Lines starting with = are interpreted as the start of a section of embedded documentation (pod), and all subsequent lines until the next =cut are ignored by the compiler. Following is the example −
This will produce the following result −

Whitespaces in Perl

A Perl program does not care about whitespaces. Following program works perfectly fine −
But if spaces are inside the quoted strings, then they would be printed as is. For example −
This will display the following result −
All types of whitespace like spaces, tabs, newlines, etc. are considered equally to the interpreter when they are used outside of the quotes. A line which contains only whitespace, with a comment, is known as a blank line, and Perl totally ignores it.

Single and Double Quotes in Perl

You can use double quotes or single quotes around literal strings as follows −
This will produce the following result −
There is a difference in single and double quotes. Only double quotes interpolate variables and special characters such as newlines \n, whereas single quote does not interpolate any variable or special character. Check below example where we are using $a as a variable to store a value and later printing that value −
This will display the following result −

"Here" Documents

Here documents are used to store or print multiline text with a great comfort. Variables can be used inside the "here" document. Below, you can see a simple syntax, check carefully there must be no space between the << and the identifier.
An identifier may be considered as a bare word or some quoted text like we used EOF below. If identifier is quoted, that quoted one determines the type of the text inside the here docoment, just as in regular quoting. An identifier that is not quoted works like double quotes.
This will display the following result −

Escaping Characters

What are the Escaping Characters in Perl?

Escaping characters in Perl is backslash (\) character used to escape any type of character that might interfere with our code. Let's take one example where we want to print double quote and $ sign −
This will produce the following result −

Perl Identifiers

A Perl identifier is a name used to identify a variable, function, class, module, or other object. A Perl variable name starts with either $, @ or % followed by zero or more letters, underscores, and digits (0 to 9).
Perl does not allow punctuation characters such as @, $, and % within identifiers. Perl is a case sensitive programming language. Thus $Manpower and $manpower are treated as two different identifiers in Perl.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Perl Scripting Topics