0% found this document useful (0 votes)
64 views

Perl Intro Lab

This document provides an introduction to the Perl programming language. It discusses that Perl is an interpreted language where source code is compiled at runtime. Perl programs are plain text files that must start with #!/usr/bin/perl. Comments in Perl start with #. All statements must end with ;. Variables in Perl start with $ for scalars, @ for arrays, and % for hashes. The document then provides examples of Hello World, variable manipulation, getting input from the keyboard, and different loop constructs in Perl programs.

Uploaded by

VISHNURAJ B
Copyright
© © All Rights Reserved
0% found this document useful (0 votes)
64 views

Perl Intro Lab

This document provides an introduction to the Perl programming language. It discusses that Perl is an interpreted language where source code is compiled at runtime. Perl programs are plain text files that must start with #!/usr/bin/perl. Comments in Perl start with #. All statements must end with ;. Variables in Perl start with $ for scalars, @ for arrays, and % for hashes. The document then provides examples of Hello World, variable manipulation, getting input from the keyboard, and different loop constructs in Perl programs.

Uploaded by

VISHNURAJ B
Copyright
© © All Rights Reserved
You are on page 1/ 7

Scripting Languages and Verification

PERL - Introduction

Perl is an interpreted language - which means that the source code is read in at
execution time. It is actually compiled at run time so there is no need for the compilation and
linking steps in building an executable as with other languages. This compilation adds some
delay in the startup of a perl program but allows it to run more efficiently once it is fully
loaded. Perl syntax looks like C.

Perl programs are just a plain text file. The first line, must start with the string
#!/usr/bin/perl. The initial character pair #! tells the Unix shell that that this script will be
using it's own interpreter which is the executable program that follows immediately. In this
case /usr/bin/perl. The actual location of the perl executable may vary depending on the
version of Unix being used. Under Unix based systems the file may be named any valid Unix
file name but it is helpful to name perl scripts with the .pl extension so that they can easily
be identified. Comments start with the # character. They may be on a line by themselves
or follow a perl statement. Any text after the # symbol is ignored by the perl interpreter. All
perl statements must end with the semi-colon ";" character. Perl ignores "white space" in
the code such as spaces, tabs and new-line characters except when found within single quoted
strings.

Running the script can be done in the terminal as “perl filename.pl”. The perl
interpreter then reads the contents of the file, compiles the script in system memory, and
executes the code.

SENSE, VIT University Page 1


Scripting Languages and Verification

Lab 1: Hello World Example

In this lab, we shall study how to create a perl script and execute the script. The standard first
example for all UNIX programs is to print “Hello World".

Step 1: Create a directory named PERL_LAB_01

mkdir PERL_LAB_01

Step 2: In PERL_LAB_01 directory, create a file named hello.pl

vi hello.pl

Step 3: Insert the following contents to the file hello.pl

#!/usr/bin/perl

print “Hello World\n”;

Step 4: Save the contents and execute the script from the terminal to see the output.

perl hello.pl

SENSE, VIT University Page 2


Scripting Languages and Verification

Lab 2: Variables

Perl has two basic variables types: scalars and lists. Scalars are single elements that
hold a value, such as a text string or number. List variables consist of arrays or hashes. Scalar
variables in perl will always begin with the $ character. Array variables in list context will
begin with the @ character and hashes in list context will begin with the % character.
Variable names should not contain any non-alphanumeric characters other than Perl's "built-
in" variables. Variables names should also not have a numeric character at the beginning of
the name. Perl variable names are case-sensitive; ie. $name and $Name are different.

2A. Scalar Variable Manipulation

Step1: Create a directory PERL_LAB_02 and in that directory, create a file named
scalar_manipulation.pl

vi scalar_manipulation.pl

Step2: Type the following contents in the created file

$name = “Tinu”;

#Variable Interpolation

print “My name is $name\n”;

print ‘My name is $name’;

print “\n”;

$age = 25;

print “Age of $name is $age\n”;

#Concatenation operator “.”

$a = “Good”;

$b = “Morning”;

$c = “\n”;

$d = $a.$b.$c;

print “\$d = $d\n”;

SENSE, VIT University Page 3


Scripting Languages and Verification

#Repetition Operator “x”

$b = $a x 2;

print "\$b = $b\n";

#Multi-line print

print <<terminate;

Hello

How are you?

terminate

Step 3: Save and execute the script.

Step 4: Analyze the output for every print statement

2B. Array Variable Manipulation

Step1: Create a file name array_manipulation.pl

vi array_manipulation.pl

Step2: Type the following contents in the created file

#Simple Array Manipulation

$num1 = 3;

@num1 = (1, "one", 1.5, $num1);

print "The array is: @num1 \n";

print "The number is $num1 \n";

$num1[0] = 5;

print "The array is: @num1 \n";

Step 3: Save and execute the script.

Step 4: Analyze the output for every print statement.

SENSE, VIT University Page 4


Scripting Languages and Verification

Lab 3: Getting data from the keyboard

To get input from the keyboard, you can use the standard filehandle <STDIN>.

Step1: Create a directory PERL_LAB_03 and in that directory, create a file named
get_input.pl

vi get_input.pl

Step2: Type the following piece of script in created file. Execute the script and analyse the
output.

print “Enter your First Name\n”;

$first_name = <STDIN>;

print “Enter your Second Name\n”;

$second_name = <STDIN>;

chomp($first_name);

chomp($second_name);

print “Welcome $first_name $second_name\n”;

print “Enter two numbers to find the sum\n”;

$num1 = <STDIN>;

$num2 = <STDIN>;

$sum = $num1 + $num2;

print “1. Sum of $num1 and $num2 is $sum\n”;

# To study the functionality of chomp function

chomp($num1);

chomp($num2);

$sum = $num1 + $num2;

print “2. Sum of $num1 and $num2 is $sum\n”;

SENSE, VIT University Page 5


Scripting Languages and Verification

Lab 4: Loops

Control structures in PERL are similar to those in C. Few control structures in perl are

 for
 foreach
 If/elseif/else
 while
 do-while, etc

Execute the following perl script and analyse the output.

#For Loop:

for ($i = 1; $i < 10 ; $i++) {

print "\$i is $i\n";

print "End of FOR LOOP1\n\n";

#Loop Control

for ($j = 10; $j >= 0; $j--) {

next if( $j == 5 );

print "The number is $j\n";

print "End of FOR LOOP2\n\n";

# While Loop

$secret_code = 2017;

while ($choice != $secret_code) {

print “Enter your Guess : \n”;

chomp($choice = <STDIN>);

print “Your guess is correct!\n”;

SENSE, VIT University Page 6


Scripting Languages and Verification

# Foreach Loop

@colors = qw(red blue green);

foreach $name (@colors)

print “Color is $name.\n”;

print "End of FOREACH LOOP\n\n";

Home Work

i) Formulate different expressions with scalar variables and show the output.
ii) Write a perl script to show the operations of shift, unshift, push, pop, reverse, split
and splice functions on arrays. Display the output on the screen after every
operation.

TASK

Write a PERL script to generate 10 random test vectors A and B within the limits
specified by the user.

SENSE, VIT University Page 7

You might also like