1/11/2022

Operators and Control Structure in PERL


Operators in PERL :

PERL has different kind of operators. In this article we are going to get familiar with them. Seven different categories of operators in PERL are listed below. These are used used every now and then in VLSI related scripting.

1. Assignment:

= is the normal assignment operator

+= -= *= /= ++ -- %= are all the same as in C/C++

2. Numeric:

+ - * / are the same as in C/C++

** is an exponentiation operator

% is the mod operator same as in C/C++

3. Relational:

Numeric comparisons: == != < <= > >=  are the same as in C/C++

String comparisons: eq ne lt le gt ge

RegEx matching/not-matching: =~ !~ (more later)

4. Logical :

|| && !    or and not

5. String:

(dot). is the string concatenation/joining operator:

x is the repetition operator: 

$var = "abc" x 3, turns $var into "abcabcabc"

6. Interpolation Operator : 

$name = "Havard"; 

$gen = "$name University"; # gives "Harding University”

7. File I/O Operators:

< : Read Operator

> : Write Operator

>> : Append Operator

|- : Output Pipe

-| : Input Pipe

Control Structure in PERL :  If-Elsif-Else

If we look carefully to the name above, Else if is the new concept as compare to C-Language . This helps to remove unnecessary nesting of If-Else loops.  When one of the conditions is found to be true, its block is executed and all the remaining branches are skipped.

The Typical Code Organisation for the if-elsif-else blocks is as below :

The code above  is self-explanatory in the way it is written. 

The if statement can be used as inline without its else/elsif counterpart  as :

Now let us look into an example of  the if-elsif-else as below:

The above code is easy to understand. Now you can play with this code by altering the conditions.

If and File-Test Conditions : VLSI Usage 

In the EDA Automation use of PERL , many times we have to check many situations that whether a file is existing on disk or is it zero size or is it writable . All these kind of tests can be done using the already existing file test switches in PERL :

-r -w -x -o : File or directory is readable,  writable, executable, or owned by user

-e : File or directory exists

-zFile exists and has zero size 

-s : File or directory exists and has nonzero size

-fEntry is a plain file

-dEntry is a directory

-lEntry is a symbolic link

-T : File is text

-B : File is binary

These switches are straight forward to use or implement inside the if-condition or elsif-condition . 

Unless-Control-Structure : Checking when the Given Condition is False 

Sometimes, you want to stay idle if the condition is true.  Do something ONLY if the condition is false. 

The unless block serves the purpose. This block of code executes only the provided condition is FALSE.

You may also use the inline version of the Unless as :

Unless Example :

Now let us look into an example of the unless block . Here first we have used a inline-unless. Here intentionally we provided a condition to fail so that the unless code block is executed. At the later part we have used two separate unless blocks to test which is getting executed based on the failure condition.

Once executed the block gives the below output :


If vs Unless Comparison : Example

In this code example we have tested multiple versions of the direct true/false statements , as available in PERL, inside both if and unless condition . This elaborate comparison helps you to understand the usage difference of if vs unless thoroughly.

Once this code is executed , it gives the below output. You may run the code at your end or compare code vs output line by line here while reading , to understand. The choice is yours.


The entire article is well Narrated in the below video :


Courtesy : Image by Jae Rue from Pixabay