PERL The Practical Extraction Report Language The Practical

  • Slides: 38
Download presentation
PERL 程式設計 The Practical Extraction Report Language

PERL 程式設計 The Practical Extraction Report Language

The • • Practical Extraction Report Language What is PERL? Who Uses PERL? Which

The • • Practical Extraction Report Language What is PERL? Who Uses PERL? Which PERL? Where to Get PERL? What Version Do I Have? /usr/src/perl 5. 005 # perl -v This is perl, version 5. 005 built for i 386 -freebsd Copyright 1987 -1998, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5. 0 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using `man perl' or `perldoc perl'. If you have access to the Internet, point your browser at http: //www. perl. com/, the Perl Home Page.

(德昭)~$ perl -v This is perl, version 5. 003 with EMBED built under linux

(德昭)~$ perl -v This is perl, version 5. 003 with EMBED built under linux at Jun 27 1996 21: 52: 16 + suidperl security patch Copyright 1987 -1996, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5. 0 source kit. (德昭)~$

PERL Scripts PERL at the Command Line • The -e Switch -e switch 用來執行命令列上的

PERL Scripts PERL at the Command Line • The -e Switch -e switch 用來執行命令列上的 perl 指令 /usr/home/tcliang$ perl -e 'print "Hello, T. C. Liang!n"' Hello, T. C. Liang! 註:在 Win 98 的 Ms. DOS 模式中,由於 command shell 對引號處理的特性, 在 command line 要寫成: perl -e "print "Hello T. C. Liangn""

PERL Scripts PERL at the Command Line • The -n Switch 在列印檔案之時,加上 -n Switch,

PERL Scripts PERL at the Command Line • The -n Switch 在列印檔案之時,加上 -n Switch, perl 將以迴圈的方式對檔案逐列處理。 /usr/home/tcliang$ perl -ne 'print' /etc/passwd xten: *: 67: X-10 daemon: /usr/local/xten: /nonexistent nobody: *: 65534: Unprivileged user: /nonexistent ftp: *: 14: 5: Anonymous FTP Admin: /var/ftp: /nonexistent rkliang: *: 1000: Ricky Liang: /home/rkliang: /usr/local/bin/bash tcliang: *: 1001: T. C. Liang: /home/tcliang: /usr/local/bin/bash /usr/home/tcliang$ perl -e 'print' /etc/passwd /usr/home/tcliang$ perl -ne 'print if /^tcliang/' /etc/passwd tcliang: *: 1001: T. C. Liang: /home/tcliang: /usr/local/bin/bash

/usr/home/tcliang$ date | perl -ne 'print "Today is $_"' Today is Mon Aug 3

/usr/home/tcliang$ date | perl -ne 'print "Today is $_"' Today is Mon Aug 3 20: 59: 56 CST 1998 /usr/home/tcliang$ perl -ne 'print' < /etc/passwd root: *: 0: 0: Charlie &: /root: /usr/local/bin/bash toor: *: 0: 0: Bourne-again Superuser: /root: daemon: *: 1: 1: Owner of many system processes: /root: /nonexistent operator: *: 2: 20: System &: /usr/guest/operator: /bin/csh bin: *: 3: 7: Binaries Commands and Source, , , : /: /nonexistent /usr/home/tcliang$ perl -ne 'print' < emp. first > emp. temp

PERL Scripts PERL at the Command Line • The -c Switch 用來檢測 perl script

PERL Scripts PERL at the Command Line • The -c Switch 用來檢測 perl script 的語法 是否正確,而不真正的執行該 script。 /usr/home/tcliang$ perl -ce 'print if /igor' emp. first Search pattern not terminated at -e line 1. /usr/home/tcliang$ perl -ce 'print if /igor/' emp. first -e syntax OK

PERL Scripts Sample Script /usr/home/tcliang$ cat first. perl #!/usr/bin/perl #My first perl script print

PERL Scripts Sample Script /usr/home/tcliang$ cat first. perl #!/usr/bin/perl #My first perl script print "Hello to you and yours!n"; /usr/home/tcliang$ perl -c first. perl syntax OK /usr/home/tcliang$ perl first. perl Hello to you and yours! /usr/home/tcliang$ chmod +x first. perl /usr/home/tcliang$. /first. perl Hello to you and yours!

Getting a Handle on Printing Filehandle • Filehandle Three predefined I/O streams a UNIX

Getting a Handle on Printing Filehandle • Filehandle Three predefined I/O streams a UNIX shell will open are: – stdin – stdout – stderr Perl does not access these streams directly, but gives them names called filehandles, they are: – STDIN – STDOUT – STDERR The print and printf functions send their output by default to STDUT.

Getting a Handle on Printing Words • Quotes – Single quotes – Double quotes

Getting a Handle on Printing Words • Quotes – Single quotes – Double quotes – Back quotes • Literals (Constants) – Numeric Literals – String Literals – Special Literals ’ ” `

The print Function print (FILEHANDLE LIST); print (LIST); print FILEHANDLE LIST; print; The function

The print Function print (FILEHANDLE LIST); print (LIST); print FILEHANDLE LIST; print; The function returns 1 if successful, 0 otherwise.

/usr/home/tcliang$ cat ex 31 #!/usr/bin/perl print "Hello", "World", "n"; print "Hello Worldn"; /usr/home/tcliang$. /ex

/usr/home/tcliang$ cat ex 31 #!/usr/bin/perl print "Hello", "World", "n"; print "Hello Worldn"; /usr/home/tcliang$. /ex 31 Hello. World Hello World /usr/home/tcliang$ cat ex 32 #!/usr/bin/perl print Hello, world, "n"; /usr/home/tcliang$. /ex 32 No comma allowed after filehandle at. /ex 32 line 2.

The print Function Printing Numeric Literals /usr/home/tcliang$ cat ex 33 #!/usr/bin/perl print "The price

The print Function Printing Numeric Literals /usr/home/tcliang$ cat ex 33 #!/usr/bin/perl print "The price is $100. n"; print "The price is $", 100, ". n"; print "The number is ", 0777, ". n"; print "The number is ", 0 x. Abc. F, ". n"; print "The unformatted number is ", 14. 56, ". n"; /usr/home/tcliang$. /ex 33 The price is $100. The number is 511. The number is 43983. The unformatted number is 14. 56.

The print Function Printing String Literals /usr/home/tcliang$ cat ex 34 #!/usr/bin/perl print "***t. In

The print Function Printing String Literals /usr/home/tcliang$ cat ex 34 #!/usr/bin/perl print "***t. In double quotest***n"; print '%%%t. In single quotest%%%n'; /usr/home/tcliang$. /ex 34 *** In double quotes *** %%%t. In single quotest%%%n

The print Function Printing Dtring Literals /usr/home/tcliang$ cat ex 35 #!/usr/bin/perl print "att. The

The print Function Printing Dtring Literals /usr/home/tcliang$ cat ex 35 #!/usr/bin/perl print "att. The UnumberE LISE ", 0777, , "n"; /usr/home/tcliang$. /ex 35 The NUMBER is 511

The print Function Printing Special Literals /usr/home/tcliang$ cat ex 36 #!/usr/bin/perl print "We are

The print Function Printing Special Literals /usr/home/tcliang$ cat ex 36 #!/usr/bin/perl print "We are on line number ", __LINE__, ". n"; print "The name of this file is ", __FILE__, ". n"; __END__ The _END_ literal is like ctrl-d or 04. Anythings after _END_ will be ignored by Perl. /usr/home/tcliang$. /ex 36 We are on line number 2. The name of this file is. /ex 36.

The print Function • The -w switch /usr/home/tcliang$ cat ex 37 #!/usr/bin/perl -w print

The print Function • The -w switch /usr/home/tcliang$ cat ex 37 #!/usr/bin/perl -w print STDOUT ellie, "t. The price is $100. n"; /usr/home/tcliang$. /ex 37 Unquoted string "ellie" may clash with future reserved word at. /ex 37 line 2. ellie The price is $100. • The Strict Pragma and Words /usr/home/tcliang$ cat ex 38 #!/usr/bin/perl use strict "subs"; $name=Ellie; print "Hi $name. n"; /usr/home/tcliang$. /ex 38 Bareword "Ellie" not allowed while "strict subs" in use at. /ex 38 line 3. Execution of. /ex 38 aborted due to compilation errors.

The printf Function printf(FILEHANDLE FORMAT, LIST); printf(FORMAT, LIST); printf FILEHANDLE FORMAT, LIST; printf FORMAT,

The printf Function printf(FILEHANDLE FORMAT, LIST); printf(FORMAT, LIST); printf FILEHANDLE FORMAT, LIST; printf FORMAT, LIST; Example: printf(”The name is %s and the number is %dn”, ”John”, 50);

The printf Function Format Specifiers Conversion Character c s d ld u lu x

The printf Function Format Specifiers Conversion Character c s d ld u lu x lx o lo e f g Definition Character String Decimal number Long decimal number Unsigned decimal number Long unsigned decimal number Hexadecimal number Long hexadecimal number Octal number Long octal number Floating point number in scientific notation Floating point number using either e or f conversion, whichever takes the least space

The printf Function Modifiers Character Definition - Left justification modifier # Integers in octal

The printf Function Modifiers Character Definition - Left justification modifier # Integers in octal format are displayed with a leading 0; intergers in hexadecimal form are displayed with a leading 0 x. + For conversions using e, e, f, and g, integers are displayed with a numeric sign + or -. 0 The displayed value is padded with zeros instead of white space.

The printf Function /usr/home/tcliang$ cat ex 310 #! /usr/bin/perl printf "Hello to you and

The printf Function /usr/home/tcliang$ cat ex 310 #! /usr/bin/perl printf "Hello to you and yours %s!n" , "Sam Mc. Goo" ; printf "The number in decimal is %dn", 45; printf "The formatted number is |%10 d|n", 100 ; printf "Left justified the number is |%-10 d|n", 100 ; printf "The number in octal is %on", 15 ; printf "The number in hex is %xn", 15 ; printf "The floating point number is |%8 f|n", 15 ; printf "The formatted floating point number is |%8. 2 f|n", 14. 3456 ; printf "The character is %c. n", '65' ; printf "The number in scientific notation is %en", 15. 25 /usr/home/tcliang$. /ex 310 Hello to you and yours Sam Mc. Goo! The number in decimal is 45 The formatted number is | 100| Left justified the number is |100 | The number in octal is 17 The number in hex is f The floating point number is |15. 000000| The formatted floating point number is | 14. 35| The character is A. The number in scientific notation is 1. 525000 e+01

Printing without Quotes The Here Document /usr/home/tcliang$ cat ex 311 #!/usr/bin/perl $price=100; print <<EOF;

Printing without Quotes The Here Document /usr/home/tcliang$ cat ex 311 #!/usr/bin/perl $price=100; print <<EOF; The price of $price is right. EOF print <<'FINIS'; The price of $price is right. FINIS print << x 4; Today is a sunny day. print <<`END` echo Hi there echo -n "The time is " date END /usr/home/tcliang$. /ex 311 The price of 100 is right. The price of $price is right. Today is a sunny day. Hi there The time is Thu Aug 20 23: 16: 44 CST 1998

Variables in PERL 在每一個 package 裡為該 package 所用到的變 數以名稱空間(Name Space)來維持其資料結構,不同 型態的變數,有其各自的名稱空間,故不以取相通的 名稱而有衝突。 Package main

Variables in PERL 在每一個 package 裡為該 package 所用到的變 數以名稱空間(Name Space)來維持其資料結構,不同 型態的變數,有其各自的名稱空間,故不以取相通的 名稱而有衝突。 Package main Scalar Name Space $department Array Name Space @department $department[0] Associative Array Name Space %department $department{’Ed’}

Scalars, Arrays, and Hashes • Scalar Variables Assignment $number = 150; $name = ”Jody

Scalars, Arrays, and Hashes • Scalar Variables Assignment $number = 150; $name = ”Jody Savage”; Curly Braces $var = net; print ”${var}workn”; Define and undefine $name = ”Tommy”; print ”Ok n” if defined $name; undef $name; The $_ Scalar Variable

Scalars, Arrays, and Hashes #!/usr/bin/perl $num = 5; $friend = “John Smith”; $money =

Scalars, Arrays, and Hashes #!/usr/bin/perl $num = 5; $friend = “John Smith”; $money = 125. 75; $now = `date`; $month = Jan; print “$numn”; print “$friendn”; print “I need $$money. n”; print qq/$friend gave me $$money. n”; print qq/ The time is $now/; print “The month is ${month}uary. n”;

Scalars, Arrays, and Hashes

Scalars, Arrays, and Hashes

Numeric Literals Example Description 12345 0 x 456 ff 0777 23. 45. 234 E-2

Numeric Literals Example Description 12345 0 x 456 ff 0777 23. 45. 234 E-2 Integer Hex Octal Float Scientific notation

String Literals • Strings are normally delimited by either single or double quotes. •

String Literals • Strings are normally delimited by either single or double quotes. • Strings containing string literals (escape sequences) are delimited by double quotes for backslash interpretation. Escape Sequences t n r f b a e 33 xff Description Tab Newline Carriage return Form feed Backspace Alarm/Bell Escape Octal character Hexadecimal character

Escape Sequences c[ l u L U Q E \ Description Control character Next

Escape Sequences c[ l u L U Q E \ Description Control character Next character is converted to lower case Next character is converted to upper case Next characters is converted to lower case until E is found Next characters is converted to upper case until E is found Backslash all following nonalphanumeric characters until E is found Ends upper or lower case conversion started with L or U Backslash

Special Literals Literal __LINE__ __FILE__ __END__ Description Represents the current line number Represents the

Special Literals Literal __LINE__ __FILE__ __END__ Description Represents the current line number Represents the current file name Represents the logical end of the script; trailing garbage is ignored. 注意:__LINE__, __FILE__, __END__ 均為連續兩個底線字元置於前後。