Learn the Basic Features of Perl Programming Language

0
6375
Perl (2) - More Hands On

Perl (2) - More Hands OnAfter 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
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:

Empty Lines
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.
1
 
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).
 
Syntax

As you see, the function works with and without parenthesis.
 
Example
The following script prints the value of the PI constant.
2
 
When executed, it will simply print the arguments passed to it.
3
 
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.
 
Example
The following script shows the usage of single and double quotes.

Now, let’s see it in action:

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?!
 
Using Semi-colons
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:

  • Scalars
  • Arrays
  • Hashes

 
Learn the Basics of C Programming Language

Scalar 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 $.
 
Examples
The following are examples of how to declare and initialize scalar variables:

Array Variables
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.
 
Examples
The following are examples of array variables declarations:

Hashes
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 ‘%’.
 
Examples
The following are examples of hash variables declarations:

Identifiers
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:

Where VAR is a scalar variable.
Example
The following script prompts the user to enter his name, and responds with a greeting.
4
 
Let’s see how this will behave.
5
 
Although it is very simple and straightforward, I am going to explain the script to highlight few important points:
6

  • 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.

 
Note
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!!

LEAVE A REPLY

Please enter your comment!
Please enter your name here