Perl Intro Lab
Perl Intro Lab
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.
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".
mkdir PERL_LAB_01
vi hello.pl
#!/usr/bin/perl
Step 4: Save the contents and execute the script from the terminal to see the output.
perl hello.pl
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.
Step1: Create a directory PERL_LAB_02 and in that directory, create a file named
scalar_manipulation.pl
vi scalar_manipulation.pl
$name = “Tinu”;
#Variable Interpolation
print “\n”;
$age = 25;
$a = “Good”;
$b = “Morning”;
$c = “\n”;
$d = $a.$b.$c;
$b = $a x 2;
#Multi-line print
print <<terminate;
Hello
terminate
vi array_manipulation.pl
$num1 = 3;
$num1[0] = 5;
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.
$first_name = <STDIN>;
$second_name = <STDIN>;
chomp($first_name);
chomp($second_name);
$num1 = <STDIN>;
$num2 = <STDIN>;
chomp($num1);
chomp($num2);
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
#For Loop:
#Loop Control
next if( $j == 5 );
# While Loop
$secret_code = 2017;
chomp($choice = <STDIN>);
# Foreach Loop
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.