Perl 0411 PDF
Perl 0411 PDF
Perl 0411 PDF
Science and Technology Support Group High Performance Computing Ohio Supercomputer Center 1224 Kinnear Road Columbus, OH 43212-1163
Introduction to Perl
Setting the Stage Data Types Operators Exercises 1 Control Structures Basic I/O Exercises 2 Regular Expressions
Functions Exercises 3 File and Directory Manipulation External Processes References Exercises 4 Some Other Topics of Interest For Further Information
2
Introduction to Perl
3
Introduction to Perl
What is Perl?
Practical Extraction and Report Language
Or: Pathologically Eclectic Rubbish Lister
Created by Larry Wall A compiled/interpreted programming language Combines popular features of the shell, sed, awk and C Useful for manipulating files, text and processes
Also for sysadmins and CGI
4
Introduction to Perl
Can download source code (C) and compile yourself (unlikely to be necessary) Pre-compiled binaries also available for most systems Support available via
Perl home page, www.perl.org Perl man page perldoc command The Usenet group comp.lang.perl
5
Introduction to Perl
Basic Concepts
A shell script is just a text file containing a sequence of shell commands
$ cat testscript #!/bin/sh echo Here is a long listing of the current directory ls -l
To run the script, first make it executable and then type its name:
$ chmod +x testscript $ testscript [output of echo and ls commands] $
6
Introduction to Perl
Basic Concepts
Similarly, a Perl script is a text file containing Perl statements To indicate that its a Perl program, include #!/usr/local/bin/perl as the first line of the file
Note that the location of Perl on your system may vary!
To run, make the script executable and then type its name at the shell prompt
$ chmod +x myscript.pl $ myscript.pl [output of script] $
Or the script may be executed by typing, at the command prompt, perl myscript.pl.
7
Introduction to Perl
Basic Concepts
$ cat welcome.pl #!/usr/local/bin/perl # Anything from # to EOL is a comment print (Enter your name: ); # what it looks like... $name = <STDIN>; # Read from standard input chop ($name); # removes last character of $name (the newline) print (Hello, $name. Welcome!\n); $ chmod +x welcome.pl $ welcome.pl Enter your name: Dave Hello, Dave. Welcome! $
8
Introduction to Perl
Basic Concepts
Statements executed in top-down order; each is executed in succession Syntax and hence style similar to that of C
Free formatting Case sensitive Statements must end with a semicolon (;) Groups of statements can be combined into blocks using curly braces ({...})
Control structures generally analogous to those in C But no main() Also no variable declarations!
Introduce and use any type of variable at any time, including growing/shrinking arrays on the fly...
9
Introduction to Perl
Basic Concepts
Perl is both an interpreted and a compiled language When run, the program is first read and compiled in memory
Not a true compilation to native machine instructions Similar to creation of Java bytecode Some optimizations are performed, e.g.
eliminating unreachable code reducing constant expressions loading library definitions
Second stage is execution via an interpreter (analogous to Java VM) Much faster than fully interpreted languages, such as the shell No object code
10
Introduction to Perl
Data Types
Scalar data Arrays Associative arrays, or hashes
11
Introduction to Perl
Scalar Data
A single number or string, depending on context References to scalars always begin with $ Variable names may contain characters, numbers and underscores Assignment is done using the = operator Examples:
$pi = 3.14159; $color = red; $old_color = was $color before; $host = `hostname`; # command substitution # (more on this later)
In general if you refer to a variable before assigning it a value, it will contain the value undef
Auto-converts to the null string () or zero, depending on context
12
Introduction to Perl
Numeric Values
No distinct types conversion handled automatically Internally, Perl treats all numbers as doubles Decimal:
$pi = 3.14159;
Hexadecimal:
$y = 0x1e; ($y has the hex value 1e, or decimal 30)
Octal:
$y = 075; ($y is now octal 75 = 61 decimal)
Scientific notation:
$z = 3e+2; ($z gets the value 300) $z = 5e-3; ($z gets the value 0.005)
13
Introduction to Perl
Strings
Sequences of characters No end of string character as in C Single-quoted (note: , not `)
no variable interpolation or backslash escape handling, e.g. $x = dog; print bob $x; # displays bob $x
Double-quoted
variable interpolation and escape handling are performed, so $x = dog; print bob $x; # displays bob dog
14
Introduction to Perl
15
Introduction to Perl
$0 $_ $$ $! $? $| $. $] $< $>
Name of currently executing script default variable for many operations the current process ID the current system error message from errno exit status of last command substitution or pipe whether output is buffered the current line number of last input the current Perl version the real uid of the process the effective uid of the process
16
Introduction to Perl
$_ is also a default for pattern matching, implicit I/O and other operations
Will see examples as we go...
If you see Perl code that appears to be missing argument(s), chances are that $_ is involved
17
Introduction to Perl
If a number is used in a place where a string is expected (for example, if you concatenate a number and a string), it is automatically converted to the string that would have been printed for that number Examples:
27.123 converts to 27.123 1e+4 converts to 10000
18
Introduction to Perl
length ()
Returns the number of characters in the string $a = hello world\n; print length($a) # prints 12
19
Introduction to Perl
Arrays are subscripted using square brackets Indexing begins at 0 The (scalar) variable $#arry is the highest assigned index of the array @arry Arrays need not be declared; they come into existence when used
Size can also change dynamically
20
Introduction to Perl
Arrays
Examples:
@nums = (2,4,6); $a = $nums[1]; $nums[3] = 4; $nums[5] = 12; $x = $#nums; # # # # # initialize an array $a = 4 @nums grows automatically @nums is now (2,4,6,4,undef,12) $x is 5
@foo = (one, two); @bar = (3.14, @foo, 2.72); # @bar = (3.14, one, two, 2.718) $foo = $bar[2]; # $foo = two (no relation to @foo) @new_array = @old_array; @huh = 1; @a = (1..5); @b = @a[1..3]; @c = (A..D); # copy entire array # becomes @huh = (1) automatically # # # # @a = (1,2,3,4,5); a slice; @b is (2,3,4) ranges operate using ASCII codes so @c = (A,B,C,D)
21
Introduction to Perl
shift/unshift
Add/remove element(s) at the beginning of a list Either a scalar or a list can be aded unshift (@a, $b); # same as @a = ($b, @a); $c = shift (@a); # returns and removes first element # of @a
22
Introduction to Perl
sort
Returns a list sorted according to ASCII string value @x = sort (joe,betty,dave); # # @y = (1,2,4,8,16,32,64); @y = sort (@y); # # @x gets (betty,dave,joe) @y is now (1,16,2,32,4,64,8)
23
Introduction to Perl
join ()
Given a delimiter and an array, joins the array elements together into a single string: $file = /usr/local/bin/prog; @tmp = split (/, $file); # @tmp = (,usr,local,bin,prog) pop (@tmp); $dir = join (\\, @tmp); # $dir is now \usr\local\bin
24
Introduction to Perl
Associative array variable names begin with a % Separate namespace from scalars and ordinary arrays
$foo, @foo and %foo are all different
25
Introduction to Perl
Associative Arrays
Examples:
$lastname{John} = Doe; $ssn{John} = 1234567890; %ranking = (UCLA,1,OSU,2); $x = $ranking{UCLA}; # $x is 1 %ranking = (UCLA => 1, OSU => 2); @y = %ranking; %z = @y; %z = %ranking; # another way to initialize; # equivalent to the above # @y is (UCLA,1,OSU,2) # %z is the same as %ranking # faster way to do the same
26
Introduction to Perl
Note that the order of the elements in a hash is undefined (it is under the internal control of Perl)
values
Returns a list of the values in an associative array Order matches that returned by keys
27
Introduction to Perl
delete
Removes a key-value pair from a hash, returning the value of the deleted element $x = delete $ranks{UCLA}; # %ranks is now just one # key-value pair, and # $x is 1
28
Introduction to Perl
$0
$ARGV[0]
$ARGV[$#ARGV]
29
Introduction to Perl
A Note on Context
Much of Perls behavior is determined by the context in which objects (including variables and function calls) appear For example, if a list appears where a scalar is expected, Perl automatically inserts the length of the list
# $len is equal to the number # of elements in @foo
$len = @foo;
print Length = , @foo; # legal, but wrong print Length = , scalar(@foo); # forces scalar context
In general, $#arry + 1 == @arry If a scalar appears where an array is expected, it is promoted to a one-element array Well see other context rules later...
30
Introduction to Perl
Operators
Numeric operators String operators Comparisons Assignments
31
Introduction to Perl
Numeric Operators
These are mostly the same as in C
5 + 3 4.4 - 0.3 3*9 27 / 3 11.2 / 6.1 10/3 2**3 10 % 3 10.6 % 3.2 3 > 2 5 != 5
# floating point, so 3.3333 # Fortran-style exponentiation # same as above # returns TRUE (not empty or 0) # returns FALSE
Also &&, ||, ?:, ... Operator associativity and precedence is identical to that in C
32
Introduction to Perl
Incrementation
Like C, Perl provides a shorthand for incrementing or decrementing variables Postfix form:
$j++; $j--; # the same as $j = $j + 1 # the same as $j = $j - 1
Prefix form:
++$j; --$j; # the same as $j = $j + 1 # the same as $j = $j - 1
Examples:
$j = 10; $x = $j++; $y = ++$j; # $x is 10, $j is 11 # $y and $j both 12
33
Introduction to Perl
String Operators
Concatenation: .
hello . world # the same as helloworld
Repetition: x
fred x 3 Bob x (1+1) (3+2) x 4 # # # # # same as fredfredfred same as BobBob same as 5 x 4 or 5555 (note auto-conversion of 5 to 5)
34
Introduction to Perl
Comparisons
Separate operators for numeric and string comparisons:
Comparison equal not equal less than greater than less than or equal to greater than or equal to Numeric == != < > <= >= String eq ne lt gt le ge
Examples:
foo == bar foo eq bar zebra gt aardvark 10 == 10 10 eq 10 # # # # # true (both are converted to 0!) false true (ASCII comparison) true false
35
Introduction to Perl
Assignments
A scalar assignment itself has a value, which is equal to the value assigned:
$b = 4 + ($a = 3);
This assigns 3 to $a, then assigns 4 + (3) or 7 to $b As in C, binary operators can be turned into assignment operators:
$a += 5; $x *= 42; $str .= ; # same as $a = $a + 5; # same as $x = $x * 42 # same as $str = $str . ;
36
Introduction to Perl
Exercises 1
1. Write a program that computes the area of a circle of radius 12.5. 2. Modify the above program so that it prompts for and accepts a radius from the user, then prints the area. 3. Write a program that reads a string and a number, and prints the string the number of times indicated by the number on separate lines. (Hint: use the x operator.) 4. Write a program that reads a list of strings and prints out the list in reverse order. 5. Write a program that reads a list of strings and a number, and prints the string that is selected by the number.
37
Introduction to Perl
Exercises 1
6. Write a program that reads and prints a string and its mapped value according to the mapping
Input red green blue Output apple leaves ocean
7. Write a program that reads a series of words with one word per line, until endof-file (ctrl-D), then prints a summary of how many times each word was seen.
38
Introduction to Perl
Control Structures
if/unless while/until for foreach do Simple constructs
39
Introduction to Perl
if/unless
Basic decision making:
if (expression) { true_statement1; true_statement2; } else { false_statement1; false_statement2; }
Curly braces are required around each block (unlike in C) expression is evaluated for a string value to determine its truth or falsehood
40
Introduction to Perl
Examples:
0 1-1 1 00 0.000 undef # # # # # # converts to 0, so false converts to 0, then 0 so false converts to 1 so true not or 0, so true true for the same reason converts to , so false
41
Introduction to Perl
if/unless
Can also include any number of elsif clauses:
if (expr1) { expr1_true1; expr1_true2; } elsif (expr2) { expr2_true1; expr2_true2; } } else { all_false1; all_false2; }
while/until
Basic iteration:
while (expression) { statement1; statement2; }
expression is tested and, if true, the following block of statements is executed At the end of the block, expression is tested again expression must become false at some point, or the loop will be infinite!
43
Introduction to Perl
while/until
Example:
$j = 1; while ($j <= 10) { print The square of $j is ; print $j * $j, \n; $j++; }
44
Introduction to Perl
for
Another looping construct:
for (init_expr; test_expr; incr_expr) { statement1; statement2; ... }
Example:
for ($j=1; $j<=10; $j++) { print The square of $j is ; print $j * $j, \n; }
45
Introduction to Perl
foreach
Allows convenient cycling through the elements in a list:
foreach $var (@list) { statement1; statement2; ... }
The scalar variable $var takes on the value of each item in @list in turn $var is local to the construct; it becomes undef when the loop is finished If you omit the scalar variable $var, Perl assumes you specified $_ instead
foreach (@a) { print; } # same as foreach $_ (@a) # same as print $_;
46
Introduction to Perl
foreach
Example:
foreach $j (1..10) { print The square of $j is ; print $j * $j, \n; }
If @list is a single array variable, then $var is actually a reference to the items in @list This means that if you modify $var in the loop, you are actually changing that element in @list:
@a = (3,5,7,9); foreach $tmp (@a) { $tmp *= 3; } # @a is now (9,15,21,27)!
47
Introduction to Perl
do while/until
Similar to C:
do { } while ($something_is_true); do { } until ($something_is_false);
48
Introduction to Perl
Simple Constructs
To loop on or branch around a single statement, you can use
print Odd\n if $n % 2 == 1; print Even\n unless ($n % 2 == 1); print $num--,\n while $num > 0;
Note position of the semicolon else is not allowed with this construct
49
Introduction to Perl
Simple Constructs
Logical constructs can be built from && (and) and || (or)
expr1 && expr2; # equivalent to: # if (expr1) { expr2 }; expr1 || expr2; # equivalent to: # unless (expr1) { expr2 };
Example:
# Try to open file; print error msg and exit on failure open (FH,/etc/llamas) || die cannot open llamas!;
50
Introduction to Perl
Basic I/O
Basics Reading from stdin The diamond operator Writing to stdout and stderr Error message shortcuts
51
Introduction to Perl
I/O Basics
I/O in Perl proceeds through filehandles, which are used to refer to input or output streams We will see later how to attach these to files (or devices) for reading and writing There are three pre-defined filehandles:
STDIN refers to the keyboard STDOUT connected to the screen STDERR connected to the screen
52
Introduction to Perl
$line = <STDIN>;
@lines = <STDIN>; # In an array context, reads all remaining # lines. Each line becomes an element of # the array @lines.
53
Introduction to Perl
Shortcut: whenever a loop test consists solely of an input operator, Perl copies the input line into the variable $_:
while (<STDIN>) { print $_; }
54
Introduction to Perl
In fact, Perl looks at @ARGV for the list of input files Can set or modify this from within the script:
@ARGV = (file1, file2); while (<>) { print; } # $_ is the default for print, too!
56
Introduction to Perl
Format string
57
Introduction to Perl
This also appears on the terminal screen by default, but can be redirected by the shell separately from the standard output
$ script.pl > output 2> err.log
58
Introduction to Perl
warn ()
Same as die, except Perl does not exit warn Debug enabled if $debug;
59
Introduction to Perl
Exercises 2
8. Write a program that accepts the name and age of the user and prints something like Bob is 26 years old. Insure that if the age is 1, year is not plural. Also, an error should result if a negative age is specified. 9. Write a program that reads a list of numbers on separate lines until 999 is read, and then prints the sum of the entered numbers (not counting the 999). Thus if you enter 1, 2, 3 and 999 the program should print 6. 10. Write a program that reads a list of strings and prints out the list in reverse order, but without using the reverse operator. 11. Write a program that prints a table of numbers and their squares from 0 to 32. Try to find a way where you dont need all the numbers from 0 to 32 in a list, then try one where you do. 12. Build a program that computes the intersection of two arrays. The intersection should be stored in a third array. For example, if @a = (1, 2, 3, 4) and @b = (3, 2, 5), then @inter = (2, 3).
60
Introduction to Perl
Exercises 2
13. Write a program that generates the first 50 prime numbers. (Hint: start with a short list of known primes, say 2 and 3. Then check if 4 is divisible by any of these numbers. It is, so you now go on to 5. It isnt, so push it onto the list of primes and continue) 14. Build a program that displays a simple menu. Each of the items can be specified either by their number or by the first letter of the selection (e.g., P for Print, E for Exit, etc.). Have the code simply print the choice selected. 15. Write a program that asks for the temperature outside and prints too hot if the temperature is above 75, too cold if it is below 68, and just right if it between 68 and 75. 16. Write a program that acts like cat but reverses the order of the lines.
61
Introduction to Perl
Regular Expressions
Overview Metacharacters Substitutions Translations Modifiers Memory Anchoring patterns Miscellaneous
62
Introduction to Perl
Overview
A regular expression defines a pattern of characters Typical uses involve pattern matching and substitution Used by many UNIX programs (grep, sed, awk, vi, emacs, ), but not always with exactly the same rules! Also appears similar to shell wildcarding (globbing), but the rules are much different
63
Introduction to Perl
RE Basics
A regular expression (RE) in Perl is indicated by enclosing it in forward slashes:
/abc/
This represents a pattern consisting of these three characters When compared against a string, the result is true if the pattern abc occurs anywhere in that string Comparison operator: =~
# print a string if it contains abc if ($line =~ /abc/) { print $line; }
Negated comparison: !~
64
Introduction to Perl
RE Basics
Consider the regular expression
Abc Each character is itself a RE which matches only that single character Case sensitivity: A does not match a
65
Introduction to Perl
Metacharacters
These characters have a special meaning inside a regular expression
Character . * ? + {} () | [] Meaning Any single character except newline (\n) zero or more of the preceding RE Zero or one of the preceding RE One or more of the preceding RE Some number of the preceding RE grouping and sub-expressions or a character class
To remove their special meaning, you can backslash-escape them Note that backslash-escapes (\n,\t,\r,\f, ) retain their special meaning inside a RE
66
Introduction to Perl
Examples
Any single character matches itself, so
bob
will match the word bob What if we want to match bob or bobby, or anything containing an o? b.b matches: bob, bib, bbb, ... bob* matches: bob, bobbb, bobcc, ... bob.* matches: bob, bobby, bob barker, ...
67
Introduction to Perl
Character Classes
Represented by [] enclosing a list of characters This matches any one of the characters in the list Examples:
What if we want b.b but only with a vowel in between? b[aeiou]b To ignore capitalization? [Bb]ob
68
Introduction to Perl
69
Introduction to Perl
More Examples
be+ matches: be, bee, beeeeeeee, ... To match bob or bobby, make use of parentheses for grouping: bob(by)? Using the or symbol to match bob or dog: (bob|dog) Combining metacharacters: b[aeiou]*b matches: bob, bab, baaab, boab, beieb, bb, ... Bo?b will only match: Bob or Bb
70
Introduction to Perl
71
Introduction to Perl
Parentheses
Used for grouping sub-expressions (bob)+ matches: bob, bobbob, bobbobbobbob, ... Also causes the enclosed pattern to be memorized These patterns can then be recalled as $1, $2, $3,
Within the pattern match, use \1, \2, \3, instead
Thus Fred(.)Barney\1 matches: FredxBarneyx or FredyBarneyy but not FredxBarneyy a(.)b(.)c\2d\1 matches: a, any one character (call it #1), b, any one character (call it #2), c, character #2, d, character #1 (For example: aXbYcYdX) Memory is also very useful when doing substitutions...
72
Introduction to Perl
String Substitutions
String modification is performed using the substitute operator:
$var =~ s/regexp/replacement-string/;
The variable $var is matched against the RE regexp. If successful, the part that matches is replaced by replacement-string
$_ = foobar; $_ =~ s/bar/bear/; # $_ is now foobear
If several parts of $var match regexp, only the first is substituted for by default
$_ = foobaring up the foobar road; $_ =~ s/bar/bear/; # $_ is now foobearing # up the foobar road
73
Introduction to Perl
Translations
Similar to the tr program in UNIX, the tr operator translates characters in regular expressions:
$str = Bob the Dog; $str =~ tr/a-z/A-Z/; $str =~ tr/BDO/xyz/; # $str now BOB THE DOG # $str now xzx THE yzG
$str =~ tr [A-Z] [a-z]; # also valid syntax # $str is now xzx the yzg
74
Introduction to Perl
Modifiers
REs can have optional modifying suffixes. These include g (global; substitute as many times as possible), i (case insensitivity), m (treat string as multiple lines), and s (treat string as a single line)
$_ = foobaring up the foobar road; $_ =~ s/bar/BEAR/g; # $_ is now fooBEARing # up the fooBEAR road
if ($str =~ /abc/i) { # same as /[Aa][Bb][Cc]/ } $_ = fooBARing up the foobar road; $_ =~ s/bar/bear/ig; # $_ is now foobearing # up the foobear road
75
Introduction to Perl
Note that matches are greedy; the longest string that matches is the one taken
$foo = fred xxxxxxxxxx barney; $foo =~ s/(x*)/boom/; # $foo is now fred boom barney # and $1 is xxxxxxxxxx
76
Introduction to Perl
Anchoring Patterns
Special notations that allow you to anchor the pattern to specific parts of the string:
Symbol ^ $ \b \B Meaning beginning of string end of string word boundary not a word boundary
Examples:
/^fred/ /betty$/ /fred\b/ /\bwiz/ /\bFred\B/ # # # # # matches matches matches matches matches fred only at the beginning of the string betty only at the end of the string fred, but not freddy wiz and wizard, but not twiz Frederick but not Fred Flinstone
77
Introduction to Perl
Alternatively, you can use a different character such as : or # as the delimiter, by explicitly giving the m (match) prefix:
# Match /etc/passwd if ($file =~ m:/etc/passwd:) { }
79
Introduction to Perl
80
Introduction to Perl
Functions
Defining a function Invoking a function Arguments Return values Local variables Example: Advanced sorting
81
Introduction to Perl
Defining a Function
Also called subroutines, or sometimes just subs General construct:
sub my-subname { statement_1; statement_2; ... }
Separate namespace from variables, so you can have a subroutine named foo along with variables $foo, @foo and %foo By default (almost) all variable references in a function are global
82
Introduction to Perl
Invoking a Function
83
Introduction to Perl
Arguments
Arguments may be passed (in parentheses) to a function Any arguments passed to the function appear in the special array @_ @_ is local to the function
If there is a global variable @_, it is saved and restored after the function exits
84
Introduction to Perl
Return Values
A function returns a value to the code that called it, which may be assigned or used in some other way The return value of a function is the value of the last expression evaluated in the body of the function
sub double_a { $a *= 2; } $a = 3; $c = &double_a; # $c is now 6
Can also use return (val); The returned value can be a scalar or a list
85
Introduction to Perl
Return Values
Another example:
sub add { $sum = 0; foreach $n (@_) { $sum += $n; } $sum; # Required! # could also use return($sum); } $a = &add(4,3); # $a is now 7 $b = $a + &add(1..5); # $b is now 7+1+2+3+4+5=22
Without the last line $sum (or the return statement), the last expression evaluated would be foreach, resulting in a null return value If $sum did not exist before invocation of add, it pops into existence when add is first invoked
86
Introduction to Perl
Local Variables
By default, most variables are global in Perl @_ is local to each function, however Can define other local variables using
local ($var1, $var2, )
Takes a list of variable names and creates local instances of them Inside the function, local variables mask any global variables with the same name(s)
Values of global variables are saved, and restored after the function exits
local can also be used inside ordinary code blocks { } In Perl 5, my is (essentially) a synonym for local
87
Introduction to Perl
Local Variables
Example:
sub greater_than { local ($n, @values) = @_; # create some local variables local (@result); # to hold the return value foreach $val (@values) { # step through arg list if ($val > $n) { # is it eligible? push (@result, $val); # include it } } return (@result); # return final list } @new = &greater_than(55,@list); # @new gets all @list > 55 @foo = &greater_than(5,1,5,15,30); # @foo is (15,30)
Note assignment of @_ to other local variables for readability Could use my in place of local here
88
Introduction to Perl
The sorting function should assume two arguments $a and $b, and return
any negative number if $a is less than $b (i.e., if $a should come before $b in the sorted list) zero if $a equals $b and any positive number if $a is greater than $b (i.e., if $a should come after $b)
89
Introduction to Perl
sub numerically { if ($a < $b) { -1; } elsif ($a == $b) { 0; } elsif ($a > $b) { 1; } } @new = sort numerically (@list); # tells sort to use numerically # in sorting the list # Shorthand: sub numerically { $a <=> $b; } # the spaceship operator; same as the # above
90
Introduction to Perl
Exercises 3
17. Construct a regular expression that matches
at least one a followed by any number of bs any number of backslashes followed by any number of asterisks three consecutive copies of whatever is contained in $whatever any five characters, including newline
18. Write a program that accepts a list of words on STDIN and searches for a line containing all five vowels (a, e, i, o, and u). 19. Modify the above program so that the five vowels have to be in order. 20. Write a program that looks through the file /etc/passwd on STDIN, printing the real name and login name of each user. (Hint: use split to break each line up into fields, then s/// to get rid of the parts of the comment field that are after the first comma.)
91
Introduction to Perl
Exercises 3
21. Write a subroutine that takes a numeric value from 1 to 9 and returns its English name (i.e., one, two ). If the input is out of range, return the original value as the name instead. 22. Taking the subroutine from the previous exercise, write a program to take two numbers and add them together, printing the result as Two plus three equals five. (Dont forget to capitalize the first letter!) 23. Create a subroutine that computes factorials. (The factorial of 5 is 5! = 5*4*3*2*1 = 120.) Try this using a normal subroutine and a recursive one (i.e., a subroutine that calls itself). 24. Build a function that takes an integer and returns a string that contains the integer displayed with a comma every three digits (i.e., 1234567 should return 1,234,567).
92
Introduction to Perl
93
Introduction to Perl
Filehandles
A filehandle is the name of an I/O connection between your Perl process and the outside world Already seen STDIN and STDOUT/STDERR
I/O connections to keyboard and screen
94
Introduction to Perl
Opening a Filehandle
The operation
open (HANDLE, filename);
opens the file filename and attaches it to HANDLE A prefix to filename controls whether file is opened for reading, writing, appending, etc. Returns true or false (actually undef) indicating success or failure of the operation
Can fail due to, e.g., permissions, file not found, etc. open (FH, myfile) or die cant open myfile!;
95
Introduction to Perl
Examples
open(PWD, /etc/passwd); # open for reading; fails if # /etc/passed doesnt exist or cant # be read # write-only; if myfile doesnt # exist it is created, else clobbered # append mode; if logfile doesnt # exist it is created # can also print to UNIX pipelines
96
Introduction to Perl
Using Filehandles
Once a filehandle has been opened for reading, you can read from it by enclosing the filehandle in <>, just as for STDIN:
open (FH, myfile); # open myfile for reading while ($line = <FH>) { # read a line from the file print $line; # echo it to STDOUT }
As before, <FH> reads the next line from the file in a scalar context In an array context, <FH> all remaining lines of the file are read and placed in an array Newlines are retained (can use chomp () to remove them) Returns undef (hence false) if there are no more lines to read
97
Introduction to Perl
Using Filehandles
To write/append to a filehandle, give the filehandle as the first argument to print:
open (LOGFILE, >>build.log); print LOGFILE Finished building application\n;
Note: no comma after the filehandle! STDOUT is the default filehandle for print and printf
98
Introduction to Perl
File Tests
Can test for existence, ownership, permissions, etc. of files and directories General form of test is
-X file
Most tests return true/false, though some return numbers (e.g. -s, which returns the size in bytes of a file)
$x = /etc/passwd; if (-e $x) { # does the file exist? print Crack some passwords!\n; }
99
Introduction to Perl
File Tests
More examples:
if (-r $file && -w $file) { # $file exists and I can read and write it . . . } chop ($fname = <STDIN>); if (-A $fname > 7) { # last accessed more than seven days ago? print Say goodbye to $fname...\n; unlink $fname; # delete the file } else { print $fname accessed recently!\n; } if (-e) { # same as if (-e $_), of course! }
100
Introduction to Perl
File Tests
File Test -r -w -x -o -e -z -s -f -d -T, -B -M, -A Meaning file or directory is readable file or directory is writable file or directory is executable file or directory is owned by user file or directory exists file or directory exists and has zero size file or directory exists and has nonzero size (return value is size in bytes) entry is a plain file entry is a directory file is text, binary modification, access time (in days)
101
Introduction to Perl
102
Introduction to Perl
Globbing
Can expand shell wildcards (globbing) by putting the globbing pattern inside <> Example:
@list = </etc/host*>;
returns a list of all filenames in /etc that begin with host In a scalar context it would return the next filename that matches, or undef if no others remain
while($next = </etc/host*>) { $next =~ s#.*/##; # remove part before last slash # (note use of # as delimiter) print one of the files is $next; }
103
Introduction to Perl
Globbing
Multiple patterns are allowed inside the glob, for example
@foo_bar_files = <foo* bar*>;
Generally, anything you could send to the shell for expansion will work in a glob Note: looks similar to regular expressions, but the meaning of the various metacharacters is very different!
104
Introduction to Perl
Operations on Files
Some useful functions for performing operations on files/directories:
unlink (filename);
Removes (unlinks) a list of files
105
Introduction to Perl
Operations on Files
mkdir (dirname, mode);
Creates a directory with permissions set by mode
mkdir (foo, 0777); # creates direcoty foo with rwx # permissions for all
rmdir (dirname);
Removes a directory
rmdir (foo); # just like rmdir foo in the shell chmod (mode,file1,file2,);
Sets permissions for listed files to mode
Operations on Files
All of these functions return true/false indicating success or failure of the operation Examples:
unlink (foo) || die Unable to delete foo; if (mkdir (tmp,0700)) { # creation was successful, proceed } else { # creation failed! }
107
Introduction to Perl
External Processes
Using backquotes system () Output to and from pipes
108
Introduction to Perl
Using Backquotes
An external command can be run by placing it in backquotes: ` ` The command output is becomes the value of the backquoted string
$now = the time is now: . `date`;
Output of the date command is concatenated with the previous string If the backquoted command appears in an array context, you get an array of strings each of which is one line of the command output
@files = `ls -l`; # each element of @files contains # one line of ls -l output
Variable interpolation does occur inside backticks Look out for newlines (use chop/chomp if desired)
109
Introduction to Perl
system()
Another way to execute an external command If given a scalar, system passes it to /bin/sh for execution
system (date);
The scalar can be anything sh can process, including multiple commands separated by semicolons If given a list, system takes the first item as the command, and subsequent items as arguments to that command
system (grep,INTEGER,prog.f);
Note that shell processing (globbing) does not occur for these arguments:
system (/bin/echo,*); # just echos *
110
Introduction to Perl
system()
Where does the output of the command go? The shell inherits STDOUT and STDERR from the Perl process, so output normally goes to the screen This can be changed using ordinary sh redirects:
system (a.out > outfile 2> err); $where = who_out . ++$i; # make a filename system ((date; who) > $where &); # interpolation
system returns the exit status of the command, usually 0 if no error occurred Backwards from normal convention:
system (date > now) && die cannot create now; die invoked if command returns nonzero (i.e., true)
111
Introduction to Perl
Writing to a pipe:
open (LPTR, | lpr); print LPTR ; now sends to the standard input of lpr When the filehandle is closed (or the script exits), the command is run
112
Introduction to Perl
References
Creating references Anonymous references Using references Passing references Nested datastructures
113
Introduction to Perl
Creating References
A reference is like a pointer in C It is a scalar object that holds the location of the data associated with some variable
Said to point to the variable
114
Introduction to Perl
Anonymous References
You can also directly create references to unnamed objects:
# array reference: $aref = [1,foo,a,42]; # hash reference: $href = { UCLA, 1, tOSU, 2, } # function reference: $code = sub { print Hello world\n; }
115
Introduction to Perl
Using References
To use a reference (dereference it, in C parlance), just put the appropriate type indicator in front of the reference variable
$foo = 12; $sref = \$foo; print $$sref; $$sref = bar;
@arry = (1,2,3); $aref = \@arry; # $aref refers to @arry @$aref = (2,4,6); # changes @arry $$aref[2] = gaak; # reference to individual element
116
Introduction to Perl
Passing References
Can also pass variables by reference to functions
foreach (@reflist) { $$_ *= 2; # $_ is a reference, # $$_ is what it points to } } &doubler (\$a, \$b, \$c);
117
Introduction to Perl
Passing References
Can also return references, of course
sub arrayinit { my @a = (1,3,5); my @b = (2,4,6); return (\@a, \@b); } ($aref, $bref) = &arrayinit ();
118
Introduction to Perl
Nested Datastructures
Perl does not support arrays of arrays directly, but you can create an array of references, each of which refers to an array The operator -> can be used to dereference array pointers
$aptr = [1,2,3]; # anonymous array reference $$aptr[0] = 3.14; # changes 0th element of the array $aptr->[0] = 3.14; # same thing # here is an array of pointers, each of which refers # to a hash consisting of a single key/value pair: $ptr = [{cow,purple},{llama,blue}]; $ptr->[1]->{llama} = scarlet; $ptr->[1]{llama} = scarlet; # equivalent
119
Introduction to Perl
Exercises 4
25. Write a program that reads a filename from STDIN, then opens the file and prints its contents preceded by the filename and a colon. For example, if the file fred contains the lines aaa, bbb and ccc, the output should be
fred: aaa fred: bbb fred: ccc
26. Write a program to read in a list of filenames and then display which of them are readable, writeable and/or executable, and which ones dont exist. 27. Write a program that accepts a list of filenames and finds the oldest file among them. Print the name of that file along with its age in days. 28. Write a program to change directory to a location specified as input, the print a listing of the files there. Do not show a list if the directory change doesnt succeed; in this case simply warn the user.
120
Introduction to Perl
Exercises 4
29. Write a program that works like rm, deleting the files given as command line arguments. (Be careful testing this!) 30. Write a program to parse the output of the data command to get the current day of the week. If it is a weekday, print get to work, otherwise go play. 31. Using references, build a structure (as in C) that represents a circle image. Your circle struct should contain four data items: the x and y coordinates of the center point, the radius and the color. (Hint: the struct can be a hash where the keys are the names of the data items and the values are reference variables pointing to the actual data.)
121
Introduction to Perl
Advanced process management Handling binary data (pack and unpack) Formats Database interfaces CGI Many more features, functions and options in virtually all the areas weve covered so far...
122
Introduction to Perl
123
Introduction to Perl
124
Introduction to Perl