Beruflich Dokumente
Kultur Dokumente
Perl Basics Data Types Operators Control Structures Lists Hashes Sub Routines
8/12/2009
What is Perl?
Practical Extraction & Reporting Language Perl is a High-level Scripting language Faster than sh or csh, slower than C More powerful than C, and easier to use No need for sed, awk, head, wc, tr, Compiles at run-time Available for Unix, Linux, Mac etc. Best Regular Expressions on Earth
Perl Basics
Leading spaces on a line are ignored Statements are terminated with a semicolon. Anything after a hash sign (#) is ignored except in strings. A string is basically a series of characters enclosed in quotes Spaces, tabs, and blank lines outside of strings are irrelevant-one space is as good as a hundred.
Perl Basics
File Naming Scheme
o o o
filename.pl (programs) filename.pm (modules collection of useful perl subroutines) filename.ph (old Perl 4 header file)
8/12/2009
8/12/2009
Data Types
Integer literal o 25 750000 1_000_000_000 o 8#100 16#FFFF0000 Floating Point literal o 1.25 50.0 6.02e23 -1.6E-8 String literal o hi there hi there,$name o print Text Utility, version $ver\n; o Use double quote for special char interpretation
8/12/2009
Data Types
Boolean
o o
8/12/2009
8/12/2009
8/12/2009
Operators
Exponentiation: **
$cube = $value ** 3; $cuberoot = $value ** (1.0/3);
Bit-level Operations
left-shift: << $val = $bits << 1; right-shift: >> $val = $bits >> 8;
8/12/2009
Operators
Assignments
o
o o
Increment: ++
$counter++ ++$counter
8/12/2009
Operators
Boolean (against bits in each byte)
o o o
Boolean Assignment
o
&= |= ^=
$picture &= $mask;
8/12/2009
Operators
Logical (expressions)
o o o o o o o
&& And operator | | Or operator ! Not operator AND And, low precedence OR Or, low precedence NOT Not, low precedence XOR Exclusive or, low precedence
8/12/2009
Operators
expr1 || expr2
expr1 is evaluated. expr2 is only evaluated if expr1 was false.
Examples
open () || die couldnt open file; $debug && print users name is $name\n;
8/12/2009
Operators
Modulo: %
o
Multiplier: x
o
o
print ride on the , choo-x2, train; (prints ride on the choo-choo-train) $stars = * x 80;
Assignment: %= x=
8/12/2009
Operators
String Concatenation: . .=
o o o o
$name = Uncle . $space . Sam; $cost = 34.99; $price = Hope Diamond, now only \$; $price .= $cost;
8/12/2009
Conditionals Operators
numeric string
Equal: == eq Not Equal != ne Less/Greater Than: < > lt gt Less/Greater or equal: <= >= le ge Zero and empty-string means False All other values equate to True
8/12/2009
Conditionals Operators
numeric string
Comparison: <=> cmp
Results in a value of -1, 0, or 1 Logical Not: !
o
Grouping: ( )
8/12/2009
Scalar Variables
Scalar o $num = 14; o $fullname = Cass A. Nova; o Variable Names are Case Sensitive o Variable name length is limited to 255 chars o Underlines Allowed: $Program_Version = 1.0;
8/12/2009
$x = "hello world"; chop($x); # $x is now "hello worl" $a = "hello world\n"; chomp ($a); # $a is now "hello world" chomp ($a); # aha! no change in $a @stuff = ("hello\n","world\n","happy days"); chomp(@stuff); # @stuff is now ("hello","world","happy
days")
8/12/2009
o o o o o
$a = <STDIN>; # get the text chomp($a); # get rid of that pesky newline A common abbreviation for these two lines is: chomp($a = <STDIN>); @a = <STDIN>; # read standard input in a list context
8/12/2009
undef value
What happens if you use a scalar variable before you give it a value? Nothing serious, and definitely nothing fatal Variables have the undef value before they are first assigned This value looks like a zero when used as a number, or the zero-length empty string when used as a string
8/12/2009
Control Structures
if statement - first style o if ($porridge_temp < 40) { print too hot.\n; } elsif ($porridge_temp > 150) { print too cold.\n; } else { print just right\n; }
8/12/2009
Control Structures
statement if condition;
print \$index is $index if $DEBUG;
o o
o
unless is a reverse if
8/12/2009
Control Structures
8/12/2009
Control Structures
8/12/2009
Control Structures
while loop
o
8/12/2009
Control Structures
8/12/2009
Control Structures
Bottom-check Loops
o o
8/12/2009
Control Structures
Loop Controls
o o o
next operator - go on to next iteration redo operator - re-runs current iteration last operator - ends the loop immediately
8/12/2009
Control Structures
o
8/12/2009
Control Structures
Loop Controls Example
o
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;
8/12/2009
No Switch Statement?!?
8/12/2009
List Variables
List (one-dimensional array) A list is ordered scalar data The smallest array has no elements, while the largest array can fill all of available memory List literal o (1,2,3) # array of three values 1, 2, and 3 o ("fred",4.5) # two values, "fred" and 4.5
8/12/2009
List Variables
o o o o o o o
@memory = (16, 32, 48, 64); @people = (Alice, Alex, Albert); First element numbered 0 (can be changed) Single elements are scalar: $names[0] = Ferd; Slices are ranges of elements
@guys = @people[1..2];
8/12/2009
List Variables
List with lots of words
o o
@a = ("fred","barney","betty","wilma"); Alternative
@a = qw(fred barney betty wilma); # better! qw quoted words
8/12/2009
List Variables
Removing an element in list
o o
($e,@fred) = @fred; # remove first element of @fred ($a) = @fred; # $a gets first element
Getting first element As an array element access o @fred = (7,8,9); o $b= $fred[0];#give7to$b(firstelementof @fred) o $fred[0] = 5; # now @fred = (5,8,9) o $fred[2]++;# increment third element of @fred
8/12/2009
List Variables
Slices o @fred[0,1]; # same as ($fred[0],$fred[1]) o @who = (qw(fred barney betty wilma))[2,3]; Assigning values beyond array subsrcipt o @fred = (1,2,3); o $fred[3] = "hi"; # @fred is now (1,2,3,"hi") o $fred[6] = "ho";
# @fred is (1,2,3,"hi",undef,undef,"ho")
o
o
Use $#fred to get the index value of the last element of @fred A -ve subscript on an array counts back from end
8/12/2009
o o o
@mylist = (1,2,3); push(@mylist,4,5,6); # @mylist = (1,2,3,4,5,6) $oldvalue = pop(@mylist); # removes the last element of @mylist
The pop function returns undef if given an empty list
8/12/2009
8/12/2009
8/12/2009
Hash Variables
Hash (associative array) Hash is like an array whose index values are any arbitrary scalars The elements of a hash have no particular order A hash variable name is a percent sign (%) followed by a letter, followed by zero or more letters, digits, and underscores
8/12/2009
Hash Variables
$fred{"aaa"} = "bbb"; # creates key "aaa", value "bbb" $fred{234.5} = 456.7; # creates key "234.5", value 456.7 %var = { name => paul, age => 33 }; Single elements are scalar
print $var{name}; $var{age}++; o
o o o o
8/12/2009
Hash Variables
8/12/2009
Hash Variables
In a scalar context, the keys function gives the number of elements (key-value pairs) in the hash
o o o
8/12/2009
Hash Variables
The value function
o
The values(%hashname) function returns a list of all the current values of the %hashname
%lastname = (); # force %lastname empty $lastname{"fred"} = "flintstone"; $lastname{"barney"} = "rubble"; @lastnames = values(%lastname); # grab the values At this point @lastnames contains either ("flintstone", "rubble") or ("rubble", "flintstone").
8/12/2009
Hash Variables
On each evaluation of this function for the same hash, the next successive key-value pair is returned until all the elements have been accessed Step thru each key value pairs
while (($first,$last) = each(%lastname)) { print "The last name of $first is $last\n"; }
8/12/2009
Hash Variables
The delete function o Perl provides the delete function to remove hash elements %fred = ("aaa","bbb",234.5,34.56); # give %fred two elements delete $fred{"aaa"}; # %fred is now just one key-value pair
8/12/2009
foreach (keys (%hash)) { } @array = values (%hash); while (@pair = each(%hash)) { print element $pair[0] has $pair[1]\n; }
8/12/2009
8/12/2009
Subroutines (Functions)
Defining a Subroutine
o o
Last value processed is the return value (could have left out word return, above)
8/12/2009
Subroutines (Functions)
Calling a Subroutine
o o o o
&subname; # no args, no return value &subname (args); retval = &subname (args); The & is optional so long as
subname is not a reserved word subroutine was defined before being called
8/12/2009
Subroutines (Functions)
Passing Arguments
o o
8/12/2009
Subroutines (Functions)
Notes
o o
8/12/2009
push( ), pop( )- stack operations on lists shift( ),unshift( ) - bottom-based ops split( ) - split a string by separator
o o
8/12/2009
8/12/2009
Special Variables
$0 $/ $\ $, $ $. name of the program running input record separator (\n) output record separator (none) output field separator for print(none) Array Elements separator input line number
8/12/2009
Special Variables
$[ first element index number (0) $! system error message (related to $ERRNO variable) $$ Perl Process ID $^O Operating Systems name
8/12/2009
Special Variables
@_ list of all the arguments @INC library directories (require, use) @ARGV command line args list %ENV Environment variables
8/12/2009
File Functions
Much faster than system()
o o o o o o o
unlink file; - deletes a file rename oldfile, newfile; rmdir directory; link existingfile, newfile; symlink existingfile, newfile; chmod 0644, file1, file2, ; chown $uid, $gid, @filelist;
yourprog -a somefile
$0 is yourprog $ARGV[0] is -a $ARGV[1] is somefile
open (FILEHANDLE, $filename) || die \ open of $filename failed: $!; while (<FILEHANDLE>) { chop $_; # or just: chop; print $_\n; } close FILEHANDLE;
open (FILEHANDLE, >$filename) || die \ open of $filename failed: $!; while (@data) { print FILEHANDLE $_\n; # note, no comma! } close FILEHANDLE;
open (FILEHANDLE, ps aux |) || die \ launch of ps failed: $!; while (<FILEHANDLE>) { chop; print $_\n; } close FILEHANDLE;
open (FILEHANDLE, | mail frank) || die \ launch of mail failed: $!; while (@data) { print FILEHANDLE $_\n; } close FILEHANDLE;
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?
Pattern Matching
A pattern is a sequence of characters to be searched for in a character string
syntax: string =~ pattern Returns true if it matches, false if not. o Example: match abc anywhere in string:
o o
if ($str =~ /abc/) { }
Pattern Matching
Symbols with Special Meanings
asterisk * - zero or more occurrences plus sign + - one or more occurrences question mark ? - zero or one occurrences carat ^ - anchor to begin of line dollar sign $ - anchor to end of line Dot . - any character other than \n quantity {n,m} - between n and m occurrences (inclusively)
[A-Z]{2,4}means ,3 ,2or 4 upper-caseletters.
Pattern Matching
\b and \B Word boundary pattern Anchors /d[^eE]f/ Exclude Character [] Character-Range Escape Sequences \d Any digit [0-9] \D Anything other than a digit [^0-9] \w Any word character [_0-9a-zA-Z] \W Anything not a word character [^_0-9a-zA-Z] \s White space [ \r\t\n\f] \S Anything other than white space [^ \r\t\n\f]
Pattern-Matching Options
Even we can specify the choices using |. For example /abc | def / /de{1,3}f/ : matches d, followed by one, two, or three occurrences of e g Match all possible patterns i Ignore case m Treat string as multiple lines o Only evaluate once s Treat string as single line x Ignore white space in pattern
Substitution Operator
Syntax : s/pattern/replacement/ $name =~ s/ASU/Arizona State University/; s/abc// s/[abc]+/0/
Translation Operator
Syntax : tr/string1/string2/ c Translate all characters not specified d Delete all specified characters s Replace multiple identical output characters with a single character
sed: s/xxx/yyy/g vi: :%s/xxx/yyy/g perl: s/xxx/yyy/g; tr: tr [A-Z] [a-z] (platform
dependent)
tr - translations
o
$1==bananas: {total += $2} END {print total} o perl: BEGIN {$total=0} if (/^bananas:\s+(\d+)/) {$total += $1} END {print $total\n}
Agenda
References & Records Packages & Modules Building CPAN Modules
8/12/2009
8/12/2009
References
The scalar value that contains the memory address is called a reference
8/12/2009
Anonymous Data
Reference to Named Anonymous Scalar \$Scalar \do {my $anon} Array \@array [ List ] Hash \%hash { List } Code \&fun sub {code }
8/12/2009
References to Scalars
To create a reference to a scalar variable, use the backslash operator: $scalar_ref = \$scalar; To create a reference to an anonymous scalar value: undef $anon_scalar_ref; $$anon_scalar_ref = 15; check whether $someref contains a simple scalar reference if (ref($someref) ne 'SCALAR') { die "Expected a scalar reference, not $someref\n"; }
8/12/2009
References to Arrays
To get a reference to an array: $aref = \@array; $anon_array = [1, 3, 5, 7, 9]; $anon_copy = [ @array ]; @$implicit_creation = (2, 4,8, 10); To get the elements of an array : $two = $aref->[2]; $two = $$aref[2]; Number of items in that referenced array : $last_idx = $#$aref; $num_items = @$aref;
8/12/2009
References to Arrays
To iterate through the entire array, use either foreach loop or a for loop: foreach $item ( @{$array_ref} ) { # $item has data } for($idx=0;$idx <=$#{ $array_ref }; $idx++) { # $array_ref->[$idx] has data }
8/12/2009
References to Hashes
To get a hash reference: $href = \%hash; $anon_hash={ "key1" => "value1",..}; $anon_hash_copy = { %hash }; To dereference a hash reference: %hash = %$href; $value = $href->{$key}; @slice=@$href{$key1,$key2}#no arrow! @keys = keys %$href;
8/12/2009
References to Hashes
To iterate the hash reference :
foreach $key ( keys %$href ) { print "$key => $href->{$key}\n"; }
8/12/2009
References to Functions
To get a code reference: $cref = \&func; $cref = sub { ... }; To call a code reference: @returned = $cref->(@arguments); @returned = &$cref(@arguments);
8/12/2009
Records
A record is a single logical unit composed of different attributes.
$Nat = { "Name" => "Leonhard Euler", "Address" => "1729 Ramanujan Lane\nMathworld", "Birthday" => 0x5bb5580, };
8/12/2009
Packages
Collect data & functions in a separate (private) namespace Similar to static functions & data in C Perl 4 concept, works in Perl 5 too Reusable code
8/12/2009
require or use
The require or use statements both pull a module into your program Require loads modules at runtime,use loads modules at compile-time The required file extension for a Perl module is ".pm". use NET::SSH;
8/12/2009
Packages
To find the current package: $this_pack = __PACKAGE__; To find the caller's package: $that_pack = caller();
8/12/2009
8/12/2009
Use h2xs -XA -n Command to generate own modules Use the make dist directive to bundle it all up into a tar archive for easy distribution.
8/12/2009
8/12/2009
8/12/2009
8/12/2009
Characteristics of OOP
Data encapsulation concept Reusability Extensibility: adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones; Maintainability: objects can be maintained separately, making locating and fixing problems easier;
8/12/2009
A class PERL package. This package Perl OOPS inis a methods for objects. provides the
for a class
A method is simply a Perl subroutine. The only catch with writing such methods is that the name of the class is the first argument. An object in Perl is simply a reference to some data item within the class. Perl provides a bless() function which is used to return a reference and which becomes an object.
8/12/2009
Defining PERL : OOPSPerson; in a Class package To create an object we need an object constructor . The object reference is created by blessing a reference to the package's class . sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; }
8/12/2009
OOPS in PERL
Even we can define our own methods in PERL . sub getFirstName { return $self->{_firstName}; }
8/12/2009
Inheritance in PERL
Acquiring the properties of one class to another class . Use @ISA for implementing inheritance in perl . Searching for a method is in the order: @ISA array , AUTOLOAD subroutine
AUTOLOAD subroutine
8/12/2009
OOPS in PERL
Method Overriding Default Autoloading Destructors and Garbage Collection
8/12/2009
Debugging in Perl
-w option is great!
o o
8/12/2009
Debugging in Perl
Debug mode: perl -d filename [args]
o
Display Commands
h Extended help h h Abbreviated help l (lowercase-L) list lines of code
l l l l sub list subroutine sub 5 list line 5 3-6 list lines 3 through 6 inclusive list next window of lines
8/12/2009
Debugging in Perl
o
Display Commands (continued) w watch point /pat search forwards for pattern pat (regular expressions legal) ?pat search backwards for pattern pat (regular expressions legal) S list all subroutines w/ packages
8/12/2009
Debugging in Perl
o
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) command execute any Perl command or line of code you want q quit
8/12/2009
Debugging in Perl
o
Stepping & Tracing s step -- execute 1 line of code. Steps over subroutines. n next -- execute 1 line of code. Steps into subroutines. r return from function
8/12/2009
Debugging in Perl
o
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
8/12/2009
Debugging in Perl
o
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
8/12/2009
Debugging in Perl
How to Learn More o man perldebug o just try it!
8/12/2009
Agenda
CGI Programming Parsing XML Documents Database Connection
Process Management and Communication Socket Programming
8/12/2009
CGI Programming
"CGI" stands for "Common Gateway Interface." CGI is a standard for interfacing external applications with information servers, such as HTTP or Web servers . CGI program is executable . A CGI program can be written in any programming language, but Perl is one of the most popular,
8/12/2009
CGI Programming
8/12/2009
CGI Programming
Steps To Create a CGI Program : File should be created in the cgi-bin/ directory until unless we configure the web server . File should have execute permissions . First line should be perl interpreter Next line should be print "Content-type: text/html\n\n";
8/12/2009
CGI Programming
To avoid entering multiple print statements : print <<endmarker; line1 line2 endmarker
8/12/2009
CGI.pm Module
The CGI.pm module is part of the standard library . use CGI qw(:standard); header; start_html; end_html;
8/12/2009
HTTP_HOST : The hostname of the page being attempted HTTP_REFERER : URL of the page that called your program HTTP_USER_AGENT: The browser type of the visitor QUERY_STRING : The query string REMOTE_ADDR : The IP address of the visitor REMOTE_HOST : The hostname of the visitor REMOTE_PORT : Port the visitor is connected to server REMOTE_USER : The visitor's username
8/12/2009
CGI.pm Module
GET Method Param Post Sendmail
8/12/2009
8/12/2009
8/12/2009
8/12/2009
Database Connection
The DBI is a database access module for the Perl programming language.
8/12/2009
8/12/2009
Database Connection
Connect : $dbh = DBI->connect($dsn, $user, $password, { RaiseError => 1, AutoCommit => 0 }); Prepare : $sth = $dbh->prepare("SELECT * from table");
8/12/2009
Available Drivers
DBD::PgPP PostgreSQL driver for the DBI DBD::mysql MySQL driver for the DBI DBD::SQLite Self Contained RDBMS in a DBI DBD::ODBC ODBC Driver for perl DBI
Gofer
8/12/2009
Creating a Process
8/12/2009
Terminating a Process
Die : die (message); warn : warn (message); exit : exit (retcode); kill : kill (signal, proclist);
8/12/2009
sleep : sleep (time); wait : procid = wait(); waitpid : waitpid (procid, waitflag);
8/12/2009
Signals
Signals are a kind of notification delivered by the operating system . To Print list of signals available : perl -e 'print join(" ", keys %SIG), "\n"'
$SIG{QUIT} = \&got_sig_quit; # call &got_sig_quit for every SIGQUIT $SIG{PIPE} = 'got_sig_pipe'; # call main::got_sig_pipe for every SIGPIPE $SIG{INT} = sub { $ouch++ }; # increment $ouch for every SIGINT $SIG{INT} = 'IGNORE'; # ignore the signal INT $SIG{STOP} = 'DEFAULT'; # restore default STOP signal handling
8/12/2009
Socket Programming
use Socket; To create a server : Create a socket with socket. Bind the socket to a port address with bind. Listen to the socket at the port address with listen. Accept client connections with accept.
Establishing a client : Create a socket with socket. Connect (the socket) to the remote machine with connect.
8/12/2009
IO::Socket
It very hard to write each function The module IO::Socket provides an easy way to create sockets which can then be used like file handles perldoc IO::Socket
8/12/2009
Socket Programming
8/12/2009
References
Perl CookBook http://www.linuxjournal.com/article/3237 http://docs.rinet.ru/P7/index.htm http://www.webthing.com/tutorials/cgifaq.html
8/12/2009