%% ================================================================================ %% This LaTeX file was created by AbiWord. %% AbiWord is a free, Open Source word processor. %% You may obtain more information about AbiWord at www.abisource.com %% ================================================================================ \documentclass[12pt]{article} \usepackage[T1]{fontenc} \usepackage{calc} \usepackage{hyperref}\usepackage{setspace} \usepackage{multicol} \usepackage[normalem]{ulem} \usepackage{color} \setlength{\oddsidemargin}{1.250000in-1in} \setlength{\textwidth}{\paperwidth - 1.250000in-1.250000in} \begin{document} \begin{center} \end{itemize} \end{slide} \begin{slide}{Useful links \& books} \begin{itemize} \end{center} \item http://www.activestate.com \item http://www.netcat.co.uk/rob/perl/win32perltut.html \item http://www.ebb.org/PickingUpPerl \item Randal L. Schwartz, }\textbf{\textit{{Learning Perl}}}{, 3}$^{\mathrm{{rd}}}${ Ed, July 2001 \item David N. Blank-Edelman, }\textbf{\textit{{Perl for System Administration}}}{, O'Reilly, 1}$^{\mathrm{{st}}}${ Ed, July 2000 \begin{center} \end{itemize} \end{slide} \begin{slide}{What is Perl?} \begin{itemize} \end{center} \item Perl is a programming language \item The best language for processing text \item Cross platform, free \& open \item Microsoft have invested heavily in ActiveState to improve support for Windows in Perl \item Has excellent connection to the operating system \item Has enormous range of modules for thousands of application types \begin{center} \end{itemize} \end{slide} \begin{slide}{Eclectic} \begin{itemize} \end{center} \item Borrows ideas from many languages, including: \item C, C++ \item Shell \item Lisp \item BASIC \item \ldotseven Fortran \item Many others\ldots \begin{center} \end{itemize} \end{slide} \begin{slide}{Why should I learn it?} \begin{itemize} \end{center} \item Consider a real-life sys-admin problem: \item You must make student accounts for 1500 students \item TEACHING BEGINS TOMORROW!!! \item The admission system gives you student enrollment data \ldots.}{\ldots but it can only give you PDF files with irregular format which \item Has a variable number of lines before the student data begins \item Has a variable number of columns between different files \item Has many rows per enrolled student \ldots\ldots\ldots. \begin{center} \end{itemize} \end{slide} \begin{slide}{Sample data for new courses:} \begin{itemize} \end{center} \item }{15 N CHAN Tai Man M 991111111 X123456(7) 28384858 CHEUNG \item 10-SEP-01 10-SEP-01 29394959 TAI MO BROTHER 91234567 \item \item There is a different number of lines above the student records \item There is a different number of characters within each column from file to file \item There are many files \item \item \begin{center} \end{itemize} \end{slide} \begin{slide}{Solution in Perl} \begin{itemize} \end{center} \begin{center} \end{itemize} \end{slide} \begin{slide}{Why not using other language?} \begin{itemize} \end{center} \item This program took a very short time to write. It is very robust, and has great portability \item Perl is strongly supported by Microsoft \item 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}{ \item Other language will require much more coding. \item The Perl solution given in last slide has: \item comments \item plenty of space to show structure \item \ldotsand handles exceptional situations (i.e., it is robust) \begin{center} \end{itemize} \end{slide} \begin{slide}{A simple Perl program} \begin{itemize} \end{center} \begin{center} \end{itemize} \end{slide} \begin{slide}{Variables} \begin{itemize} \end{center} \item There are three basic types of variable: \item \textbf{\textit{{Scalar}}}{ (can be a number or string or\ldots)} \item \textbf{\textit{{Array}}}{ (an ordered array of scalars)} \item \textbf{\textit{{Hash}}}{ (an unordered array of scalars indexed by strings/keys instead of numbers)} \item \begin{center} \end{itemize} \end{slide} \begin{slide}{@}}\textbf{{\LARGE{}array and \%hashes} \begin{itemize} \end{center} \item @}{array \item Starts with a @ \item Indexes start at 0, like in C \item \%hashes \item Unfamiliar concept to many of you \item Like an array, but indexed by strings/keys \item A data structure like a database \item \item \item \begin{center} \end{itemize} \end{slide} \begin{slide}{\$}}\textbf{{\LARGE{}scalars:} \begin{itemize} \end{center} \item Start with a dollar sign \$ \item Hold a single value, not a collection \item A string is a scalar, so is a number \item Example program: \item \$string = "snm"; \item \$num1 = 100; \item \$num2 = 1.01; \item print "The string is \$string, number 1 is \$num1 and number 2 is \$num2\ensuremath{\backslash}n"; \item Run this program, result being displayed is \item The string is snm, number 1 is 100 and number 2 is 1.01 \begin{center} \end{itemize} \end{slide} \begin{slide}{Typing} \begin{itemize} \end{center} \item Typing: declaring the type of variable \item A basic step in almost all programming languages. \item Example: int x=10 \item In previous example, there is no typing, no declaration of the type of variable. \item Typing is important in large programming projects. \item In short programs, it is much easier and efficient not using typing/declaration \item The variable prefix \$ makes it easy for you to see where the variables are and what type of variable it is. \item Perl is said to be a }\uline{{loosely typed}}{ language as opposed to other }\uline{{strongly typed}}{ languages such as C++ \begin{center} \end{itemize} \end{slide} \begin{slide}{Variable Interpolation} \begin{itemize} \end{center} \item }{\$}{string="perl"; \item \$num=20; \item print "Doubles: The string is \$string and the number is \$num\ensuremath{\backslash}n"; \item print 'Singles: The string is \$string and the number is \$num\ensuremath{\backslash}n'; \item \item Result of running the above program: \item Doubles: The string is perl and the number is 20 \item Singles: The string is \$string and the number is \$num\ensuremath{\backslash}n \item \item Note that }\uline{{double quotes}}{ supports variable interpolation, }\uline{{single quotes}}{ does not. \begin{center} \end{itemize} \end{slide} \begin{slide}{Escaping} \begin{itemize} \end{center} \item \uline{{Escaping}}{ means that just the special symbols/characters (\$, @, \#, etc.) are printed instead of referring to the variables. } \item The special characters, such as \$, @ and \#, are also called }\uline{{metacharacters}}{. \item Escaping also turns some non-special characters into something special, like the new line \ensuremath{\backslash}n example. \item What will the following command print? \item print "home directory is c:\ensuremath{\backslash}\ensuremath{\backslash}perl\ensuremath{\backslash}\ensuremath{\backslash}\ensuremath{\backslash}n"; \item \$num1 = 10; \item print \ensuremath{\backslash}\$num1 is \$num1"; \item \begin{center} \end{itemize} \end{slide} \begin{slide}{Strings and Increments} \begin{itemize} \end{center} \item \$}{string="perl"; \item \$num=20; \item print "The string is \$string and the number is \$num\ensuremath{\backslash}n"; \item \$num++; \item \$string++; \item print "The incremented string is \$string and the incremented number is \$num\ensuremath{\backslash}n"; \item A number can be incremented, so is a string. \item What will be displayed when the above program is run? \begin{center} \end{itemize} \end{slide} \begin{slide}{Print: a list operator} \begin{itemize} \end{center} \item The print function is a }\uline{{list operator}}{. It accepts a list of things, separated by commas, to print. For example, \item }{\$var="Perl"; \item \$num=10; \item print "Two \ensuremath{\backslash}\$nums are \$num * 2 and adding one to \ensuremath{\backslash}\$var makes \$var++\ensuremath{\backslash}n"; \item print "Two \ensuremath{\backslash}\$nums are ", \$num * 2," and adding one to \ensuremath{\backslash}\$var makes ", \$var++,"\ensuremath{\backslash}n"; \item print "\ensuremath{\backslash}\$var is now \$var\ensuremath{\backslash}n"; \item Notice the difference between the three lines being printed. \item \begin{center} \end{itemize} \end{slide} \begin{slide}{Subroutines} \begin{itemize} \end{center} \item 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). \item A subroutine is defined by starting with }\textit{{sub}}{ and then }\textit{{its name}}{. A pair of curly bracket }\textit{{\{ \}}}{ is required to enclose the code of the subroutine. The area between the two brackets is called a }\textit{{block}}{. The prefix }\textit{{\&}}{ is used when calling a subroutine. \item For example, \item \$num=10; \# sets \$num to 10 \item \&print\_results; \# prints variable \$num \item \$num++; \item \&print\_results; \item \$num*=3; \item \&print\_results; \item sub print\_results \{ print "\ensuremath{\backslash}\$num is \$num\ensuremath{\backslash}n"; \} \begin{center} \end{itemize} \end{slide} \begin{slide}{Test: truth in Perl} \begin{itemize} \end{center} \item To test for truth, we can use }\textit{{if}}{, }\textit{{while}}{ and }\textit{{unless}}{ functions. \item What is true in Perl \item Any string is true except for }{""}{ and }{"0"}{. \item Any number is true except for }{0}{. This includes negative numbers. \item Any undefined variable is false. A undefined variable is one which doesn't have a value, ie has not been assigned to. \item For example; \item \$day=Monday"; \item if (\$day eq Monday") \{ print Go to school\ensuremath{\backslash}n"; \begin{center} \end{itemize} \end{slide} \begin{slide}{Test for truth: single variable} \begin{itemize} \end{center} \item \&}{isit; \# \$test1 is at this moment undefined \item \$test1="hello"; \# a string, not equal to "" or "0" \item \&isit; \item \$test1=0.0; \# \$test1 is now a number, effectively 0 \item \&isit; \item \$test1="0.0"; \# \$test1 is a string, but NOT effectively 0 ! \item \&isit; \item sub isit \{ \item if (\$test1) \{print "\$test1 is true\ensuremath{\backslash}n"; \} \# tests \$test1 for truth or not \item else \{print "\$test1 is false\ensuremath{\backslash}n"; \} \# else statement if it is not true \item \}}{ \begin{center} \end{itemize} \end{slide} \begin{slide}{Test for truth: expression} \begin{itemize} \end{center} \item \$}{x=5; \item \$y=5; \item if (\$x - \$y) \{ \item print '\$x - \$y is ',\$x-\$y," which is true\ensuremath{\backslash}n"; \item \} else \{ \item print '\$x - \$y is ',\$x-\$y," which is false\ensuremath{\backslash}n"; \item \} \item \begin{center} \end{itemize} \end{slide} \begin{slide}{Multiple tests with elseif} \begin{itemize} \end{center} \item \$}{age=25; \item \$max=30; \item \$min=18; \item \#Example 1, using two if statements \item if (\$age > \$max) \{ print "Too old !\ensuremath{\backslash}n"; \} \item if (\$age < \$min) \{ print "Too young !\ensuremath{\backslash}n"; \} \item \#Example 2, using elseif \item if (\$age > \$max) \{ print "Too old !\ensuremath{\backslash}n"; \} \item elsif (\$age < \$min) \{ print "Too young !\ensuremath{\backslash}n"; \} \item else \{ print "Just right !\ensuremath{\backslash}n"; \} \item Compare example 1 and 2 to note the usefulness of elseif \begin{center} \end{itemize} \end{slide} \begin{slide}{Comparison: Equality} \begin{itemize} \end{center} \item \$}{num1=15; \item \$num2=15; \item if (\$num1 == \$num2) \{ print "num1 equal num2.\ensuremath{\backslash}n"; \} \item else \{ print "num1 not equal num2.\ensuremath{\backslash}n";\} \item \item \$name1 = 'Mark'; \item \$name2 = 'Tony'; \item if (\$name1 == \$name2) \{ print "name1 equal name2.\ensuremath{\backslash}n"; \} \item else \{ print "name1 not equal name2\ensuremath{\backslash}n"; \} \item Is Mark equal to Tony? Yes, numerically! \item To compare string with string, use }\textit{{eq}}{ rather than }=={. \begin{center} \end{itemize} \end{slide} \begin{slide}{Comparison Operators} \begin{itemize} \end{center} \item Comparison Numeric String \item Equal == eq \item Not equal != ne \item Greater than > gt \item Less than < lt \item Greater than or equal to >= ge \item Less than or equal to <= le \begin{center} \end{itemize} \end{slide} \begin{slide}{User Input: STDIN} \begin{itemize} \end{center} \item To get input from user through keyboard, use the function. For example, \item print "Please tell me your name: "; \item \$name=; \item print "Your input is, \$name\ensuremath{\backslash}n"; \item Compare the above example with the one below \item print "Please tell me your name: "; \item \$name=; \# these two lines can be reduced to just \item chop \$name; \# one line as }\textit{{chop(\$name=)}}{ \item print "Your input is, \$name\ensuremath{\backslash}n" \# it is better/safer to use }\textit{{chomp}}{ rather than }\textit{{chop} \item The first example will take in the name input as well as the }\textit{{enter}}{ (new line) character \item In the second example, the chop function remove the last character input. \begin{center} \end{itemize} \end{slide} \begin{slide}{Arrays} \begin{itemize} \end{center} \item Perl has two types of array: arrays and associative arrays (or, hashes). We will first talk about arrays which you are quite familiar with. \item Array is an ordered list of scalar variables, as demonstrated in the example below. \item @names=("Muriel","Gavin","Susanne","Sarah","Anna"); \item print "The elements of \ensuremath{\backslash}@names are @names\ensuremath{\backslash}n"; \item print "The first element is \$names[0] \ensuremath{\backslash}n"; \item print "The third element is \$names[2] \ensuremath{\backslash}n"; \item print 'There are ',scalar(@names)," elements in the array\ensuremath{\backslash}n"; \item 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. \item \begin{center} \end{itemize} \end{slide} \begin{slide}{\$scalar and @array} \begin{itemize} \end{center} \item The following example shows the difference between arrays (with @ prefix) and single scalar variable (with \$ prefix). \item \$}{myvar="scalar variable"; \item @myvar=("one","element","of","an","array","called","myvar"); \item print \$myvar; \# refers to the contents of a scalar variable called myvar \item print "\ensuremath{\backslash}n"; \item print \$myvar[1]; \# refers to the 2nd element of the array, notice the \$ prefix! \item print "\ensuremath{\backslash}n"; \item print @myvar; \# refers to all the elements of array myvar \item print "\ensuremath{\backslash}n"; \item print "@myvar "; \# notice the better print format \begin{center} \end{itemize} \end{slide} \begin{slide}{Accessing arrays} \begin{itemize} \end{center} \item Example of accessing array \item print "Enter a number :"; \item chomp (\$x=); \item @names=("Muriel","Gavin","Susanne","Sarah","Anna"); \item print "You requested element \$x who is \$names[\$x]\ensuremath{\backslash}n"; \item print "The first two elements are @names[0,1]\ensuremath{\backslash}n"; \item print "The first three elements are @names[0..2]\ensuremath{\backslash}n"; \item print "The last element is @names[-1]\ensuremath{\backslash}n"; \item print "The index number of the last element is \$\#names \ensuremath{\backslash}n"; \item Notice that an element in an array is index by a number (\$x = 1, 2, 3, etc.). \item Also notice that the last line returns the index number of the last element. \begin{center} \end{itemize} \end{slide} \begin{slide}{Arrays and For Loops} \begin{itemize} \end{center} \item A simple for loop for accessing elements in array. \item @}{names=("Muriel","Gavin","Susanne","Sarah","Anna","Paul","Trish","Simon"); \item for (\$x=0; \$x <= \$\#names; \$x++) \{ print "\$names[\$x]\ensuremath{\backslash}n"; \} \item For loop with range operator \item for \$x (0 .. \$\#names) \{ print "\$names[\$x]\ensuremath{\backslash}n"; \} \item The }\textit{{foreach}}{ function \item foreach \$person (@names) \{ print "\$person"; \} \item The Default Input and Pattern Searching Variable. \item foreach (@names) \{ print "\$\_"; \} }\textit{{\# If a variable is not specified, \$\_ is used by default} \item foreach (@names) \{ print ; \} }\textit{{\# \$\_ is printed by default. \$\_ is used even it is written.} \begin{center} \end{itemize} \end{slide} \begin{slide}{Premature end to for loop} \begin{itemize} \end{center} \item Stopping a for loop \item while (1) \{ \item \$x++; \item print "\$x: You can press CTRL-C to interrupt a perl program?\ensuremath{\backslash}n"; \} \item The }\textit{{last }}{operator \item @names=('Mrs Smith','Mr Jones','Ms Samuel','Dr Jansen','Sir Philip'); \item foreach \$person (@names) \{ print "\$person\ensuremath{\backslash}n"; last if \$person=\~{}/Dr /; \} \item The \~{}/Dr/ is an example of regular expression that we would discuss later. \item \begin{center} \end{itemize} \end{slide} \begin{slide}{Changing the elements of an array} \begin{itemize} \end{center} \item Adding a new element with }\textit{{push}}{ operator \item print "Enter a name :"; \item chomp (\$x=); \item @names=("Muriel","Gavin","Susanne","Sarah"); \item print "@names\ensuremath{\backslash}n"; \item push (@names, \$x); \item print "@names\ensuremath{\backslash}n"; \item Other operators are \item Pop : removes and returns value from end of array \item Shift : removes and returns value from beginning of array \item Unshift : add value to the beginning of array \item \begin{center} \end{itemize} \end{slide} \begin{slide}{Regular Expressions (regex)} \begin{itemize} \end{center} \item Perl is widely renowned for excellence in text processing, and regular expressions are one of the big factors behind this fame. \item 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 *.*. \item 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. \item Regular expressions are constructed using simple concepts like conditionals and loops. \item 'regular expression' is often abbreviated as regexp or regex. \item \begin{center} \end{itemize} \end{slide} \begin{slide}{Regex - a simple word matching} \begin{itemize} \end{center} \item "Hello World" =\~{} /World/; \# matches \item "Hello World" is a simple double quoted string \item World is the regular expression \item the // enclosing /World/ tells Perl to search a string for a match. \item 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. \item In this case, World matches the 2nd word in "Hello World", so the expression is true. \item Expressions like this are useful in conditionals: \item if ("Hello World" =\~{} /World/) print "It matches\ensuremath{\backslash}n"; \} \item else \{ print "It doesn't match\ensuremath{\backslash}n;\} \item \begin{center} \end{itemize} \end{slide} \begin{slide}{Why use regex} \begin{itemize} \end{center} \begin{center} \end{itemize} \end{slide} \begin{slide}{Why use regex} \begin{itemize} \end{center} \item The previous regex based program is slightly modified as below. \item Using the }\textit{{/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 }\textit{{the FAQ}}{ section before I join Perl discussion. \begin{center} \end{itemize} \end{slide} \begin{slide}{Match \& not match} \begin{itemize} \end{center} \begin{center} \end{itemize} \end{slide} \begin{slide}{Character Class} \begin{itemize} \end{center} \begin{center} \end{itemize} \end{slide} \begin{slide}{Character Class more example} \begin{itemize} \end{center} \begin{center} \end{itemize} \end{slide} \begin{slide}{More examples of regex matching} \begin{itemize} \end{center} \item Matching at specific points{ } \item T}{o match at the end of the line, }{use \$, for example, }\textbf{\textit{{/a\$/}}}{ match those characters ending with }\textit{{a}}{. \item T}{he caret \^{} negates a character class, for example, }\textbf{\textit{{[\^{}KCZ]arl}}}{. \item The caret \^{} }{also matches at the beginning of the string}{, for example, }\textbf{\textit{{/\^{}n/i}} \item Negating the regex \item T}{o negate the entire regex}{,}{ change =\~{} to !\~{} (Remember ! means 'not equal to'.) }{ \item For example, }(\$\_ !\~{}/[KC]arl/)}}{ }{or, just }\textbf{\textit{{(!/[KC]arl/) \begin{center} \end{itemize} \end{slide} \begin{slide}{Return the match} \begin{itemize} \end{center} \begin{center} \end{itemize} \end{slide} \begin{slide}{Return the match: \$1, \$2} \begin{itemize} \end{center} \item 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. \item The regex then carry on by matching for the character @ ( \ensuremath{\backslash} 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. \begin{center} \end{itemize} \end{slide} \begin{slide}{Match \& return the unkown} \begin{itemize} \end{center} \item The example on last slide requires that the email address be know in advanced. \item In the example below, the regex does not know what to match for. \item In the /(.*)/ regex, the dot or period . match for any character, and the * match for zero or more characters before the dot/period. \item Hence, the first line will print the whole sentence assigned to \$\_ \item In the second line, the regex /<(.*)>/ match start with < and end with >, hence the parenthesis ( ) only put the string starting < and ending > into \$1 \begin{center} \end{itemize} \end{slide} \begin{slide}{Opening Files} \begin{itemize} \end{center} \item Assume you have a file called data.txt in the c:\ensuremath{\backslash}perl directory, the following program can open the file and read the content. \item If the }\textit{{open}}{ }{operation fails, the code next to }\textit{{or}}{ is evaluated. The code }\textit{{dies }}{means exiting the script. The }\textit{{\$!}}{ tells at which line the program exits. \item The special variable }{\$. }{is the current line number, starting at 1. \begin{center} \end{itemize} \end{slide} \begin{slide}{Opening Files} \begin{itemize} \end{center} \item 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. \item \begin{center} \end{itemize} \end{slide} \begin{slide}{Writing \& appending data to a File} \begin{itemize} \end{center} \item To open a file for writing data, add > to the filename. \item To open a file for appending data, add >> to the filename. \item When print to a file, just specify the }\textit{{filehandle name}}{. \item Closing a file after use is not mandatory unless you wish to open another file. \begin{center} \end{itemize} \end{slide} \begin{slide}{Associative Arrays} \begin{itemize} \end{center} \item 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{. \item Hashes are a list of scalars, but instead of being accessed by index number, }they are accessed by a key{. \item \begin{center} \end{itemize} \end{slide} \begin{slide}{@Array \& \%Hash} \begin{itemize} \end{center} \item So if we want 'Belgium' from }{@myarray }{and also from }{\%myhash }{, it'll be: \item print "\$myarray[1]"; \item print "\$myhash\{'BE'\}"; \item Notice that the }{\$ }{prefix is used, because it is a scalar variable. \item Also, notice that hash uses braces }{\{ \} }{instead of square brackets. \item So why use hashes ? When you want to look something up by a keyword. \item 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. \item You could do it with arrays, but more complicated. \begin{center} \end{itemize} \end{slide} \begin{slide}{Hash example} \begin{itemize} \end{center} \item 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. \item Since the keys in the example below are all uppercase, need to use }\textit{{tr}}{ to change the input to uppercase. \item 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. \item The values of a hash can be duplicates, but never the keys. \begin{center} \end{itemize} \end{slide} \begin{slide}{Array Vs Hash} \begin{itemize} \end{center} \item 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. \item 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. \item 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. \begin{center} \end{itemize} \end{slide} \begin{slide}{Accessing Hash} \begin{itemize} \end{center} \begin{center} \end{itemize} \end{slide} \begin{slide}{Hash: iteration} \begin{itemize} \end{center} \item keys and values return a list. \item we can iterate over a list, }{such as in the example below \item }{foreach (keys \%countries) \item }{\{ print "The key \$\_ contains \$countries\{\$\_\}\ensuremath{\backslash}n"; \} \item Another example similar to above \item }{while ((\$code,\$name)=each \%countries) \item }{\{ print "The key \$code contains \$name\ensuremath{\backslash}n"; \} \item 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. \item \begin{center} \LARGE{}Hash: }{\LARGE{}Sorting \end{center} \item A simple }\textbf{\textit{{sort}}}{: \item foreach (sort keys \%countries) \item \{ print "The key \$\_ contains \$countries\{\$\_\}\ensuremath{\backslash}n"; \} \item A simple, }\textbf{\textit{{reverse sort}}}{: \item foreach (reverse sort keys \%countries) \item \{ print "The key \$\_ contains \$countries\{\$\_\}\ensuremath{\backslash}n"; \} \item \begin{center} \LARGE{}External Commands \end{center} \item There are several ways for }{Perl }{to}{ start external commands}{, such as, \item system \item exec }{: doesnt work support Win32 well. \item `backticks` \item System}}\textbf{{: \item This runs an external command for you, then carries on with the script. \item 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 . \begin{center} \end{itemize} \end{slide} \begin{slide}{External commands: }}\textbf{\textit{{\LARGE{}system}} \begin{itemize} \end{center} \item This example demonstrates }{the use of external command - }\textit{{system}}{. }{Note that vole should be any executable command in your computer}{. \item system("vole"); \item print "\ensuremath{\backslash}n\ensuremath{\backslash}nResult: \$?\ensuremath{\backslash}n\ensuremath{\backslash}n"; \item system("vol"); \item print "\ensuremath{\backslash}n\ensuremath{\backslash}nResult: \$?\ensuremath{\backslash}n\ensuremath{\backslash}n"; \item \item \begin{center} \textbf{\textit{{\LARGE{}system}}}\LARGE{} for user/network administration \end{center} \item 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. \item }{system("net use }{drive\_letter}{: \ensuremath{\backslash}\ensuremath{\backslash}\ensuremath{\backslash}\ensuremath{\backslash}}{xxx}{\ensuremath{\backslash}\ensuremath{\backslash}}{yyy}{ }{zzz}{ /user:}{abc}{"); \item \#where drive\_letter is the drive that you want to map to, say d: \item \#where xxx is the server name or ip address \item \#yyy is the shared folder name and zzz is the password of the user abc. \begin{center} \end{itemize} \end{slide} \begin{slide}{External command: backticks} \begin{itemize} \end{center} \item Backticks}}\textbf{{: {These `` are different again to system and exec. }{(}{Don't confuse single quotes '' with backticks ``}{)}{ } \item Backticks}{ also start external processes, but }return the output of the process. }}\textbf{{ {You can then do whatever you like with the output.. \item \$volume=`vol`; \item print "The contents of the variable \ensuremath{\backslash}\$volume are:\ensuremath{\backslash}n\ensuremath{\backslash}n"; \item print \$volume; \begin{center} \LARGE{}Perl functions Vs }{\LARGE{}external calls \end{center} \item T}{here are plenty of excellent modules }{and built in functions }{which }{can provide the same results with the use of external commands. \item Perl functions }}\textbf{{are better than}}\textbf{{ external programs{ because Perl's functions are: } \item 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; \item faster, as every external process significantly slows your program; \item don't usually require regexing to find the result you want; \item don't rely on output in a particular format, which might be changed in the next version of your OS or application; \item 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. \item \end{document}