After finishing the introduction and writing our first program, more hands-on are needed to get more familiar with Perl. In this article, we are also going to discover some of features of the Perl language syntax. Here we go!!
White spaces before, after, and within Perl statements are totally ignored. This is unlike what we see in languages like Python and UNIX/Linux Shell. For instance, Python language uses indentation (shifting the line by a number of white spaces) to distinguish a function code, a loop body, class definition, code inside an if statement, etc. In such languages, any extra “unexpected” white space will cause the interpreter to report error. Fortunately, Perl doesn’t care about these white spaces at all. So, use them freely.
For example, the following two statements are identical:
print "Hello, I am a Perl programmer"
print "Hello, I am a Perl programmer"
Like white spaces, the Perl interpreter also ignores empty lines in your script files.
So, the hello.pl program could be like this, and still has the same output.
Printing to Standard Output
What we have done in our first script is just printing a text message to the standard output. To write some data to the standard output, use the print function. print is a built-in function that takes one or more arguments and prints them all to the screen (or to a file as we will see later in this series).
print (“text to print”)
print (“text”, “to”, “print”)
print “text”, “to”, “print”
As you see, the function works with and without parenthesis.
The following script prints the value of the PI constant.
When executed, it will simply print the arguments passed to it.
Single and Double Quotes
To delimit a text, we can use either single or double quotes. Both single and double quotes behave almost the same way in most cases. The only difference is that content within single quotes is treated literally, which is not the case in double quotes. To understand the difference, it is better to illustrate it by an example.
The following script shows the usage of single and double quotes.
#This script illustrates the usage of quotes
#Date: Sep 2015
print ("Hello, I am a Perl Programmer\n");
print ('Hello,I am a Perl Programmer\n');
Now, let’s see it in action:
[root@server01 Perl]# ./quotes.pl
Hello, I am a Perl Programmer
Hello,I am a Perl Programmer\n[root@server01 Perl]#
Have you seen this?! The characters in the double-quoted string have been interpreted to their meanings; taking into consideration that \n represents a newline character. On the other hand, the same string with the exact characters has been treated (printed) literally when enclosed by single quotes, so \n has been printed as \n (not as a new line).
A very big difference, isn’t it?!
Take it as a rule:
A Perl statement must be terminated with a semi-colon, unless it is the final statement in a block. In this case the semicolon is optional.
Variables in Perl
Perl supports three main types of variables:
Scalar variables are the normal ordinary variables supported by all programming languages. A scalar variable can hold one value at a time. This value could be a number or a string. Scalar variables are identified from other variables types by being preceded by a dollar sign $.
The following are examples of how to declare and initialize scalar variables:
$firstname = “Ahmed”;
$Player_Name = ‘Alessandro Del Piero’;
$PI = 3.14;
Arrays can be defined as ordered collections of values (scalars). Individual values can be accesses using a zero-based index. Arrays’ Names in Perl are preceded by the ‘@’ character.
The following are examples of array variables declarations:
@numbers = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
@COUNTRIES = (“England”, “Germany”, “Italy”, “Brazil”, “India”);
@Names = (“Mohamed Sherif”, “Ahmed Ali”, “Ahmed Ramzy”);
Some references call it Associative Arrays. Hashes are collections of key-value pairs. They can be considered as a special type of arrays that use keys for indexing instead of the ordered numeric indexes. Hashes are identified by being preceded by the percent character ‘%’.
The following are examples of hash variables declarations:
%student_name_BN = (“Ahmed”, 183, “Said”, 676, “Gaafer”, 435, “Hamza”, 448);
%Machine_hosts_IPs = (“websrv”, ‘192.168.1.10’, “dnssrv”, ‘192.168.1.250’, “defroute”, ‘192.168.1.1’);
Identifiers are unique names, used to identify variables (scalars, arrays, or hashes), and subroutines. Perl identifiers must:
- Start with either a letter or underscore.
- Not contain any special characters.
Also, you should know that Perl identifiers are case sensitive. So, $counter, $Counter, and $COUNTER are considered different names for different scalar variables.
Getting User’s Input from STDIN
You can prompt the user to enter a line of text at the standard input, and assign the input line to a scalar variable for further processing. The syntax for this operation is as follows:
$VAR = <stdin>;
Where VAR is a scalar variable.
The following script prompts the user to enter his name, and responds with a greeting.
Let’s see how this will behave.
Although it is very simple and straightforward, I am going to explain the script to highlight few important points:
- Line 1: The Shebang.
- Lines 2-5: comments that document the purpose of the script, its author, and the date it was written.
- Line 6: prints a message to the user, asking him to enter his name.
- Line 7: uses the line-input operator <stdin> to get a line of text from the user. the input line is then assigned to the scalar variable $name.
- Line 8: prints the welcome message containing the user’s name.
Notice that the welcome message (printed by Line 8) was printed correctly, without the need to print the new line character “\n” at the end. This is because inputs read using the <stdin> operator contain a “hidden” trailing new line character. In this example, this trailing newline was useful. Later, we will go into situations wherein we need to get rid of this trailing newline.
* * * * * *
In this article, we have talked about the basic features of the Perl syntax. We have gone very quickly through the main types of variables used in Perl. Also, we have learned how to get input from STDIN and write output to STDOUT.
More to come, so, just wait!!