introduction to perl
play

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.


  1. Introduction to Perl Pinkhas Nisanov

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

  3. 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

  4. 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

  5. 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

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

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

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

  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;

  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;

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

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

  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

  14. Namespace $var1 = “val1”; $main::var1; # it's “val1” package PkgA; $PkgA::var1; # it's “aaaa” $var1 = “aaaa”; package PkgB; $PkgB::var1; # it's “bbbb” $var1 = “bbbb”; All these variables are global

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

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

  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

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

  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

  20. Object Oriented programming sub turn { package Fiat; my ( $obj, $direct ) = @_; my $model = $obj->{ “model” }; @ISA = ( “Car” ); $obj->setDirect( $model, $direct ); my $totalCount = 0; } sub new { sub openWindow { my ( $class, $model ) = @_; my ( $obj, $direct ) = @_; my $self = {}; down( $obj->{ “doorGlass” } ); ++$totalCount; } $self->{ “model” } = $model; bless ( $self, $class ); sub DESTROY { return $self; --totalCount; } } 1;

  21. Functional programming

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

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

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

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

  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)

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

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

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

  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

  31. Examples

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend