Introduction to Perl - Nisanov

9 downloads 236 Views 1MB Size Report
Perl culture. Perl - Practical Extraction and Report Language. Perl 1.0 released December 18, 1987 by Larry Wall ... perl -i.bak -pe 's/\buser2\b/removed/g' rpl.txt.
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. 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

Perl culture There's More Than One Way to Do It - TMTOWTDI CPAN - Comprehensive Perl Archive Network Perl Mongers - www.perl.org.il YAPC - Yet Another Perl Conference

Where to start http://www.perl.org http://www.perl.org/books/beginning-perl/ http://perldoc.perl.org/ http://www.perl.com http://www.perl.org.il

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

Perl architecture

Perl code

Opcodes

perl -MO=Terse ./hello.pl

Perl VM

Basic data types - Scalar $scalar = “string”; $num = 1.234; $reference = \$scalar; $newScalar = $$reference;

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;

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;

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

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

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

Namespace $var1 = “val1”;

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

package PkgA; $var1 = “aaaa”;

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

package PkgB; $var1 = “bbbb”;

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

All these variables are global

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

Modules require “./mylib/ModuleA.pm; # old use mylib::ModuleA; # new

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

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

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

Object Oriented programming 3 basic rules 1) To create class, build package 2) To create method, write subrotine 3) To create object, bless reference

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;

Functional programming

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 outer 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

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

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

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

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

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

Functional programming my $line; my @list1; my $f1 = new IO::File ( "< ../rpl.txt" ); while ( defined ( $line = ) ) { 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 = ) ) { my @rec = CSVsplit( $line ); push @list2, $rec[2]; } $f2->close(); print "@list2\n";

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 = ) ) { my @rec = CSVsplit( $line ); $recFunc->( \@rec ); } $f1->close(); }

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

Examples