Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture...

31
Introduction to Perl Pinkhas Nisanov

Transcript of Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture...

Page 1: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Introductionto

PerlPinkhas Nisanov

Page 2: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Perl culture

Perl - Practical Extraction and Report Language

Perl 1.0 released December 18, 1987 by Larry Wall

Page 3: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Perl culture

Perl PoemsBEFOREHAND: close door, each window & exit; wait until time. open spellbook, study, read (scan, select, tell us);write it, print the hex while each watches, reverse its length, write again; kill spiders, pop them, chop, split, kill them. unlink arms, shift, wait & listen (listening, wait),sort the flock (then, warn the "goats" & kill the "sheep"); kill them, dump qualms, shift moralities, values aside, each one; die sheep! die to reverse the system you accept (reject, respect);next step, kill the next sacrifice, each sacrifice, wait, redo ritual until "all the spirits are pleased"; do it ("as they say").do it(*everyone***must***participate***in***forbidden**s*e*x*).return last victim; package body; exit crypt (time, times & "half a time") & close it, select (quickly) & warn your next victim;AFTERWORDS: tell nobody. wait, wait until time; wait until next year, next decade; sleep, sleep, die yourself, die at last

Page 4: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Perl culture

CPAN - Comprehensive Perl Archive Network

There's More Than One Way to Do It - TMTOWTDI

Perl Mongers - www.perl.org.il

YAPC - Yet Another Perl Conference

Page 5: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Where to start

http://www.perl.org/books/beginning-perl/

http://www.perl.org

http://www.perl.com

http://www.perl.org.il

http://perldoc.perl.org/

Page 6: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Hello World!

echo 'print "Hello World!\n";' | perl

perl -e 'print "Hello World!\n";'

executable text file hello.pl#!/usr/bin/perl print "Hello World!\n";

Page 7: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Perl architecture

Perl code Opcodes PerlVM

perl -MO=Terse ./hello.pl

Page 8: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Basic data types - Scalar

$scalar = “string”;

$num = 1.234;

$reference = \$scalar;

$newScalar = $$reference;

Page 9: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Basic data types - Array

@array = ( “str1”, 1.23, $scalar );

$array[0] = “string”;

$arrayRef = [ 1, 2, 3, 4 ];

$arrayRef->[2]; # it's value is: 3

@newArray = @$arrayRef;

Page 10: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Basic data types - Hash

%hash = ( “key1” => 1.23, “key2” => $scalar );

$hash{“key3”} = “string”;

$hashRef = { “key4” => “string” };

$hashRef->{ “key4” }; # it's value is: “string”

%newHash = %$hashRef;

Page 11: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Control structures

If () { } elsif () { } else { }

unless () { }

while () { }

until () { }

for ( ; ; ) { }

foreach () { }

Page 12: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Procedures

sub proc1 {( $arg1, $arg2 ) = @_;$var = $arg1 . $arg2;return $var;

}&proc1( “str1”, “str2” );

$procRef = \&proc1;$procRef->( “aaa”, “bbb” );

$doubleProcRef = sub { ($num)=@_; return 2*$num; }

Page 13: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Perl command-line

ps -ef | perl -ne '@prd=split /\s+/; print $prd[1],"\n" if $prd[0] eq "pinkhasn";'

perl -i.bak -pe 's/\buser2\b/removed/g' rpl.txt

perl -ne '@usrdt=split /\,/; print $usrdt[1]." - ".$usrdt[2]. "\n";' table1.csvperl -MCSV -ne '@usrdt=CSVsplit($_); print $usrdt[1]." - ".$usrdt[2]. "\n";' table1.csv

Page 14: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Namespace

$var1 = “val1”;

package PkgA;$var1 = “aaaa”;

package PkgB;$var1 = “bbbb”;

$main::var1; # it's “val1”

$PkgA::var1; # it's “aaaa”

$PkgB::var1; # it's “bbbb”

All these variables are global

Page 15: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Scope

my $elemType = “type1”;foreach my $element ( @list ) {

my $elemSize = getSize( $element );procElem( $element, $elemSize, $elemType );

}

sub proc1 {my ( $arg1 ) = @_;my $argRef = \$arg1;return $argRef;

}

Page 16: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Modules

require “./mylib/ModuleA.pm; # old

use mylib::ModuleA; # new

BEGIN {require mylib/ModuleA.pm;ModuleA::import();

}

Page 17: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Modules

file ./mylib/ModuleA.pmpackage mylib::ModuleA;

sub square {my ( $arg1 ) = @_;return $arg1 * $arg1;

}

my $num = 5;my $sq = mylib::ModuleA::square( $num );# “$sq” is 25

Page 18: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Object Oriented programming

my $car1 = new Fiat ( “Panda” );my $car2 = Truck->new( “Mack” );

$car1->openWindow();

foreach my $tObj ( $car1, $car2 ) {$tObj->turn( “left” );

}

Page 19: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Object Oriented programming

3 basic rules

1) To create class, build package

2) To create method, write subrotine

3) To create object, bless reference

Page 20: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Object Oriented programming

package Fiat;

@ISA = ( “Car” );my $totalCount = 0;

sub new {my ( $class, $model ) = @_;my $self = {};++$totalCount;$self->{ “model” } = $model;bless ( $self, $class );return $self;

}

sub turn {my ( $obj, $direct ) = @_;my $model = $obj->{ “model” };$obj->setDirect( $model, $direct );

}

sub openWindow {my ( $obj, $direct ) = @_;down( $obj->{ “doorGlass” } );

}

sub DESTROY {--totalCount;

}

1;

Page 21: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Functional programming

Page 22: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Functional programming

3 basic features

1) first-class functions (including anonymous functions)a first class function can be created during the execution of a program, stored in a data structure, passed as an argument to another function

2) closuresA closure is a function created by a program at run time. This idea is written as a function that appears entirely within the body of another function. The nested, inner function may refer to local variables of the outer function. As the outer function executes, it creates a closure of the inner function.

3) recursionThe definition of an operation in terms of itself

Page 23: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Functional programming

recursion

sub factorial{my ( $num ) = @_;return $num > 1 ? $num * factorial( $num – 1 ) : 1;

}

Page 24: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Functional programming

iterator

sub iterBuild { my @elems = @_; my $st = 0; my $it = sub { $st = 0 if $st > $#elems; return $elems[ $st++ ]; }; return $it;}

Page 25: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Functional programming

iterator

my $iter1 = iterBuild( 1, 2, 3 );my $iter2 = iterBuild( qw( a b c d ) );

foreach ( 1..10 ) {print "Iterator 1111: " . $iter1->() . "\n";print "Iterator 2222: " . $iter2->() . "\n\n";

}

Page 26: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Functional programming

lazy evaluation

Delaying evaluation of procedure arguments until the last possible moment (e.g., until they are required by a primitive operation)

Page 27: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Functional programming

lazy evaluation# compute ongoing sum of last two numbersmy $code = sub { my ( $x, $y ) = ( 0, 1 ); my $next = sub { ($x, $y) = ($y, $x + $y); return ($x, $next); }; return ($x, $next);};

my $value;while($code) { ($value, $code ) = $code->(); print "Next value in the series: $value\n"; sleep 1;}

Page 28: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Functional programming my $line; my @list1; my $f1 = new IO::File ( "< ../rpl.txt" ); while ( defined ( $line = <$f1> ) ) {

my @rec = CSVsplit( $line );push @list1, $rec[1];

} $f1->close(); print "@list1\n";

my @list2; my $f2 = new IO::File ( "< ../table1.csv" ); while ( defined ( $line = <$f2> ) ) {

my @rec = CSVsplit( $line );push @list2, $rec[2];

} $f2->close(); print "@list2\n";

Page 29: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Functional programmingmy @list1;csvProc( "../rpl.txt", sub { my $rec = shift; push @list1, $rec->[1]; } );print "@list1\n";

my @list2;csvProc( "../table1.csv", sub { my $rec = shift; push @list2, $rec->[2]; } );print "@list2\n";

sub csvProc { my ( $fileName, $recFunc ) = @_; my $f1 = new IO::File ( "< $fileName" ); my $line; while ( defined ( $line = <$f1> ) ) { my @rec = CSVsplit( $line ); $recFunc->( \@rec ); } $f1->close();}

Page 30: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

extension system

h2xs -A -n Foo

Foo/ppport.hFoo/lib/Foo.pmFoo/Foo.xsFoo/Makefile.PLFoo/READMEFoo/t/Foo.tFoo/ChangesFoo/MANIFEST

Page 31: Introduction to Perl - Nisanov · 2008-07-31 · Introduction to Perl Pinkhas Nisanov. Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18,

Examples