Vlsi Design Automation Using Perl
Vlsi Design Automation Using Perl
USING PERL
INTRODUCTION:
• It stands for Practical Extraction and Report Language developed by
Larry Wall, especially designed for text processing.
• Perl is a general-purpose programming language originally developed for
text manipulation and now used for a wide range of tasks including
system administration, web development, network programming, GUI
development, and more.
• It runs on a variety of platforms, such as Windows, Mac OS, and the
various versions of UNIX.
This statement uses the -e flag to avoid the creation of a script. This
method of writing in an Interactive mode will not allow the user to
write a Multiline code as in the debugger.
SCRIPTING MODE:
• Script Mode in Perl is used to write Perl programs which have more than a few
lines of code and are a bit complex for the Interactive mode.
• Script mode in Perl can be used by the help of a text editor to write the Perl
program and to save it in a file called a script and then execute the saved file
by using the command line.
• This file must be saved with a .pl extension and should be placed in the same
folder of which the directory path is given to the command line.
Perl –e
Example:
• parentheses for functions arguments can be usedor omit them according to your personal
taste.
print("Hello, world\n");
print "Hello, world\n";
#!/usr/bin/perl
# This is a single line comment
print "Hello, world\n";
=begin comment
This is all part of multiline comment. You can use as
many lines as you like These comments will be ignored
by the compiler until the next =cut is encountered.
=cut
Whitespaces in Perl
• A Perl program does not care about whitespaces
#!/usr/bin/perl
print "Hello, world\n";
• But if spaces are inside the quoted strings, then they would be printed as is .
#!/usr/bin/perl
# This would print with a line break in the middle
print "Hello
world\n";
Single and Double Quotes in Perl
• Only double quotes interpolate variables and special characters such as
newlines \n, whereas single quote does not interpolate any variable or
special character.
#!/usr/bin/perl
print "Hello, world\n";
print 'Hello, world\n';
#!/usr/bin/perl $a = 10;
print "Value of a = $a\n";
print 'Value of a = $a\n';
EOF:
OUTPUT:
Escaping Characters
• Perl uses the backslash (\) character to escape any type of character
that might interfere with our code.
#!/usr/bin/perl
$result = "This is \"number\"";
print "$result\n";
print "\$result\n";
Perl Identifiers
• A Perl identifier is a name used to identify a variable, function, class,
module, or other object.
Scalar
Scalars are simple variables. They are preceded by a dollar sign ($). A scalar
1
is either a number, a string, or a reference. A reference is actually an
address of a variable, which we will see in the upcoming chapters.
Arrays
2 Arrays are ordered lists of scalars that you access with a numeric index,
which starts with 0. They are preceded by an "at" sign (@).
Hashes
3 Hashes are unordered sets of key/value pairs that you access using the keys
as subscripts. They are preceded by a percent sign (%).
Numeric Literals
• Perl stores all the numbers internally as either signed integers or double-
precision floating-point values.
• Numeric literals are specified in any of the following floating-point or integer
formats −
Type Value
Integer 1234
Negative integer -100
Floating point 20.00
Scientific notation 16.12E14
Hexadecimal 0xffff
Octal 0577
String Literals
• Strings are sequences of characters.
• They are usually alphanumeric values delimited by either single (') or double
(") quotes.
• Double-quoted string literals allow variable interpolation, and single-quoted
strings are not.
• There are certain characters when they are proceeded by a back slash, have
special meaning and they are used to represent like newline (\n) or tab (\t).
#!/usr/bin/perl
$age = 25; # An integer assignment
$name = "John Paul"; # A string
$salary = 1445.50; # A floating point
print "Age = $age\n";
print "Name = $name\n";
print "Salary = $salary\n";
Array Variables
• An array is a variable that stores an ordered list of scalar values.
• Array variables are preceded by an "at" (@) sign.
• To refer to a single element of an array, you will use the dollar sign ($)
with the variable name followed by the index of the element in square
brackets.
Hash Variables
• A hash is a set of key/value pairs. Hash variables are preceded by a percent
(%) sign.
• To refer to a single element of a hash, you will use the hash variable name
followed by the "key" associated with the value in curly brackets.
#!/usr/bin/perl
%data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);
#!/usr/bin/perl
@names = ('John Paul', 'Lisa', 'Kumar’);
@copy = @names;
$size = @names;
print "Given names are : @copy\n";
print "Number of names are : $size\n";
PERL SCALARS:
• A scalar is a single unit of data. That data might be an integer number,
floating point, a character, a string, a paragraph, or an entire web page.
NUMERIC SCALARS:
• A scalar is most often either a number or a string
STRING SCALARS:
SCALAR OPERATORS
Multiline Strings
SPECIAL LITERALS:
• three special literals __FILE__, __LINE__, and __PACKAGE__ represent
the current filename, line number, and package name at that point
in your program.
PERL-ARRAYS:
• An array is a variable that stores an ordered list of scalar values.
• Array variables are preceded by an "at" (@) sign.
• To refer to a single element of an array, you will use the dollar sign
($) with the variable name followed by the index of the element in
square brackets.
Array Creation
Array variables are prefixed with the @ sign and are populated using
either parentheses or the qw operator.
#!/usr/bin/perl
@var_10 = (1..10);
@var_20 = (10..20);
@var_abc = (a..z);
print "@var_10\n"; # Prints number from 1 to 10
print "@var_20\n"; # Prints number from 10 to 20
print "@var_abc\n"; # Prints number from a to z
Array Size
• The size of an array can be determined using the scalar context on the
array - the returned value will be the number of elements in the array.
• The value returned will always be the physical size of the array, not the
number of valid elements.
#!/usr/bin/perl
@array = (1,2,3);
$array[50] = 4;
$size = @array;
$max_index = $#array;
print "Size: $size\n";
print "Max Index: $max_index\n";
Adding and Removing Elements in Array
• Perl provides a number of useful functions to add and remove elements in an
array
#!/usr/bin/perl
@days = qw/Mon Tue Wed Thu Fri Sat Sun/;
@weekdays = @days[3,4,5];
print "@weekdays\n";
@weekdays = @days[3..5];
Print “@weekdays\n”;
Replacing Array Elements
#!/usr/bin/perl
@nums = (1..20);
print "Before - @nums\n";
splice(@nums, 5, 5, 21..25);
print "After - @nums\n";
Transform Strings to Arrays
#!/usr/bin/perl
@numbers = (1,3,(4,5,6));
print "numbers = @numbers\n";
#!/usr/bin/perl
@odd = (1,3,5);
@even = (2, 4, 6);
@numbers = (@odd, @even);
print "numbers = @numbers\n";
Selecting Elements from Lists
• The list notation is identical to that for arrays. You can extract an element from
an array by appending square brackets to the list and giving one or more
indices
#!/usr/bin/perl
@list = (5,4,3,2,1)[1..3];
print "Value of list = @list\n";
$var = (5,4,3,2,1)[4];
print “ Value of var = $var\n”;
Perl - Hashes
Creating Hashes
• Hashes are created in one of the two following ways.
• In the first method, you assign a value to a named key on a one-by-one
basis −
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
• here all the keys have been preceded by hyphen (-) and no quotation is
required around them −
%data = (-JohnPaul => 45, -Lisa => 30, -Kumar => 40);
Accessing Hash Elements
• When accessing individual elements from a hash, you must prefix the variable
with a dollar sign ($) and then append the element key within curly brackets
after the name of the variable.
#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
print "$data{'John Paul'}\n";
print "$data{'Lisa'}\n";
print "$data{'Kumar'}\n";
Extracting Slices
• You can extract slices of a hash just as you can extract slices from an array.
• You will need to use @ prefix for the variable to store the returned value
because they will be a list of values.
#!/uer/bin/perl
%data = (-JohnPaul => 45, -Lisa => 30, -Kumar => 40);
@array = @data{-JohnPaul, -Lisa};
print "Array : @array\n";
Extracting Keys and Values
• You can get a list of all of the keys from a hash by using keys function,
which has the following syntax
keys %HASH
• you can use values function to get a list of all the values. This function
has the following syntax − values %HASH
Checking for Existence
• If you try to access a key/value pair from a hash that doesn't exist, you'll
normally get the undefined value, and if you have warnings switched on, then
you'll get a warning generated at run time.
• You can get around this by using the exists function, which returns true if the
named key exists, irrespective of what its value might be.
#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
if( exists($data{'Lisa'} ) ) {
print "Lisa is $data{'Lisa'} years old\n";
} else {
print "I don't know age of Lisa\n";
}
Getting Hash Size
• You can get the size - that is, the number of elements from a hash by
using the scalar context on either keys or values.
• Simply saying first you have to get an array of either the keys or values
and then you can get the size of array.
#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar'
=> 40);
@keys = keys %data;
$size = @keys;
print "1 - Hash size: is $size\n";
@values = values %data;
$size = @values;
print "2 - Hash size: is $size\n ”;
Add and Remove Elements in Hashes
• Adding a new key/value pair can be done with one line of code using simple
assignment operator.
• But to remove an element from the hash you need to use delete function.
Perl Conditional Statements - IF...ELSE
• Perl conditional statements helps in the decision making, which require that the
programmer specifies one or more conditions to be evaluated or tested by the
program, along with a statement or statements to be executed if the condition
is determined to be true, and optionally, other statements to be executed if the
condition is determined to be false.
Sr.No. Statement & Description
if statementAn if statement consists of a boolean expression followed by
1
one or more statements.
if...else statementAn if statement can be followed by an optional else
2
statement.
if...elsif...else statementAn if statement can be followed by an
3
optional elsif statement and then by an optional else statement.
unless statementAn unless statement consists of a boolean expression
4
followed by one or more statements.
unless...else statementAn unless statement can be followed by an
5
optional else statement.
unless...elsif..else statementAn unless statement can be followed by
6
an optional elsif statement and then by an optional else statement.
switch statementWith the latest versions of Perl, you can make use of
7 the switch statement. which allows a simple way of comparing a variable
value against various conditions.
Perl IF Statement
• A Perl if statement consists of a boolean expression followed by one
or more statements.
if(boolean_expression) {
# statement(s) will execute if the given condition is true
}
Perl IF...ELSE statement
• A Perl if statement can be followed by an optional else statement, which
executes when the boolean expression is false.
if(boolean_expression) {
# statement(s) will execute if the given condition is true
} else {
# statement(s) will execute if the given condition is false
}
Perl IF...ELSIF statement
• An if statement can be followed by an optional elsif...else statement,
which is very useful to test the various conditions using single if...elsif
statement.
if(boolean_expression 1) {
# Executes when the boolean expression 1 is true
} elsif( boolean_expression 2) {
# Executes when the boolean expression 2 is true
} elsif( boolean_expression 3) {
# Executes when the boolean expression 3 is
true } else {
# Executes when the none of the above condition
is true
}
Perl unless Statement
unless(boolean_expression) {
# statement(s) will execute if the given condition is false
}
Perl UNLESS...ELSE statement
unless(boolean_expression) {
# statement(s) will execute if the given condition is false
} else {
# statement(s) will execute if the given condition is true
}
Perl UNLESS...ELSIF statement
unless(boolean_expression 1) {
# Executes when the boolean expression 1 is false
} elsif( boolean_expression 2) {
# Executes when the boolean expression 2 is true
} elsif( boolean_expression 3) {
# Executes when the boolean expression 3 is true
} else {
# Executes when the none of the above condition is met
}
Perl switch Statement
• A switch statement allows a variable to be tested for equality against a
list of values. Each value is called a case, and the variable being
switched on is checked for each switch case.
The ? : Operator
• There may be a situation when you need to execute a block of code several
number of times.
• In general, statements are executed sequentially: The first statement in a
function is executed first, followed by the second, and so on.
• A loop statement allows us to execute a statement or group of statements
multiple times
Sr.No. Loop Type & Description
while(condition) {
statement(s);
}
#!/usr/local/bin/perl
$a = 10;
# while loop execution
while( $a < 20 ) {
printf "Value of a: $a\n"; $a = $a + 1;
}
Perl until Loop
• An until loop statement in Perl programming language repeatedly executes a
target statement as long as a given condition is false.
until(condition) {
statement(s);
}
#!/usr/local/bin/perl
$a = 5;
# until loop execution
until( $a > 10 ) {
printf "Value of a: $a\n"; $a = $a + 1;
}
Perl for Loop
• A for loop is a repetition control structure that allows you to efficiently write a
loop that needs to execute a specific number of times.
#!/usr/local/bin/perl
@list = (2, 20, 30, 40, 50);
# foreach loop execution
foreach $a (@list) {
print "value of a: $a\n";
}
Perl do...while Loop
• Unlike for and while loops, which test the loop condition at the top of the
loop, the do...while loop checks its condition at the bottom of the loop.
• A do...while loop is similar to a while loop, except that a do...while loop is
guaranteed to execute at least one time.
do {
statement(s);
}while( condition );
#!/usr/local/bin/perl
$a = 10;
# do...while loop execution
do{
print "Value of a: $a\n”;
$a = $a + 1;
}while( $a < 20 );
Perl nested Loop
• A loop can be nested inside of another loop. Perl allows to nest all type of loops to
be nested.
Loop Control Statements
• Loop control statements change the execution from its normal
sequence.
#!/usr/local/bin/perl
for( ; ; ) {
printf "This loop will run forever.\n";
}
• You can terminate the above infinite loop by pressing the Ctrl + C
keys.
Perl - Subroutines
sub subroutine_name {
body of the subroutine }
• The typical way of calling that Perl subroutine is as follows −
subroutine_name( list of arguments );
&subroutine_name( list of arguments );
EXAMPLE:
#!/usr/bin/perl
# Function definition
sub Hello {
print "Hello, World!\n";
}
# Function call
Hello();
Passing Arguments to a Subroutine
Passing Lists to Subroutines
Passing Hashes to Subroutines
• When you supply a hash to a subroutine or operator that accepts a list, then
hash is automatically translated into a list of key/value pairs.
Returning Value from a Subroutine
Private Variables in a Subroutine
• By default, all variables in Perl are global variables, which means they can be
accessed from anywhere in the program.
• But you can create private variables called lexical variables at any time
with the my operator.
• The my operator confines a variable to a particular region of code in which it
can be used and accessed.
• Outside that region, this variable cannot be used or accessed. This region is
called its scope.
• lexical scope is usually a block of code with a set of braces around it, such as
those defining the body of the subroutine or those marking the code blocks
of if, while, for, foreach, and eval statements.
sub somefunc {
my $variable; # $variable is invisible outside somefunc()
my ($another, @an_array, %a_hash); # declaring many variables at once
}
Temporary Values via local()
• The local is mostly used when the current value of a variable must be
visible to called subroutines. A local just gives temporary values to global
(meaning package) variables. This is known as dynamic scoping.
Perl - File I/O
• The basics of handling files are simple: you associate a filehandle with an
external entity (usually a file) and then use a variety of operators and
functions within Perl to read and update the data stored within the data
stream associated with the filehandle.
• A filehandle is a named internal Perl structure that associates a physical file with
a name.
• All filehandles are capable of read/write access, so you can read from and update
any file or device associated with a filehandle
• Three basic file handles are - STDIN, STDOUT, and STDERR, which
represent standard input, standard output and standard error devices
respectively.
Opening and Closing Files
Open Function
• Following is the syntax to open file.txt in read-only mode. Here less than < sign
indicates that file has to be opend in read-only mode.
open(DATA, "<file.txt");
• Here DATA is the file handle, which will be used to read the file.
#!/usr/bin/perl
open(DATA, "<file.txt") or die "Couldn't open file file.txt, $!";
while(<DATA>) {
print "$_"; }
• Following is the syntax to open file.txt in writing mode. Here less than > sign
indicates that file has to be opend in the writing mode.
• A double >> opens the file for appending, placing the file pointer at the end,
so that you can immediately start appending information. However, you can't
read from it unless you also place a plus sign in front of it
Close Function
• To close a filehandle, and therefore disassociate the filehandle from the
corresponding file, you use the close function
close FILEHANDLE
close(DATA) || die "Couldn't close file properly";
The <FILEHANDL> Operator
• The main method of reading the information from an open filehandle is the
<FILEHANDLE> operator. In a scalar context, it returns a single line from the
filehandle.