Introduction to Perl Pinkhas Nisanov Perl culture Perl - - - PowerPoint PPT Presentation

introduction to perl
SMART_READER_LITE
LIVE PREVIEW

Introduction to Perl Pinkhas Nisanov Perl culture Perl - - - PowerPoint PPT Presentation

Introduction to Perl Pinkhas Nisanov Perl culture Perl - Practical Extraction and Report Language Perl 1.0 released December 18, 1987 by Larry Wall Perl culture Perl Poems BEFOREHAND: close door, each window & exit; wait until time.


slide-1
SLIDE 1

Introduction to Perl

Pinkhas Nisanov

slide-2
SLIDE 2

Perl culture

Perl - Practical Extraction and Report Language Perl 1.0 released December 18, 1987 by Larry Wall

slide-3
SLIDE 3

Perl culture

Perl Poems

BEFOREHAND: close door, each window & exit; wait until time.

  • pen 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

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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/

slide-6
SLIDE 6

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";

slide-7
SLIDE 7

Perl architecture

Perl code Opcodes Perl VM perl -MO=Terse ./hello.pl

slide-8
SLIDE 8

Basic data types - Scalar

$scalar = “string”; $num = 1.234; $reference = \$scalar; $newScalar = $$reference;

slide-9
SLIDE 9

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;

slide-10
SLIDE 10

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;

slide-11
SLIDE 11

Control structures

If () { } elsif () { } else { } unless () { } while () { } until () { } for ( ; ; ) { } foreach () { }

slide-12
SLIDE 12

Procedures

sub proc1 { ( $arg1, $arg2 ) = @_; $var = $arg1 . $arg2; return $var; } &proc1( “str1”, “str2” ); $procRef = \&proc1; $procRef->( “aaa”, “bbb” ); $doubleProcRef = sub { ($num)=@_; return 2*$num; }

slide-13
SLIDE 13

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.csv perl -MCSV -ne '@usrdt=CSVsplit($_); print $usrdt[1]." - ".$usrdt[2]. "\n";' table1.csv

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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; }

slide-16
SLIDE 16

Modules

require “./mylib/ModuleA.pm; # old use mylib::ModuleA; # new BEGIN { require mylib/ModuleA.pm; ModuleA::import(); }

slide-17
SLIDE 17

Modules

file ./mylib/ModuleA.pm

package mylib::ModuleA; sub square { my ( $arg1 ) = @_; return $arg1 * $arg1; } my $num = 5; my $sq = mylib::ModuleA::square( $num ); # “$sq” is 25

slide-18
SLIDE 18

Object Oriented programming

my $car1 = new Fiat ( “Panda” ); my $car2 = Truck->new( “Mack” ); $car1->openWindow(); foreach my $tObj ( $car1, $car2 ) { $tObj->turn( “left” ); }

slide-19
SLIDE 19

Object Oriented programming

3 basic rules

1) To create class, build package 2) To create method, write subrotine 3) To create object, bless reference

slide-20
SLIDE 20

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;

slide-21
SLIDE 21

Functional programming

slide-22
SLIDE 22

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) closures

A 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
  • uter function. As the outer function executes, it creates a closure of the

inner function.

3) recursion

The definition of an operation in terms of itself

slide-23
SLIDE 23

Functional programming

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

slide-24
SLIDE 24

Functional programming

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

slide-25
SLIDE 25

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"; }

slide-26
SLIDE 26

Functional programming

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

slide-27
SLIDE 27

Functional programming

lazy evaluation

# compute ongoing sum of last two numbers my $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; }

slide-28
SLIDE 28

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";

slide-29
SLIDE 29

Functional programming

my @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(); }

slide-30
SLIDE 30

extension system

h2xs -A -n Foo Foo/ppport.h Foo/lib/Foo.pm Foo/Foo.xs Foo/Makefile.PL Foo/README Foo/t/Foo.t Foo/Changes Foo/MANIFEST

slide-31
SLIDE 31

Examples