Introduction to Perl
PDT-009679
Course: Introduction to Perl Code: PDT-009679 Content Owner: Matt Jones ([email protected]) Last Update: 30 December 2009 Web site: http://perl.intel.com Contributors: Kamlesh Kumar ([email protected]) Ken Stephens ([email protected]) Ingo Schmiegel ([email protected]) Steve Willoughby ([email protected]) Michael Ewan ([email protected]) Phil Brothers ([email protected]) All of the Perl instructors at Intel Costa Rica!
Introduction to Perl - Index
Topic ....................................slide number ............................................................71 " .............................................................72 #! ...........................................................11 $ (anchor)............................................150 $$ ........................................................195 $| .........................................................192 $1-9 .....................................................166 && .......................................................111 ()..........................................................151 (|).........................................................152 *...........................................................147 . ..........................................................139 ? ..........................................................148 [] ..........................................................140 [-] .........................................................141 [^].........................................................142 ^ (anchor) ............................................150 ` .............................................................74 || ..........................................................111 + ..........................................................146 <> ........................................................184 <>, globbing ........................................211 A advanced Perl .....................................248 anchor .................................................150 and ......................................................112 arguments ...........................................230 array ......................................................42 atan2().................................................129 autoflush..............................................192 B blocks ..................................................113 boolean .................................................21 boolean operators .................................38 C character set shortcuts........................143 chdir ....................................................205 chmod .................................................209 chown..................................................210 close....................................................183 closedir................................................203 comment ...............................................10 conditional assignment .......................103 cos() ....................................................129 D-E delete.................................................. 187 do/if..................................................... 102 environment variables .......................... 26 escape codes ....................................... 76 exit........................................................ 12 exp() ................................................... 129 F filehandles .......................................... 179 for ....................................................... 106 foreach ............................................... 107 format ................................................... 10 G globbing.............................................. 211 goto .................................................... 114 greedy ................................................ 149 grep().................................................. 170 grouping ............................................. 151 H-J hash...................................................... 59 hash functions ...................................... 61 if.......................................................... 100 join........................................................ 51 L labels .................................................. 113 last...................................................... 114 links in UNIX....................................... 189 list ......................................................... 42 log() .................................................... 129 M match function .................................... 161 match options ..................................... 163 match tagging..................................... 166 matching............................................. 135 math ................................................... 129 mkdir................................................... 204 my......................................................... 27 N-O next..................................................... 114 numbers ............................................... 20 open ................................................... 180 opendir ............................................... 201 operators, numeric ............................... 29 operators, string ................................... 32 or ........................................................ 112
Introduction to Perl - Index
P-Q pop ........................................................47 print .......................................................85 printf ......................................................86 process ID ...........................................195 prototypes ...........................................236 push ......................................................45 q functions.............................................78 R range operator ......................................43 readdir .................................................202 redo .....................................................114 references, perl ...................................247 references, subroutines ......................241 regular expressions.............................133 rename ................................................188 repetition operators .............................145 return...................................................237 S scalar.....................................................18 scope.....................................................27 shebang ................................................11 shell execution ......................................74 shift........................................................48 sin() .....................................................129 sort, alphabetical...................................54 sort, hash ..............................................64 sort, numeric .........................................55 splice .....................................................52 split........................................................49 sqrt()....................................................129 string functions......................................34 strings....................................................19 subroutines .........................................222 substitute tagging................................167 substitution function ............................164 substitution options .............................165 T-Z text strings.............................................71 time .....................................................124 undef .....................................................22 unless..................................................110 unlink...................................................187 unshift....................................................46 until......................................................110 while ....................................................104
1. Introduction
What to expect in this course Perls strengths and weaknesses Typographical conventions used
Introduction to Perl
PDT-009679
General format of Perl code Getting started with Perl
PDT-009679 September 2009 Slide 5
Agenda
Part I
1. Introduction - Lab 1 2. Data Handling Break Lab 2a Lab 2b
What to expect in this course
Part II
3. Quoting Text - Lab 3 4. Advanced Printing - Lab 4 / break 5. Flow Control - Lab 5 6. Built-In Functions - Lab 6
The basics of Perl, which is a lot, so youll be able to code right away Plan on participating in discussions and ask questions when they come to you Give the labs an honest effort because the only way to learn Perl is to use it
PDT-009679 September 2009 Slide 2
PDT-009679 September 2009 Slide 6
Agenda
Part III
7. Reg. Expressions Lab 7a / break RE functions Lab 7b
A brief history of Perl
Part IV
8. Input & Output - Lab 8 / break 9. Subroutines - Lab 9 10. Wrap-up
Created by Larry Wall in 1987 while working as a programmer at Unisys Is a "backronym" for Practical Extraction and Report Language Is interpreted, rather than compiled Perl 5 was released in 1997 and it is still being maintained
PDT-009679 September 2009 Slide 3
PDT-009679 September 2009 Slide 7
Perls strengths & weaknesses
Strengths: String parsing File formatting UNIX utilities Large user base Lots of docs Modules and libraries Weaknesses: Interfacing w/ HW Intense math
Getting started with Perl
Well focus on UNIX, but carefully crafted code is portable to Windows Name your file anything you want; some editors provide advantages to using a .pl or .perl extension and Windows may associate those extensions with the interpreter Start with the "shebang" line
#!/usr/intel/bin/perl -w
PDT-009679 September 2009 Slide 8
PDT-009679 September 2009 Slide 11
Typographical conventions used
Regular text Block bold text Course content Code examples and runtime (keyboard) input Runtime (screen) output (> = prompt) Placeholders for substituted data
Getting started with Perl
Get in the habit of including this
use strict;
End with an exit line
exit (0);
Block regular text Italicized text
Give the file execute permissions
> chmod u+x script.pl
Run the script
> script.pl
PDT-009679 September 2009 Slide 12
PDT-009679 September 2009 Slide 9
General format of Perl code
Perl is free-form, so pick a coding style individually (or as a team) and stick with it White space (including returns) is legal just about anywhere within the code A semicolon (;) is used to separate simple statements, usually at the end of each The pound sign (#) denotes a comment to the end of the line; nothing for blocks
PDT-009679 September 2009 Slide 10
A simple Perl script
unix> more simple.pl #!/usr/intel/bin/perl w use strict; my $name = "John Smith"; print "Hi $name, how are you?\n"; print "2 times 5 is ", 2*5, "\n"; exit(0); unix> simple.pl Hi John Smith, how are you? 2 times 5 is 10
PDT-009679 September 2009 Slide 13
Lab 1: Introduction
Write a Perl script that will print your name and the product of your two favorite numbers Make it similar to the simple Perl script on the previous slide If you have any questions, ask them!
Scalar data types
A scalar is the basic data type in Perl String: "This is a string" Number: 5 Boolean: 1 (true), 0 (false) Undef: no valid string, number, or ref.
PDT-009679 September 2009 Slide 14
PDT-009679 September 2009 Slide 18
Lab 1: Sample output
Hi Matt Jones, how are you? 15 times 16 is 240
Strings
A string is a sequence of zero or more characters A literal string may be defined by enclosing it in quotes A string variable holds a single value and its name is always preceded by a dollar sign ($) $string = "Hello world!";
PDT-009679 September 2009 Slide 15
PDT-009679 September 2009 Slide 19
2. Data Handling
Scalar data types Variable names Variable scope Operators Lists and arrays Data manipulations
PDT-009679 September 2009 Slide 17
Numbers
A number is a single numeric value Examples of numeric values:
240 22.4 6.02e23 0xff3c 0577 1_395_423 # # # # # # integer floating point sci. notation hexadecimal octal _ is OK
PDT-009679 September 2009 Slide 20
Boolean (true or false)
The empty string (""), the zero string ("0"), the number zero (0), and any undefined value are false. Everything else is true. Boolean values are used primarily in control structures (conditionals and loop statements)
PDT-009679 September 2009 Slide 21
Variable names
A scalar variable starts with a $ and is followed by a letter or an underscore, then any number of alphanumerics Valid names $first_score $Strvar2 $_sorted Invalid names $2bit varname $var.name
PDT-009679 September 2009 Slide 24
Undef
Perl has a special scalar value: undef This is both a function, undef(), and a value used in advanced data structures The undef command can be used to unassign a variable (delete from memory) Undef has no value, it is undefined
Special variables
We will cover several special variables in this class, they are: $_ the default variable (incl. @_) $1-9 placeholder variables $$ the process ID $| the autoflush variable @ARGV the command line arguments
PDT-009679 September 2009 Slide 25
PDT-009679 September 2009 Slide 22
Undef
Use undef() to undefine a variable Use defined() to determine whether a variable is defined Examples:
undef($var); if ($var) {} if (! (defined($var))) {} # false # true
Environment variables
Environment variables may be accessed from a Perl script using $ENV{env}, where env is the environment variable name They can be used directly, created, or assigned to other variables; this is better than hard-coding into your script If the environment variables change while the script is running, the changes won't be noticed by the script
PDT-009679 September 2009 Slide 23
PDT-009679 September 2009 Slide 26
Variable scope
Variables do not have to be declared, but doing so is considered good practice Variables are global by default, but those should be use sparingly to avoid problems A variables scope can be restricted using the "my" function within a block of code
Example: my $string = "hello";
Numeric operators
Strings may be auto-incremented, too!
$var = "a9"; $var++; # $var is now b0 $var = "aa"; $var++; # $var is now ab $var = "zz"; $var++; # $var is now aaa
PDT-009679 September 2009 Slide 30
Any block of code can have local variables
PDT-009679 September 2009 Slide 27
Operators
Perl has lots of operators for use with strings and numbers Operator precedence and associatively are important, so be careful You can use the UNIX command "perldoc perlop" to view information about Perl operators
PDT-009679 September 2009 Slide 28
Numeric operators
Assignment operators perform an operation on a variable and assign it to the same variable name
=, +=, -=, *=, /=
Examples:
$x = 3; $x += 5; # $x is now 8 $x = 3; $x *= 5; # $x is now 15
PDT-009679 September 2009 Slide 31
Numeric operators
Arithmetic operators
+, -, *, /, %, **, <<, >>
String operators
Two strings can be concatenated using the . (period) operator
$x = "one"; $y = " two"; $z = $x.$y; # $z is "one two"
Automatic operators increment or decrement a variable by one
$var = 5; $var++; # $var is now 6 $var--; # $var is back to 5
This is more commonly done using the string interpolation behavior of double quote marks:
$x = "one"; $y = " two"; $z = "$x$y"; # $z is "one two"
This can also be done using the string concatenation assignment operator (.=)
$z = "one"; $z .= " two" # $z is "one two"
PDT-009679 September 2009 Slide 29
PDT-009679 September 2009 Slide 32
Operators
Warning: Performing numeric operations on strings and string operations on numbers can have some unexpected results, so make sure you know what you're doing!!
$x = "one"; $y = " two"; $z = $x + $y; # $z is 0
String functions
$line = "WWID = 10010010"; $wwid = substr($line, -8, 8); # $wwid is 10010010 This is a useful (and faster) way to grab the last number of characters from a string without using regular expressions (which we'll cover later)
PDT-009679 September 2009 Slide 33
PDT-009679 September 2009 Slide 36
String functions
lc(), uc() change case of an entire string to lower case or upper case lcfirst(), ucfirst() change case of the first letter of a string to lower or upper case substr(), length() extract a substring from or find the length of a string chomp(), chop() remove the new line or the last character from a string
PDT-009679 September 2009 Slide 34
String functions
$len = length($name); # $len is 17 $str = "plants\n"; # \n is newline chomp($str); # removes newline # $str is "plants" (no newline) chop($str); # removes last char. # $str is "plant" (no newline)
PDT-009679 September 2009 Slide 37
String functions
$str = "fred"; $ustr = uc($str); # $ustr is FRED $ufstr = ucfirst(lc($str)); # $ufstr is Fred $name = "first last"; substr($name, 6, 0) = "middle "; # $name is first middle last
Boolean operators
Numeric logical operations
<, >, <=, >=, ==, != Note: If $x = "0" and $y = "0.0" then $x == $y is true, but $x eq $y is false.
String logical operations
lt, gt, le, ge, eq, ne
Bitwise AND, OR, NOT, and XOR
&, | (pipe), ~ (tilde), ^ (carat)
Logical AND, OR, and NOT
&&, ||, !
PDT-009679 September 2009 Slide 35
PDT-009679 September 2009 Slide 38
Scalars: Review
String, numeric, boolean, and undef Scope is global by default (use my) Scalars are single elements Variable names begin with $ Be sure and use the right operator based on the context of the evaluation
PDT-009679 September 2009 Slide 39
Range operator
The range operator is represented by a double dot (..) and is used to generate a list of numbers or letters. It can be used as constant initialization for a list, or can be used in a loop.
@list = (1..100); foreach $var (a..z) {}
PDT-009679 September 2009 Slide 43
15-minute Break
Using arrays
@ra = (3,4,5,6,7); @ra = (3..7); @b = @ra; $el = $ra[2]; ($a, $b) = @ra; # a list of integers # using range operator # copy @ra to @b # assigns 5 to $el # assigns 1st 2 elements
($a, $b, @c) = @ra; # assigns all elements print @ra; print "@ra"; # prints 34567 # prints 3 4 5 6 7
PDT-009679 September 2009 Slide 40
PDT-009679 September 2009 Slide 44
Lists and arrays
A list is an ordered group of scalars An array is a named list preceded by @ Arrays are indexed by the position of each element in the array, starting with zero and ending with $#array Empty an array with $#array = -1; or @array = ();
PDT-009679 September 2009 Slide 42
Adding items to the end
Syntax: push (arrayName, list); Examples: @a = ("1","2","3"); push (@a,"4"); @b = ("5","6"); push (@a,@b); # assigns @a # @a = 1 2 3 4 # assigns @b # @a = 1 2 3 4 5 6
PDT-009679 September 2009 Slide 45
Adding items to the front
Syntax: unshift (arrayName, list); Examples: @a = ("4","5","6"); unshift (@a,"3"); @b = ("1","2"); unshift (@a,@b); # assigns @a # @a = 3 4 5 6 # assigns @b # @a = 1 2 3 4 5 6
Splitting a string
The "split" function breaks a string into part on the basis of a specified delimiter returning a list of string fragments between each delimiter Syntax: split (delimiter, string [, maxItems]); Note: the delimiter may be a simple string or regular expression; the default maxItems value is unlimited
PDT-009679 September 2009 Slide 46
PDT-009679 September 2009 Slide 49
Removing items from the end
Syntax: pop (arrayName); Examples: @a = ("1","2","3"); $b = pop(@a); # assigns @a # $b = 3, @a = 1 2
Splitting a string
Examples: $s = "1:2:3"; @a = split(":",$s); # assign $s # @a = 1 2 3
$s = "list file1 file2"; # re-assigns $s ($key, $rest) = split(/\s+/, $s, 2); # $key = "list"; # $rest = "file1 file2"; Note: /\s+/ is a regular expression (RE) that means "one or more white spaces"
Note: this changes the value of $#a
PDT-009679 September 2009 Slide 47
PDT-009679 September 2009 Slide 50
Removing items from the front
Syntax: shift (arrayName); Examples: @a = ("1","2","3"); $b = shift(@a); # assigns @a # $b = 1, @a = 2 3
Joining a list
The "join" function attaches the elements of a list into a string with fields separated by a delimiter Syntax: join (delimiter, list); Examples: @a = ("1","2","3"); $s = join(", ",@a); # assign @a # $s = "1, 2, 3"
Note: the index values of the original elements are decremented
PDT-009679 September 2009 Slide 48
PDT-009679 September 2009 Slide 51
Splicing a list
The "splice" function for lists is similar to the "substr" function for strings Syntax: splice (a1, offset, length, a2); Notes: a2 is inserted into a1 starting at offset and overwriting length items If splice is assigned to a list, what is overwritten will be stored in the new list
Sorting a list (numerical)
Numerical sorting of a list Syntax: @l2 = sort {$a <=> $b} @l1; Examples:
@a1 = ("3","12","2"); @a2 = sort {$a <=> $b} @a1; # assign @a1 # @a2 = 2 3 12
Note: using an alpha sort would result in @a2 = 12 2 3, which is not as expected; use {$b <=> $a} for a descending sort
PDT-009679 September 2009 Slide 52
PDT-009679 September 2009 Slide 55
Splicing a list
Examples: @a1 = ("a","b","c"); # assign @a1 @a2 = ("d","e","f"); # assign @a2 @a3 = splice (@a1, 1, 1, @a2); # a1 gets (a, d, e, f, c) # a3 gets (b)
Arrays: Review
Arrays are defined using @array Individual elements are accessed using $array[index], where index starts at zero and ends at $#array Use the proper type of sort depending on the data being sorted
PDT-009679 September 2009 Slide 53
PDT-009679 September 2009 Slide 56
Sorting a list (alphabetical)
Alphabetical (ASCII) sorting of a list Syntax: @list2 = sort @list1; Examples: @a1 = ("b","c","a"); # assign @a1 @a2 = sort @a1; # @a2 = a b c
Lab 2a: Data Handling
Write a Perl script that starts with the following string assignment $str = "list banana carrot apple plum peach orange prune"; http://perl.intel.com/intro/labs/lab2a.pl Print an alphabetically sorted list of all items except the first word (list)
Sample Output:
@a2 = sort {$b cmp $a} @a1; # @a2 = c b a
apple banana carrot orange peach plum prune
PDT-009679 September 2009 Slide 54
PDT-009679 September 2009 Slide 57
Hashes (associative arrays)
A hash is an unordered list of values that are paired with a "key" The "key" is like the index for an array Each key as an associated value A hash variable is defined using a % Use {key} to refer to an element in a hash; for example: $hash{key}
PDT-009679 September 2009 Slide 59
Using hash functions
foreach $k (keys %hash) { print "$hash{$k}\n"; } foreach $v (values %hash) { print "found value $v\n";} while (($k, $v) = each (%hash)) { print "$k, $v\n"; }
PDT-009679 September 2009 Slide 62
Using hashes
Example: %hash = ("john" => "32", "bill" => 45, "joe", "25", "fred", 24); $hash{"harry"} = 42; # adds "harry" => 42 to the table $age = $hash{"john"}; # age is 32
Some hash functions
delete $hash{key} deletes the key and value from %hash defined $hash{key} returns true if the value associated with key is defined in %hash exists $hash{key} returns true if the specified key exists in %hash
PDT-009679 September 2009 Slide 60
PDT-009679 September 2009 Slide 63
Some hash functions
keys(%hash) returns a list (array) of all of the keys in %hash or number (scalar) of elements in the hash values(%hash) returns a list of all of the values in %hash each(%hash) returns key-value pairs from %hash; usually in while loops
Sorting hash keys
Sort defaults to an ASCII string sort The <=> operator is used for numerical sorting; the cmp function is used for alphabetical sorting Examples: foreach $k (sort keys %hash) {} foreach $k (sort {$a cmp $b} keys %hash) {} foreach $k (sort {$b cmp $a} keys %hash) {} foreach $k (sort {$a <=> $b} keys %hash) {} foreach $k (sort {$b <=> $a} keys %hash) {}
PDT-009679 September 2009 Slide 61
PDT-009679 September 2009 Slide 64
10
Hashes: Review
Hash table syntax is %hash Hash element syntax is $hash{key} Hash functions: keys(), values(), each(), delete(), defined(), exists() Can use sort on hash keys
Agenda
Part III
7. Reg. Expressions Lab 7a / break RE functions Lab 7b
Part IV
8. Input & Output - Lab 8 / break 9. Subroutines - Lab 9 10. Wrap-up
PDT-009679 September 2009 Slide 65
PDT-009679 September 2009 Slide 68
Lab 2b: Data Handling
Write a Perl script that defines the following hash table %hash = ("1" => "one", "3" => "three, "11" => "eleven", "4" => "four"); http://perl.intel.com/intro/labs/lab2b.pl Print a numerically sorted list of the keys
Sample Output: 1 3 4 11
3. Quoting Text
Using simple text strings Interpolating string text Using special characters Using escape codes Using "q" functions
PDT-009679 September 2009 Slide 66
PDT-009679 September 2009 Slide 70
Agenda
Part I
1. Introduction - Lab 1 2. Data Handling Break Lab 2a Lab 2b
Simple text strings
Part II
3. Quoting Text - Lab 3 4. Advanced Printing - Lab 4 / break 5. Flow Control - Lab 5 6. Built-In Functions - Lab 6
String (text) literal values are enclosed in quotation marks: 'Hello, world!' Every character between the quotes is kept literally as typed (except \\ & \') $str = 'The value of x is $x\n'; print "$str\n"; > The value of x is $x\n
PDT-009679 September 2009 Slide 71
PDT-009679 September 2009 Slide 67
11
Interpolated string text
Enclosing text in double-quotes allows for special symbols to be interpolated into the string: $x = 12; $str = "The value of x is $x\n"; print $str; > The value of x is 12 (new line)
PDT-009679 September 2009 Slide 72
Special characters
Backslash (\) introduces a special character sequence which would be difficult to type directly The string constructed will contain the special characters represented by code
PDT-009679 September 2009 Slide 75
Interpolated string text
Double-quoted text may include:
String escape codes
\n \r \t \f \b \$ \' newline (UNIX) return (Win) tab form feed backspace dollar sign single quote \a \e \nnn \xnn \cx \@ \" alarm bell escape octal code nnn hex code nn control-x character at sign double quote
Variable names beginning with $ (scalar) or @ (list), but not % (hash) Array element subscript expressions Literal math expressions (i.e., 4 * 3 will not be changed to 12) Special character "escape codes"
PDT-009679 September 2009 Slide 73
PDT-009679 September 2009 Slide 76
Shell execution string
Use back ticks to execute a shell string command; the results are assigned to a scalar: $currdir = `pwd`; Note that the newline character is included in the returned variable, so chomp ($currdir = `pwd`) is recommended to remove it
String escape code examples
print "Attention\a\a\a!\n"; print "He said, \"She said, 'Hello!'\"\n"; print "Name:\t$name\n"; print "The fee is \$100.00.\n"; print 'The fee is $100.00';
PDT-009679 September 2009 Slide 74
PDT-009679 September 2009 Slide 77
12
"q" functions
Single quoting: $str = q/Hello/; Double quoting: $str = qq/$name/; Execution string: $pwd = qx/pwd/; Quote words: @list = qw/one two/; Can use almost anything instead of / $str = q#Hello#;
PDT-009679 September 2009 Slide 78
Lab 3: Quoting Text
Print each string on its own line: print "$str1\n"; Are the results what you expected? If not, why not?
PDT-009679 September 2009 Slide 81
Quoting Text: Review
String (text) literal values are enclosed in quotation marks: 'Hello, world!' Every character between the quotes is kept literally as typed (except \\ & \') Double-quotes allow for special symbols to be interpolated into the string Use back ticks to execute a shell string command Backslash (\) introduces a special character sequence "q" functions allow the use of most anything instead of quotes
PDT-009679 September 2009 Slide 79
Lab 3: Sample output
1: This is string 1 2: This is not $string1 3: This is string #3 4: This is not "This is string #3" 5: /eng/eng10/msjones/perl_intro
PDT-009679 September 2009 Slide 82
Lab 3: Quoting Text
Write a script that has these vars defined: $str1 $str2 $str3 $str4 $str5 = = = = = "This is string 1"; 'This is not $str1'; q/This is string #3/; qq/This is not "$str3"/; qx(pwd); # UNIX only
4. Advanced Printing
print statement defaults Formatted print function Controlling the print field String format print function
http://perl.intel.com/intro/labs/lab3.pl
PDT-009679 September 2009 Slide 80
PDT-009679 September 2009 Slide 84
13
print statement defaults
Syntax: print [filehandle] [list]; # no , Defaults: filehandle = STDOUT (screen) list = $_ (the default variable) Examples: print STDOUT $_; print $_; print; # These examples # produce the # same result
Controlling the print field width
Syntax: %[width]field_specifier Example: %7s String with a minimum of 7 characters If the data to be printed are not as wide as the field provided, the data will be right justified by default (padded with spaces)
PDT-009679 September 2009 Slide 88
PDT-009679 September 2009 Slide 85
Formatted print function
Syntax:
printf ([filehandle] "line", arg, );
Field width examples
printf ("Name: %7s %d\n", "Joe", 10); printf ("Name: %7s %d\n", "Bob", 7); printf ("Name: %7s %d\n", "Steve", 12); printf ("Name: %7s\n", "1234567"); Name: Name: Name: Joe 10 Bob 7 Steve 12
The line can contain field specifiers:
%d %x %f %s Integer decimal (base 10) Integer hexadecimal (base 16) Floating point number String
All other text is printed as-is
PDT-009679 September 2009 Slide 86
Name: 1234567
PDT-009679 September 2009 Slide 89
printf function examples
printf ("hello world\n");
> hello world
Left justifying
Syntax: %[-[width]]field_specifier
printf ("Name: %-7s %d\n", "Joe", 10); printf ("Name: %-7s %d\n", "Bob", 7); printf ("Name: %-7s %d\n", "Steve", 12); printf ("Name: %-7s\n", "1234567");
printf (OF "integer = %d\n", 75); # no ,
> integer = 75
printf ("string = %s\n", "testing");
Name: Joe 10 7 12
> string = testing
Name: Bob Name: Steve Name: 1234567
printf ("hex value of 75 is %x\n", 75);
> hex value of 75 is 4b
PDT-009679 September 2009 Slide 87
PDT-009679 September 2009 Slide 90
14
Padding with zeros Syntax: %[0[width]]field_specifier Example: %03d
printf ("Name: %-7s Agent: %03d\n", "Bond", 7); Name: Bond Agent: 007
Advanced printing: Review
print; is the default print statement printf ("line", arg, ); is for formatted printing of arguments using field specifiers within line $var = sprintf (); is for assigning a formatted string to a variable
PDT-009679 September 2009 Slide 91
PDT-009679 September 2009 Slide 94
Floating point numbers
Syntax: %[0]<total>.<dec>f Examples:
printf ("Value = %8.2f\n", $fpt); Eight total characters (including dot), two decimal digits, padded with white space > Value = 63.25 printf ("Value = %09.3f\n", $fpt); Nine total characters (including dot), three decimal digits, padded with zeros > Value = 00063.252
Lab 4: Advanced Printing
Store your full name and WWID in scalar vars Print your name twice (left justified; 20 characters, right justified) Print your WWID twice (left justified; 15 characters, padded with zeros) Print your WWID and name (WWID takes up 15 spaces, is left justified, and padded with white space; there should be 8 white spaces between your WWID and your name)
PDT-009679 September 2009 Slide 95
The least significant digit will be rounded
PDT-009679 September 2009 Slide 92
String format print function
Returns a formatted string Syntax: sprintf (line, arg, ); Example:
$dec = 75; $hex = sprintf("%x", $dec); print "Hex value of $dec is $hex\n"; > Hex value of 75 is 4b
Lab 4: Sample output
My name is: Matthew S. Jones My name is: Matthew S. Jones
My WWID is: 10074409 My WWID is: 000000010074409 WWID 10074409 Matthew S. Jones
PDT-009679 September 2009 Slide 93
PDT-009679 September 2009 Slide 96
15
15-minute Break
Control structures: if
Examples: if ($x > 10) { } if ($flag) { } else { } if ($x > 10) { } elsif ($x > 5) { } else { }
PDT-009679 September 2009 Slide 97
PDT-009679 September 2009 Slide 101
5. Flow Control
If / unless While / until For / foreach Reversing some functions Next / last / goto / redo
Reverse function: if, do/if
Syntax: statement if (expr) Examples:
print "x > 10\n" if $x > 10; $i = 10 if $i > 10; print "Debug: x=$x\n" if $debug; do {$x = 0; $y = 0;} if $reset;
To execute more than one statement, use do { } if (expr);
PDT-009679 September 2009 Slide 102
PDT-009679 September 2009 Slide 99
Control structures: if
if (expr) { } elsif (expr2) { } else { } If expr is true then the first { } block will be executed; if not and expr2 is true then the second { } block will be executed; if not then the third { } block will be executed The elsif and else statements are optional, but the braces {} are mandatory The inverse function is unless
Conditional assignment
Syntax: condition ? then : else; Example:
$x = ($val <= $max) ? $val : $max;
Equivalent:
if ($val <= $max) {$x = $val;} else {$x = $max;}
PDT-009679 September 2009 Slide 100
PDT-009679 September 2009 Slide 103
16
Control structures: while
Syntax: while (expr) { } The boolean expression is evaluated If it is true, then the block is executed Repeat evaluate / execute loop until the boolean expression is false The inverse function is do until
PDT-009679 September 2009 Slide 104
Control structures: foreach
Syntax: foreach $var (list) { } Executes { } once for each element in the list, assigning that element to $var $_ is used if $var is omitted
PDT-009679 September 2009 Slide 107
Control structures: while
Example: my $done = 0; my $count = 1; while (! $done) { print "$count\n"; $count++; if ($count == 10) {$done = 1;} }
Control structures: foreach
Example: @list = ("one", "two", "three"); foreach $str (@list) { print "$str\n"; } > one > two > three
PDT-009679 September 2009 Slide 108
PDT-009679 September 2009 Slide 105
Control structures: for
Syntax: for (init; test; mod) { } Example:
for ($i = 0; $i < 10; $i++) { }
Control structures: foreach
Example: foreach $i (1..5) { print "$i\n"; } > 1 > 2 > 3 > 4 > 5
Good for counting loops Equivalent:
$i = 0; while ($i < 10) { ; $i++; }
PDT-009679 September 2009 Slide 106
PDT-009679 September 2009 Slide 109
17
Reversing some functions
Most of these functions can be reversed, putting the statement first, like this:
statement statement statement statement if condition; unless condition; while condition; until condition;
Blocks and labels
A block is a [labeled] sequence of statements surrounded by braces. MYBLOCK: { $x = 5; $y = 10; $z = $x + $y; }
PDT-009679 September 2009 Slide 113
The condition is tested before the statements are executed Using do statement while condition will always execute the statement once
PDT-009679 September 2009 Slide 110
Control structures: &&, ||
expr1 && expr2; is equivalent to if (expr1) { expr2;} expr1 || expr2; is equivalent to if (! expr1) { expr2;} Examples:
getColor() && print "getColor passed\n"; getColor() || print "getColor failed\n";
Loop modifiers
next [label]; - go to the next loop iteration immediately, for the innermost (or labeled) loop block last [label]; - immediately break out of the innermost (or labeled) loop block goto label; - immediately go to a labeled line of code redo; start loop again without checking conditional statement
PDT-009679 September 2009 Slide 111
PDT-009679 September 2009 Slide 114
Control structures: and, or
and and or work like && and ||, but have lower precedence Examples:
open my $fh, <, "myFile or die "myfile: $!"; getColor() and print "Running getColor\n";
Using loop modifiers and labels
OUTER: while ($outer = 1) { $a++; $x = 1; while ($inner = 1) { chomp ($in = <STDIN>); # gets keyboard input goto FINISH if ($in eq "done"); next if ($in eq "skip"); $x++; print "A: $a , X: $x, A*X:", $a * $x, "\n"; next OUTER if ($x * $a > 10); } } FINISH:
PDT-009679 September 2009 Slide 112
PDT-009679 September 2009 Slide 115
18
Flow control: Review
Control structures
if / unless conditional execution while / until looping during a condition for / foreach counting / list loops
Lab 5: Sample output
3 2 1 2 1 2 4 1 candy card flowers gum keychain pants shirt sticker
Loop modifiers
next last goto redo go to the next iteration of this loop break out of this loop go directly to a labeled line of code start loop again without checking cond.
PDT-009679 September 2009 Slide 116
PDT-009679 September 2009 Slide 119
Flow control: Review
Comparison operators
&& second expression will evaluate only if first expression is true || second expression will evaluate only if first expression is false and / or just like && and ||, except with lower precedence
6. Built-In Functions
Time functions Math functions
PDT-009679 September 2009 Slide 117
PDT-009679 September 2009 Slide 121
Lab 5: Flow Control
Start with the code in this file:
http://perl.intel.com/intro/labs/lab5.pl
Time functions
Time is relative to January 1, 1970 UTC (Coordinated Universal Time) Time zone Local time Greenwich Mean Time (GMT = UTC)
Write a Perl script to parse each line, split the data and store each item in a hash table. Keep a count of each occurrence of each item (case insensitive). Print out an alphabetical list of items with the number of times each occurs in the list.
PDT-009679 September 2009 Slide 118
PDT-009679 September 2009 Slide 122
19
Relative time
Number of seconds since 01/01/1970 Syntax: time(); Example:
$starttime = time(); do some stuff $endtime = time(); $runtime = $endtime - $starttime; print "Total run time was $runtime sec.\n";
Greenwich Mean Time
Called just like localtime() with same return values; if you want universal time (rather than local time) Syntax: gmtime();
PDT-009679 September 2009 Slide 123
PDT-009679 September 2009 Slide 126
Converting time
Turns the time into something readable Syntax: localtime(); Returns a list: seconds, minutes, hour (023), day of the month, month (January = 0, etc.), year (minus 1900), day of the week (Sunday = 0, etc.), day of the year, Daylight Saving Time flag (true or false)
Math functions
sqrt(value) square root exp(value) exponential log(value) natural log sin(x) sine cos(x) cosine atan2(y, x) arc tangent
PDT-009679 September 2009 Slide 124
PDT-009679 September 2009 Slide 127
Example of localtime()
@timelist = localtime(); printf("Today is %d/%d/%d\n", $timelist[4]+1, $timelist[3], $timelist[5]+1900); Today is 2/21/2007
Built-in functions: Review
Currtime since 1/1/1970: time() Local time and day elements: localtime() GMT time and day elements : gmtime() Math functions: sqrt(), exp(), log(), sin(), cos(), atan2()
PDT-009679 September 2009 Slide 125
PDT-009679 September 2009 Slide 128
20
Lab 6: Built-In Functions
Write a Perl script that will tell you the current date and time in the following format: Month day, year at hour:min {am|pm} Note: use the full month name, no leading zero on the day, four digit year, use AM/PM (not 24-hour), use two digit minutes Sample output: February 21, 2007 at 8:00 am
7. Regular Expressions
Patterns Wild cards Operators Matching data Altering data
PDT-009679 September 2009 Slide 129
PDT-009679 September 2009 Slide 133
Agenda
Part I
1. Introduction - Lab 1 2. Data Handling Break Lab 2a Lab 2b
What is a regular expression?
Part II
3. Quoting Text - Lab 3 4. Advanced Printing - Lab 4 / break 5. Flow Control - Lab 5 6. Built-In Functions - Lab 6
A string consisting of
Regular characters Special characters
Used to match strings Best explained through examples
PDT-009679 September 2009 Slide 131
PDT-009679 September 2009 Slide 134
Agenda
Part III
7. Reg. Expressions Lab 7a / break RE functions Lab 7b
Matching strings
Part IV
8. Input & Output - Lab 8 / break 9. Subroutines - Lab 9 10. Wrap-up
Exact string matching: eq
if ($name eq "Mike") { }
General string matching: =~
if ($name =~ /Mike/) { }
For which of the following values for $name would each return true?
Bill, Mike, Mickey, Michael, Mikey
PDT-009679 September 2009 Slide 132
PDT-009679 September 2009 Slide 135
21
Regular expressions
Check if a string matches a pattern:
if ($name =~ /Mike/) { }
Any single character: .
The most basic wildcard is a single dot (period), which matches any single character (except \n) Examples:
$str =~ /a./ aabb cats $str =~ /ca../ cable cats bats bca
Or if is does not match a pattern:
if ($name !~ /Mike/) { } if (!($name =~ /Mike/)) { }
abracadabra
PDT-009679 September 2009 Slide 136
PDT-009679 September 2009 Slide 139
Literal character matching
This expression is true if any portion of $str contains the sequence ZZYZX:
$str =~ /ZZYZX/
Limited set of characters: []
Similar to . but allows you to limit the set of characters to match Examples:
$str =~ /[ab]a/ aabbs a1b2 ball ab24 aaron 123dc2g $str =~ /[abc][123]/
Any of the following values of $str would return true:
ZZYZX, ZZZYZXX, ZZYZZYZXZX
PDT-009679 September 2009 Slide 137
PDT-009679 September 2009 Slide 140
Matching operators
Syntax:
string =~ /RE/ string !~ /RE/
Range of characters: [-]
Specify a range of consecutive characters by using a hyphen Examples:
$str =~ /[a-z]/ abc nose fred knows 123abc gnats $str =~ /n[a-cn-p][a-z]./
Returns: True or False Examples: $string = "apple";
$string =~ /ap/ $string =~ /ab/ $string !~ /pear/
PDT-009679 September 2009 Slide 138
TRUE FALSE TRUE
PDT-009679 September 2009 Slide 141
22
Inverse character set: [^]
Negate the character set by putting a carat at the start of the range Examples:
$str =~ /a[^b]/ abad phases act fase fact please $str =~ /[^abc][a-z]s/
Repetition operators
Repetition operators (+ * ?) are "greedy" in that they match as much text as possible in the string Examples:
$str = "root:X:0:0:Operator:/:/bin/csh"; $str =~ /.*:/ root:X:0:0:Operator:/:/bin/csh
PDT-009679 September 2009 Slide 142
PDT-009679 September 2009 Slide 145
Character set shortcuts
\d \D \w \W \s \S Any digit Any non-digit Any word character Any non-word character Any space Any non-space [0-9] [^0-9] [a-zA-Z_0-9] [^a-zA-Z_0-9] [ \t\n\f\r] [^ \t\n\f\r]
One or more of previous: +
Any item followed by a + matches that item one or more times Examples:
$str =~ /ab+c/ abc acaabbbbcc abbbbbcd abc1mnn44d $str =~ /1+mn+\d+/ x1111mnnnn3xx
PDT-009679 September 2009 Slide 143
PDT-009679 September 2009 Slide 146
Shortcut examples
$str =~ /a\d\db/ a45b ac 5 ts ac 5 ts ab45a45bc xx 7 y xx 7 y 9a99ba aa7 8 bbb aa7 8 bbb
Zero or more of previous: *
Any item followed by a * matches that item zero or more times Examples:
$str =~ /ab*c/ acc m abc mn9a acaabbbcc x1111mnnnn3xx abbbbbcd abc1mnn44d $str =~ /1*mn*\d*/
$str =~ /\w \d \w/
$str =~ /\w\s\d\s\w/
PDT-009679 September 2009 Slide 144
PDT-009679 September 2009 Slide 147
23
Zero or one of previous: ?
Any item followed by a ? matches that item zero or one time(s) Examples:
$str =~ /ab?c/ acc 98057 back aabcc 80055 $str =~ /800?5/
Grouping: ()
Use () to group multiple characters into one item; useful for repetition ops Examples:
$str =~ /(abc)+/ abc abcabcabcd
PDT-009679 September 2009 Slide 148
PDT-009679 September 2009 Slide 151
Greedy and non-greedy
By default, a wildcard modifier is "greedy" It will match as many characters as possible while still allowing the rest of the pattern to match If you want it to match the minimum number of times possible, follow the quantifier with a "?" *? match 0 or more times +? match 1 or more times ?? match 0 or 1 time {n}? match exactly n times {n,}? match at least n times {n,m}? match at least n but not more than m times
Alternatives: (|)
Separation of choices within a grouping Examples:
$str =~ /^(a|b)/ apple apple banana pine ant pineapple $str =~ /^(apple|pine)/
PDT-009679 September 2009 Slide 149
PDT-009679 September 2009 Slide 152
Anchors: ^ and $
Use ^ to anchor to the beginning and $ to anchor to the end of a string Examples:
$str =~ /^abc$/ abc allstars aabcc abcs abcc balls $str =~ /^a.*s/
Advanced Character Matching
\b match word boundary, i.e. the transition from word to non-word character, matches between \w and \W {m,n} match between m and n number of characters, only. \1 or $1 back reference, side effect of grouping with (), $1 through $9 and nesting allowed.
PDT-009679 September 2009 Slide 153
PDT-009679 September 2009 Slide 150
24
Regular expressions: Review
. [a-z] [^a-z] x* x+ x?
Any character Any in range Any not in range Zero or more One or more Zero or one
Lab7a: Sample output
Matching apple: RE matches "apple" RE does NOT match "aple" RE does NOT match "appl" RE does NOT match "pineapple" The rest are in the code at the bottom
PDT-009679 September 2009 Slide 157
^ $ () (x|y) \1-9 \d \D \s \S \w \W
Beginning / end anchors Grouping Alternatives Backreference Digit / non-digit Space / non-space Word character / non-wc
x{n,m} From n to m times
PDT-009679 September 2009 Slide 154
Lab7a: Regular expressions
Start with the code here:
http://perl.intel.com/intro/labs/lab7a.pl
15-minute Break
Use the chart on the next page Construct a RE that matches the word in the table, but does NOT match the other words given in the code Use all of the special characters listed and any other characters you need
PDT-009679 September 2009 Slide 155 PDT-009679 September 2009 Slide 158
Lab7a: Regular expressions
Word(s)
apple banana mississippi John Doe Error Error 007 Not found Error: file not fo
Regular expression functions
Matching Substitution
Instructions
First word none Last word Case insensitive First word First word, white space optional Preceeding zeros optional Case insensitive, end of line File is variable
Special characters
^ (as anchor) ( ) + [ ] { } $ (as anchor) \s + \b +
Tagging Other functions that use REs
\d
^ (as anchor)
^ (as anchor) \b \s ^ (as anchor) * [ ] [^] \d ^ \s +
$ (as anchor) .* $ (as anchors) +
x y (any numbers) Pinned to beginning and end
PDT-009679 September 2009 Slide 156
PDT-009679 September 2009 Slide 160
25
Match function
Syntax: m/RE/ Example:
if ($line =~ m/status.*ok/) { }
Substitution function
Syntax: s/RE/newtext/options Examples:
$str =~ s/a/b/; $str =~ s/^\s+//; $str =~ s/name:[\s]+\w+/bill/;
Note: m is the default function, so it is optional
PDT-009679 September 2009 Slide 161
PDT-009679 September 2009 Slide 164
Syntax variations
Examples:
if ($line =~ m/status.*ok/) { } if ($line =~ m:status.*ok:) { } if ($line =~ m[status.*ok]) { }
Substitution options
i, o, g same as match /re/newtext/e evaluate first Examples:
$str =~ s/abc/xyz/ig; $str =~ s/date/&getDate()/e;
PDT-009679 September 2009 Slide 162
PDT-009679 September 2009 Slide 165
Match options
/re/i matches irrespective of case /re/o compiles RE only once /re/g matches globally (multiple) Example:
while ($line =~ /Error/ig) { }
Match tagging
m/RE/; - use RE to define the tag reference variables $1 through $9 Stores in internal variables; available until next evaluation; reference from left to right; can nest tags Examples:
m/((\w+),\s*(\w+))/; $fn = $3; $ln = $2; $name = "$3 $2";
PDT-009679 September 2009 Slide 163
PDT-009679 September 2009 Slide 166
26
Substitute tagging
s/RE/newtext/; - use RE to define the tag reference variables $1 through $9 Stores in internal variables; available until next evaluation; reference from left to right; can nest tags Examples:
$name =~ s/((\w+),\s*(\w+))/$3 $2/;
Functions that use REs
grep() returns a sub-list of items that match the RE from a larger list Syntax: list2 = grep /RE/, list1; Examples:
@list = grep /^Error/, @list; @dfiles = grep (/^\./, @files); @files = grep(! /^\./, @files);
PDT-009679 September 2009 Slide 167
PDT-009679 September 2009 Slide 170
Evaluation example
@list = (1, 2, 3); foreach $item (@list) { $item =~ s/(\d+)/$1**2/e; print "$item "; } > 1 4 9
PDT-009679 September 2009 Slide 168
RE Functions: Review
m/RE/ is the default function Options: i (irrespective of case), o (compile once), g (global), e (evaluate first) s/RE/newtext/options will substitute use RE with grouping to define the tag reference variables $1 through $9 list2 = grep /RE/, list1; is a function that uses regular expressions
PDT-009679 September 2009 Slide 171
Compile once example
Use /o to tell Perl that $pat never changes in the while loop my $pat = [E|e]rror; while (<>) {print if /$pat/o;} This can be used to improve performance if the pattern doesnt change
PDT-009679 September 2009 Slide 169
Lab 7b: Regular Expressions
Given the standard array:
@errList = ("error", "Error", "Errors", "Error:", "No errors", "5 errors", "1 Error", "117 errors"); http://perl.intel.com/intro/labs/lab7b.pl
Part I:
Print out only the words that start with "error" or "error:" Do not print the word "Errors" Ignore case
PDT-009679 September 2009 Slide 172
27
Lab 7b: Regular Expressions
Part II:
Print out the number of errors when the format is either "No errors" or "number errors" Example: "5 errors" -> "There are 5 errors." Then print the total number of errors found.
Agenda
Part III
7. Reg. Expressions Lab 7a / break RE functions Lab 7b
Part IV
8. Input & Output - Lab 8 / break 9. Subroutines - Lab 9 10. Wrap-up
PDT-009679 September 2009 Slide 173
PDT-009679 September 2009 Slide 176
Lab 7b: Sample output
Part I: error Error Error: Part II: There are 0 error(s) There are 5 error(s) There are 1 error(s) There are 117 error(s) There are 123 errors total!
8. Input & Output
Standard filehandles File I/O Directory I/O Miscellaneous I/O
PDT-009679 September 2009 Slide 174
PDT-009679 September 2009 Slide 178
Agenda
Part I
1. Introduction - Lab 1 2. Data Handling Break Lab 2a Lab 2b
Standard filehandles
Part II
3. Quoting Text - Lab 3 4. Advanced Printing - Lab 4 / break 5. Flow Control - Lab 5 6. Built-In Functions - Lab 6
Filehandles are identifiers that represent a particular instance of opening a file until it is closed STDIN (default is keyboard) STDOUT (default is monitor) STDERR (default is monitor)
PDT-009679 September 2009 Slide 175
PDT-009679 September 2009 Slide 179
28
Opening files for reading: <
Syntax: open (filehandle, <, filename); Examples:
open ($fh, <, file.dat) || die "Error: $!"; open ($fh, <, /tmp/t.file) || die "Error: $!";
Reading from System Commands Syntax:
open (filehandle, -|, "cmd options");
Old style:
open (INH, $myfile") || die "Error"; What if $myfile is >weirdo<?
Examples:
open ($readh, -|, /bin/ps elf) or die Error: $!;
Returns a boolean value
false (0) if it failed true (not 0) if it succeeded This is the same for all uses of open
Old style:
open (FH, cmd opts|);
PDT-009679 September 2009 Slide 180
PDT-009679 September 2009 Slide 183
Opening files for writing: >
Syntax: open (filehandle, >, filename); Examples:
open ($fh, >, file.dat) || die -E- $!"; open ($fh, >, $outfile) || die -E- $!";
Controlling System Commands
Syntax:
open (filehandle, |-, "cmd options");
Examples:
open ($writeh, |-, /usr/lib/sendmail -t oi) or die Error: $!; # use: print $writeh "From:, To:, Cc:, Subject:; mail is sent upon close
If the file exists, it will be overwritten, unless To open a file in read/write mode, use +< To empty the file first, use +>
Old style:
open (FH, |cmd opts);
PDT-009679 September 2009 Slide 181
PDT-009679 September 2009 Slide 184
Opening files for appending: >>
Syntax: open (filehandle, >>, filename); Examples:
open || open || ($fh, >>, die "Error: ($fh, >>, die "Error: /tmp/tmp.file) $!"; $logfile) $!";
Closing a file
Syntax: close (filehandle); Examples:
close ($fh);
Old Style:
close (LOG);
If the file does not exist, it will be created
PDT-009679 September 2009 Slide 182 PDT-009679 September 2009 Slide 185
29
The diamond operator: <>
The easiest way to read one line of data is with the "diamond" operator Syntax: <filehandle> Examples:
chomp ($line = <STDIN>); open (INPUT, $infile); while (<INPUT>) {print;} close (INPUT);
PDT-009679 September 2009 Slide 186
Deleting files
Syntax: unlink filename [, ]; Examples:
unlink $tmpfile; unlink @filelist;
# from keyboard # print each # line in # $infile
This is generally preferable to a system call (system "rm $tmpfile";) since those are platform-specific
PDT-009679 September 2009 Slide 189
The diamond operator: <>
If no filehandle is given, the files listed as command line arguments (@ARGV) are read; standard input is read if no files are listed Syntax: <> Examples:
chomp ($line = <>); # returns input line while (<>) {print;} # assigns to $_
Renaming files
Syntax: rename oldname, newname; Examples:
rename $oldname, $newname; rename ($tmp, "$tmp.old");
This is generally preferable to a platform-specific system call
PDT-009679 September 2009 Slide 187
PDT-009679 September 2009 Slide 190
File test operators
Used to test attributes of a file
-f Does the file exist? -x Is the file executable? Returns a boolean value Useful if you may want to open a file in a different mode depending on result
Links in UNIX
A symlink (soft link) "points" to a file, directory or other symlink A hard link is another name for an existing file or directory UNIX examples:
> ln s xyz mylink (soft) > ln xyz mylink (hard)
Example:
if (! f $filename) { print "$filename not found!\n"; }
PDT-009679 September 2009 Slide 188
PDT-009679 September 2009 Slide 191
30
Creating a link
Syntax:
hard: link filename, linkname; soft: symlink filename, linkname;
File statistics
Syntax: stat (filename or filehandle); Returns an array of values related to the argument
0 Device where the file resides 1 Inode number 2 File mode (type and permissions) 3 Number of hard links to the file 4 User ID of owner 5 Group ID of group 6 The device type 7 The file's size (bytes) 8 When the file was last read 9 When the file was last edited 10 When inode was last changed 11 Preferred block size for I/O 12 The number of blocks allocated
Examples:
link ($theFile, $newLink); symlink "/tmp/datafile.dat", $newLink;
PDT-009679 September 2009 Slide 192
PDT-009679 September 2009 Slide 195
Reading a soft link
Will return what the symlink points to Syntax: readlink(linkname); Examples:
symlink("myfile", "mylink"); $name = readlink("mylink"); print "name = $name\n"; name = myfile
File statistics
Example:
@data = stat ("datafile.dat");
Example:
($dev, $inode, $mode, $nlink, $uid, $gid, $type, $size, @rest) = stat(INPUT);
PDT-009679 September 2009 Slide 193
PDT-009679 September 2009 Slide 196
Autoflush: $|
Output is buffered by default, turn this off by setting autoflush ($|) to true Example:
$| = 1; print "This is a test\n";
The process ID: $$
The current process ID is the variable $$ and it is unique across all processes on the system at that moment The PID can be used as a seed value for a random number generator:
srand(time() ^ ($$ + ($$ <<15))); $val = int(rand(10));
This is especially useful if text is being printed without the \n and computations are done before more printing on the same line
PDT-009679 September 2009 Slide 194
PDT-009679 September 2009 Slide 197
31
The process ID: $$
The PID can also be used to name a unique temporary file if more than one person could be running a script at the same time:
$tmp = "/tmp/temp.file.$$";
Directory I/O
Opening directories Reading directories Closing directories Making directories Changing the working directory
PDT-009679 September 2009 Slide 198
PDT-009679 September 2009 Slide 202
File I/O example
if (! f $infile) { die "$infile not found\n"; } open(my $inh, <, $infile) or die "Can't open $infile: $!"; while(my $line = <$inh>) { chomp $line; $line =~ s/#.*//; $line =~ s/^\s+|\s+$//g; next if $line eq ; # remove carriage return # remove line comment # remove white space # ignore blanks
Opening directories
Syntax: opendir (dirhandle, dirPath); Examples:
opendir (my $dirh, $dirname); opendir (my $tmph, "/tmp");
($key, $value) = split(/\s+/, $line, 2); $datalist{$key} = $value; } close($inh);
Note: File handles and directory handles are similar
PDT-009679 September 2009 Slide 199
PDT-009679 September 2009 Slide 203
File I/O: Review
Opening a file for reading (<), writing (>) or appending (>>): open (filehandle, how, filename); Reading a line: $line = <filehandle>; Writing a line: print filehandle "text"; Closing a file: close (filehandle);
Reading directories
Syntax: readdir (dirhandle); Returns: list of file names (relative) Examples:
opendir (my $dirh, "/tmp"); @list = readdir($dirh); # all files @list2 = grep(!/^\./, @list) # non-hidden files
PDT-009679 September 2009 Slide 200
PDT-009679 September 2009 Slide 204
32
Closing directories
Use close for files and closedir for directories, though both are the same Syntax: closedir (dirhandle); Example:
closedir ($dirh);
Directory I/O: Review
Use opendir (dirhandle, dirPath); to open a directory Use readdir (dirhandle); to read the contents of the directory that has been opened Use closedir (dirhandle); to close a directory Use mkdir (dirname, permissions); to make a directory where permissions is an octal number Use chdir (dirname); to change the CWD
PDT-009679 September 2009 Slide 205
PDT-009679 September 2009 Slide 208
Making directories
Syntax: mkdir (dirname, permissions); Examples:
mkdir ($dirname, 0777); mkdir ("/tmp/scripts", 0755);
Miscellaneous I/O
Changing permissions Changing ownership and groups Globbing
Note: don't forget to add the 0 on the front of the permissions bits to make them octal numbers; the umask will affect the final permissions
PDT-009679 September 2009 Slide 206
PDT-009679 September 2009 Slide 210
Changing the CWD
Syntax: chdir (dirname); Examples:
use Cwd; # portable get cwd chdir ("/home/jdoe"); my $cwd = cwd(); print "curr dir = ", $cwd, "\n"; chdir ("/usr/bin"); $cwd = cwd(); print "curr dir = ", $cwd, "\n"; curr dir = /home/jdoe curr dir = /usr/bin
Changing permissions
Syntax: chmod permissions, filename [, ]; Examples:
chmod 0755, $mydir; chmod 0777, @filelist; chmod 0544, $filename, $dirname;
Note: don't forget to add the 0 on the front of the permissions bits to make them octal numbers; is not recursive in directories
PDT-009679 September 2009 Slide 207
PDT-009679 September 2009 Slide 211
33
Changing ownership
Syntax: chown uid, gid, filename [, ]; Examples:
chown $uid, $newGid, $myfile; chown $newUid, $newGid, @fileList; chown $uid, $newGid, $filename;
Globbing hidden files
Globbing normally skips hidden files:
@files = <*>; # all except hidden
To see hidden files, you must specify them explicitly. In UNIX, hidden files have filenames beginning with a period:
@hiddenfiles = <.*>; # only hidden
Note: only the superuser (root) can change the owner; this function can be used to change the group
Note: that will include "." (current) and ".." (parent) directories.
PDT-009679 September 2009 Slide 212
PDT-009679 September 2009 Slide 215
Globbing with <>
Another way to get a list of files from a directory is to use the <> operator with a wildcard character Syntax: <globstring> Examples:
@textfiles = <*.txt>; while (<*.data>) { } # gets .txt files # while *.data
Miscellaneous I/O: Review
Changing permission (modes) of files or directories: chmod modes, filename; Changing ownership and group of files or directories: chown userid, groupid, filename; Globbing (listing filenames):
@filenames = <*.txt>; @filenames = glob("*.txt");
PDT-009679 September 2009 Slide 216
Note: the globstring may contain shell wildcards, not regular expressions
PDT-009679 September 2009 Slide 213
Globbing function
Another way is to use the glob function Syntax: glob(globstring) Examples:
@textfiles = glob("*.txt"); while (glob("*.data")) { }
Lab 8: Input & Output
There are three parts to this lab: Open and read a directory Read a file Write to a file
Note: the globstring may contain shell wildcards, not regular expressions
PDT-009679 September 2009 Slide 214 PDT-009679 September 2009 Slide 217
34
Lab 8: Directory reading
Create an empty directory called lab8 Download the following files into lab8
http://perl.intel.com/intro/labs/lab8a.txt http://perl.intel.com/intro/labs/lab8b.txt
Lab 8: Sample output Part A: lab8a.txt lab8b.txt Part B: 5 3 2 3 Candy Gum Key chain T-shirt
Write a routine to list all of the files that end with ".txt" in the current directory
PDT-009679 September 2009 Slide 218
PDT-009679 September 2009 Slide 221
Lab 8: File reading
Start with the first part of this lab Read the contents of each ".txt" file Store each unique item into a hash table Ignore case (i.e., CANDY = candy) Keep a count of each unique item
15-minute Break
PDT-009679 September 2009 Slide 219
PDT-009679 September 2009 Slide 222
Lab 8: File writing
Start with the second part of this lab Open a file called "results" in lab8 Print a list of each unique item name into "results" Print the count of each item next to the name The item name should have its first letter capitalized The counts should align nicely, for example: Candy Shirt 4 2
9. Subroutines
Writing subroutines Local variables Passing arguments Returning values
PDT-009679 September 2009 Slide 220
PDT-009679 September 2009 Slide 224
35
Writing subroutines
A subroutine
is a section of code has a name can have data passed into it can have local variables can pass data back to the caller is great for organizing and reusing code
Subroutine shortcuts
The example subroutine could be shortened to: sub multiply {$_[0] * $_[1];} This will run a bit faster, but isn't clear $_[0] is the first element of the @_ array, which is the first argument passed into the subroutine $_[1] is the second element passed in
PDT-009679 September 2009 Slide 225
PDT-009679 September 2009 Slide 228
Example subroutine
sub multiply { my ($value, $multiplier) = @_ ; return $value * $multiplier; } Calling this subroutine: $answer = &multiply(27, 30); print "$answer times two is ", &multiply($answer, 2), "\n";
Local variables
Variables in Perl are global by default You can declare local variables with my Including use strict; will enforce this. Variables declared with my are only visible in the block in which it is defined (and its subblocks) Inner blocks may define the same variable name without harming outer ones
PDT-009679 September 2009 Slide 229
PDT-009679 September 2009 Slide 226
Notes about subroutines
With subroutines, you can
skip data type declarations for variables being passed in or returned ignore return value since the value of the last evaluated expression is returned by default pass the wrong number of argument, though this may cause problems (function prototypes fixes this) use and modify global variables
PDT-009679 September 2009 Slide 227
Local variables example
sub ListAvailableProduce { my @produce = GetAllProduce(); foreach my $p (@produce) { print "$p\n" if $stock{$p} > 0; } } The scope (visibility) of $p is only within the foreach loop's code block; %stock is global
PDT-009679 September 2009 Slide 230
36
Local variables example
my $num = 5; { my $num = 10; # local this block print "inner num = $num\n"; } print "outer num = $num\n"; inner num = 10 outer num = 5
Passing argument are unwound
Why doesn't this work? sub sumparallel { my(@a,@b) = @_; print "\@a contains: @a\n"; print "\@b contains: @b\n"; for (my $i = 0; $i <= $#a; $i++) { $a[$i] += $b[$i]; } @a; }
PDT-009679 September 2009 Slide 234
PDT-009679 September 2009 Slide 231
Passing arguments
Argument are passed by value sub sumArray { my (@a) = @_; my $total; my ($i); foreach $i (@a) {$total += $i;} print "$total\n"; }
Passing arguments in
This argument order is OK: sub payrange { my ($low, $high, @people) = @_; } This argument order is NOT: sub payrange { my (@people, $low, $high) = @_; }
PDT-009679 September 2009 Slide 232
PDT-009679 September 2009 Slide 235
Calling subroutines
Syntax:
subName(argList[, ]); subName argList[, ]; &subName(argList[, ]); &subName argList[, ];
Passing hashes as arguments
Hashes are passed similar to arrays They are unwound into key/value pairs A hash table can be passed at the end of the argument list
Examples:
&subArray(@list); subArray $a,$b,$c;
PDT-009679 September 2009 Slide 233 PDT-009679 September 2009 Slide 236
37
Passing hashes example
sub price { my ($over, %prices) = @_; foreach my $item (keys %prices) { if ($prices{$item} > $over) { print "price of $item is"; print " $prices{$item}\n"; } } }
PDT-009679 September 2009 Slide 237
Returning multiple values
sub listcustomers() { return ("jane", "john"); } -orsub listcustomers() { my @customers = ("jane"); push @customers, "john"; return @customers; # as an array } # as list
PDT-009679 September 2009 Slide 240
Prototypes
Prototypes are a way to enforce quantity and types of arguments to prevent problems Syntax: sub name(prototype); Examples:
sub multiply($$); sub proc_rpt($$;$$$); # 2 scalars # 2-5 scalars
Returning hashes
sub listcustomers() { # as a hash return (owner=>"jane", admin=>"john"); } -orsub listcustomers() { my %results; my $name; return ($name, %results); } # the hash # must be # last
Where: $ is scalar, @ is array, and % is hash
PDT-009679 September 2009 Slide 238
PDT-009679 September 2009 Slide 241
Returning values
All subroutines return something, but it doesn't have to be used Syntax: return value; Example:
sub multiple { my ($a, $b) = @_; return ($a * $b); }
Examples of calling subroutines
$x = &mult($a, $b); ($midX, $midY) = &midpt($x1, $y1, $x2, $y2); @pt = &midpt(@coords); ($name, @list) = &getData($infile); &processData($x, $y, @list);
PDT-009679 September 2009 Slide 242
Note: in this example "return" is optional
PDT-009679 September 2009 Slide 239
38
References and Subroutines
As weve seen, Perl subroutines pass arguments by value and unwind arrays and hashes. There is a better way using references. A reference is like a pointer in C or C++, and stores an address to a data structure. References are always scalars.
PDT-009679 September 2009 Slide 243
Subroutines: Review
Arguments are passed in via @_ Array and hash data are "unwound" and should be passed at the end of the list (or use references) Any number of args can be passed and any number of vals can be returned The default return value is the last expression value in the subroutine The return value can be ignored
PDT-009679 September 2009 Slide 246
References example
$aref = \@array; $href = \%hash; &refex($aref, $href); sub refex { my $ref1 = $_[0]; my $ref2 = $_[1]; while (($k, $v) = each %$ref2) { push (@$ref1, $v); } }
Lab 9: Subroutines, part A
Download the starting code:
http://perl.intel.com/intro/labs/lab9a.pl
Write the subroutine "askproduct" which should ask the user (from STDIN) for the name of a single product Write the subroutine "printproduct" which should accept any number of product names as arguments and print one per line:
Item: productname is in stock
PDT-009679 September 2009 Slide 244
PDT-009679 September 2009 Slide 247
References with prototypes
# Solves the unwinding problem with arrays sub sumparallel(\@\@) { my($ar, $br) = @_; my @a = @$ar; my @b = @$br; print "\@a contains: @a\n"; print "\@b contains: @b\n"; for (my $i = 0; $i <= $#a; $i++) { $a[$i] += $b[$i]; } return @a; } @z = sumparallel(@x, @y);
PDT-009679 September 2009 Slide 245
Lab 9: Subroutines, part B
Download the starting code:
http://perl.intel.com/intro/labs/lab9b.pl
Write a subroutine "listfruit" that accepts the hash of fruit/quantity pairs given and returns a list of just the fruit names Use local variables within the subroutine Write the main code in the same file to call your subroutine and print what it returns
PDT-009679 September 2009 Slide 248
39
Lab 9: Sample output
Part A: Press ENTER key alone to stop typing data. Please enter a product: Cheese Item: Cheese is in stock Please enter a product: Milk Item: Milk is in stock Please enter a product: Item: Ornaments is in stock Item: Wine Glasses is in stock Part B: apple banana orange
PDT-009679 September 2009 Slide 249
Advanced Perl
The advanced course material is on-line Find it at http://perl.intel.com The best way to learn Perl is to experiment and look at code others have written
PDT-009679 September 2009 Slide 253
10. Wrap-up
References Advanced Perl Course feedback
Course feedback
Please fill-out the Intel University course feedback survey on-line If you have additional comments, please email them to the course owner
[email protected]
PDT-009679 September 2009 Slide 251
PDT-009679 September 2009 Slide 254
References
In UNIX at Intel: perldoc f <function> or perldoc <module> Lots of books: look at Amazon.com Lots of web sites: go to perldoc.perl.org or do a Google search for "Perl" Lots of expertise at Intel, just ask around and someone will help; look for people with Perl books on their desks!
PDT-009679 September 2009 Slide 252
40