Anda di halaman 1dari 45

PERL PRACTICAL

EXTRACTION AND REPORT


LANGUAGE

C. Prayline Rajabai
Assistant Professor
SENSE
VIT University
TOPICS
History and Concepts of PERL

Scalar Data, Arrays and List Data, Control structures,


Hashes

Basics I/O, Regular Expressions

Functions, Miscellaneous control structures

Formats, Directory access

File and Directory manipulation

process management, System database access

User data manipulation.


2
HISTORY AND CONCEPTS OF PERL
PERL Practical Extraction and Report Language
Developed by Larry Wall
Easy Language to learn and use.
It is a scripting language Why in VLSI?
To make the impossible harder job easy.

The Perl slogans:


There's more than one way to do it (TMTOWTDI)
Easy things should be easy and hard things should be
possible.
3
TYPICAL SCENARIO
RTL

cfg
Design
Synth

Formal

Test Bench Lint

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

Each process may generate other files(log files)

Testcase run will generate its own log files

N number of testcase will create n no. of files.

How to check whether each testcase is passed/ failed?


Perl Script It parses through all the files and generate the
report of pass/fail list.

Automation is achieved
5
NEED OF PERL SCRIPTS CONTD.,
Managing multiple associated files for
reading/executing

Generating a report in .xls format

Creating a specific directory structure

Managing a file for any change in names

Reading a specific content in multiple files

Managing command line inputs

Automation of VLSI Verification process


6
PERL - INTRODUCTION
Scripting language used basically for test data
manipulation

Interpreted language does not need a special


compiler to turn the script to working code

Runs on almost all platforms

Does not require any special editor Notepad or vi


editor is sufficient

Loosely typed language


7
FIRST PERL PROGRAM

#!/usr/bin/perl

#My first program in PERL

Print Hello World\n;

How to execute the program?

Enter the program in a test file and run it.

Perl does not limit the size of the data.


8
VARIABLES

Types of Variables

Scalar variables - $

Array variables - @

Hash variables - %

9
SCALAR VARIABLES - $
Examples:

$a = 10;

$name = Tinu;

$average = 24.57;

Variables do not have any fixed data types hence called as


loosely packed language

Scalar variables can be integer, floating point, string

How to print scalar variables?

Print My name is $name\n; 10


VARIABLE INTERPOLATION
Variable names are replaced by their values within double
quotes variable interpolation

Print My name is $name\n;

My name is Tinu

Print My name is $name\n;

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;

Repetition Operator (x)

Example:
$a = Good;
$b = $a x 2; 13
OPERATIONS ON STRINGS CONTD.,
Escaping (\) Escapes all the perl special
characters like $, @, #, etc.,

Print The value of \$a is $a\n;

Variable interpolation will not happen

Line Oriented Quoting (<<) To print multiple lines


Example:
Print << terminate;
Hello
How are you? 14

terminate
LIST AND ARRAYS
List is a ordered list of scalars

Array is a variable that holds a list denoted by @ symbol

Element of an array is a scalar

How much can be the array size ?


Minimum 0

Maximum any based on virtual memory

Example:
(10,20,$a,40)
(red,green,blue,yellow)
()
15
(1..100) -> .. List constructor
INITIALIZING AN ARRAY

@colors = (red,green,blue,yellow);

@colors = qw(red green blue yellow);

Contents of one array can be assigned to another array.

Example:

@months1 = @months2;

@total_colors = qw(black @colors white);

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);

$# is the index of the last element of the array

Print Output : $#list\n;

Output : 4
17
ARRAY MANIPULATIONS CONTD.,
Shift and unshift operations These operations are
performed on the first element of the array

Shift removes the first element of the array

Unshift inserts a new element at the start of the array.

Example:

@colors = qw(red blue green black);

$first = shift@colors ;

# $first -> red and @colors -> (blue green black)

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

Pop removes the last element of the array

Push inserts a new element at the end of the array

Example:

@colors = qw(red blue green black);

$last = pop@colors ;

# $last -> black and @colors -> (red blue green)

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)

Reversing an array reverse keyword reverses the original


array
Example:
@num = qw(1 2 3 4);
@rev_num = reverse@num;
@num = reverse@num; 20

# @rev_num and @num -> (4 3 2 1)


ARRAY MANIPULATIONS CONTD.,
Removing specific elements of an array splice keyword is
used to remove the specified elements in an array

Example:

@removed = splice(@num,1,2);

# 1st argument is the array name

# 2nd argument is the array index to begin splicing

# 3rd argument is the no. of elements to be removed

# @num -> (1 4) and @removed -> (2 3)

21
HASHES
Hashes are also known as associative arrays

Stored as key value pairs

Each key has its corresponding value -> keys must be unique

Array can be converted to hash and vice versa

A hash variable begins with %.

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;

A hash can be converted to an array.


Example:
@list = %directory;

The difference is in the way we access the elements of


the array and hash.
24
ACCESSING THE HASH ELEMENT
Given the hash key, the value can be accessed using {}

%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' =>40);

print "$data{'John Paul'}\n";

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

Syntax : keys %hash

It returns an array of all keys of the hash variable.

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.

Syntax : values %hash

It returns a normal array of all values of the hash variable.

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.

Available control structures are

for

foreach

If/elseif/else

while

do-while, etc

Blocks are executed based on some decisions i.e., True or False

A block is a sequence of statements enclosed in a pair of { }.

Blocks may be nested within other blocks.


30
TRUE AND FALSE IN PERL
Three things are considered as FALSE in Perl.

Value 0

Empty string ( )

Undefined value

All other values are considered as TRUE in Perl.

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

print Enter your ID\n;

chomp($name = <STDIN>);

if ($name eq pray) {

print Welcome Prayline;

} elsif ($name eq prti) {

print Welcome Preethi;

} else {

print Sorry no match found\n;


33
}
WHILE LOOP
Syntax :

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);
}

@colors = qw(red blue green);


foreach $name (@colors)
{
print Color is $name.\n;
}
36
BREAKING AND SKIPPING OUT OF A LOOP
last keyword is used to exit out of a loop
immediately based on some condition.

last if($str eq exit);

next keyword is used to skip the remaining


statements of a block and does not exit of the loop.

next if($i > 10);

37
UNLESS LOOP
In if control structure, the block of code is
executed only when the conditional expression is
true.

If a block of code needs to be executed only when


the condition is false, change if to unless.
Example :
unless ($i > 10) {
Print Count Value is : $i;
}

unless says to run the block of code unless the


38
condition is true.
THE UNTIL CONTROL STRUCTURE
It works as the reverse condition of a while loop.

This loop runs until the conditional expression


returns true.

Example :
$j = 1;
$i = 20;
until ($j > $i) {
$j *= 2;
}

This is used especially when the first clause is very


39
short and the second clause is several lines of code.
COMPARISON OPERATORS

Comparison Numeric String

Equal == Eq

Not equal != Ne

Greater than > Gt

Less than < Lt

Greater than or equal >= Ge

Less than or equal to <= Le

40
41
WRITE A PROGRAM TO COUNT THE ODD NUMBERS
IN A LIST.

@list = qw(1 3 4 5 6 2 59 20 34 33 45 65 66 21);

$count = 0;

foreach $number (@list) {

if($number%2 == 1) {

print Number $number is odd\n;

$count++;

print Number of odd numbers in the list : $count\n; 42


WRITE A PROGRAM TO FIND THE SECRET CODE
Make a program that will repeatedly ask the user to guess
a secret number from 1 to 100 until the user guesses the secret
number. Your program should pick the number at random by using
the magical formula int(1 + rand 100). When the user guesses
wrong, the program should respond Too high or Too low. If the
user enters the word quit or exit, or if the user enters a blank
line, the program should quit. Of course, if the user guesses
correctly, the program should quit then as well!

43
PROGRAM
$secret = int(1 + rand 100); elsif ($guess < $secret) {

while (1) { print "Too small. Try again!\n";

print "Please enter a guess from 1 to } elsif ($guess == $secret) {


100: "; print "That was it!\n";
chomp($guess = <STDIN>); last;
if ($guess eq exit) { } else {
print "Sorry you gave up. The number print "Too large. Try again!\n";
is $secret.\n";
}
last;
}
}

44
PRACTICE PROGRAMS
1. Write a perl script to print every element of an
array using foreach and while loop.

2. Write a perl script to sort an array without using


the sort function.

3. Write a perl script to reverse the array without


using the reverse function.

4. Write a perl script to create an array by reading


one item at a time from the standard input.
45

Anda mungkin juga menyukai