Useful links & books
•	http://www.activestate.com
•	http://www.netcat.co.uk/rob/perl/win32perltut.html
•	http://www.ebb.org/PickingUpPerl
•	Randal L. Schwartz, Learning Perl, 3rd Ed,  July 2001
•	David N. Blank-Edelman, Perl for System Administration, O’Reilly, 1st Ed, July 2000
What is Perl?
•	Perl is a programming language
•	The best language for processing text
•	Cross platform, free & open
•	Microsoft have invested heavily in ActiveState to improve support for Windows in Perl
•	Has excellent connection to the operating system
•	Has enormous range of modules for thousands of application types
Eclectic
•	Borrows ideas from many languages, including:
•	C, C++
•	Shell
•	Lisp
•	BASIC
•	…even Fortran
•	Many others…
Why should I learn it?
•	Consider a real-life sys-admin problem:
•	You must make student accounts for 1500 students
•	TEACHING BEGINS TOMORROW!!!
•	The admission system gives you student enrollment data ….… but it can only give you PDF files with irregular format which 
•	Has a variable number of lines before the student data begins
•	Has a variable number of columns between different files
•	Has many rows per enrolled student ……….
Sample data for new courses:
 15    N    CHAN Tai Man                     M 991111111     X123456(7)                        28384858              CHEUNG
                  10-SEP-01         10-SEP-01                    29394959                                                 TAI MO                                                                                                                      BROTHER                                                                                                       91234567

•	There is a different number of lines above the student records
•	There is a different number of characters within each column from file to file
•	There are many files

Solution in Perl
Why not using other language?
•	This program took a very short time to write.  It is very robust, and has great portability
•	Perl is strongly supported by Microsoft
•	Perl is optimized for text and systems administration programming.  For problems such as the irregular admission data described before, Perl is second to no other programming language 
•	Other language will require much more coding.
•	The Perl solution given in last slide has:
•	comments
•	plenty of space to show structure
•	…and handles exceptional situations (i.e., it is robust) 
A simple Perl program
Variables
•	There are three basic types of variable:
•	Scalar (can be a number or string or…)
•	Array (an ordered array of scalars)
•	Hash (an unordered array of scalars indexed by strings/keys instead of numbers)

@array and %hashes
•	@array
•	Starts with a @
•	Indexes start at 0, like in C
•	%hashes
•	Unfamiliar concept to many of you
•	Like an array, but indexed by strings/keys
•	A data structure like a database

$scalars:
•	Start with a dollar sign $
•	Hold a single value, not a collection
•	A string is a scalar, so is a number
•	Example program:
$string = "snm";
$num1 = 100;
$num2 = 1.01;
print "The string is $string, number 1 is $num1 and number 2 is $num2\n";
•	Run this program, result being displayed is 
The string is snm, number 1 is 100 and number 2 is 1.01
Typing
•	Typing: declaring the type of variable 
•	A basic step in almost all programming languages.
•	Example:  int x=10
•	In previous example, there is no typing, no declaration of the type of variable.
•	Typing is important in large programming projects.
•	In short programs, it is much easier and efficient not using typing/declaration
•	The variable prefix $ makes it easy for you to see where the variables are and what type of variable it is.
•	Perl is said to be a loosely typed language as opposed to other strongly typed languages such as C++
Variable Interpolation
	$string="perl"; 
	$num=20; 
	print "Doubles: The string is $string and the number is $num\n"; 
	print 'Singles: The string is $string and the number is $num\n'; 

•	Result of running the above program:
	Doubles: The string is perl and the number is 20
	Singles: The string is $string and the number is $num\n

•	Note that double quotes supports variable interpolation, single quotes does not.
Escaping
•	Escaping means that just the special symbols/characters ($, @, #, etc.) are printed instead of referring to the variables.  
•	The special characters, such as $, @ and #, are also called metacharacters.
•	Escaping also turns some non-special characters into something special, like the new line \n example.
•	What will the following command print?
	print "home directory is c:\\perl\\\n"; 
	$num1 = 10;
	print “\$num1 is $num1"; 

Strings and Increments
$string="perl"; 
$num=20; 
print "The string is $string and the number is $num\n"; 
$num++; 
$string++; 
print "The incremented string is $string and the incremented number is $num\n"; 
•	A number can be incremented, so is a string.
•	What will be displayed when the above program is run?
Print: a list operator
•	The print function is a list operator.  It accepts a list of things, separated by commas, to print.  For example,
	$var="Perl"; 
	$num=10; 
	print "Two \$nums are $num * 2 and adding one to \$var makes $var++\n";
	print "Two \$nums are ", $num * 2," and adding one to \$var makes ", $var++,"\n";
	print "\$var is now $var\n"; 
•	Notice the difference between the three lines being printed.

Subroutines
•	A subroutine is a user-defined function that is used more than once in a program.  Subroutines can be in the beginning, middle or end of a Perl program (or script).
•	A subroutine is defined by starting with sub and then its name.  A pair of curly bracket { } is required to enclose the code of the subroutine.  The area between the two brackets is called a block.  The prefix & is used when calling a subroutine.
•	For example,
$num=10; 		# sets $num to 10 
&print_results; 	# prints variable $num 
$num++; 
&print_results; 
$num*=3; 
&print_results; 
sub print_results { print "\$num is $num\n"; } 
Test: ‘truth’ in Perl
•	To test for truth, we can use if, while and unless functions.
•	What is ‘true’ in Perl
•	Any string is true except for "" and "0". 
•	Any number is true except for 0. This includes negative numbers. 
•	Any undefined variable is false. A undefined variable is one which doesn't have a value, ie has not been assigned to. 
•	For example; 
•	$day=“Monday"; 
•	if ($day eq “Monday") { print “Go to school\n"; } 
Test for truth: single variable
&isit; 			# $test1 is at this moment undefined 
$test1="hello";		# a string, not equal to "" or "0" 
&isit; 
$test1=0.0; 		# $test1 is now a number, effectively 0 
&isit; 
$test1="0.0"; 		# $test1 is a string, but NOT effectively 0 ! 
&isit; 
sub isit { 
if ($test1) {print "$test1 is true\n"; } 	# tests $test1 for truth or not 
else {print "$test1 is false\n"; } 	# else statement if it is not true 
} 
Test for truth: expression
$x=5; 
$y=5; 
if ($x - $y) { 
	print '$x - $y is ',$x-$y," which is true\n"; 
} else { 
	print '$x - $y is ',$x-$y," which is false\n"; 
} 

Multiple tests with elseif
$age=25; 
$max=30; 
$min=18; 
#Example 1, using two if statements
if ($age > $max) { print "Too old !\n"; } 
if ($age < $min) { print "Too young !\n"; } 
#Example 2, using elseif
if ($age > $max) { print "Too old !\n"; } 
elsif ($age < $min) { print "Too young !\n"; } 
else { print "Just right !\n"; } 
•	Compare example 1 and 2 to note the usefulness of elseif
Comparison: Equality
$num1=15; 
$num2=15; 
if ($num1 == $num2) { print "num1 equal num2.\n"; } 
	else { print "num1 not equal num2.\n";} 

$name1 = 'Mark'; 
$name2 = 'Tony'; 
if ($name1 == $name2) { print "name1 equal name2.\n"; } 
	else { print "name1 not equal name2\n"; } 
•	Is Mark equal to Tony?  Yes, numerically!  
•	To compare string with string, use eq rather than ==. 
Comparison Operators
•	Comparison			Numeric		String
Equal			==		eq
Not equal			!=		ne
Greater than			>		gt
Less than			<		lt
Greater than or equal to	>=		ge
Less than or equal to		<=		le
User Input: STDIN
•	To get input from user through keyboard, use the <STDIN> function.  For example,
print "Please tell me your name: "; 
$name=<STDIN>; 
print "Your input is, $name\n"; 
•	Compare the above example with the one below
print "Please tell me your name: "; 
$name=<STDIN>; 		# these two lines can be reduced to just
chop $name; 		# one line as  chop($name=<STDIN>) 
print "Your input is, $name\n" 	# it is better/safer to use chomp rather than chop
•	The first example will take in the name input as well as the enter (new line) character
•	In the second example, the chop function remove the last character input.
Arrays
•	Perl has two types of array: arrays and associative arrays (or, hashes).  We will first talk about arrays which you are quite familiar with. 
•	Array is an ordered list of scalar variables, as demonstrated in the example below.
	@names=("Muriel","Gavin","Susanne","Sarah","Anna"); 
	print "The elements of \@names are @names\n"; 
	print "The first element is $names[0] \n"; 
	print "The third element is $names[2] \n"; 
	print 'There are ',scalar(@names)," elements in the array\n"; 
•	Notice the @ prefix for referring to arrays.  Also notice that you can refer to individual variable in the list, or, the whole collection of variables.

$scalar and @array
•	The following example shows the difference between arrays (with @ prefix) and single scalar variable (with $ prefix).
	$myvar="scalar variable"; 
	@myvar=("one","element","of","an","array","called","myvar"); 
	print $myvar; 		# refers to the contents of a scalar variable called myvar 
	print "\n";
	print $myvar[1]; 	# refers to the 2nd element of the array, notice the $ prefix! 
	print "\n";
	print @myvar; 		# refers to all the elements of array myvar 
	print "\n";
	print "@myvar "; 	# notice the better print format
Accessing arrays
•	Example of accessing array
print "Enter a number :"; 
chomp ($x=<STDIN>); 
@names=("Muriel","Gavin","Susanne","Sarah","Anna"); 
print "You requested element $x who is $names[$x]\n"; 
print "The first two elements are @names[0,1]\n"; 
print "The first three elements are @names[0..2]\n";
print "The last element is @names[-1]\n"; 
print "The index number of the last element is $#names \n"; 
•	Notice that an element in an array is index by a number ($x = 1, 2, 3, etc.).
•	Also notice that the last line returns the index number of the last element.
Arrays and For Loops
•	A simple for loop for accessing elements in array.
@names=("Muriel","Gavin","Susanne","Sarah","Anna","Paul","Trish","Simon"); 
for ($x=0; $x <= $#names; $x++) { print "$names[$x]\n"; } 
•	For loop with range operator
for $x (0 .. $#names) { print "$names[$x]\n"; }
•	The foreach function
foreach $person (@names) { print "$person"; } 
•	The Default Input and Pattern Searching Variable. 
foreach (@names) { print "$_"; } 	# If a variable is not specified, $_ is used by default
foreach (@names) { print ; }	# $_ is printed by default.  $_ is used even it is written.
Premature end to for loop
•	Stopping a for loop
while (1) { 
	$x++; 
	print "$x: You can press CTRL-C to interrupt a perl program?\n"; } 
•	The last operator
	@names=('Mrs Smith','Mr Jones','Ms Samuel','Dr Jansen','Sir Philip'); 
	foreach $person (@names) { print "$person\n"; last if $person=~/Dr /; }
•	The ~/Dr/ is an example of regular expression that we would discuss later. 

Changing the elements of an array
•	Adding a new element with push operator
print "Enter a name :"; 
chomp ($x=<STDIN>);
@names=("Muriel","Gavin","Susanne","Sarah"); 
print "@names\n"; 
push (@names, $x); 
print "@names\n"; 
•	Other operators are
•	Pop : removes and returns value from end of array
•	Shift : removes and returns value from beginning of array
•	Unshift : add value to the beginning of array

Regular Expressions (regex)
•	Perl is widely renowned for excellence in text processing, and regular expressions are one of the big factors behind this fame. 
•	What is a regular expression? A regular expression is simply a string that describes a pattern. 一Patterns are in common use these days; examples are the patterns typed into a search engine to find web pages and the patterns used to list files in a directory, e.g., ls *.txt or dir *.*. 
•	In Perl, the patterns described by regular expressions are used to search strings, extract desired parts of strings, and to do search and replace operations.
•	Regular expressions are constructed using simple concepts like conditionals and loops. 
•	'regular expression' is often abbreviated as regexp or regex. 

Regex - a simple word matching
	"Hello World" =~ /World/;  	# matches
•	"Hello World" is a simple double quoted string 
•	World is the regular expression
•	the // enclosing /World/ tells Perl to search a string for a match.
•	The operator =~ associates the string with the regexp match and produces a true value if the regexp matched, or false if the regexp did not match. 
•	In this case, World matches the 2nd word in "Hello World", so the expression is true. 
•	Expressions like this are useful in conditionals:
    if ("Hello World" =~ /World/) { print "It matches\n"; }
    else { print "It doesn't match\n“;}

Why use regex
Why use regex
•	The previous regex based program is slightly modified as below. 
•	Using the /i switch, which specifies case-insensitivity, the program works for all variations, such as "the Faq" and "the FAQ".   And, the “the faq” can be within a text, such as, “I would like to read the FAQ section before I join Perl discussion”.
Match & not match
Character Class
Character Class – more example
More examples of regex matching
•	Matching at specific points 
•	To match at the end of the line, use $, for example, /a$/ match those characters ending with a. 
•	The caret ^ negates a character class, for example, [^KCZ]arl.
•	The caret ^ also matches at the beginning of the string, for example, /^n/i
•	Negating the regex
•	To negate the entire regex, change =~ to !~ (Remember ! means 'not equal to'.)   
•	For example, ($_ !~/[KC]arl/) or, just (!/[KC]arl/)
Return the match
Return the match: $1, $2
•	In the example below, the regex begin matching for ‘<‘, when ‘<‘ has been found, the first parenthesis ( ) start to match for ‘robert’ and put the value into the variable $1 when matched.  
•	The regex then carry on by matching for the character @ ( \ escape the special function of @), when @ has been found, the 2nd parenthesis ( ) start to match for ‘netcat.co.uk’ and put the value into the variable $2 when matched.
Match & return the unkown
•	The example on last slide requires that the email address be know in advanced. 
•	In the example below, the regex does not know what to match for.
•	In the /(.*)/ regex, the dot or period ‘.’ match for any character, and the * match for zero or more characters before the dot/period.
•	Hence, the first line will print the whole sentence assigned to $_
•	In the second line, the regex /<(.*)>/ match start with ‘<‘ and end with ‘>’, hence the parenthesis ( ) only put the string starting < and ending > into $1
Opening Files
•	Assume you have a file called data.txt in the c:\perl directory, the following program can open the file and read the content.
•	If the open operation fails, the code next to or is evaluated.  The code ‘dies’ means exiting the script.  The $! tells at which line the program exits.
•	The special variable $. is the current line number, starting at 1. 
Opening Files
•	The line input operator, the angle brackets <>, reads from the beginning of the file up until and including the first newline. The read data goes into $_. On the next iteration of the loop data is read from where the last read left off, up to the next newline. And so on until there is no more data. When that happens the condition is false and the loop terminates. 

Writing & appending data to a File
•	To open a file for writing data, add > to the filename.
•	To open a file for appending data, add >> to the filename.
•	When print to a file, just specify the filehandle name.
•	Closing a file after use is not mandatory unless you wish to open another file. 
Associative Arrays
•	Arrays are an ordered list of scalar variables, which you access by their index number starting at 0. The elements in arrays always stay in the same order. 
•	Hashes are a list of scalars, but instead of being accessed by index number, they are accessed by a key. 

@Array & %Hash
•	So if we want 'Belgium' from @myarray and also from %myhash , it'll be: 
•	print "$myarray[1]"; 
•	print "$myhash{'BE'}"; 
•	Notice that the $ prefix is used, because it is a scalar variable. 
•	Also, notice that hash uses braces { } instead of square brackets. 
•	So why use hashes ? When you want to look something up by a keyword. 
•	Suppose we wanted to create a program which returns the name of the country when given a country code.  E.g., input ES, and the program return Spain. 
•	You could do it with arrays, but more complicated.
Hash example
•	Notice the way %countries is defined - exactly the same as a normal array, except that the values are put into the hash in key/value pairs. 
•	Since the keys in the example below are all uppercase, need to use tr to change the input to uppercase. 
•	Each key of a hash must be unique.  You can not have two keys with the same name.  If you do define a certain key twice, the second value overwrites the first. 
•	The values of a hash can be duplicates, but never the keys. 
Array Vs Hash
•	So why use arrays ? One excellent reason is because when an array is created, its variables stay in the same order you created them in. 
•	With a hash, Perl reorders elements for quick access.  Use print to (as in the example below) to see that there is no recognizable sequence at all. 
•	If you were writing code that stored a list of variables over time and you wanted it back in the order you found it in, don't use a hash. 
Accessing Hash
Hash: iteration
•	keys and values return a list. 
•	we can iterate over a list, such as in the example below
	foreach (keys %countries) 
	{ print "The key $_ contains $countries{$_}\n"; } 
•	Another example similar to above
	while (($code,$name)=each %countries) 
	{ print "The key $code contains $name\n"; } 
•	The each function returns each key/value pair of the hash, and is slightly faster. In this example we assign them to a list (with the parens). Eventually there are no more pairs, which returns false to the while loop and it stops. 

Hash: Sorting
•	A simple sort:
foreach (sort keys %countries) 
{ print "The key $_ contains $countries{$_}\n"; } 
•	A simple, reverse sort: 
foreach (reverse sort keys %countries) 
{ print "The key $_ contains $countries{$_}\n"; } 

External Commands
•	There are several ways for Perl to start external commands, such as,
•	system 
•	exec : doesn’t work support Win32 well.
•	`backticks` 
•	System: 
•	This runs an external command for you, then carries on with the script. 
•	It always returns, and the value it returns goes into $? So that you can test to see if the program worked.  Actually you are testing to see if it could be started, what the program does when it runs is outside your control if you use system . 
External commands: system
•	This example demonstrates the use of external command - system. Note that vole should be any executable command in your computer. 
system("vole"); 
print "\n\nResult: $?\n\n"; 
system("vol"); 
print "\n\nResult: $?\n\n"; 

system for user/network administration
•	Using the external command “system” together with the net command of Windows, you can perform various user/network administration work.  For example, try the following script.
	system("net use drive_letter: \\\\xxx\\yyy zzz /user:abc"); 
	#where drive_letter is the drive that you want to map to, say d:
	#where xxx is the server name or ip address
	#yyy is the shared folder name and zzz is the password of the user abc.
External command: backticks
•	Backticks: These `` are different again to system and exec. (Don't confuse single quotes '' with backticks ``) 
•	Backticks also start external processes, but return the output of the process.  You can then do whatever you like with the output.. 
$volume=`vol`; 
print "The contents of the variable \$volume are:\n\n"; 
print $volume; 
Perl functions Vs external calls
•	There are plenty of excellent modules and built in functions which can provide the same results with the use of external commands.  
•	Perl functions are better than external programs because Perl's functions are: 
•	portable (usually, but there are exceptions). This means you can write a script on your Mac PowerBook, test it on an NT box and then use it live on your Unix box without modifying a single line of code; 
•	faster, as every external process significantly slows your program; 
•	don't usually require regexing to find the result you want; 
•	don't rely on output in a particular format, which might be changed in the next version of your OS or application; 
•	are more likely to be understood by a Perl programmer -- for example, $files=`ls`; on a Unix box means little to someone that doesn't know that  ls is the Unix command for listing files, as  dir is in Windows.