Introduction To Perl - Nisanov

3m ago
7 Views
0 Downloads
1.00 MB
31 Pages
Last View : Today
Last Download : n/a
Upload by : Cannon Runnels
Transcription

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

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

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

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

Related Documents:

Why Perl? Perl is built around regular expressions -REs are good for string processing -Therefore Perl is a good scripting language -Perl is especially popular for CGI scripts Perl makes full use of the power of UNIX Short Perl programs can be very short -"Perl is designed to make the easy jobs easy,

Perl can be embedded into web servers to speed up processing by as much as 2000%. Perl's mod_perl allows the Apache web server to embed a Perl interpreter. Perl's DBI package makes web-database integration easy. Perl is Interpreted Perl is an interpreted language, which means that your code can be run as is, without a

Run Perl Script Option 3: Create a Perl script my_script.pl: Run my_script.pl by calling perl: 8/31/2017 Introduction to Perl Basics I 10 print Hello World!\n; perl ./my_script.pl Option 4: For a small script with several lines, you can run it directly on the command line: perl -e print Hello World!\n;

Run Perl Script Option 3: Create a Perl script my_script.pl: Run my_script.pl by calling perl: 8/31/2017 Introduction to Perl Basics I 10 print Hello World!\n; perl ./my_script.pl Option 4: For a small script with several lines, you can run it directly on the command line: perl -e print Hello World!\n;

Other Perl resources from O’Reilly Related titles Learning Perl Programming Perl Advanced Perl Programming Perl Best Practices Perl Testing: A Developer’s . Intermedi

Perl's creator, Larry Wall, announced it the next day in his State of the Onion address. Most notably, he said "Perl 6 is going to be designed by the community." Everyone thought that Perl 6 would be the version after the just-released Perl v5.6. That didn't happen, but that's why "Perl" was in the name "Perl 6."

tutorial Sorry about that but I have to keep my tutorial's example scripts short and to the point Finally, this is a tutorial for Perl/Tk only I will not be teaching perl here So if you know perl, continue But if you are a beginner to perl, I would recommend that you read my perl tutorial

Trading on SIX Swiss Exchange Introduction 7 159 Sensitivity: Public 1. Introduction SIX Swiss Exchange AG’s (SIX Swiss Exchange) trader training and testing programmes set high