introduction to perl. 2 what is perl? practical extraction and report language practical extraction...
TRANSCRIPT
![Page 1: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/1.jpg)
Introduction to PerlIntroduction to Perl
![Page 2: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/2.jpg)
22
What is Perl?What is Perl?
Practical Extraction and Report Practical Extraction and Report Language Language
实用摘录与报告语言实用摘录与报告语言
Pathlogical Eclectic Rubbish ListerPathlogical Eclectic Rubbish Lister 反常,折中的垃圾陈列器反常,折中的垃圾陈列器
![Page 3: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/3.jpg)
33
What is Perl?What is Perl?
Perl is a Portable Scripting LanguagePerl is a Portable Scripting Language– No compiling is needed.No compiling is needed.– Runs on Windows, UNIX and LINUXRuns on Windows, UNIX and LINUX
Fast and easy text processing capabilityFast and easy text processing capability Fast and easy file handling capabilityFast and easy file handling capability Written by Larry WallWritten by Larry Wall ““Perl is the language for getting your job Perl is the language for getting your job
done.”done.”
![Page 4: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/4.jpg)
44
How to Access PerlHow to Access Perl Off the school networkOff the school network
– Located on the 100.10 machines at: Located on the 100.10 machines at: /usr/local/bin/perl/usr/local/bin/perl
To install at home To install at home – www.perl.comwww.perl.com Has rpm's for Linux Has rpm's for Linux– www.activestate.comwww.activestate.com Has binaries for Windows Has binaries for Windows
Latest Version is 5.8Latest Version is 5.8– To check if Perl is working and the version To check if Perl is working and the version
numbernumber % perl -v% perl -v
![Page 5: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/5.jpg)
55
The Basic Hello World The Basic Hello World ProgramProgram
Program:Program:
#!/usr/local/bin/perl -w#!/usr/local/bin/perl -w
print “Hello World!\n”;print “Hello World!\n”;
Save this as “hello.pl”Save this as “hello.pl” Give it executable permissionsGive it executable permissions
– chmod ug+x hello.plchmod ug+x hello.pl Run it as follows:Run it as follows:
– ./hello.pl./hello.pl
![Page 6: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/6.jpg)
66
““ Hello World” Hello World” ObservationsObservations
““ .pl” extension is optional but is .pl” extension is optional but is commonly usedcommonly used
The first line “#!/usr/local/bin/perl” tells The first line “#!/usr/local/bin/perl” tells UNIX where to find PerlUNIX where to find Perl
““-w” switches on warning : not required -w” switches on warning : not required but a really good ideabut a really good idea
![Page 7: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/7.jpg)
77
How to use PerlHow to use Perl
SyntaxSyntax
A Web Server ExampleA Web Server Example
![Page 8: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/8.jpg)
88
要记住,过早的优化往往是错误的根源。如要记住,过早的优化往往是错误的根源。如果您在 果您在 Perl Perl 中写了一个原型,并用其他语中写了一个原型,并用其他语言来重写是没有问题的。原型意味着能够方言来重写是没有问题的。原型意味着能够方便地开发。便地开发。
![Page 9: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/9.jpg)
99
Perl Perl 解释器解释器
速度和 速度和 BenchmarkBenchmark
Perl Perl 的容错能力的容错能力
![Page 10: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/10.jpg)
1010
Perl Perl 的缺憾的缺憾 速度 算法,内置函数速度 算法,内置函数 训练训练 面向对象面向对象 线程以及统一字符编码线程以及统一字符编码
![Page 11: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/11.jpg)
1111
Perl Perl 的优势的优势
某些方面的优势,例如:某些方面的优势,例如: 正则表达式正则表达式 隐含的函数声明隐含的函数声明 不严格的语法不严格的语法 象日用文档似的程序结构象日用文档似的程序结构
通用的灵活,大大减少开发的时间通用的灵活,大大减少开发的时间 几乎没有任何 几乎没有任何 C C 或 或 C++ C++ 能做而 能做而 Perl Perl 不不
能的事情能的事情
![Page 12: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/12.jpg)
1212
SyntaxSyntax
LiteralsLiterals Types of VariablesTypes of Variables Operators Operators String FunctionsString Functions LoopLoop
![Page 13: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/13.jpg)
1313
Numerical LiteralsNumerical Literals
Numerical LiteralsNumerical Literals– 66 IntegerInteger– 12.612.6 Floating PointFloating Point– 1e101e10 Scientific NotationScientific Notation– 6.4E-336.4E-33 Scientific NotationScientific Notation– 4_348_3484_348_348 Underscores instead of Underscores instead of
commas for long commas for long numbersnumbers
![Page 14: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/14.jpg)
1414
String LiteralsString Literals
String LiteralsString Literals– ““There is more than on way to do it!”There is more than on way to do it!”– 'Just don't create a file called -rf.''Just don't create a file called -rf.'– ““Beauty?\nWhat's that?\n”Beauty?\nWhat's that?\n”– “”“”– ““Real programmers can write assembly Real programmers can write assembly
in any language.”in any language.”
Quotes from Larry WallQuotes from Larry Wall
![Page 15: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/15.jpg)
1515
Types of VariablesTypes of Variables Types of variables:Types of variables:
– Scalar variables : $a, $b, $cScalar variables : $a, $b, $c– Array variables : @arrayArray variables : @array– Hash variables : %hashHash variables : %hash– File handles : STDIN, SRC, DESTFile handles : STDIN, SRC, DEST
Variables do not need to be declaredVariables do not need to be declared Variable type (int, char, ...) is decided at Variable type (int, char, ...) is decided at
run time run time – $a = 5; # now an integer$a = 5; # now an integer
– $a = “perl”; # now a string$a = “perl”; # now a string
![Page 16: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/16.jpg)
1616
Operators on Scalar Operators on Scalar VariablesVariables
Numeric and Logic OperatorsNumeric and Logic Operators– Typical : +, -, *, /, %, ++, --, +=, -=, *=, /=, ||, Typical : +, -, *, /, %, ++, --, +=, -=, *=, /=, ||,
&&, ! ect …&&, ! ect …– Not typical: ** for exponentiationNot typical: ** for exponentiation
String OperatorsString Operators– Concatenation: “.” - similar to strcatConcatenation: “.” - similar to strcat
$first_name = “Larry”; $first_name = “Larry”;
$last_name = “Wall”;$last_name = “Wall”;
$full_name = $first_name $full_name = $first_name .. “ “ “ “ .. $last_name; $last_name;
![Page 17: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/17.jpg)
1717
Equality Operators for Equality Operators for StringsStrings
Equality/ Inequality : eq and neEquality/ Inequality : eq and ne
$language = “Perl”;$language = “Perl”;
if ($language == “Perl”) ...if ($language == “Perl”) ...# Wrong!# Wrong!
if ($languageif ($language eq eq “Perl”) ...“Perl”) ... #Correct#Correct
– Use eq / ne rather than == / != for Use eq / ne rather than == / != for stringsstrings
![Page 18: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/18.jpg)
1818
Relational Operators for Relational Operators for StringsStrings
Greater thanGreater than– Numeric : >Numeric : > String : gtString : gt
Greater than or equal toGreater than or equal to– Numeric : >=Numeric : >= String : geString : ge
Less thanLess than– Numeric : <Numeric : < String : ltString : lt
Less than or equal toLess than or equal to– Numeric : <=Numeric : <= String : leString : le
![Page 19: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/19.jpg)
1919
String FunctionsString Functions
Convert to upper caseConvert to upper case– $name = uc($name);$name = uc($name);
Convert only the first char to upper caseConvert only the first char to upper case– $name = ucfirst($name);$name = ucfirst($name);
Convert to lower caseConvert to lower case– $name = lc($name);$name = lc($name);
Convert only the first char to lower caseConvert only the first char to lower case– $name = lcfirst($name);$name = lcfirst($name);
![Page 20: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/20.jpg)
2020
If ... else ... statementsIf ... else ... statements Similar to C/C++ - except the scope Similar to C/C++ - except the scope
braces are REQUIRED!!braces are REQUIRED!!
if ( $os eq “Linux” ) { if ( $os eq “Linux” ) {
print “Sweet!\n”; print “Sweet!\n”;
}}
elsif ( $os eq “Windows” ) {elsif ( $os eq “Windows” ) {
print “Time to move to Linux, buddy!\n”;print “Time to move to Linux, buddy!\n”;
}}
else {else {
print “Hmm...!\n”;print “Hmm...!\n”;
}}
![Page 21: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/21.jpg)
2121
Unless ... else StatementsUnless ... else Statements Unless Statements are the opposite of if ... Unless Statements are the opposite of if ...
else statements. else statements.
unless ($os eq “Linux”) {unless ($os eq “Linux”) {
print “Time to move to Linux, buddy!\n”;print “Time to move to Linux, buddy!\n”;
}}
else {else {
print “Sweet!\n”;print “Sweet!\n”;
}}
And again remember the braces are required!And again remember the braces are required!
![Page 22: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/22.jpg)
2222
While LoopWhile Loop While loop: Similar to C/C++ but again While loop: Similar to C/C++ but again
the braces are the braces are required!!required!!
Example :Example :$i = 0;$i = 0;
while ( $i <= 1000 ) {while ( $i <= 1000 ) {
print “$i\n”; print “$i\n”;
$i++;$i++;
}}
![Page 23: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/23.jpg)
2323
Until LoopUntil Loop The until function evaluates an The until function evaluates an
expression repeatedly until a specific expression repeatedly until a specific condition is met. condition is met.
Example:Example:
$i = 0;$i = 0;
until ($i == 1000) {until ($i == 1000) {
print “$i\n”;print “$i\n”;
$i++;$i++;
}}
![Page 24: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/24.jpg)
2424
For LoopsFor Loops Like C/C++ Like C/C++
– Example :Example : for ( $i = 0; $i <= 1000; $i++ ) {for ( $i = 0; $i <= 1000; $i++ ) { print “$i\n”;print “$i\n”; }}
Another way to create a for loopAnother way to create a for loop– ExampleExample
for $i(0..1000) {for $i(0..1000) { print “$i\n”;print “$i\n”; }}
![Page 25: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/25.jpg)
2525
Minimal Perl Web Server(1)Minimal Perl Web Server(1)#!/usr/bin/perl #!/usr/bin/perl
use Socket;use Socket;use Carp;use Carp;use FileHandle; use FileHandle;
# (1) use port 8080 by default, unless overridden on command line# (1) use port 8080 by default, unless overridden on command line$port = (@ARGV ? $ARGV[0] : 8080); $port = (@ARGV ? $ARGV[0] : 8080);
# (2) create local TCP socket and set it to listen for connections# (2) create local TCP socket and set it to listen for connections$proto = getprotobyname('tcp');$proto = getprotobyname('tcp');socket(S, PF_INET, SOCK_STREAM, $proto) || die;socket(S, PF_INET, SOCK_STREAM, $proto) || die;setsockopt(S, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die;setsockopt(S, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die;bind(S, sockaddr_in($port, INADDR_ANY)) || die;bind(S, sockaddr_in($port, INADDR_ANY)) || die;listen(S, SOMAXCONN) || die; listen(S, SOMAXCONN) || die;
![Page 26: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/26.jpg)
2626
Minimal Perl Web Server(1)Minimal Perl Web Server(1)# (3) print a startup message# (3) print a startup messageprintf(" <<<Type-O-Serve Accepting on Port %d>>>\n\n",printf(" <<<Type-O-Serve Accepting on Port %d>>>\n\n",
$port);$port);
while (1){ while (1){ # (4) wait for a connection C # (4) wait for a connection C $cport_caddr = accept(C, S); $cport_caddr = accept(C, S); ($cport,$caddr) = sockaddr_in($cport_caddr); ($cport,$caddr) = sockaddr_in($cport_caddr); C->autoflush(1); C->autoflush(1);
# (5) print who the connection is from # (5) print who the connection is from $cname = gethostbyaddr($caddr,AF_INET); $cname = gethostbyaddr($caddr,AF_INET); printf(" <<<Request From '%s'>>>\n",$cname); printf(" <<<Request From '%s'>>>\n",$cname);
![Page 27: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/27.jpg)
2727
Minimal Perl Web Server(1)Minimal Perl Web Server(1) # (6) read request msg until blank line, and print on screen # (6) read request msg until blank line, and print on screen
while ($line = <C>) { while ($line = <C>) { print $line; print $line; if ($line =~ /^\r/) { last; } if ($line =~ /^\r/) { last; }
} } # (7) prompt for response message, and input response lines, # (7) prompt for response message, and input response lines, # sending response lines to client, until solitary "." # sending response lines to client, until solitary "." printf(" <<<Type Response Followed by '.'>>>\n"); printf(" <<<Type Response Followed by '.'>>>\n");
while ($line = <STDIN>) while ($line = <STDIN>) { {
$line =~ s/\r//; $line =~ s/\r//; $line =~ s/\n//; $line =~ s/\n//; if ($line =~ /^\./) { last; } if ($line =~ /^\./) { last; } print C $line . "\r\n"; print C $line . "\r\n";
} } close(C);close(C);
} }
![Page 28: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/28.jpg)
2828
Resources For PerlResources For Perl Books:Books:
– Learning PerlLearning Perl By SchwartzBy Schwartz Published by O'ReillyPublished by O'Reilly
– Programming PerlProgramming Perl By Larry Wall,Tom Christiansen and Jon By Larry Wall,Tom Christiansen and Jon
OrwantOrwant Published by O'ReillyPublished by O'Reilly
Web SiteWeb Site– http://safari.oreilly.comhttp://safari.oreilly.com
Contains both Learning Perl and Contains both Learning Perl and Programming Perl in ebook formProgramming Perl in ebook form
![Page 29: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/29.jpg)
2929
Web Sources for PerlWeb Sources for Perl
WebWeb– www.perl.comwww.perl.com– www.perldoc.comwww.perldoc.com– www.perl.orgwww.perl.org– www.perlmonks.orgwww.perlmonks.org
![Page 30: Introduction to Perl. 2 What is Perl? Practical Extraction and Report Language Practical Extraction and Report Language 实用摘录与报告语言 实用摘录与报告语言 Pathlogical](https://reader030.vdocuments.site/reader030/viewer/2022033010/5697bf941a28abf838c8fea8/html5/thumbnails/30.jpg)
Thank you Thank you