Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIX System Programming

Similar presentations


Presentation on theme: "UNIX System Programming"— Presentation transcript:

1 UNIX System Programming
Perl Practical Extraction and Report Language Not designed for the Web Designed by Larry Wall in 1986 to create reports Drew upon useful features in other programming languages (Pathologically Eclectic Rubbish Lister) UNIX System Programming

2 UNIX System Programming
Perl overview Originally called PEARL, but a conflict with an existing graphics language resulted in the shortening of the name Language includes pattern matching, file handling, and scalar data It has syntax patterned after C and was originally designed to operate as a shell script Its power is that it views programs and files as data It has proven to be an effective language for interacting with Web pages UNIX System Programming

3 UNIX System Programming
Perl structure ► Variables in Perl are either integers or strings and begin with the symbol $. ► A simple Perl program consists of a series of print statements. ► It has the usual sequence of control structures such as for, while, and until loops and if conditional. UNIX System Programming

4 UNIX System Programming
Perl is good for … Quick scripts, complex scripts Parsing & restructuring data files CGI-BIN scripts High-level programming Networking libraries Graphics libraries Database interface libraries UNIX System Programming

5 UNIX System Programming
Bad for ….. Compute-intensive applications (use C, C++, Java) Hardware interfacing (device drivers…) UNIX System Programming

6 UNIX System Programming
Perl structure Comment lines begin with: # First line should look like: #!/bin/perl File Naming Scheme filename.pl (programs) filename.pm (modules) filename.ph (old Perl 4 header file) Example prog: print “Hello, World!\n”; UNIX System Programming

7 UNIX System Programming
Perl structure Statements must end with semicolon. $a = 0; Should call exit() function when finished. Exit value of zero means success exit (0); # successful Exit value non-zero means failure exit (2); # failure UNIX System Programming

8 UNIX System Programming
Perl structure ► Perl shares an ability to process regular expressions like several other process languages Example use: $ENV{'USER'} = ‘mvz’ will be true if login name mvz is the string $ENV{'USER'} (i.e., is the user running this program). This shows how Perl can interact with the system environment UNIX System Programming

9 UNIX System Programming
Perl: Example script if ($food eq "spinach") { print "You ate spinach, so you get dessert!"; } elsif ($food eq "broccoli") { print "Broccoli's OK. Maybe you'll get dessert."; } else { print "No spinach, no dessert!"; } UNIX System Programming

10 Perl: The learning process
Access to Perl Interpreter on a server Can download and install for free Perl on UNIX server (needed to learn basic UNIX commands). Basic Text Editor (e.g., Vi, Vim, Emacs, etc.) UNIX System Programming

11 UNIX System Programming
J. Tan Computer Networks Perl UNIX System Programming CS UNIX System Programming

12 UNIX System Programming
J. Tan Computer Networks Perl UNIX System Programming CS UNIX System Programming

13 UNIX System Programming
J. Tan Computer Networks Perl UNIX System Programming CS UNIX System Programming

14 UNIX System Programming
Data Types Integer Nondecimal Integer (Octal 567) 0xff (Hex FF) 0b (Binary ) UNIX System Programming

15 UNIX System Programming
Data Types Perl allows underscores for readability: 0b1011_1011 0x0000_FFFF UNIX System Programming

16 UNIX System Programming
Data Types Floating Point e E-8 String ‘hi there’ “hi there, $name” qq(tin can) print “Text Utility, version $ver\n”; UNIX System Programming

17 UNIX System Programming
J. Tan Computer Networks Perl UNIX System Programming CS UNIX System Programming

18 UNIX System Programming
J. Tan Computer Networks Perl strict - Perl pragma to restrict unsafe constructs use ‘refs’; // generate run-time errors if symbolic refs used use ‘vars’; // generate compile-time errors if var is not declared or out of scope use ‘subs’ // if bareword is used to call subs e.g., $SIG{PIPE} = Plumber; instead of $SIG{PIPE} = "Plumber” use strict; no strict ‘vars’ ; UNIX System Programming CS UNIX System Programming

19 UNIX System Programming
Data Types Boolean “” represents False all other values represents True UNIX System Programming

20 UNIX System Programming
Data Types Scalar $num = 14; $fullname = “Cass A. Nova”; Variable Names are case-sensitive Underlines Allowed: $Program_Version = 1.0; UNIX System Programming

21 UNIX System Programming
J. Tan Computer Networks Perl UNIX System Programming CS UNIX System Programming

22 UNIX System Programming
J. Tan Computer Networks Perl UNIX System Programming CS UNIX System Programming

23 UNIX System Programming
J. Tan Computer Networks Perl UNIX System Programming CS UNIX System Programming

24 UNIX System Programming
J. Tan Computer Networks Perl UNIX System Programming CS UNIX System Programming

25 Basic I/O – Input from STDIN
J. Tan Computer Networks Basic I/O – Input from STDIN Evaluating this operator in a scalar context gives you the next line of input, or undef, if there are no more lines. $a = <STDIN>; # read the next line UNIX System Programming CS UNIX System Programming

26 Basic I/O – Input from STDIN
J. Tan Computer Networks Basic I/O – Input from STDIN ► Evaluating this operator in an array context gives you all of the remaining lines as a list. ► One line per element. @a = <STDIN>; # read the next line UNIX System Programming CS UNIX System Programming

27 Basic I/O – Input from STDIN
J. Tan Computer Networks Basic I/O – Input from STDIN ► To read a scalar value for <STDIN> and use value as the controlling expression of a loop: while (<STDIN>) { # like while ($_ = <STDIN>) chomp; # like chomp ($_ ) # process $_ here ; } UNIX System Programming CS UNIX System Programming

28 Input from Diamond Operator <>
J. Tan Computer Networks Input from Diamond Operator <> Unlike <STDIN>, < > gets data from file(s) specified on the command line that invoked the Perl program. E.g., if Perl file is called foo, consisting of #!/usr/bin/perl -w while (<>) { print $_ ; } UNIX System Programming CS UNIX System Programming

29 Input from Diamond Operator <>
J. Tan Computer Networks Input from Diamond Operator <> And you invoke foo with: foo file1 file2 file3 < > will read each line of file1 followed by each line of file2 followed by each line of file3. Acts like UNIX’s cat(enate) command UNIX System Programming CS UNIX System Programming

30 Input from Diamond Operator <>
J. Tan Computer Networks Input from Diamond Operator <> ► < > does not look at command line arguments. ► Uses array ► $#ARGV returns the number of arguments ► Set by Perl to contain the list of command-line arguments Example: in-program. @ARGV = (“passwd1”,”passwd2”,”passwd3”); while (<>) { #process files passwd? print “This line is $_ “; } UNIX System Programming CS UNIX System Programming

31 UNIX System Programming
J. Tan Computer Networks <> example: ► print lines that are not _________ ? while (<>) { chop; if ($_ ne “zatoichi”) { print “$_ \n“; } UNIX System Programming CS UNIX System Programming

32 UNIX System Programming
J. Tan Computer Networks Output to STDOUT ► print or printf: ► Parenthes is optional, needed for clarification & readability. E.g., print (2+3),”hello”; # wrong! Prints 5, ignores “hello” print ((2+3),”hello”); # right, prints 5hello print 2+3,”hello”; # also right, prints 5hello UNIX System Programming CS UNIX System Programming

33 UNIX System Programming
J. Tan Computer Networks Output to STDOUT ► printf for formatted output (from C) E.g., format width printf “%15s %5d %10.2f\n”, $s, $n, $r; string integer floating point UNIX System Programming CS UNIX System Programming

34 Built-in Perl Operators: chop
Scalar Context Chop off and return the last character of a string $s = “string”; $c = chop($s); $s  “strin” $c  “g” Array Context Chop off the last character of every string in the array, but return the last character of the last element of the array. @s = (“s1”, “s2”, “s3”); = (“s”, “s”, “s”) UNIX System Programming

35 UNIX System Programming
chomp Scalar Context Remove and return the last character of a string, if and only if it equals the current record separator value (\n). $s = “string\n”; $c = chomp($s); $s  “string” $c  “\n” Array Context Remove the last character of every string in an array only if it is the current record separator value. @s = (“hello\n”, “world”); = (“hello”, “world”) UNIX System Programming

36 UNIX System Programming
J. Tan Computer Networks Perl UNIX System Programming CS UNIX System Programming

37 UNIX System Programming
J. Tan Computer Networks Perl UNIX System Programming CS UNIX System Programming

38 UNIX System Programming
J. Tan Computer Networks Perl UNIX System Programming CS UNIX System Programming

39 Examples with strings…..
J. Tan Computer Networks Examples with strings….. $fred = ‘hi’ ; $barney = “a test of ”.’$fred’; # yields ‘a test of $fred’ $barney2 = “a test of \$fred”; # same result as above UNIX System Programming CS UNIX System Programming

40 Examples with strings…..
J. Tan Computer Networks Examples with strings….. $fred = “pay”; $fredday = “wrong!”; $barney = “It’s $fredday” # not It’s payday, but It’s wrong! $barney = “It’s ${fred}day” # now, $barney = “It’s payday” $barney2 = “It’s $fred” . “day” # another way to do it, and another … $barney3 = “It’s”.$fred.”day”; UNIX System Programming CS UNIX System Programming

41 UNIX System Programming
Arrays List (one-dimensional array) @memory = (16, 32, 48, 64); @people = (“Alice”, “Alex”, “Albert”); First element numbered 0 (can be changed) Single elements are scalar: $names[0] = “Fred”; Slices are ranges of elements @guys How big is my list? print “Number of people: $#people\n”; UNIX System Programming

42 UNIX System Programming
J. Tan Computer Networks Examples of Arrays (1, 2, 3) # array of three values (“fred”,4.5) # two values Elements can be expressions: ($a, 17) # two values, current value # of $a, and 17 ($b+$c, $a+$d) # two values Empty array: ( ) UNIX System Programming CS UNIX System Programming

43 UNIX System Programming
J. Tan Computer Networks Examples of Arrays Ranges allowed: (1..3) # same as (1, 2, 3) (2..4,10,12) # (2,3,4,10,12) ($a..$b) # depends on those values Float ranges: ( ) # (1.2,2.2,3.2,4.2,5.2) ( ) # (1.2,2.2,3.2,4.2,5.2) UNIX System Programming CS UNIX System Programming

44 Examples of Array Variables
J. Tan Computer Networks Examples of Array Variables @fred = (1,2,3) # fred array gets 3 elements @barney # that is copied to $barney Array variable name may appear in array literal list: @fred = (“one”,”two”); @barney = # = (4,5,”one”,”two”,6,7) @barney = # puts 8 in front # UNIX System Programming CS UNIX System Programming

45 Examples of Array Variables
J. Tan Computer Networks Examples of Array Variables When array variable is assigned a scalar variable, number assigned is length of array: @fred = (4,5,6); $a # $a gets 3, current length # UNIX System Programming CS UNIX System Programming

46 Examples – Element Access
J. Tan Computer Networks Examples – Element Access @fred = (7,8,9); $b = $fred[0]; # $b = 7 $fred[0] = 5 = (5,8,9) $fred[2]++ ; # increment 3rd element of ($fred[0],$fred[1]) = ($fred[1],$fred[0]) # swap first two UNIX System Programming CS UNIX System Programming

47 Examples – Element Access
J. Tan Computer Networks Examples – Element Access Slice: access list of elements @fred[0,1] ; # same as ($fred[0],$fred[1]) @fred[0,1] # swap @fred[0,1,2] # make all 3 elements same as # 2nd element @fred[1,2] =(9,10) # change last 2 values # 9 and 10 instead of $ UNIX System Programming CS UNIX System Programming

48 Examples: Index Expressions
J. Tan Computer Networks Examples: Index Expressions Index can be any expression that returns a number, e.g., @fred = (7,8,9); $a = 2; $b = $fred[$a]; # $fred[2] = 9 $c = $fred[$a-2] # c = $fred[0] = 7 UNIX System Programming CS UNIX System Programming

49 Examples: Out-of-bounds access
J. Tan Computer Networks Examples: Out-of-bounds access undef: value that represents something that is not defined. @fred = (1,2,3) $fred[i] for 0 > i > 2 yields undef value e.g., $barney = $fred[7] # $barney is now undef UNIX System Programming CS UNIX System Programming

50 Examples: Out-of-bounds assignment
J. Tan Computer Networks Examples: Out-of-bounds assignment Assigning a value beyond the end of the current array automatically extends the array, giving undef to all intermediate values, if any. @fred = (1,2,3); $fred[3] = “hi”; is now (1,2,3,”hi”) $fred[6] = “ho”; = (1,2,3,”hi”,undef,undef,”ho”)) UNIX System Programming CS UNIX System Programming

51 Examples: Array as a stack
J. Tan Computer Networks Examples: Array as a stack Top of stack: right-hand side of array. $newvalue); # @mylist = $newvalue) $oldvalue = # removes last element UNIX System Programming CS UNIX System Programming

52 UNIX System Programming
J. Tan Computer Networks Examples: push Push also accepts a list of values to be pushed. @mylist=(1,2,3); # @mylist = (1,2,3,4,5,6) UNIX System Programming CS UNIX System Programming

53 Examples: shift( ) & unshift( )
J. Tan Computer Networks Examples: shift( ) & unshift( ) push( ), pop( ): operates on right side of list shift( ), unshift( ) acts on left side. //insert left end = = UNIX System Programming CS UNIX System Programming

54 Examples: shift( ) & unshift( )
J. Tan Computer Networks Examples: shift( ) & unshift( ) @fred = (5,6,7); # push() from left end = (2,3,4,5,6,7) $x = // delete left end # pop() from left end # $x = 2 = (3,4,5,6,7) shift( ) and pop( ) returns undef on an empty array. UNIX System Programming CS UNIX System Programming

55 UNIX System Programming
J. Tan Computer Networks reverse( ) Reverses the order of the elements. @a = (7,8,9); @b = = (9,8,7) @b = reverse(7,8,9); # same thing UNIX System Programming CS UNIX System Programming

56 UNIX System Programming
J. Tan Computer Networks sort( ) Sorts arguments as ascending ASCII order of single strings. Original list is unaltered. @x = sort(“small”,”medium”,”large”); = (“large”,”medium”,” small”); @y = (1,2,4,8,16,32,64); @z = = (1,16,2,32,64,8) # numbers are interpreted as string values UNIX System Programming CS UNIX System Programming

57 Variable interpolation of arrays
J. Tan Computer Networks Variable interpolation of arrays Like scalars, array values may be interpolated into a double-quoted string. @fred = (“hello”,”Dolly”); $y = 2; $x = “This is $fred[1]’s place”; # “This is Dolly’s place” $x = “This is $fred[$y-1]’s place”; UNIX System Programming CS UNIX System Programming

58 Variable interpolation of arrays
J. Tan Computer Networks Variable interpolation of arrays When and how to escape [ ? @fred = (“hello”,”dolly”); $fred = “right”; # want to say “this is right[1]” $x = “This is $fred[1]”; # wrong! gives “This is dolly”; must \[ $x = “This is $fred\[1]”; # or ……….. UNIX System Programming CS UNIX System Programming

59 Variable interpolation of arrays
J. Tan Computer Networks Variable interpolation of arrays When and how to escape [ ? $x = “This is $fred”.”[1]”; $x = “This is ${fred}[1]”; UNIX System Programming CS UNIX System Programming

60 Scalar vs Array Context
Perl variables act differently depending on the context they’re evaluated in. = (“Mon”, “Tue”, “Fri”); @days evaluated in an array context returns the contents of array Mon Tue Fri @days evaluated in a scalar context returns the number of elements in array $n  $n = 3 UNIX System Programming

61 Scalar vs Array Context
Many perl operators and functions behave differently depending on context. Note that if a function expects a scalar context and you pass in an array, the array will be evaluated in a scalar context UNIX System Programming

62 Associative Arrays (AA)
J. Tan Computer Networks Associative Arrays (AA) Hash arrays: index values are not small non-negative integers. Index values: arbitrary scalars, called keys. AA elements: no particular order. Analogy: consider AA as a deck of filing cards. - top half of each card is the key - bottom half is the value - each time a value is inserted, a new card is created. UNIX System Programming CS UNIX System Programming

63 Associative Arrays (AA)
J. Tan Computer Networks Associative Arrays (AA) AA variable name is a percent (%) sign followed by a letter, followed by ≥ 0 letters, digits, and underscores. %samurais= (“zt”,”zatoichi”,”az”,”azumi”,”sh”,”shintaro”); Elements: (key, scalar) pair. Reference: $samurais{$key} where $key=“zt” for example. Notice symbols and braces used. UNIX System Programming CS UNIX System Programming

64 Associative Arrays (AA)
J. Tan Computer Networks Associative Arrays (AA) Adding new elements: $samurais{“sn”} = “sanjuro”; $samurais{234.5} = 45.6; # creates key “234.5”, value 45.6 Watch your symbols! print $samurais{“zt”}; # prints “zatoichi” print $samurais{234.5} += 3 #  48.6 UNIX System Programming CS UNIX System Programming

65 Literal Representation of an AA
J. Tan Computer Networks Literal Representation of an AA Need to access AA as a whole, e.g., to initialize or copy an entire AA ? Unfold AA into an array list: E.g., @samurai_list = %samurais; # unfold %samurais: = # (“zt”,”zatoichi”,”az”,”azumi”,”sh”,”shintaro”); UNIX System Programming CS UNIX System Programming

66 Literal Representation of an AA
J. Tan Computer Networks Literal Representation of an AA E.g., @pokemon_list = %pokemon; = (“pk”,”pikachu”,”tp”,”togepi”,”gy”,”gyrados”); %yugioh # create %yugioh like %pokemon. Auto-fold %yugioh = %pokemon; # faster way to do the same thing %anime = (“pk”,”pikachu”,”tp”,”togepi”,”gy”,”gyrados”); # create %anime from literal values UNIX System Programming CS UNIX System Programming

67 UNIX System Programming
J. Tan Computer Networks The keys( ) operator keys(%array_name) returns: A list of all current keys of %array_name, e.g., %lastname = ( ) ; # force %lastname empty $lastname{“don”} = “corleone”; # create new entries $lastname{“tom”} = “hagen”; @first = keys(%lastname) # contains either (“don”,”tom”) or reverse of that UNIX System Programming CS UNIX System Programming

68 UNIX System Programming
J. Tan Computer Networks The values( ) operator values(%array_name) returns: A list of all current values of %array_name, e.g., %lastname = ( ) ; # force %lastname empty $lastname{“don”} = “corleone”; # create new $lastname{“tom”} = “hagen”; # entries @last = values(%lastname) # contains either (“corleone”,”hagen”) or reverse of that UNIX System Programming CS UNIX System Programming

69 UNIX System Programming
Variable Types Hash (associative array) %var = {“name” => “paul”, “age” => 33}; Single elements are scalar print $var{“name”}; $var{age}++; How many elements are in my hash? @allkeys = keys(%var); $num = $#allkeys; UNIX System Programming

70 UNIX System Programming
J. Tan Computer Networks The each( ) operator Examine every element of AA? Use keys() operator to extract each key to index into the associative array (Try it.) Or… use the each() operator, i.e., each(%arrayname) while (($first,$last) = each(%lastname)) { print “Last name of $first is $last\n”; } UNIX System Programming CS UNIX System Programming

71 UNIX System Programming
J. Tan Computer Networks The delete( ) operator Delete a (key, value) pair: %anime = (“pk”,”pikachu”,”tp”,”togepi”,”gy”,”gyrados”); delete $anime(“tp”); # now anime = (“pk”,”pikachu”,”gy”,”gyrados”); UNIX System Programming CS UNIX System Programming

72 UNIX System Programming
J. Tan Computer Networks Operators Math The usual suspects: * / $total = $subtotal * (1 + $tax / 100.0); Exponentiation: ** $cube = $value ** 3; $cuberoot = $value ** (1.0/3); Bit-level Operations left-shift:<< $val = $bits << 1; right-shift:>> $val = $bits >> 8; UNIX System Programming CS UNIX System Programming

73 UNIX System Programming
J. Tan Computer Networks Operators Assignments As usual: = += -= *= /= **= <<= >>= $value *= 5; $longword <<= 16; Increment: $counter++ or ++$counter Decrement: -- $num_tries-- or --$num_tries UNIX System Programming CS UNIX System Programming

74 UNIX System Programming
J. Tan Computer Networks Perl # Like an odometer! UNIX System Programming CS UNIX System Programming

75 UNIX System Programming
J. Tan Computer Networks Operators Boolean (against bits in each byte) Usual operators: & | Exclusive-or: ^ Bitwise Negation: ~ $picture = $backgnd & ~$mask | $image; Boolean Assignment &= |= ^= $picture &= $mask; UNIX System Programming CS UNIX System Programming

76 UNIX System Programming
J. Tan Computer Networks Operators Logical (expressions) && And operator | | Or operator ! Not operator AND And, low precedence OR Or, low precedence NOT Not, low precedence XOR Exclusive or, low precedence UNIX System Programming CS UNIX System Programming

77 UNIX System Programming
J. Tan Computer Networks Operators Short Circuit Operators expr1 && expr2 expr1 is evaluated. expr2 is only evaluated if expr1 was true. expr1 || expr2 expr2 is only evaluated if expr1 was false. Examples open (…) || die “couldn’t open file”; $debug && print “user’s name is $name\n”; UNIX System Programming CS UNIX System Programming

78 UNIX System Programming
J. Tan Computer Networks Operators Modulo: % $a = 123 % 10; ($a is 3) Multiplier: x print “ride on the ”, “choo-”x2, “train”; (prints “ride on the choo-choo-train”) $stars = “*” x 80; Assignment: %= x= UNIX System Programming CS UNIX System Programming

79 UNIX System Programming
J. Tan Computer Networks Operators String Concatenation: = $name = “Uncle” . $space . “Sam”; $cost = 34.99; $price = “Hope Diamond, now only \$”; $price .= “$cost”; UNIX System Programming CS UNIX System Programming

80 UNIX System Programming
J. Tan Computer Networks Conditionals numeric string Equal: == eq Less/Greater Than: < > lt gt Less/Greater or equal: <= >= le ge Zero and empty-string means False All other values equate to True UNIX System Programming CS UNIX System Programming

81 UNIX System Programming
J. Tan Computer Networks Examples $a = 3; $b = ($a += 4); # $a, $b are both 7 now Unfortunately, order of evaluation of operands is unspecified  some expressions are undetermined: UNIX System Programming CS UNIX System Programming

82 UNIX System Programming
J. Tan Computer Networks Examples Unfortunately, order of evaluation of operands is unspecified  some expressions are undetermined: $a = 3; $b = ($a+=2) * ($a-=2); # BAD CODE: $b could be 15 or 3! UNIX System Programming CS UNIX System Programming

83 UNIX System Programming
J. Tan Computer Networks Autoincrement rule: For prefix: $d = 17 ; $e = ++$d ; # $d, $e are both 18 (prefix op) For postfix: result of expression is value of variable before it is incremented: $c = 17 ; $d = $c++; # $d = 17, but $c = 18 UNIX System Programming CS UNIX System Programming

84 UNIX System Programming
J. Tan Computer Networks Autoincrement rule: Unlike C, autoincrement works on floats! $c = 4.2 $c++ ; # makes it 5.2 UNIX System Programming CS UNIX System Programming

85 UNIX System Programming
J. Tan Computer Networks Conditionals numeric string Comparison: <=> cmp Results in a value of -1, 0, or 1 Logical Not: ! if (! $done) { print “keep going”; } Grouping: ( ) UNIX System Programming CS UNIX System Programming

86 Operator Precedence (low-to-high)
J. Tan Computer Networks Operator Precedence (low-to-high) right = += -= *= etc. right ?: nonassoc .. (range) left || left && left | ^ left & nonassoc == <=> != eq ne cmp nonassoc lt gt le; ge (string based) < > <= >= left << >> left left * / % x left =~ !~ right ! ~ and unary minus right ** nonassoc UNIX System Programming CS UNIX System Programming

87 UNIX System Programming
J. Tan Computer Networks Control Structures “if” statement - first style if ($porridge_temp < 40) { print “too hot.\n”; } elsif ($porridge_temp > 150) { print “too cold.\n”; } else { print “just right\n”; } UNIX System Programming CS UNIX System Programming

88 UNIX System Programming
J. Tan Computer Networks Control Structures “if” statement - second style statement if condition; print “\$index is $index” if $DEBUG; Single statements only Simple expressions only UNIX System Programming CS UNIX System Programming

89 UNIX System Programming
J. Tan Computer Networks Control Structures If you want to omit the then part of a control block and have the else part, it is natural to say: “do that if this is false” rather than “do that if not this is true”. Use “unless” (which is a reverse if) “unless” is a reverse “if” print “millenium is here!” unless $year < 2000; UNIX System Programming CS UNIX System Programming

90 UNIX System Programming
J. Tan Computer Networks Control Structures statement unless condition; unless ($year < 2000) { print “millenium is here!”; } UNIX System Programming CS UNIX System Programming

91 True/False interpretations
J. Tan Computer Networks True/False interpretations 0 # false 1-1 # computes to 0, then “0”, false 1 # converts to “1”, so true “” # empty string, so false “1” # not “” or “”, so true “00” # not “” or “0”, so true (weird!) “0.00” # same as “00” undef # evaluates to “”, so false UNIX System Programming CS UNIX System Programming

92 Control Structures – For loops
J. Tan Computer Networks Control Structures – For loops “for” loop - first style for (initial; condition; increment) { code …. } for (my $i=0; $i<10; $i++) { print “hello\n”; UNIX System Programming CS UNIX System Programming

93 UNIX System Programming
J. Tan Computer Networks For loops – Style 1 for ($i=0; $i<10; $i++) { print “hello\n”; } UNIX System Programming CS UNIX System Programming

94 UNIX System Programming
J. Tan Computer Networks For loops – Style 2 “for” loop (or foreach) - second style for [variable] (range) { code } for $name { print “$name is an employee.\n”; Takes a list of values and assigns them one at a time to a scalar variable, and executes the code with that assignment in effect. UNIX System Programming CS UNIX System Programming

95 For/Foreach loop example
J. Tan Computer Networks For/Foreach loop example @a = (1,2,3,4,5); print “List a: “ ”\n\n”; print “List b: “ foreach $b { print “$b”; } Takes a list of values and assigns them one at a time to a scalar variable, and executes the code with that assignment in effect. UNIX System Programming CS UNIX System Programming

96 The $_ default buffer – Style 3
J. Tan Computer Networks The $_ default buffer – Style 3 May omit scalar variable. Perl will use $_ . @a = (1,2,3,4,5); foreach { print; } UNIX System Programming CS UNIX System Programming

97 UNIX System Programming
J. Tan Computer Networks While loop while (condition) { code } $cars = 7; while ($cars > 0) { print “cars left: ”, $cars--, “\n”; } UNIX System Programming CS UNIX System Programming

98 UNIX System Programming
J. Tan Computer Networks Until until (condition) { code } $cars = 7; until ($cars <= 0) { print “cars left: ”, $cars--, “\n”; } UNIX System Programming CS UNIX System Programming

99 UNIX System Programming
J. Tan Computer Networks Do-while Bottom-check Loop: do { code } while (condition); $value = 0; do { print “Enter Value: ”; $value = <STDIN>; } until ($value > 0); UNIX System Programming CS UNIX System Programming

100 UNIX System Programming
J. Tan Computer Networks Next, Redo, Last Loop Controls “next” operator - go on to next iteration “redo” operator - re-runs current iteration “last” operator - ends the loop immediately while (cond) { next if $a < 5; } UNIX System Programming CS UNIX System Programming

101 UNIX System Programming
J. Tan Computer Networks Control Structures You can break out of multilevel loops using labels. Break out of multilevel loops using labels top: while (condition) { for ($car { … do stuff … next top if situation; } } UNIX System Programming CS UNIX System Programming

102 Loop Controls Examples
J. Tan Computer Networks Loop Controls Examples while ($qty = &get_quantities) { last if $qty < 0; # denotes the end next if $qty % 2; # skip even quantities $total += $qty; $qtycnt++; } print “Average of Odd Quantities: ”, $total/$qtycnt, “\n”; UNIX System Programming CS UNIX System Programming

103 UNIX System Programming
J. Tan Computer Networks No Switch Statement?!? Perl needs no switch (case) statement. Use if/else combinations instead if (cond1) { … } elsif (cond2) { … } elsif… else… This will be optimized at compile time UNIX System Programming CS UNIX System Programming

104 Perl regular expressions
Perl provides a direct translation of regular expressions. E.g., regular expression a+b+ in Perl script: #!/usr/bin/perl $_ = <STDIN>; # Read in input to argument $_ if (/a+b+$/) { print “yes\n”;} else { print “no\n”;} # Match $_ with a+b+ UNIX System Programming

105 Perl regular expressions
► The pattern /X/ is matched against the string argument. ► This pattern succeeds if the regular expression a+b+ extends from the initial character of the input line () to the end of the input line ($). ► To succeed if a+b+ is contained within a string, we only need the pattern /a+b+/. UNIX System Programming

106 UNIX System Programming
J. Tan Computer Networks Simple Regex in Perl Example: if (/abc/) { print “$_” ; } What is being tested against the regex? Our good old friend, the $_ variable. UNIX System Programming CS UNIX System Programming

107 UNIX System Programming
J. Tan Computer Networks Simple Regex in Perl Unlike grep (operated on every line of file), Perl code looks at just one line. if (/abc/) { print “$_” ; } Add loop to look at every line: while (<>) { UNIX System Programming CS UNIX System Programming

108 UNIX System Programming
J. Tan Computer Networks Simple Regex in Perl Like grep, pattern includes zero or more b’s followed by a c: while (<>) { if (/ab*c/) { print “$_” ; } UNIX System Programming CS UNIX System Programming

109 Built-in Perl Operators: pack
J. Tan Computer Networks Built-in Perl Operators: pack The TEMPLATE is a sequence of characters that give the order and type of values, as follows: A An ascii string, will be space padded. a An ascii string, will be null padded. c A signed char value. C An unsigned char value. s A signed short value. S An unsigned short value. i A signed integer value. I An unsigned integer value. a = ASCII string (null padded) A = ASCII string (space padded) b = bit string (low-to-high order) B = bit string (high-to-low order) c = signed char C = unsigned char d = double precision float (native format) f = single precision float (native format) h = hexadecimal string (low nybble first) H = hexadecimal string (high nybble first) i = signed integer value I = unsigned integer value l = signed long value L = unsigned long value n = short in “network” (big-endian) order N = long in “network” (big-endian) order p = pointer to string P = pointer to structure (fixed-length string) s = signed short value S = unsigned short value v = short in “VAX” (little-endian) order V = long in “VAX” (little-endian) order u = uuencoded string x = null byte X = back up a byte @ = Null-fill to absolute position A and a grab one character (or more if more specified) unless followed by * which means gobble up as many values as are left. UNIX System Programming CS UNIX System Programming

110 Built-in Perl Operators: pack
J. Tan Computer Networks Built-in Perl Operators: pack I A signed long value. L An unsigned long value. f A single-precision float in the native format. d A double-precision float in the native format. p A pointer to a string. v A short in "VAX" (little-endian) order. V A long in "VAX" (big-endian) order. a = ASCII string (null padded) A = ASCII string (space padded) b = bit string (low-to-high order) B = bit string (high-to-low order) c = signed char C = unsigned char d = double precision float (native format) f = single precision float (native format) h = hexadecimal string (low nybble first) H = hexadecimal string (high nybble first) i = signed integer value I = unsigned integer value l = signed long value L = unsigned long value n = short in “network” (big-endian) order N = long in “network” (big-endian) order p = pointer to string P = pointer to structure (fixed-length string) s = signed short value S = unsigned short value v = short in “VAX” (little-endian) order V = long in “VAX” (little-endian) order u = uuencoded string x = null byte X = back up a byte @ = Null-fill to absolute position A and a grab one character (or more if more specified) unless followed by * which means gobble up as many values as are left. UNIX System Programming CS UNIX System Programming

111 Built-in Perl Operators: pack
J. Tan Computer Networks Built-in Perl Operators: pack x A null byte. X Back up a byte. @ Null fill to absolute position. u A uuencoded string. h A hex string (low nybble first). H A hex string (high nybble first). Multiplier: A5 = AAAAA (beware! – later) a = ASCII string (null padded) A = ASCII string (space padded) b = bit string (low-to-high order) B = bit string (high-to-low order) c = signed char C = unsigned char d = double precision float (native format) f = single precision float (native format) h = hexadecimal string (low nybble first) H = hexadecimal string (high nybble first) i = signed integer value I = unsigned integer value l = signed long value L = unsigned long value n = short in “network” (big-endian) order N = long in “network” (big-endian) order p = pointer to string P = pointer to structure (fixed-length string) s = signed short value S = unsigned short value v = short in “VAX” (little-endian) order V = long in “VAX” (little-endian) order u = uuencoded string x = null byte X = back up a byte @ = Null-fill to absolute position A and a grab one character (or more if more specified) unless followed by * which means gobble up as many values as are left. UNIX System Programming CS UNIX System Programming

112 UNIX System Programming
J. Tan Computer Networks Pack examples $foo = pack("cccc",65,66,67,68); # foo eq "ABCD" $foo = pack("c4",65,66,67,68); # same thing $foo = pack("ccxxcc",65,66,67,68); # foo eq "AB\0\0CD" a = ASCII string (null padded) A = ASCII string (space padded) b = bit string (low-to-high order) B = bit string (high-to-low order) c = signed char C = unsigned char d = double precision float (native format) f = single precision float (native format) h = hexadecimal string (low nybble first) H = hexadecimal string (high nybble first) i = signed integer value I = unsigned integer value l = signed long value L = unsigned long value n = short in “network” (big-endian) order N = long in “network” (big-endian) order p = pointer to string P = pointer to structure (fixed-length string) s = signed short value S = unsigned short value v = short in “VAX” (little-endian) order V = long in “VAX” (little-endian) order u = uuencoded string x = null byte X = back up a byte @ = Null-fill to absolute position A and a grab one character (or more if more specified) unless followed by * which means gobble up as many values as are left. UNIX System Programming CS UNIX System Programming

113 UNIX System Programming
J. Tan Computer Networks Pack examples $foo = pack("s2",1,2); # "\1\0\2\0" on little-endian # "\0\1\0\2" on big-endian $foo = pack("a14","abcdefg"); # "abcdefg\0\0\0\0\0\0\0" a = ASCII string (null padded) A = ASCII string (space padded) b = bit string (low-to-high order) B = bit string (high-to-low order) c = signed char C = unsigned char d = double precision float (native format) f = single precision float (native format) h = hexadecimal string (low nybble first) H = hexadecimal string (high nybble first) i = signed integer value I = unsigned integer value l = signed long value L = unsigned long value n = short in “network” (big-endian) order N = long in “network” (big-endian) order p = pointer to string P = pointer to structure (fixed-length string) s = signed short value S = unsigned short value v = short in “VAX” (little-endian) order V = long in “VAX” (little-endian) order u = uuencoded string x = null byte X = back up a byte @ = Null-fill to absolute position A and a grab one character (or more if more specified) unless followed by * which means gobble up as many values as are left. UNIX System Programming CS UNIX System Programming

114 UNIX System Programming
J. Tan Computer Networks Pack examples $foo = pack("a4","abcd","x","y","z"); # "abcd" $foo = pack("aaaa","abcd","x","y","z"); # "axyz" Note: “a4” != “aaaa” a = ASCII string (null padded) A = ASCII string (space padded) b = bit string (low-to-high order) B = bit string (high-to-low order) c = signed char C = unsigned char d = double precision float (native format) f = single precision float (native format) h = hexadecimal string (low nybble first) H = hexadecimal string (high nybble first) i = signed integer value I = unsigned integer value l = signed long value L = unsigned long value n = short in “network” (big-endian) order N = long in “network” (big-endian) order p = pointer to string P = pointer to structure (fixed-length string) s = signed short value S = unsigned short value v = short in “VAX” (little-endian) order V = long in “VAX” (little-endian) order u = uuencoded string x = null byte X = back up a byte @ = Null-fill to absolute position A and a grab one character (or more if more specified) unless followed by * which means gobble up as many values as are left. UNIX System Programming CS UNIX System Programming

115 Built-in Perl Operators: pack
J. Tan Computer Networks Built-in Perl Operators: pack Take a list of values and pack it into a binary structure $buf = pack("CCCC", 80, 101, 114, 108); print(“$buf\n”); Useful in database records $record = pack(“A40 A A40 s”, $first, $middle, $last, $age); a = ASCII string (null padded) A = ASCII string (space padded) b = bit string (low-to-high order) B = bit string (high-to-low order) c = signed char C = unsigned char d = double precision float (native format) f = single precision float (native format) h = hexadecimal string (low nybble first) H = hexadecimal string (high nybble first) i = signed integer value I = unsigned integer value l = signed long value L = unsigned long value n = short in “network” (big-endian) order N = long in “network” (big-endian) order p = pointer to string P = pointer to structure (fixed-length string) s = signed short value S = unsigned short value v = short in “VAX” (little-endian) order V = long in “VAX” (little-endian) order u = uuencoded string x = null byte X = back up a byte @ = Null-fill to absolute position A and a grab one character (or more if more specified) unless followed by * which means gobble up as many values as are left. UNIX System Programming CS UNIX System Programming

116 Built-in Perl Operators: unpack
Reverse of pack, take a binary value and expand it into a list value. ($first, $middle, $last, $age) = unpack(“A40 A A40 s”, $record) UNIX System Programming

117 Built-in Perl Operators: time
Returns the number of non-leap seconds since January 1, 1970, UTC. Useful for comparing things like file modification times or for feeding to localtime or gmtime. UNIX System Programming

118 UNIX System Programming
J. Tan Computer Networks local/gmtime localtime Convert a time value to a nine element list corrected for local time. ( $sec,$min,$hour,$mday,$mon,$year, $wday,$yday,$isdst)= localtime(time); gmtime Convert a time value to a nine element list corrected for Greenwhich (England) time. $wday,$yday,$isdst)= gmtime(time); isdst = is daylight savings time or not UNIX System Programming CS UNIX System Programming

119 UNIX System Programming
J. Tan Computer Networks local/gmtime 0 <= sec <= <= min <= <= hour <= <= mday <= 31, the day of the month <= mon <= 11 , 0=Jan, 1=Feb, 2=Mar, etc. year has 1900 subtracted from it, i.e. years since The year 1999 is 99 and the year 2000 is <= wday <= 6 , the day of the week , 0=Sunday, 1=Monday, etc <= yday <= 366 , the day of the year isdst = {0,1} , if = 1, DST in effect. isdst = is daylight savings time or not UNIX System Programming CS UNIX System Programming

120 Built-in Perl Operators: splice
splice ARRAY, OFFSET, [LENGTH], [LIST] Removes the elements designated by OFFSET and LENGTH from an array replaces them with the elements of LIST, if any. Returns the elements removed from the array. The array grows or shrinks as necessary. UNIX System Programming

121 Built-in Perl Operators: splice
If LENGTH is omitted, removes everything from OFFSET onward. If LENGTH is negative,  removes the elements from OFFSET onward except for -LENGTH elements at the end of the array. UNIX System Programming

122 Built-in Perl Operators: splice
If both OFFSET and LENGTH are omitted, removes everything. If OFFSET is past the end of the array,  perl issues a warning, and splices at the end of the array. UNIX System Programming

123 Built-in Perl Operators: splice
Direct Method Splice Equivalent (right side push) ARRAY, OFFSET, [LENGTH], [LIST] -1) If OFFSET is negative it starts that far from the end of the array and removes items. (delete from left) 0, 1) 0, 0, $x, $y) $a[$x] = $y $x, 1, $y) UNIX System Programming

124 Built-in Perl Operators: substr
substr EXPR, OFFSET, [LENGTH] Extract a substring out of string given by EXPR and return it Substring starts at OFFSET and goes for LENGTH characters (or end of string if LENGTH not specified) UNIX System Programming

125 Built-in Perl Operators: substr
substr("Once upon a time", 3, 4); # returns "e up" substr("Once upon a time", 7); # returns "on a time" UNIX System Programming

126 Built-in Perl Operators: split
splits up a string and places it into an array. function uses a regular expression works on the $_ variable unless otherwise specified. E.g., $info = “Johansson:Scarlett:Actor:14, Leafy Drive"; @personal = split(/:/, $info); which has the same overall effect as: @personal = (“Johansson", “Scarlett", "Actor", "14, Leafy Drive"); UNIX System Programming

127 UNIX System Programming
J. Tan Computer Networks Pattern Matching See if strings match a certain pattern syntax: string =~ pattern Returns true if it matches, false if not. Example: match “abc” anywhere in string: if ($str =~ /abc/) { … } But what about complex concepts like: between 3 and 5 numeric digits optional whitespace at beginning of line UNIX System Programming CS UNIX System Programming

128 UNIX System Programming
J. Tan Computer Networks Pattern Matching Recall that regular expressions are a way to describe character patterns in a string Example: match “john” or “jon” /joh?n/ Example: match money values /\$\d+\.\d\d/ Complex Example: match times of the day /\d?\d:\d\d(:\d\d)? (AM|PM)?/i UNIX System Programming CS UNIX System Programming

129 UNIX System Programming
J. Tan Computer Networks Pattern Matching Symbols with Special Meanings period any single character char set [0-9a-f] - one char matching these Abbreviations \d a numeric digit [0-9] \w a word character [A-Za-z0-9_] \s whitespace char [ \t\n\r\f] \D, \W, \S - any character but \d, \w, \s \n, \r, \t - newline, carriage-return, tab \f, \e formfeed, escape \b word break UNIX System Programming CS UNIX System Programming

130 UNIX System Programming
J. Tan Computer Networks Pattern Matching Symbols with Special Meanings: * zero or more occurrences one or more occurrences ? zero or one occurrences ^ anchor to begin of line $ anchor to end of line {n,m} - between n and m occurrences (inclusively) [A-Z]{2,4} - “2, 3, or 4 uppercase letters”. UNIX System Programming CS UNIX System Programming

131 UNIX System Programming
J. Tan Computer Networks Pattern Matching Ways of Using Patterns: Matching if ($line =~ /pattern/) { … } Substitution $name =~ s/ASU/Arizona State University/; Translation $command =~ tr/A-Z/a-z/; # lowercase it UNIX System Programming CS UNIX System Programming

132 UNIX System Programming
J. Tan Computer Networks Pattern Matching Extracting Matched Patterns group pieces using parentheses $1 will be leftmost group, $2 next, $3, … if (/(\d?\d):(\d\d):(\d\d)/) { ($h,$m,$s) = ($1,$2,$3); } You can even use them in the pattern! /File (\w+)\.GIF renamed to \1\.gif/ s/(\d+) is larger than (\d+)/\2 is smaller than \1/ UNIX System Programming CS UNIX System Programming

133 UNIX System Programming
J. Tan Computer Networks Grouping Patterns #!/usr/bin/perl –w $_ = “fred xxxxxxxxxx barney”; s/x*/boom/; Or use a multipler: s/x{5,10}/boom; # matches 5 to 10 x’s s/x{5,}/boom; # matches 5 or more x’s s/x{5}/boom; # matches exactly 5 x’s s/a.{5}b/boom # letter a separated from letter b by # any 5 non-newline characters. UNIX System Programming CS UNIX System Programming

134 Anchoring Patterns (\b)
J. Tan Computer Networks Anchoring Patterns (\b) Matching operation: Pattern to be matched is dragged through the string from left to right, matching the first possible opportunity. Anchors ensure that parts of the pattern line up with particular parts of the string. UNIX System Programming CS UNIX System Programming

135 Anchoring Patterns (\b)
J. Tan Computer Networks Anchoring Patterns (\b) \b (boundary) examples: /fred\b/; # matches fred, but not frederick /\bwiz/; # matches wiz and wizard, but not geewiz /\bDoc\b/; # matches Doc, but not Doctor or WhatsupDoc /\b\+\b/; # matches “x+y” but not “++” or “□+□“ UNIX System Programming CS UNIX System Programming

136 Anchoring Patterns (\b)
J. Tan Computer Networks Anchoring Patterns (\b) E.g., /abc\bdef/; # never matches (impossible for boundary at # indicated point Likewise, \B requires that there not be a boundary at the Indicated point, e.g., /\BDoc\B/; # matches Doctor, but not Doc UNIX System Programming CS UNIX System Programming

137 The =~ operator (for regex’s)
J. Tan Computer Networks The =~ operator (for regex’s) $a = “hello world”; $a =~ /^he/; # true $a =~ /(.)\1/; # true; matches double l Backreference if ($a =~ /(.)\1/) { # true, so yes…. do some stuff…. } UNIX System Programming CS UNIX System Programming

138 The =~ operator (for regex’s)
J. Tan Computer Networks The =~ operator (for regex’s) print “Any last request? “; if (<STDIN> =~ /^[yY]/){ # does input begin with y? print “What might that request be? “; <STDIN>; # discard a line of stdin print “Sorry, I’m unable to do that.\n”; } UNIX System Programming CS UNIX System Programming

139 Subroutines (Functions)
J. Tan Computer Networks Subroutines (Functions) Defining a Subroutine: sub name { code } Arguments passed in via list: sub multiply { my ($a, $b) = @_; return $a * $b; } Last value processed is the return value (could have left out word “return”, above) UNIX System Programming CS UNIX System Programming

140 UNIX System Programming
Subroutines sub sub_name { statement_1; statement_2; ... } sub_name(); UNIX System Programming

141 UNIX System Programming
Subroutine arguments sub print_val { print “The parameter is $_[0]\n”; } ... print_val(“a value”); $_[0] is the first value of array. Note that you can also use shift to assign the value to a variable. $val = shift; //(left pop) print “The parameter is $val\n”; UNIX System Programming

142 UNIX System Programming
Variable scope sub my_sub { print $what; } $what here is a global variable Can declare local variables with my. sub my_sub2 { my $what = “what”; UNIX System Programming

143 Subroutines (Functions)
J. Tan Computer Networks Subroutines (Functions) Calling a Subroutine: &subname; # no args, no return value &subname (args); retval = &subname (args); The “&” is optional as long as… subname is not a reserved word subroutine was defined before being called UNIX System Programming CS UNIX System Programming

144 Subroutines (Functions)
J. Tan Computer Networks Subroutines (Functions) Passing Arguments: Passes the value Lists are expanded @a = (5,10,15); @b = (20,25); # assumes mysub undefined yet this passes five arguments: 5,10,15,20,25 mysub can receive them as 5 scalars, or one array UNIX System Programming CS UNIX System Programming

145 Subroutines (Functions)
J. Tan Computer Networks Subroutines (Functions) Examples: sub good1 { my($a,$b,$c) } sub good2 { } good2($one, $two, $three); UNIX System Programming CS UNIX System Programming

146 Subroutines (Functions)
J. Tan Computer Networks Subroutines (Functions) Examples: sub good3 { } good3($name, sub bad1 { // why? } @a will absorb all args, $b will have nothing. UNIX System Programming CS UNIX System Programming

147 Launching External Programs
J. Tan Computer Networks Launching External Programs The “system” library call example: system (“ls -la”); Returns exit status of program it launched a shell actually runs, so you can use: pipes (“|”) redirection (“<”, “>”) Be wary: platform-dependencies (“ps aux”) Very unsecure in multi-user environment UNIX System Programming CS UNIX System Programming

148 UNIX System Programming
J. Tan Computer Networks System call examples #!/usr/bin/perl my $status = system("vi fred.txt"); # useful when useful if you do not need to capture the output of the the program UNIX System Programming CS UNIX System Programming 148

149 UNIX System Programming
J. Tan Computer Networks System call examples # or if you do not want to involve the shell #!/usr/bin/perl my $status = system("vi", "fred.txt"); if (($status >>=8) != 0) { //bit shift to get return value die "Failed to run vi"; } UNIX System Programming CS UNIX System Programming 149

150 Launching External Programs
J. Tan Computer Networks Launching External Programs Run a command, insert output inline $numlines = `wc -l < /etc/passwd`; Non-quoted execution qx(rm -f /tmp/file) qx(ls -l) # alternative to ‘ls -l‘ fork( ) and exec( ) just like in C wait( ) - wait for child processes to exit UNIX System Programming CS UNIX System Programming

151 UNIX System Programming
J. Tan Computer Networks The qx command // If you need to capture the output of the program #!/usr/bin/perl my $info = qx(uptime); print "Uptime is: $info\n"; UNIX System Programming CS UNIX System Programming 151

152 Or use a backquote command
J. Tan Computer Networks Or use a backquote command // If you need to capture the output of the program #!/usr/bin/perl = `who`; foreach my $i { print "$i is online\n"; } UNIX System Programming CS UNIX System Programming 152

153 UNIX System Programming
J. Tan Computer Networks Special Variables $$ current process’ PID $` $’ pre-match & post-match strings $. input line number $/ input record separator (\n) $, output field separator for print $\ output record separator (none) UNIX System Programming CS UNIX System Programming

154 UNIX System Programming
J. Tan Computer Networks Special Variables $! system error message (related to $ERRNO variable) $[ first element index number (0) $^O Operating System’s name @INC library directories (require, use) @ARGV command line args list %ENV Environment variables UNIX System Programming CS UNIX System Programming

155 UNIX System Programming
J. Tan Computer Networks File Functions Much faster than system()… unlink “file”; - deletes a file rename “oldfile”, “newfile”; rmdir “directory”; link “existingfile”, “newfile”; symlink “existingfile”, “newfile”; chmod 0644, “file1”, “file2”, …; chown $uid, UNIX System Programming CS UNIX System Programming

156 UNIX System Programming
J. Tan Computer Networks Command Line Args $0 = program name @ARGV array of arguments to program zero-based index (default for all arrays) Example $ yourprog -a somefile $0 is “yourprog” $ARGV[0] is “-a” $ARGV[1] is “somefile” UNIX System Programming CS UNIX System Programming

157 UNIX System Programming
<> operator Perl has a few special filehandles <STDIN> is standard input <STDOUT> is standard output <> is the file or files specified on the command line. All return a single line in a scalar context or undef if all lines have been read (i.e., end-of-file). UNIX System Programming

158 UNIX System Programming
Basic I/O open – opens a filehandle open(FILEHANDLE, “filename”); open(FH, “>outfile”); open(FH, “>>logfile”); close – closes a filehandle File handles also closed upon reopen or program exit UNIX System Programming

159 UNIX System Programming
J. Tan Computer Networks Basic File I/O Reading a File open (FILEHANDLE, “$filename”) || die \ “open of $filename failed: $!”; while (<FILEHANDLE>) { chop $_; # or just: chop; print “$_\n”; } close FILEHANDLE; UNIX System Programming CS UNIX System Programming

160 UNIX System Programming
J. Tan Computer Networks Basic File I/O Reading from a Pipe open (FILEHANDLE, “ps aux |”) || die \ “launch of ‘ps’ failed: $!”; while (<FILEHANDLE>) { chop; print “$_\n”; } close FILEHANDLE; UNIX System Programming CS UNIX System Programming

161 UNIX System Programming
J. Tan Computer Networks Basic File I/O How does <> work? opens each ARGV filename for reading if no ARGV’s, reads from stdin great for writing filters, here’s “cat”: while (<>) { print; # same as print “$_”; } UNIX System Programming CS UNIX System Programming

162 UNIX System Programming
J. Tan Computer Networks Basic File I/O Writing a File open (FILEHANDLE, “>$filename”) || die\ “open of $filename failed: $!”; while { print FILEHANDLE “$_\n”; # note, no comma! } close FILEHANDLE; UNIX System Programming CS UNIX System Programming

163 UNIX System Programming
J. Tan Computer Networks Basic File I/O Writing to a Pipe open (FILEHANDLE, “| mail frank”) || die \ “launch of ‘mail’ failed: $!”; while { print FILEHANDLE “$_\n”; } close FILEHANDLE; UNIX System Programming CS UNIX System Programming

164 UNIX System Programming
Output print [FILEHANDLE] [LIST] Prints a string, or a comma-separated list of strings to FILEHANDLE (or standard out if FILEHANDLE is omitted). Note that because list is comma-separated, there cannot be a comma between FILEHANDLE and LIST. print { $OK ? “STDOUT” : “STDERR” } “stuff\n”; UNIX System Programming

165 UNIX System Programming
J. Tan Computer Networks File Test Operators Existence: if (-e “file”) Readable, Writable, Executable: -r -w -x Zero size file, “Stuff” in file: -z -s File, Directory, Symlink: -f -d -l Named Pipe, Socket: -p -S TTY attached: -t if (-t STDIN) # is stdin a terminal or not? UNIX System Programming CS UNIX System Programming

166 UNIX System Programming
J. Tan Computer Networks Implcit Loops: -n flag Very useful for processing files a line at a time. If you type something like: $ perl -n -e 'some code' file1 Perl will interpret that as: LINE: while (<>) { # your code goes here } UNIX System Programming CS UNIX System Programming 166

167 UNIX System Programming
J. Tan Computer Networks Implcit Loops: -n flag <> operator will read all of the files on the command line a line at a time. Each line of the input files will be put into $_ so that you can process it. As a example, try: $ perl -n -e 'print "$. - $_"' file This gets converted to: LINE: while (<>) { print "$. - $_" } This code prints each line of the file together with the current line number UNIX System Programming CS UNIX System Programming 167

168 UNIX System Programming
J. Tan Computer Networks Implcit Loops: -n flag $ cat temp aaa bbb ccc $ perl -n -e 'print "$. - $_"' temp 1 – aaa 2 – bbb 3 - ccc UNIX System Programming CS UNIX System Programming 168

169 UNIX System Programming
J. Tan Computer Networks Implcit Loops: -p flag -p option always prints the contents of $_ each time around the loop. It creates code like this: LINE: while (<>) { # your code goes here } continue { print or die "-p destination: $!\n"; } UNIX System Programming CS UNIX System Programming 169

170 UNIX System Programming
J. Tan Computer Networks Implcit Loops: -p flag $ cat temp aaa bbb ccc $ perl -p -e ‘$_ = "$. - $_"' temp // perl -n -e 'print "$. - $_"' temp 1 – aaa 2 – bbb 3 – ccc No need to explicitly call print because –p calls it automatically UNIX System Programming CS UNIX System Programming 170

171 Perl versus Unix Utilities
J. Tan Computer Networks Perl versus Unix Utilities “-n” iterates over filename args perl -ne ‘cmds’ < file perl -ne ‘cmds’ file1 file2 file3 perl -ne print “$a[0]\n” if $a[0]>1024’ “-p” is “-n” with printing to stdout perl -pe ‘cmds’ < file perl -pe ‘cmds’ file1 file2 file3 perl -pe ‘s/^#/;/’ < zonefile > zonefile.new UNIX System Programming CS UNIX System Programming

172 Perl versus Unix Utilities
J. Tan Computer Networks Perl versus Unix Utilities What’s that again? perl -ne print “$a[0]\n” if $a[0]>1024’ …is the same as this program: #!/bin/perl while (<>) { @a=split; if ($a[0] > 1024) { print “$a[0]\n”; } } UNIX System Programming CS UNIX System Programming

173 Perl versus Unix Utilities
J. Tan Computer Networks Perl versus Unix Utilities sed - substitutions sed: s/xxx/yyy/g vi: :%s/xxx/yyy/g perl: s/xxx/yyy/g; tr - translations tr: tr ‘[A-Z]’ ‘[a-z]’ (platform dependent) perl: tr/A-Z/a-z/; (platform independent) UNIX System Programming CS UNIX System Programming

174 Perl versus Unix Utilities
J. Tan Computer Networks Perl versus Unix Utilities wc - char / word / line counting wc: wc -l < filename perl: perl -ne ‘END{print “$.\n”}’ < filename awk - BEGIN and END constructs awk: BEGIN {total=0} $1==“bananas:” {total += $2} END {print total} perl: BEGIN {$total=0} if (/^bananas:\s+(\d+)/) {$total += $1} END {print “$total\n”} UNIX System Programming CS UNIX System Programming

175 Perl versus Unix Utilities
J. Tan Computer Networks Perl versus Unix Utilities eval - Parse arbitrary code at run time! $pattern = ‘s/\bprez\b/The President/gi’; while (<>) {eval $pattern; print} could read $pattern from a config file Conversion Utilities a2p - awk to perl translator s2p - sed to perl translator find2perl - Unix “find” to perl! UNIX System Programming CS UNIX System Programming

176 Perl and Other Languages
J. Tan Computer Networks Perl and Other Languages Perl and C Perl 5 can call C libraries! C can call Perl 5 modules! Perl and Fortran perl has a “format” output facility (fixed-width fields, padding control, …) UNIX System Programming CS UNIX System Programming

177 Environment Variables
J. Tan Computer Networks Environment Variables Stored in %ENV hash print “I am $ENV{USER}\n”; $home = $ENV{“HOME”}; $ENV{‘PATH’} .= “:/usr/local/bin”; UNIX System Programming CS UNIX System Programming

178 UNIX System Programming
J. Tan Computer Networks Unix Signals %SIG hash defines signal handlers $SIG{ALRM} = ‘handler’; alarm(5); …busy work… exit 0; sub handler { print “caught SIGALRM\n”; exit 1; } UNIX System Programming CS UNIX System Programming

179 UNIX System Programming
J. Tan Computer Networks Unix Signals Signals are used for… periodically checking something without having to slow down loops with counters and ‘if’ statements. checkpointing long program runs. UNIX System Programming CS UNIX System Programming

180 UNIX System Programming
J. Tan Computer Networks Unix Signals Signals are sent using “kill” kill 1, $pid; kill 9, $pid1, $pid2, $pid3; BSD and SysV signal handling work differently! (Unix kernel differences) Make sure all functions called by a signal handler are re-entrant (resumeable). Note: sleep() uses the Alarm signal! UNIX System Programming CS UNIX System Programming

181 UNIX System Programming
J. Tan Computer Networks Unix Signals Common Signals in Unix 1 SIGHUP Hangup 2 SIGINT Interrupt (^C) 3 SIGQUIT Coredumps program (^\) 4 SIGILL Illegal Instruction 8 SIGFPE Floating Point Exception 9 SIGKILL Kill process (can’t catch or ignore this signal) UNIX System Programming CS UNIX System Programming

182 UNIX System Programming
J. Tan Computer Networks Unix Signals Common Signals in Unix (continued) 10 SIGBUS Bus Error 11 SIGSEGV Segmentation Violation 14 SIGALRM Alarm clock went off 28 SIGWINCH Window size change 30 SIGUSR1 User defined signal #1 31 SIGUSR2 User defined signal #2 UNIX System Programming CS UNIX System Programming

183 UNIX System Programming
J. Tan Computer Networks Unix Signals UNIX System Programming CS UNIX System Programming 183

184 The use Compiler Directive
J. Tan Computer Networks The use Compiler Directive For using modules added to Perl automatically export function and variable names to the main namespace UNIX System Programming CS UNIX System Programming

185 UNIX System Programming
J. Tan Computer Networks Pragma Used by the use command to turn other compiler directives on and off. E.g., to force Perl to use integer math instead of floating-point math to speed up certain sections of your program. print("Floating point math: ", 10 / 3, "\n"); use integer; print("Integer math: ", 10 / 3, "\n"); displays: Floating point math: Integer math: 3 UNIX System Programming CS UNIX System Programming

186 UNIX System Programming
J. Tan Computer Networks Pragma Pragmas can be turned off using the no compiler directive. E.g., the following statement turns off the integer pragma: no integer; UNIX System Programming CS UNIX System Programming

187 UNIX System Programming
J. Tan Computer Networks Pragma List Pragma Description integer Forces integer math instead of floating point or double precision math. less Requests less of something - like memory or cpu time - from the compiler. sigtrap Enables stack backtracing on unexpected signals. strict Restricts unsafe constructs. This pragma is highly recommended! Every program should use it. subs Lets you pre-declare function names. UNIX System Programming CS UNIX System Programming

188 UNIX System Programming
J. Tan Computer Networks The strict Pragma The most important pragma is strict. It generates compiler errors if unsafe programming is detected. There are three specific things that are detected: Symbolic references Non-local variables (those not declared with my())and variables that aren't fully qualified. Non-quoted words that aren't subroutine names or file handles. Symbolic references use the name of a variable as the reference to the variable. They are a kind of shorthand widely used in the C programming language, but not available in Perl. UNIX System Programming CS UNIX System Programming

189 UNIX System Programming
J. Tan Computer Networks Packages Collect data & functions in a separate (“private”) namespace Similar to static functions & data in C Reusable code UNIX System Programming CS UNIX System Programming

190 UNIX System Programming
J. Tan Computer Networks Packages Access packages by file name or path: require “getopts.pl”; require “/usr/local/lib/perl/getopts.pl”; require “../lib/mypkg.pl”; UNIX System Programming CS UNIX System Programming

191 UNIX System Programming
J. Tan Computer Networks Packages Packages hide their “private” data and subroutines: Command: package pkgname; Stays in effect until next “package” or end of block { … } or end of file. Default package is “main” Some things are global though STDIN, $., $<, …) UNIX System Programming CS UNIX System Programming

192 UNIX System Programming
J. Tan Computer Networks Packages Package name in variables $pkg::counter = 0; Package name in subroutines sub pkg::mysub ( ) { … } &pkg::mysub($stuff); UNIX System Programming CS UNIX System Programming

193 UNIX System Programming
J. Tan Computer Networks Packages # Get Day Of Month Package package getDay; sub main::getDayOfMonth { local ($sec, $min, $hour, $mday) = localtime; return $mday; }; UNIX System Programming CS UNIX System Programming

194 UNIX System Programming
J. Tan Computer Networks Packages Calling the package require “/path/to/getDay.pl”; $day = &getDayOfMonth; In Perl 5, you can leave off “&” for previously defined functions: $day = getDayOfMonth; UNIX System Programming CS UNIX System Programming

195 UNIX System Programming
J. Tan Computer Networks Packages Packages available to you look in your perl library directory… /usr/local/lib/perl5 /usr/local/perl /usr/share/lib/perl /usr/lib/perl5/5.8.8 (on the goonies cluster) Type “man perlmod” for more info See CPAN archive on the Internet UNIX System Programming CS UNIX System Programming

196 UNIX System Programming
J. Tan Computer Networks Perl pods What are pods? UNIX System Programming CS UNIX System Programming

197 UNIX System Programming
J. Tan Computer Networks Strict, my and Modules my() function creates lexical variables instead of local variables. Are variables declared with my() local to a package? No, these are lexical variables that are never stored inside the symbol table. If you need to declare variables that are local to a package, fully qualify your variable name in the declaration or initialization statement: use strict; $main::foo = ''; package Math; $Math::PI = && $Math::PI; UNIX System Programming CS UNIX System Programming

198 UNIX System Programming
J. Tan Computer Networks Strict, my and Modules $Math::PI = && $Math::PI; The && $Math::PI part of the second declaration is used to avoid getting error messages from the -w command line option. Since the variable is inside a package, there is no guarantee that it will be used by the calling script and the -w command line option generates a warning about any variable that is only used once. By adding the harmless logical and to the declaration, the warning messages are avoided. UNIX System Programming CS UNIX System Programming

199 Run time error printing
J. Tan Computer Networks Run time error printing carp, croak; use Carp; use strict; package Foo; sub foo { main::carp("carp called at line " . __LINE__ . ", \n but foo() was called"); main::croak("croak called at line " . __LINE__ . ", } package main; Foo::foo(); UNIX System Programming CS UNIX System Programming

200 Run time error printing
J. Tan Computer Networks Run time error printing This program displays: carp called at line 9, but foo() was called at e.pl line 18 croak called at line 10, This example uses a compiler symbol, __LINE__, to incorporate the current line number in the string passed to both carp() and croak(). UNIX System Programming CS UNIX System Programming

201 UNIX System Programming
J. Tan Computer Networks Two Useful Libraries Timelocal - opposite of localtime() Easily converts time/date string elements back into seconds-since-1970! use timelocal; $time = timelocal($sec,$min,$hours,$mday, $mon,$year); or call “timegm” - opposite of gmtime (Universal coordinated time zone) UNIX System Programming CS UNIX System Programming

202 Random Number Generation
J. Tan Computer Networks Random Number Generation Initialize random number seed srand num; srand (time( ) ^ ($$ + ($$ << 15)) ); Get a random float between 0..n $val = rand n; print “I am”, rand 100, “ percent sure.\n”; If n is not given, returns value between 0..1 UNIX System Programming CS UNIX System Programming

203 UNIX System Programming
J. Tan Computer Networks Debugging in Perl -w option is great! #!/bin/perl -w tells you about… misused variables using uninitialized data/variables identifiers that are used only once and more UNIX System Programming CS UNIX System Programming

204 UNIX System Programming
J. Tan Computer Networks Debugging in Perl Debug mode: perl -d filename [args] Display Commands h Extended help h h Abbreviated help l (lowercase-L) list lines of code l sub list subroutine sub l 5 list line 5 l list lines 3 through 6 inclusive l list next window of lines UNIX System Programming CS UNIX System Programming

205 UNIX System Programming
J. Tan Computer Networks Debugging in Perl Display Commands (continued) w list window around current location S list all subroutines w/ packages V [pkg] list all variables [in package pkg] X [vars] list variables in this package /pat search forwards for pattern pat (regular expressions legal) ?pat search backwards for pattern pat (regular expressions legal) UNIX System Programming CS UNIX System Programming

206 UNIX System Programming
J. Tan Computer Networks Debugging in Perl Display Commands (continued) - (hyphen) list previous window p expr prints Perl expression expr = alias value set a command alias H [-num] history (list previous cmds) !num redo a debug command command execute any Perl command or line of code you want q quit UNIX System Programming CS UNIX System Programming

207 UNIX System Programming
J. Tan Computer Networks Debugging in Perl Stepping & Tracing s step -- execute 1 line of code. Steps “over” subroutines. n next -- execute 1 line of code. Steps “into” subroutines. [RETURN] repeats previous “step” or “next” r return from function t toggle trace mode UNIX System Programming CS UNIX System Programming

208 UNIX System Programming
J. Tan Computer Networks Debugging in Perl Breakpoints b line set a breakpoint at line line b sub set a breakpoint at subroutine sub d line delete breakpoint D delete all breakpoints c [line] continue running until next breakpt [set a temporary breakpoint at line] L List all breakpoints UNIX System Programming CS UNIX System Programming

209 UNIX System Programming
J. Tan Computer Networks Debugging in Perl Actions a line cmd executes perl code cmd each time line is reached A delete all line actions < cmd set action right before the debugging prompt > cmd set action right after the debugging prompt UNIX System Programming CS UNIX System Programming

210 UNIX System Programming
J. Tan Computer Networks Debugging in Perl How to Learn More man perldebug just try it! UNIX System Programming CS UNIX System Programming

211 UNIX System Programming
J. Tan Computer Networks Advanced Topics Security setuid & variable tainting References multidimensional arrays pass by reference (args to subroutines) Modules Objects UNIX System Programming CS UNIX System Programming

212 UNIX System Programming
J. Tan Computer Networks Advanced Topics Formatted printing Standard Perl Library modules Launching and monitoring child processes: fork( ) exec( ) wait ( ) UNIX System Programming CS UNIX System Programming

213 UNIX System Programming
J. Tan Computer Networks CPAN Comprehensive Perl Archive Network Hundreds of reusable Perl modules written by people on the Internet Archive sites ftp://ftp.cs.colorado.edu/pub/perl/CPAN/ ftp://ftp.cdrom.com/pub/perl/CPAN/ ftp://ftp.orst.edu/pub/packages/CPAN/ UNIX System Programming CS UNIX System Programming

214 UNIX System Programming
J. Tan Computer Networks CPAN Files README.html -- introduction CPAN.html -- more detail MIRRORED.BY -- CPAN servers list Tree of Modules modules/by-category/ modules/by-module/ modules/by-author/ UNIX System Programming CS UNIX System Programming

215 UNIX System Programming
J. Tan Computer Networks How to Learn More On-line man pages man perl -- starting point man perlref -- references man perldoc -- documentation man perlmod -- modules man perlobj -- objects Standard Perl Library modules are in perldoc command gives manpage-like info example: perldoc Getopt::Std UNIX System Programming CS UNIX System Programming

216 UNIX System Programming
J. Tan Computer Networks Conclusions Perl is very Powerful Perl programs can be Simple Perl programs can be Complex Perl is your friend UNIX System Programming CS UNIX System Programming


Download ppt "UNIX System Programming"

Similar presentations


Ads by Google