The document provides an overview of the topics to be covered in a basic Perl programming course, including an introduction to Perl, variables, control structures, loops, subroutines, regular expressions, Boolean logic, and file handling. The agenda lists the main topics as Perl introduction, variables, control structures, loops, defining and using subroutines, regular expressions, using Boolean logic for true/false conditions, and file handling. Examples are then provided for many of the programming concepts.
2. Agenda
Perl introduction
Variables
Control Structures
Loops
Defining and using subroutines
Regular Expression
Using Boolean (True/False)
File handling
August 2, 2012 2
3. Perl Introduction
• Perl is a general-purpose programming language
originally developed for text manipulation. Now, Perl
used for web development, system administration,
network programming, core generation and more.
• Open Source and free licencing.
• Support both procedural and OOP.
• Excellent text handling and regular expressions
August 2, 2012 3
4. Installation Perl
• Install ActivePerl 5.14.2
• Install Eclispse 3.6 or greater
• In Eclipse IDE go to menu item “Help > Install New Software…” to install
EPIC plugins of Perl at link: http://e-p-i-c.sf.net/updates/testing
• After finish installed Perl plugins, go to menu “Run -> External Tools ->
External Tools... “ to add and active the configuration for running Perl
August 2, 2012 4
5. Variables
Scalar:
$myString = “Hello” ; hello
$num1 = 10.5; 10.5
$num2 = $num1; 10.5
print “Number is :$num1”; Number is :10.5
Prefix characters on Perl:
• $: variable containing scalar values such as a number or a string
• @: variable containing a list with numeric keys
• %: variable containing a list with strings as keys
• &: subroutine
• *: matches all structures with the associated name
August 2, 2012 5
6. Variables
Array (1):
@colors = (“Red”, “Blue”, “Orange”);
$colors[0] = “Red”;
$colors[1] = “Blue”;
$colors[2] = “Orange”;
print “Our colors variable contains: @ colors ”;
Our colors variable contains : Red Blue Orange
print “First element of the colors is: $colors[0]”;
First element of the colors is: Red
@CombinedArray =(@Array1,@Array2);// merge 2
arrays
@hundrednums = (101 .. 200);
August 2, 2012 6
7. Variables
Array (2):
Perl functions for working with arrays:
• pop - remove last element of an array:
• push - add an element to the end of array;
• shift - removes first element of an array;
• unshift - add an element to the beginning of array;
• sort - sort an array.
EG:
@colors = (“Red”, “Blue”, “Orange”);
print “Our colors variable contains: @ colors ”;
Our colors variable contains : Red Blue Orange
pop @colors;
print “Colors after applying pop function:
@array1[0..$#array1]";
Colors after applying pop function: Red Blue
August 2, 2012 7
13. Defining and using subroutines
$var1 = 100;
$var2 = 200;
$result = 0;
$result = my_sum();
print "$resultn";
sub my_sum {
$tmp = $var1 + $var2;
return $tmp;
}
=> 300
August 2, 2012 13
14. Regular Expression
Perl Regular Expressions are a strong point of Perl:
• b: word boundaries • *: zero or more times
• d: digits • +: one or more times
• n: newline • ?: zero or one time
• r: carriage return • {p,q}: at least p times and at most q times
• s: white space characters • {p,}: at least p times
• t: tab • {p}: exactly p times
• w: alphanumeric characters
• ^: beginning of string
• $: end of string
• Dot (.): any character
• [bdkp]: characters b, d, k and p
• [a-f]: characters a to f
• [^a-f]: all characters except a to f
• abc|def: string abc or string def
• [:alpha:],[:punct:],[:digit:], … - use inside character class e.g., [[:alpha:]]
August 2, 2012 14
15. Regular Expression : substitutions
• The “s/<partten>/<replace_partten>/” substitution
operator does the ‘search and replace’
- append a g to the operator to replace every occurrence.
- append an i to the operator, to have the search case insensitive
Examples:
$line = “He is out with Barney. He is really
happy!”;
$line =~ s/Barney/Fred/; #He is out with Fred. He
is really happy!
$line =~ s/Barney/Wilma/;#He is out with Fred. He is
really happy! (nothing happens as search failed)
$line = “He is out with Fred. He is really happy”
$line =~ s/He/She/g; #She is out with Fred. She is
really happy!
$text =~ s/bug/feature/g; # replace all occurrences
of "bug"
August 2, 2012 15
16. Regular Expression : translations
• The "tr/<partten> /<replace_partten>/" operator
performs a substitution on the individual characters.
Examples:
$x =~ tr/a/b/; # Replace each "a" with "b".
$x =~ tr/ /_/; # Convert spaces to underlines.
$x =~ tr/aeiou/AEIOU/; # Capitalise vowels.
$x =~ tr/0-9/QERTYUIOPX/; # Digits to
letters.
$x =~ tr/A-Z/a-z/; # Convert to lowercase.
August 2, 2012 16
17. Regular Expression : matching
• The “m//” (or in short //) operator checks for matching .
Examples:
$x =~ m/dd/; # Search 2 digits.
$x =~ m/^This/; # Search string begin with “This”.
$x =~ m/string$/; # Search string end with “This” .
$x =~ m /sds/; # Search a digit with white
space in front and after it
$x =~ m/^$/; # Search for blank line.
August 2, 2012 17
18. Regular Expression : split & join
• Split breaks up a string according to a separator.
$line = “abc:def:g:h”;
@fields = split(/:/,$line) =>
#(‘abc’,’def’,’g’,h’)
• Join glues together a bunch of pieces to make a string.
@fields = (‘abc’,’def’,’g’,h’)
$new_line = join(“:”,@fields) => #“abc:def:g:h”
August 2, 2012 18
20. Logical Tests: AND, OR
• AND
Value of B
Value of A 1 '0.0' B string 0 empty str undef
1 1 0.0 B string 0 empty str undef
'0.0' 1 0.0 B string 0 empty str undef
A string 1 0.0 B string 0 empty str undef
0 0 0 0 0 0 0
empty str empty str empty str empty str empty str empty str empty str
undef undef undef undef undef undef undef
August 2, 2012 20
21. Logical Tests: AND, OR
• OR
Value of B
Value of empty
1 '0.0' B string 0 undef
A str
1 1 1 1 1 1 1
'0.0' 0.0 0.0 0.0 0.0 0.0 0.0
A string A string A string A string A string A string A string
0 1 0.0 B string 0 empty str undef
empty
1 0.0 B string 0 empty str undef
str
undef 1 0.0 B string 0 empty str undef
August 2, 2012 21
22. Exclusive OR: XOR
• XOR
Value of B
Value of empty
1 '0.0' a string 0 undef
A str
1 false false false true true true
'0.0' false false false true true true
a string false false false true true true
0 true true true false false false
empty
true true true false false false
str
undef true true true false false false
August 2, 2012 22
23. File handling
• Opening a File:
open (SRC, “my_file.txt”);
• Reading from a File
$line = <SRC>; # reads upto a newline character
• Closing a File
close (SRC);
August 2, 2012 23
24. File handling
• Opening a file for output:
open (DST, “>my_file.txt”);
• Opening a file for appending
open (DST, “>>my_file.txt”);
• Writing to a file:
print DST “Printing my first line.n”;
• Safeguarding against opening a non existent file
open (SRC, “file.txt”) || die “Could not open file.n”;
August 2, 2012 24
25. File Test Operators
• Check to see if a file exists:
if ( -e “file.txt”) {
# The file exists!
}
• Other file test operators:
-r readable
-x executable
-d is a directory
-T is a text file
August 2, 2012 25
26. File handling sample
• Program to copy a file to a destination file
#!/usr/bin/perl -w
open(SRC, “file.txt”) || die “Could not open source file.n”;
open(DST, “>newfile.txt”);
while ( $line = <SRC> )
{
print DST $line;
}
close SRC;
close DST;
August 2, 2012 26