You are on page 1of 136

Agenda

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

Whats Perl Good For?


Quick scripts, complex scripts Parsing & restructuring data files CGI-BIN scripts High-level programming
o o o

Networking libraries Graphics libraries Database interface libraries

Whats Perl Bad For?

Compute-intensive applications (use C) Hardware interfacing (device drivers)

Installation for Unix / Windows


Unix Windows

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)

Should call exit() function when finished


o

Exit value of zero means success


exit (0); # successful

Exit value non-zero means failure


exit (2); # failure

8/12/2009

Invoking perl from cmd-line


o o o

perl -w (-w to give all warnings) printhelloworld\n; __END__

Output : hello world Perl Help


o o

man perl perldoc perlfunc (perl built-in functions)

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

00.0representsFalse all other values represents True

8/12/2009

Numeric and String Literals


Numbers - This is the most basic data type. Strings - A string is a series of characters that are handled as one unit. Arrays - An array is a series of numbers and strings handled as a unit. Associative Arrays - This is the most complicated data type.

8/12/2009

Special Esc Sequence chars


\n,\r, \t, \a, \b, \x are also available o \l Lowercase next letter o \L Lowercase all following letters until \E o \u Uppercase next letter o \U Uppercase all following until \E o \Q Backslash-quote all nonalphanumerics until \E o \E Terminate \L , \U, or \Q

8/12/2009

Operators

Math(operator prcedeces are as in C)


o o

The usual suspects: + - * /


$total = $subtotal * (1 + $tax / 100.0);

Exponentiation: **
$cube = $value ** 3; $cuberoot = $value ** (1.0/3);

Bit-level Operations
left-shift: << $val = $bits << 1; right-shift: >> $val = $bits >> 8;

8/12/2009

Operators

Assignments
o

As usual: = += -= *= /= **= <<= >>=


$value *= 5; $longword <<= 16;

o o

Increment: ++
$counter++ ++$counter

Decrement: - $num_tries-- --$num_tries

8/12/2009

Operators
Boolean (against bits in each byte)
o o o

Usual operators: & | Exclusive-or: ^ Bitwise Negation: ~


$picture = $backgnd & ~$mask | $image;

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

Short Circuit Operators


expr1 && expr2
expr1 is evaluated. expr2 is only evaluated if expr1 was true.

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

$a = 123 % 10; ($a is 3)

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;

String Repetition Operator x o "fred" x 3 # is "fredfredfred"

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

chop and chomp functions


chop function (any last char in the string is removed)
o o o o o o o

$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")

chomp function (only newline char removed)

8/12/2009

Reading values from keyboard <STDIN>

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

if statement - second style


o

statement if condition;
print \$index is $index if $DEBUG;

o o
o

Single statements only Simple expressions only


statement unless condition;
print millenium is here! unless $year < 2000;

unless is a reverse if

8/12/2009

Control Structures

for loop - first style


o

for (initial; condition; increment) { code }

for ($i=0; $i<10; $i++) { print hello\n; }

for loop - second style


o

for [variable] (range) { code }

for $name (@employees) { print $name is an employee.\n; }

8/12/2009

Control Structures

for loop with default loop variable


for (@employees) { print $_ is an employee\n; print; # this prints $_ }

Foreach and For are actually the same.

8/12/2009

Control Structures
while loop
o

while (condition) { code }


$cars while print } while = 7; ($cars > 0) { cars left: , $cars--, \n; ($game_not_over) {}

8/12/2009

Control Structures

until loop is opposite of while


o

until (condition) { code }


$cars until print } while = 7; ($cars <= 0) { cars left: , $cars--, \n; ($game_not_over) {}

8/12/2009

Control Structures

Bottom-check Loops
o o

do { code } while (condition); do { code } until (condition);


$value = 0; do { print Enter Value: ; $value = <STDIN>; } until ($value > 0);

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

while (cond) { next if $a < 5; }


You can break out of multilevel loops using labels (not covered in this presentation)

8/12/2009

Control Structures
o

Break out of multilevel loops using labels


top: while (condition) { for ($car in @cars) { do stuff next top if situation; } }

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?!?

Perl needs no Switch (Case) statement. Use if/else combinations instead


o

if (cond1) { } elsif (cond2) { } elsif else

This will be optimized at compile time

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

List constructor operator


(1 .. 5) # same as (1, 2, 3, 4, 5) (1.2 .. 5.2) # same as (1.2, 2.2, 3.2, 4.2, 5.2)

@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];

How big is my list?


print Number of people: $#people\n;

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

Printing the whole list


o o o

print("The answer is ",@a,"\n"); ($a,$b,$c) = (1,2,3); ($a,$b) = ($b,$a); # swap $a and $b

Converting into scalar

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

Push and Pop functions


The push and pop functions do things to the "right" side of a list o push(@mylist,$newvalue); # like @mylist = (@mylist,$newvalue)
first argument must be an array variable name

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

Shift and unshift functions


unshift and shift functions perform the corresponding actions on the "left" side of a list o unshift(@fred,$a); # like @fred = ($a,@fred); o unshift(@fred,$a,$b,$c); # like @fred = ($a,$b,$c,@fred); o $x = shift(@fred); # like ($x,@fred) = @fred; o shift returns undef if given an empty array variable o @fred = (5,6,7); o unshift(@fred,2,3,4); # @fred is now (2,3,4,5,6,7) o $x = shift(@fred); # $x gets 2, @fred is now (3,4,5,6,7)

8/12/2009

Reverse and Sort functions


reverse function o @a = (7,8,9); o @b = reverse(@a); # gives @b the value of (9,8,7) o @b = reverse(7,8,9); # same thing Sort function o @x = sort("small","medium","large"); o # @x gets "large","medium","small" o @y = (1,2,4,8,16,32,64); o @y = sort(@y); # @y gets 1,16,2,32,4,64,8

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

How many elements are in my hash?


@allkeys = keys(%var); $num = $#allkeys;

8/12/2009

Hash Variables

The keys function


o

yields a list of all the current keys in the hash


$fred{"aaa"} = "bbb"; $fred{234.5} = 456.7; @list = keys(%fred); # @list gets ("aaa",234.5) or # (234.5,"aaa") foreach $key (keys (%fred)) {
# once for each key of %fred # show key and value

print "at $key we have $fred{$key}\n";


}

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

if (keys(%somehash)) { # if keys() not zero: ...; # array is non empty }

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

The each function


o

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

Dealing with Hashes

keys( ) - get an array of all keys


o o o

foreach (keys (%hash)) { } @array = values (%hash); while (@pair = each(%hash)) { print element $pair[0] has $pair[1]\n; }

values( ) - get an array of all values


each( ) - get key/value pairs

8/12/2009

Dealing with Hashes

exists( ) - check if element exists


o o

if (exists $ARRAY{$key}) { } delete $ARRAY{$key};

delete( ) - delete one element

8/12/2009

Subroutines (Functions)

Defining a Subroutine
o o

sub name { code } Arguments passed in via @_ list


sub multiply { my ($a, $b) = @_; return $a * $b; }

Last value processed is the return value (could have left out word return, above)

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

Passes the value Lists are expanded


@a = (5,10,15); @b = (20,25); &mysub(@a,@b);
this passes five arguments: 5,10,15,20,25 mysub can receive them as 5 scalars, or one array

8/12/2009

Subroutines (Functions)
Notes
o o

Recursion is legal local is the Perl 4 version of my

8/12/2009

Other Useful Functions

push( ), pop( )- stack operations on lists shift( ),unshift( ) - bottom-based ops split( ) - split a string by separator
o o

@parts = split(/:/,$passwd_line); while (split) # like: split (/\s+/, $_)

splice( ) - remove/replace elements substr( ) - substrings of a string

8/12/2009

Other Useful Functions


length( ) - length in bytes (strings) stat( ) - file stats (times, sizes, ...) pack( ),unpack( ) - byte/record packing hex( ) - hex string to integer conv printf( ) - formatted printing sprintf( ) - printf into a string
o

$str = sprintf (expense = %.2f, $expns);

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;

Command Line Args


$0 = program name @ARGV array of arguments to program zero-based index (default for all arrays) Example
o

yourprog -a somefile
$0 is yourprog $ARGV[0] is -a $ARGV[1] is somefile

Basic File I/O


Reading a File
o

open (FILEHANDLE, $filename) || die \ open of $filename failed: $!; while (<FILEHANDLE>) { chop $_; # or just: chop; print $_\n; } close FILEHANDLE;

Basic File I/O


Writing a File
o

open (FILEHANDLE, >$filename) || die \ open of $filename failed: $!; while (@data) { print FILEHANDLE $_\n; # note, no comma! } close FILEHANDLE;

Basic File I/O


Predefined File Handles
o o o o

<STDIN> input <STDOUT> output <STDERR> output


print STDERR big bad error occurred\n;

<> ARGV or STDIN

Basic File I/O


How does <> work?
opens each ARGV filename for reading o if no ARGVs, reads from stdin o great for writing filters, heres cat:
o

while (<>) { print; # same as print $_; }

Basic File I/O


Reading from a Pipe
o

open (FILEHANDLE, ps aux |) || die \ launch of ps failed: $!; while (<FILEHANDLE>) { chop; print $_\n; } close FILEHANDLE;

Basic File I/O


Writing to a Pipe
o

open (FILEHANDLE, | mail frank) || die \ launch of mail failed: $!; while (@data) { print FILEHANDLE $_\n; } close FILEHANDLE;

File Test Operators


Existence: if (-e file)
o
o o o o

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/

Options for the Substitution Operator


g Change all occurrences of the pattern i Ignore case in pattern e Evaluate replacement string as expression m Treat string to be matched as multiple lines o Evaluate only once s Treat string to be matched as single line x Ignore white space in pattern

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

Perl versus Unix Utilities


sed - substitutions
o o o o

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

perl: tr/A-Z/a-z/; (platform


independent)

Perl versus Unix Utilities


wc - char / word / line counting
o o

wc: wc -l < filename perl: perl -ne END{print $.\n} <


filename

awk - BEGIN and END constructs


o

awk: BEGIN {total=0}

$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

Hash / Array Limitations


Hashes and arrays can store only scalar data What if we want to create a hash of complex records The solution -References

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

Automating Module Clean-Up


For set-up code, put executable statements outside subroutine definitions in the module file For clean-up code, use an END subroutine in that module. END { logmsg("shutdown");close(LF) or die "close $Logfile failed: $!";}

8/12/2009

Preparing a Module for Distribution

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

Speeding Module Loading with SelfLoader


When you load a module using require or use, the entire module file must be read and compiled Solution is using SelfLoader; _DATA__ sub abc { .... } sub def { .... } SelfLoaded or AutoLoaded subroutines have no access to lexical variables in the file whose DATA block they are in

8/12/2009

Reporting Errors and Warnings Like Built-Ins


To print the errors that are there in our modules . Use Carp ;

8/12/2009

Building and Installing a CPAN Module


% % % % % % % gunzip Some-Module-4.54.tar.gz tar xf Some-Module-4.54 cd Some-Module-4.54 perl Makefile.PL make make test make install

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

#!/usr/bin/perl -w tells you about


misused variables using uninitialized data/varables identifiers that are used only once and more

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

CGI Environment Variables

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

Parsing XML documents


Use Perl module called XML::Simple for Parsing XML Documents . XML::Simple works by parsing an XML file and returning the data within it as a Perl hash reference . Within this hash, elements from the original XML file play the role of keys, and the CDATA between them takes the role of values

8/12/2009

Parsing XML documents


XML::Simple class exposes two methods, XMLin() and XMLout() . The XMLin() method reads an XML file or string and converts it to a Perl representation . XMLout() method does the reverse, reading a Perl structure and returning it as an XML document instance .

8/12/2009

Controlling parser behaviour


The ForceArray option is a Boolean flag that tells XML::Simple to turn XML elements into regular indexed arrays instead of hashes .
KeyAttr, which can be used to tell XML::Simple to use a particular element as a unique "key" when building the hash representation of an XML document.

8/12/2009

Database Connection
The DBI is a database access module for the Perl programming language.

8/12/2009

Notation and Conventions


$dbh Database handle object $sth Statement handle object $drh Driver handle object $rows Number of rows processed (if available, else -1) $fh A filehandle undef NULL values are represented by undefined values in Perl \%attr Reference to a hash of attribute values passed to methods

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

Eval : system fork : pipe : exec :

eval (string); : system(list); procid = fork(); pipe (infile, outfile); exec(list);

8/12/2009

Terminating a Process

Die : die (message); warn : warn (message); exit : exit (retcode); kill : kill (signal, proclist);

8/12/2009

Execution Control Functions

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

Net::Ping Module Net::Telnet Module Net::FTP Module Net::SSH::Perl Module

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

You might also like