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