C. Prayline Rajabai
Assistant Professor
SENSE
VIT University
TOPICS
History and Concepts of PERL
cfg
Design
Synth
Formal
Testcase
Cfg
Project Testbench
Sim_RTL
Sim_GLS
log
SDF
Gate
Netlist Netlist
4
Library
NEED OF PERL SCRIPTS IN VLSI
All the design and tesbench files(.v) need to be
compiled/ elaborated/ simulated
Automation is achieved
5
NEED OF PERL SCRIPTS CONTD.,
Managing multiple associated files for
reading/executing
#!/usr/bin/perl
Types of Variables
Scalar variables - $
Array variables - @
Hash variables - %
9
SCALAR VARIABLES - $
Examples:
$a = 10;
$name = Tinu;
$average = 24.57;
My name is Tinu
My name is $name
11
EXPRESSIONS WITH SCALARS
Expression using scalar variables are similar to C
syntax
Examples:
$a = 10;
$a++; $a--;
$a = $b ** 10; ->Exponentiation
$a = $b%10; -> Modulus
$bal = $bal + $deposit;
$bal += $deposit;
12
OPERATIONS ON STRINGS
Concatenation using dot operator (.)
Example:
$a = Good;
$b = Morning;
$c = \n;
$d = $a.$b.$c;
Example:
$a = Good;
$b = $a x 2; 13
OPERATIONS ON STRINGS CONTD.,
Escaping (\) Escapes all the perl special
characters like $, @, #, etc.,
terminate
LIST AND ARRAYS
List is a ordered list of scalars
Example:
(10,20,$a,40)
(red,green,blue,yellow)
()
15
(1..100) -> .. List constructor
INITIALIZING AN ARRAY
@colors = (red,green,blue,yellow);
Example:
@months1 = @months2;
16
ARRAY MANIPULATIONS
Accessing Array Elements Using array indices
Example:
@list = (1,2,3,4);
$first = $list[0];
$last = $list[3];
$list[1]++ -> Array becomes (1,3,3,4)
$list[2] = hi; -> Array becomes (1,3,hi,4);
Output : 4
17
ARRAY MANIPULATIONS CONTD.,
Shift and unshift operations These operations are
performed on the first element of the array
Example:
$first = shift@colors ;
Unshift(@colors,white);
18
#@colors -> (white blue green black)
ARRAY MANIPULATIONS CONTD.,
Pop and push operations These operations are performed
on the last element of the array
Example:
$last = pop@colors ;
push(@colors,white);
19
#@colors -> (red blue green white)
ARRAY MANIPULATIONS CONTD.,
Sorting an array - sort keyword sorts the elements of the
array lexicographically.
Example:
@num = qw(10 2 5 25 15 1)
@new = sort@num
# @new -> (1 10 15 2 25 5)
Example:
@removed = splice(@num,1,2);
21
HASHES
Hashes are also known as associative arrays
Each key has its corresponding value -> keys must be unique
keys
Example:
%marks = ( a => 50,
b => 20,
c => 60,
d => 80 ); values
22
INITIALIZING HASH ARRAY
There are two ways to specify the hash array
Example1:
%directory = (0001 , Shruthi,
0002 , Surya,
0004 , Ravi,
0005 , Rani);
Example2:
%directory = (0001 => Shruthi,
0002 => Surya,
0004 => Ravi,
0005 => Rani );
23
CONVERSION OF ARRAY TO HASH
An array can be converted to hash.
Example:
@list = qw (0001 Shruthi 0002 Surya 0004 Ravi 0005 Rani);
%directory = @list;
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' =>40);
print "$data{'Lisa'}\n";
print "$data{'Kumar'}\n";
Output :
45
30
40 25
EXTRACTING KEYS AND VALUES
We can get the list of all of the keys from a hash by
using keys function
Example :
%data = ('John Paul' => 45,
'Lisa' => 30, 'Kumar' => 40);
@names = keys %data; Output:
print "$names[0]\n"; Kumar
print "$names[1]\n"; Lisa
print "$names[2]\n"; John Paul 26
EXTRACTING KEYS AND VALUES CONTD.,
Similarly we can get the list of all the values from a hash by
using values function.
Example :
%data = ('John Paul' => 45,
'Lisa' => 30, 'Kumar' => 40);
@ages = values %data; Output:
print "$ages[0]\n"; 30
print "$ages[1]\n"; 45
print "$ages[2]\n"; 40 27
ADD & REMOVE ELEMENTS IN HASHES
Example :
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
@keys = keys %data;
$size = @keys;
print "1 - Hash size: is $size\n";
# adding an element to the hash;
$data{'Alina'} = 55;
@keys = keys %data;
$size = @keys; Output:
print "2 - Hash size: is $size\n"; 1 - Hash size: is 3
# delete the same element from the hash;
2 - Hash size: is 4
delete $data{'Alina'};
3 - Hash size: is 3
@keys = keys %data;
$size = @keys;
28
print "3 - Hash size: is $size\n";
SWAPPING KEYS AND VALUES
We can swap the key/value pairs of a hash variable using
reverse function
Example :
%data = ('John Paul' => 45, 'Lisa' => 30,
'Kumar' => 40);
%revdata = reverse %data;
29
CONTROL STRUCTURES
Control structures in PERL are similar to those in C.
for
foreach
If/elseif/else
while
do-while, etc
Value 0
Empty string ( )
Undefined value
31
IF/ELSEIF/ELSE
Syntax :
if (test expression) {
# If test expression is true do this
} else{
#if test expression is false do this
}
Example
if ($name eq VIT) {
print(Welcome VITan\n);
} else {
print(You are not a VITan\n);
}
32
Else block is optional
ELSEIF EXAMPLE
Example
chomp($name = <STDIN>);
if ($name eq pray) {
} else {
while(condition)
{
statement(s);
}
Example
$secret_code = IND;
while ($choice ne $secret_code) {
print Enter your Guess : \n;
chomp($choice = <STDIN>);
}
print Your guess is correct!\n;
34
FOR LOOP
Syntax
for ( init; condition; increment ) {
statement(s);
}
Example:
for($i = 1; $i <= 10; $i++) {
print $i\n;
}
35
FOREACH LOOP
Foreach Function to iterate over a list
Syntax
foreach var (list) {
statement(s);
}
37
UNLESS LOOP
In if control structure, the block of code is
executed only when the conditional expression is
true.
Example :
$j = 1;
$i = 20;
until ($j > $i) {
$j *= 2;
}
Equal == Eq
Not equal != Ne
40
41
WRITE A PROGRAM TO COUNT THE ODD NUMBERS
IN A LIST.
$count = 0;
if($number%2 == 1) {
$count++;
43
PROGRAM
$secret = int(1 + rand 100); elsif ($guess < $secret) {
44
PRACTICE PROGRAMS
1. Write a perl script to print every element of an
array using foreach and while loop.