Documente Academic
Documente Profesional
Documente Cultură
Author(s)
Authorized by
Creation/Revision Date
Version
Company Confidential
COPYRIGHT NOTICE
All ideas and information contained in this document are the intellectual property of
Education and Research Department, Infosys Technologies Limited. This document is
not for general distribution and is meant for use only for the person they are
specifically issued to. This document shall not be loaned to anyone, within or outside
Infosys, including its customers. Copying or unauthorized distribution of this
document, in any form or means including electronic, mechanical, photocopying or
otherwise is illegal.
Education and Research Department
Infosys Technologies Limited
Electronic City
Hosur Road
Bangalore - 561 229, India.
Tel: 91 80 852 0261-270
Fax: 91 80 852 0362
www.infy.com
mailto:E&R@infy.com
Date
Mar 2007
1.0
June 2007
2.0
May 2009
ER/CORP/CRS/LA82/005
Author(s)
Nihar Bhatt
Pragati Sheel
Nihar Bhatt
Pragati Sheel
Sowmya Kamath K
Jayaram Reddy
Chamakura
Reviewer(s)
Ajeesh G. P.
Comments
Initial Draft
Ajeesh G.P.
Review comments
incorporated.
FP Restructuring 2009
Amit Tewari
Table of Contents
Contents
ER/CORP/CRS/LA82/005
ii
Table of Contents
ER/CORP/CRS/LA82/005
iii
Context
This document contains assignments to be completed as part of the hands on for the
subject Perl Scripting (Course code: LA82).
Note: In order to complete the course, assignments in this document must be
completed in the sequence mentioned.
Day 1 Assignments
Assignment 1: Create the first Perl Script
Objective: To learn how to write a Perl Script, to interpret and execute it.
Problem Description: Writing a Perl Script to display Hello Perl!
Estimated time: 10 minutes
Step 1: Create a file named First.pl using any editor. Eg.Notepad.
Step 2: Open the file and type the following code
#!c:/perl/bin/perl #Using Perl Interpreter
#########################################################
# FileName
: hello.pl
# Description : First Perl Script. Prints "Hello Perl!"
# Author
: E&R Department, Infosys Technologies Ltd.
# Date
: 26-Feb-2007
#########################################################
#Clearing the screen
system("cls");
#Print the Hello Perl Message
print "Hello Perl!";
########################################################
# End of file hello.pl
########################################################
ER/CORP/CRS/LA82/5
1 of 29
ER/CORP/CRS/LA82/5
2 of 29
Step 4: Error message says, (Do you need to predeclare Print?). The number 23
indicate the line number in which the error occurred.
Go to the line indicated by compiler and inspect the error.
Step 5: Change the case of Print to print and execute again.
Step 6: If there are no typographical errors, program should execute successfully.
The program executes and you see the desired output on the screen.
ER/CORP/CRS/LA82/5
3 of 29
Note:
Example: Usage of printf statement:
$Number=1;
printf(Size of int data type: %d,$Number);
Example: syntax of warn statement:
warn Wrong Choice;
ER/CORP/CRS/LA82/5
4 of 29
Objective: To understand the use of control structure IF and the do until loop
Problem Description: Writing a Perl script to calculate the grade for a student based
on their marks.
Estimated time: 15 minutes
Step 1: Create a Perl script file Use_Of_If.pl
Step 2: Accept the name and the marks of the student in scalar variables. Store the
resultant grade also.
Step 3: Display the name, marks and grade in a formatted output.
Step 4: Check is the user wants to repeat the process. Use the dountil loop to
repeat the above steps
Use of IF statement
Use of do..until loop
User input from the terminal
Note: Calculate the grade according to ENR Evaluation Policy.
ER/CORP/CRS/LA82/5
5 of 29
1:
2:
3:
4:
1.
2.
3.
4.
5.
ER/CORP/CRS/LA82/5
6 of 29
Step 5: Use the array functions POP, PUSH, UNSHIFT and SHIFT to implement the
choice.
Summary of this assignment:
Addition and Deletion of elements in an ARRAY.
More logic coverage on arrays.
Note: Use POP,PUSH, UNSHIFT and SHIFT.
ER/CORP/CRS/LA82/5
7 of 29
1:
2:
3:
4:
ER/CORP/CRS/LA82/5
8 of 29
Problem Description: Writing a Perl script to Count the occurrence of each word in a
string.
Estimated time: 20 minutes
Step
Step
Step
Step
1:
2:
3:
4:
$hello
$_test
ER/CORP/CRS/LA82/5
9 of 29
c.
d.
e.
f.
$now_is_the_time_to_come_to_the_aid_of_the_party
$fries&gravy
$96tears
$tea_for_2
6) What is wrong with the following program? (Hint: there might be more than one
bug!)
#!c:/perl/bin/perl
$value = <STDIN>;
if ($value = 17) {
print ("You typed the number 17.\n");
else {
print ("You did not type the number 17.\n");
7) Find and fix the bugs in the following program:
#!c:/perl/bin/perl
$num = <STDIN>;
chop ($num);
$x = "";
$x += "hello";
if ($x != "goodbye" | $x == "farewell") {
$result = $num eq 0 ? 43;
} else {
$result = ++$num++;
}
print("the result is $result\n");
8) What happens when you refer to an array element that has not yet been defined?
9) What is wrong with the following program? (See if you can figure out what's wrong
without checking the listings in today's lesson.)
#!c:/perl/bin/perl
@input = <STDIN>;
$currline = 1;
while ($currline < @input) {
@words = split(/ /, $input[$currline]);
@words = sort(@words);
$input[$currline] = join(" ", @words);
$currline++;
}
print (@input);
10) How can I know how many entries are in a hash?
ER/CORP/CRS/LA82/5
10 of 29
Day 2 Assignments
Assignment 1: Introduction to Subroutines
Objective: To understand calling a subroutine.
Problem Description: Writing a Perl script that calls a subroutine to calculate the
sum of an integer array passed as an argument.
Estimated time: 15 minutes
Step 1: Create a Perl script file sum.pl.
Step 2: Read the numbers from the user.
Step 3: Write a subroutine which calculates and returns the sum of these two
numbers.
Step 4: Call the subroutine with user input and display the return value.
ER/CORP/CRS/LA82/5
11 of 29
Use of My variable
Note: Use the same variable name to store the results for the addition
of numbers and concatenation of strings.
ER/CORP/CRS/LA82/5
12 of 29
Subroutine in depth
Use of local variable
ER/CORP/CRS/LA82/5
13 of 29
ER/CORP/CRS/LA82/5
14 of 29
ER/CORP/CRS/LA82/5
15 of 29
ER/CORP/CRS/LA82/5
16 of 29
ER/CORP/CRS/LA82/5
17 of 29
ER/CORP/CRS/LA82/5
18 of 29
ER/CORP/CRS/LA82/5
19 of 29
ER/CORP/CRS/LA82/5
20 of 29
6)
a.
b.
c.
d.
e.
7)
a.
b.
c.
d.
e.
8) Julius Ceasar is said to have used the famous Ceasar Cypher to encrypt his
communications with Rome. When encrypting, the Ceasar Cypher substitutes
letter-for-letter like this:
A -> C
B -> D
C -> E
...
X -> Z
Y -> A
Z -> B
Write a program that performs a Ceasar Cypher on its input.
As a test, if you feed the program "Ceasar Cypher", you should get "Egcuct
Earjgt". Be especially careful that the "y" in "cypher" maps to an "a".
Day 3 Assignments
Assignment 1: Understanding database connectivity with Oracle
Objective: To perform database connectivity with Oracle.
Problem Description: Writing a Perl script to run a query to fetch data from oracle.
Estimated time: 10 minutes
Step 1: Create a file DatabaseConnect.pl.
ER/CORP/CRS/LA82/5
21 of 29
Step 2: Type and analyze the code given below to connect and run a simple query
given below.
#!c:/perl/bin/perl #Using Perl Interpreter
#########################################################
# FileName
: DatabaseConnect.pl
# Description : Perl script to perform database connectivity
# Author
: E&R Department, Infosys Technologies Ltd.
# Date
: 18-May-2009
#########################################################
use DBI; #Using DBI module
use DBD::Oracle; #Using DBD-Oracle module
#Connecting to database.
my $dbh = DBI->connect('dbi:Oracle:<Connection
string>','<UserID>','<Password>');
#Statement to create oracle table
my $sql = qq{ CREATE Table Account
(accountno NUMBER(4) PRIMARY KEY,
customerID NUMBER(3) constraint FK_Account
references Customer(custID),
accounttype CHAR(2) check (accounttype in
('SB','CA')),
DOP DATE,Amount NUMBER(7,2),
Status CHAR(1) check (Status in ('F','C'))
)};
my $sth = $dbh->do($sql);
#Disconnecting from the database
$dbh->disconnect();
Step 3: Verify in oracle if table is created and insert 5 records into this table.
Summary of this assignment:
ER/CORP/CRS/LA82/5
22 of 29
ER/CORP/CRS/LA82/5
23 of 29
ER/CORP/CRS/LA82/5
24 of 29
Problem Description: Writing a Perl script to perform insert, update and display of
data from Oracle.
Estimated time: 30 minutes
Step 1: Create a file DMLOperation.pl.
Step 2: Create the menu-driven script
1. Insert Customer
2. Insert Account
3. Update Account
4. Delete Account
5. View Customer Data
6. View Account Data
7. Exit
Step 3: Implement option 1 using DB handle.
Step 4: Implement option 2 using statement handle.
Step 5: For option 3, create an update string using the inputs and then use statement
handle to update the table.
Step 6: Implement option 4 as parameterized query.
Step 7: Implement 5 using fetch_array()
Step 8: Implement 6 using fetch().
Summary of this assignment:
25 of 29
2:
3:
4:
5:
ER/CORP/CRS/LA82/5
26 of 29
Step 3: Create the guest.pl to generate dynamic web page to display the data
received from form in guestbook.pl
#!c:/perl/bin/perl #Using Perl Interpreter
#########################################################
# FileName
: Guest.pl
# Description : CGI script to display input received from
# Guestbook.pl.
# Author
: E&R Department, Infosys Technologies Ltd.
# Date
: 22-Jun-2007
#########################################################
# Split name-value pairs using & in QUERY_STRING
@inputs = split(/&/, $ENV{QUERY_STRING});
#replacing the Hex values in input with equivalent characters
foreach $input (@inputs) {
ER/CORP/CRS/LA82/5
27 of 29
print header;
#printing HTML Header
print title("Dynamic Page Example");
print h1({align=>'center'},"Dynamic Page");
#Displaying output in tabular form
print "Dear $FORM{'name'},<BR>Thanks for filling out our Form.
Good Luck<br><br>";
print table({border=>2},Tr({align=>CENTER,valign=>TOP},
[
th('Name:').td($FORM{'name'}),
th('E-mail').td($FORM{'email'}),
th('Comments').td($FORM{'comments'})
]
)
);
ER/CORP/CRS/LA82/5
28 of 29
Step 4: Display the Environmental Variables (%ENV) and identify the changes in both cases.
Summary of this assignment:
Understanding the working of GET and POST methods
ER/CORP/CRS/LA82/5
29 of 29