Professional Documents
Culture Documents
Part I ! Introduction To Perl Scripting: Dr. K. Najeeb
Part I ! Introduction To Perl Scripting: Dr. K. Najeeb
Dr. K. Najeeb
(Email: k.najeeb@gmail.com)
1/67
Objectives of the Presentation
2/67
Expected Outcomes
3/67
Outline of the Presentation
1 Motivations
5 Introduction to AWK
4/67
Outline
1 Motivations
5 Introduction to AWK
5/67
Practice, Practice . . . . . . and Practice
Confucius 450BC
6/67
Why do we Learn Different Programming Languages?
7/67
Realities in Life
8/67
Smart Scripting Languages
1 Motivations
5 Introduction to AWK
10/67
What is Perl?
11/67
History of Perl
12/67
Features of Perl
13/67
Where we fit Perl?
14/67
What Perl Does Well?
15/67
Some of the Applications
16/67
How is it different from C?
17/67
Example 1
18/67
Outline
1 Motivations
5 Introduction to AWK
19/67
How to Run Perl Program?
Option 1
Run Perl script with .pl extension from the command line with
the perl command
20/67
How to Run Perl Program?
Option 1
Run Perl script with .pl extension from the command line with
the perl command
Option 2
Run Perl with -e option
20/67
How to Run Perl Program?
Option 3
Run with eval perl
21/67
How to Run Perl Program?
Option 3
Run with eval perl
Option 4
Run the script by adding execute permission to the script
$chmod 766 sum.pl
Execute the script by typing ./sum in the terminal
21/67
Variable Naming Rules in Perl
22/67
Perl > Data Types
Scalar type
Scalars are simple variables
They are preceded by a dollar sign $
It can be of type:
integer, floating point, string,binary data,reference
Arrays or Lists:- Arrays are basically an ordered collection of
scalars and accessed by indices from zero on-wards
They are preceded by @
Can contain mixture of scalar types
Hashes / Dictionary:- are unordered sets of key/value pairs
Start with percent sign %
Keys are unique and can be any scalar value
Values can be any scalar value
23/67
Example 1 ~ example1.pl
24/67
Dissection of example1.pl
25/67
Example2 ~ example2.pl
#!/usr/bin/perl
$inputline = <STDIN>;
print( $inputline );
26/67
Try this Script!!!
Problem Definition
Write a script to read radius of a circle through keyboard and the
display radius, diameter, circumference and area of the circle on the
screen
27/67
Try this Script!!!
Problem Definition
Write a script to read radius of a circle through keyboard and the
display radius, diameter, circumference and area of the circle on the
screen
Refer Refer example3.pl in the directory
#! usr/bin/perl
print "What is the radius of the circle?";
chomp ($r = <>);
$diameter = (2 * $r);
$area = (3.14 * ($r ** 2));
$cir = ($diameter * 3.14);
print "Radius: $r\n Diameter: $diameter\n
Circumference: $cir\n Area: $area \n";
27/67
Array Creation and Use~ example4.pl
#! usr/bin/perl
@array = (1, 2, ’Hello’);
@days = qw/Mon Tue Wed Thu Fri Sat Sun/;
print "$days[0]\n";
print "$days[-1]\n";
@var_10 = (1..10);
print "@var_10\n";
@var_abc = (a..z);
print "@var_abc \n";
print "size of array days=",scalar @days,"\n";
print "max index of array days =",$#days, "\n";
28/67
Adding and Removing Elements in the Array
~ example5.pl
#!/usr/bin/perl
@days=qw/mon tue wed thu fri sat sun/;
pop @days;
print "@days \n";
push @days,"sun";
print "@days \n";
shift @days;
print "@days \n";
unshift @days,"mon";
print "@days \n";
splice(@days, 2, 3, (’sun’,’sat’,’fri’));
print "After - @days\n";
29/67
Try This
Problem Statement
How to remove an element from the middle of an array in Perl?
30/67
Try This
Problem Statement
How to remove an element from the middle of an array in Perl?
my @days = (’mon’,’tue’,’wed’,’thu’,’fri’,’sat’,’sun’);
$size=int(@days/2);
splice @days, $size, 1;
print "@days\n"; #String Interpolation
30/67
Slicing Array Elements ~ example7.pl
#!/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";
@weekdays = @days[-4..-2];
print "@weekdays\n";
@weekday = @days[4..2];
print "@weekday\n";
31/67
Hashes ~ example8.pl
32/67
Hashes ~ example8.pl
#!/usr/bin/perl
%data = (’Apple’, 145, ’Banana’, 30, ’Grapes’, 60);
%data1 = (’John Paul’ => 45, ’Lisa’ => 30, ’Kumar’ => 40);
print "\$data{’Apple’} = $data{’Apple’}\n";
print "\$data{’Banana’} = $data{’Banana’}\n";
print "\$data{’Grapes’} = $data{’Grapes’}\n";
print %data, "\n";
while ( ($k,$v) = each %data1 ) {
print "$k => $v\n";
}
32/67
Operators in Perl
33/67
while loop
until loop
for loop
for each loop
do while loop
nested loop
34/67
while and for loop
#!/usr/local/bin/perl
$a = 10;
while( $a < 20 )
{
printf "Value of a: $a\n";
$a = $a + 1;
}
35/67
while and for loop
35/67
until and do while loop
until loop
example example11.pl
#!/usr/local/bin/perl
$a = 5;
until( $a > 10 )
{
printf "Value of a: $a\n";
$a = $a + 1;
}
36/67
until and do while loop
#!/usr/local/bin/perl #!/usr/local/bin/perl
$a = 5; $a = 5;
until( $a > 10 ) until( $a > 10 )
{ {
printf "Value of a: $a\n"; printf "Value of a: $a\n";
$a = $a + 1; $a = $a + 1;
} }
36/67
if Statement
Example 2
example15.pl
Example 1
example14.pl print "What is your age? ";
my $age = <STDIN>;
print "What is your age? "; if ($age >= 18)
my $age = <STDIN>; {
if ($age >= 18) print "you can vote.\n";
{ }
print "you can vote.\n"; else
} {
print "You are young\n";
}
37/67
Switch Statement in Perl
#!/usr/local/bin/perl
use Switch;
$var = 10;
@array = (10, 20, 30);
%hash = (’key1’ => 10, ’key2’ => 20);
switch($var){
case 10 {print "number 100\n"}
case "a" {print "string a" }
case [1..10,42] {print "no. in list"}
case (\@array) {print "no. in list"}
case (\%hash) {print "no. in hash"}
else {print "not true"}
}
38/67
2D array example
my $Table;
for($i=0;$i <3;$i++) {
for($j=0;$j < 3; $j++) {
printf ("enter number Table[%d][%d] ==>",$i,$j);
$value=<STDIN>;
chop($value); # Chop last character
$Table->[$i][$j] = $value; } }
for($i=0;$i <3;$i++) {
for($j=0;$j < 3; $j++){
printf(" %d ",$Table->[$i][$j]); }
print " \n"; }
39/67
What is the Output of the Script?
@list=(1..5);
$size= @list-1;
$size1= $#list;
printf("%d %d \n", $size,$size1);
40/67
What is the Output of the Script?
@list=(1..5);
$size= @list-1;
$size1= $#list;
printf("%d %d \n", $size,$size1);
See example18.pl 4 4
40/67
System Variables in Perl
Concept
Variables which have a predefined and special meaning in Perl
41/67
Pointer Concept in Perl ~ example23.pl
42/67
Function concept ~ example24.pl
sub check
{
my ($a,$b) = @_; #@_ is Array system variable
$k = $a+$b;
return $k;
}
$e= check(3,4);
print "\n $e\n";
43/67
Identify Syntax Errors, if any
Script 1
if ($remaining > 0)
print $x;
44/67
Identify Syntax Errors, if any
Script 1
if ($remaining > 0)
print $x;
Missing braces
Script 2
for ($i = 1; $i < $n; $i++)
print a[$i], "\n";
44/67
Identify Syntax Errors, if any
Script 1
if ($remaining > 0)
print $x;
Missing braces
Script 2
for ($i = 1; $i < $n; $i++)
print a[$i], "\n";
Missing $ for a
Script 3
while ($c = 1)
{
$c = do_thing($m, $q);
44/67 }
Identify Syntax Errors, if any
Script 1
if ($remaining > 0)
print $x;
Missing braces
Script 2
for ($i = 1; $i < $n; $i++)
print a[$i], "\n";
Missing $ for a
Script 3
while ($c = 1)
{ == instead of =
$c = do_thing($m, $q);
44/67 }
Outline
1 Motivations
5 Introduction to AWK
45/67
Regular Expressions in Perl
What is Regular Expression?
A regular expression is a string of characters that defines a
pattern
Regular expressions in Perl are similar to sed, grep, and awk
Perl has a powerful Reg Exp engine
Allows us to look for patterns in our data
Why RegExps?
Given a large text file. We have to delete all the blank lines
scattered here and there.
As system administrator, you have to delete all video and mp3
files recursively traversing all the user directories
Linux admin has to analyze the log file for security reasons
Matching passwords in applications based on the conditions
Text validation
46/67
Why RegExps?
Scenario
6 to 12 characters in length
Must have at least one uppercase letter
Must have at least one lower case letter
Must have at least one digit
Should contain other characters
47/67
Why RegExps?
Scenario
6 to 12 characters in length
Must have at least one uppercase letter
Must have at least one lower case letter
Must have at least one digit
Should contain other characters
Sample Pattern
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{6,12}$
47/67
Try this Problem
Problem statement
Write a Perl script to read a text file and find all matches for a string
foo
48/67
Try this Problem
Problem statement
Write a Perl script to read a text file and find all matches for a string
foo
Refer example17.pl
48/67
Example17.pl - An overview
50/67
Reg Exp. Contd
51/67
Predict the Output
$id = ’[a-zA-Z][a-zA-Z0-9_]*’;
$line = " and AND1 (a,b,c)";
if ($line=~/^\s*(and|or|nand|xor)\s*($id)\s*\((.*)\)/xigs)
# x i g s are match operator modifiers
# x --- Allows to use white space in the expression
# for clarity.
# i --- Makes the match case insensitive.
# g --- Globally finds all matches.
# s --- Allows use of . to match a newline character.
{
print "\nTrue\n";
}
52/67
Do this task
53/67
Predict the Output ~ example27.pl
54/67
Predict the Output ~ example27.pl
∧ and $ match the very start and very end of the string
54/67
Linux Basics: Quick Review
55/67
Linux Basics: Quick Review
55/67
Part II: AWK Programming
56/67
Outline
1 Motivations
5 Introduction to AWK
57/67
Introduction
In FOSS Lab
Cycles may be given for
Text processing
Producing formatted text reports
Performing string operations
58/67
Example: AWK for producing formatted text output
59/67
Try this
Problem Statement
Assume that you have been recruited as a developer in a Company.
On the first day itself, Your BIG BOSS has given a large text file and
directed you to remove all blank lines and return the modified one
within say 3 minutes. Suggest four possible ways to find solutions
to this problem
60/67
Try this
Problem Statement
Assume that you have been recruited as a developer in a Company.
On the first day itself, Your BIG BOSS has given a large text file and
directed you to remove all blank lines and return the modified one
within say 3 minutes. Suggest four possible ways to find solutions
to this problem
Solution 1
Load this file using one editor and manually delete all blank lines
60/67
Try this
Problem Statement
Assume that you have been recruited as a developer in a Company.
On the first day itself, Your BIG BOSS has given a large text file and
directed you to remove all blank lines and return the modified one
within say 3 minutes. Suggest four possible ways to find solutions
to this problem
Solution 1
Load this file using one editor and manually delete all blank lines
Solution 2
Write a script and solve the problem. See the script example28.pl
60/67
Try this
Problem Statement
Assume that you have been recruited as a developer in a Company.
On the first day itself, Your BIG BOSS has given a large text file and
directed you to remove all blank lines and return the modified one
within say 3 minutes. Suggest four possible ways to find solutions
to this problem
Solution 3
Use one powerful editor and use RegExp features to complete the
task
61/67
Try this
Problem Statement
Assume that you have been recruited as a developer in a Company.
On the first day itself, Your BIG BOSS has given a large text file and
directed you to remove all blank lines and return the modified one
within say 3 minutes. Suggest four possible ways to find solutions
to this problem
Solution 3
Use one powerful editor and use RegExp features to complete the
task
Solution 4
awk ’NF’ data4.txt > data5.txt
62/67
AWK Script Structure
63/67
AWK Workflow
64/67
AWK Example ~ script2.awk
65/67
Part III: Make utility
66/67
67/67