Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004
Lenwood Heath, Virginia Tech, Fall, Perl Background Practical Extraction and Report Language (Perl) Created by Larry Wall, mid-1980's Language combining capabilities of shell programming, awk, grep, lex, sed, and a number of other UNIX utilities Powerful, complex scripting language We learn just a bit!
Lenwood Heath, Virginia Tech, Fall, Scalars Basic data type in Perl is scalar Most scalar values are numbers or character strings Programmer forces interpretation of a scalar value as a number or string by operations used Special scalar value undef is neither number nor string, just "undefined"
Lenwood Heath, Virginia Tech, Fall, Numbers Integers: 45, 974, -892, 0 Real numbers: 45.0, , , 2.5e-3 Octal: 055 Hexadecimal: 0x2d Binary: 0b101101
Lenwood Heath, Virginia Tech, Fall, Numeric Operators Arithmetic: 4+5, 9-7, -9*-3, 10/3 Modulus (remainder): 102 % 9 is 3 Comparisons:, =, ==, != Spaceship: ( -1, 0, or 1 ) Logical And &&and Or ||or Not !not
Lenwood Heath, Virginia Tech, Fall, Strings Zero or more characters: "", "one" No concept of null-termination Single (literal) quotes 'tab\tnl\n'tab\tnl\n 9 Double (interpreted) quotes "tab\tnl\n"tab_nl_ 7 Escaped double quote "Here's a double quote \"."
Lenwood Heath, Virginia Tech, Fall, String Operators Concatenation: "Learning "."Perl" Comparisons: lt, gt, le, ge, eq, ne Index: position of a substring in a string index('Learning Perl','rni') 3 index("Learning Perl",'nr')-1 Substring: select a substring substr('Learning Perl',1,2)ea String positions start at 0
Lenwood Heath, Virginia Tech, Fall, Scalar Variables Scalar variable identifier begins with $ $colors = "red green blue"; $count = $count+1; Shortcuts and alternatives: $colors.= ' purple'; $count += 1; $count++; Interpolation: "Count is $count.\n"
Lenwood Heath, Virginia Tech, Fall, Lists Sequence of scalars (5.7,"house\tbarn",'-9.2') qw shortcut — equivalent lists: ("VT","UNC","NCSU","UVa","Wake") qw/ VT UNC NCSU UVa Wake / qw{ VT UNC NCSU UVa Wake }
Lenwood Heath, Virginia Tech, Fall, Arrays An array is a list-valued variable Array identifier = qw(red green blue); Array element reference: $id[index] $colors[2]# Value is 'blue' $colors[8] = 'purple'; substr($colors[1],0,3)# 'gre'
Lenwood Heath, Virginia Tech, Fall, Simple Perl Script #!/usr/bin/perl $dotted = `ping -c 1 $dotted`; Result: cs2204]$ dot_ping www cslab vt edu PING owlstation.cs.vt.edu ( ) 56(84) bytes of data. 64 bytes from : icmp_seq=1 ttl=64 time=0.536 ms
Lenwood Heath, Virginia Tech, Fall, Numeric Functions ASCII code — ord('?') is 63 ASCII character — chr(63) is '?' Absolute value — abs(-11) is 11 Integer value — int( ) is 295 Square root — sqrt(16) is 4 Natural logarithm — log( ) is 5.69 Integer value — int( ) is 295 Random number — rand(10) was
Lenwood Heath, Virginia Tech, Fall, String Functions Length — length('Learning Perl') is 13 Find substring — indexrindex Extract substring — substr Lower case — lc('9Jp.iR') is ' 9jp.ir' Upper case — uc('9Jp.iR') is ' 9JP.IR' Remove last character — chop($word); Remove newline at end — chomp($line);
Lenwood Heath, Virginia Tech, Fall, Array Functions Stack; top on the right 2 3/; to 'top' to returns 'top' Stack; top on the left 7 a/; to 'a' to returns 'a' Reverse a list Reverse qw/8 l a p 7/ returns qw/7 p a l 8/
Lenwood Heath, Virginia Tech, Fall, Array Functions (Continued) Array to = qw/9b4 x.; pbj/; is '9b4--x.;--pbj' String to array split(/b/,$words) is qw/9 4--x.;--p j/ Sorting — lexicographic order sort qw/red green blue/ returns qw/blue green red/ Sorting — numerical order sort { $a $b } (94,-1,55) returns (-1,55,94)
Lenwood Heath, Virginia Tech, Fall, Input Text files Sequence of lines, each terminated by a newline File access by a file handle — standard input STDIN Read a line $line = ; Read remaining = ;
Lenwood Heath, Virginia Tech, Fall, Output Standard output — STDOUT print 'A line to', " standard output\n"; Standard error — STDERR print STDERR "Arguments OK\n"; warn "Unable to find config file.\n"; die "Unexpected system error";
Lenwood Heath, Virginia Tech, Fall, if Statementt if (condition) { statements; } elsif (condition) { statements; } else { statements; } if (not defined $ARGV[0]) { die "Usage:\n\tpaint [COLOR]\n"; }
Lenwood Heath, Virginia Tech, Fall, while Statementt while (condition) { statements; } $term = $ln = 0; while ($line = ) { chomp $line; $ln++; if ($line eq $term) { print "Term $term on line $ln.\n"; break; }
Lenwood Heath, Virginia Tech, Fall, for Statement for (initialization; test; increment) { statements; } for ($i = 0; $i < length($line); $i++) { if (lc($char) eq substr($line,$i,1)) { print "Character $char found.\n"; last; } elsif (uc($char) eq substr($line,$i,1)) { print "Character $char found.\n"; last; }
Lenwood Heath, Virginia Tech, Fall, foreach Statement foreach $x { # execute for each element of list statements; } foreach $color (qw/red green blue purple/) { print "$color is a color!\n"; }
Lenwood Heath, Virginia Tech, Fall, Sample Script #!/usr/bin/perl $word = while ($line = ) { if (index($line,$word) > -1) { } else { } foreach $line { print $line; }
Lenwood Heath, Virginia Tech, Fall, Topics for Next Lecture Subroutines Regular expressions Hashes File input/output File tests Invoking UNIX commands