资源预览内容
第1页 / 共100页
第2页 / 共100页
第3页 / 共100页
第4页 / 共100页
第5页 / 共100页
第6页 / 共100页
第7页 / 共100页
第8页 / 共100页
第9页 / 共100页
第10页 / 共100页
亲,该文档总共100页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
1,Thinking in Perl -Perl DBI,Charlie Cao (Cao, Xiaobing) CMB Ext. 78272,2,Contents,Overview Data Types Variables Common Predefined Variables Constants Reference Control Structures Terseness Regular Expression Subroutines Using Modules DBI (Database Interface) Convert DBLib to DBI (DBD),3,What is Perl?,Perl is short for “ Practical Extraction and Report Language“ (Pathologically Eclectic Rubbish Lister) designed by Larry Wall (12, Dec. 1987). Perl is a general-purpose programming language originally developed for text manipulation and now used for a wide range of tasks including system administration, web development, network programming, GUI development, and more. The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal). Its major features are that its easy to use, supports both procedural and object-oriented (OO) programming, has powerful built-in support for text processing, and has one of the worlds most impressive collections of third-party modules.,4,Running Perl programs,Windows (ActivePerl 5.10, Download): perl sample.pl sample.pl perl sample.pl perl -e “print hello world!“ Unix/Linux (Perl 5.8.0): perl sample.pl chmod 0777 (a+x) sample.pl sample.pl (#!/export/opt/perl/5.8.0/bin/perl) perl sample.pl perl -e “print hello world!“,5,Perldoc command,perldoc perl Overview of Perl language perldoc module name (e.g DBI) help doc of the module perldoc f (subroutine name (e.g split) help doc of the subroutine,6,Emulated IDE- Ultra Edit,7,Emulated IDE- Ultra Edit,8,Emulated IDE- Ultra Edit,9,Data Types,Scalar Integer 0 -40 255 61298040283768 61_298_040_283_768 Float 1.25 255.000 7.25e45 -6.5e24 -12e-24 Octal 047 49 (Dec) Hex 0x1f 31 (Dec) Binary 0b11111111 255 (Dec),10,Data Types,Scalar String Double quote: $result = 14; print (“The value of $result is $result.n“) #The value of $result is 14.,11,Data Types,Scalar String Single quote: $text = This is two lines of text ; $text = “This is twonlines of textn“; Subroutines: q /Hello world/ # gets Hello world qq /Hello world/ # gets “Hello world” length($string); substr(string, offset, length) uc/lc(string) join(expr, list) # $text = join(, lines); ltrim/rtrim(string),12,Data Types,Scalar String/Numeric conversion $string = “43“; $number = 28; $result = $string + $number; # $result = 71 $result = $string.$number; # $result = “4328“ $result = $string x 3; # $result = “434343“ $result = “hello“ * 5; # $result = 0 $result = “12a34“ +1; # $result = 13,13,Data Types,List Samples: (1,2,3) (1,2,3,) () (1 100) (1757) (5 1) (0,2 6,10,12) ($m $n) (0 $#array) (“fred”, “barney”, “betty”, “wilma”, “dino”) ($rocks0,$rocks1,$rocks2,$rocks3) = (“fred”, “barney”, “betty”, “wilma”, “dino”); rocks = (“fred”, “barney”, “betty”, “wilma”, “dino”); ($fred, $barney) = ($barney, $fred); copy = quarry; $abc=“00“,“01“,“10“,“11“; $var=$abc-11; Subroutines: QW qw(fred barney betty wilma dino) qw ! fred barney betty wilma dino ! qw # fred barney betty wilma dino # qw fred barney betty wilma dino ($rocks0,$rocks1,$rocks2,$rocks3) = qw/talc mica feldspar quartz/; rocks = qw / bedrock slate lava /; ($fred, $barney) = qw ;,14,Data Types,List Subroutines: pop/push array = 59; $fred = pop(array); #$fred = 9,array =(5,6,7,8) $barney = pop array; #$barney = 8, array =(5,6,7) pop array; #array =(5,6) push(array,0); #array = (5,6,0) push array,8; #array =(5,6,0,8) push array,110; #array others =qw/9 0 2 1 0 /; push array,others;,15,Data Types,List Subroutines: shift/unshift array = qw# dino fred barney #; $m = shift (array); #$m = “dino”, array = (“fred”, “barney”) $n = shift array; #$n = ”fred”, array =(“barney”) shift array; #array is empty $o = shift array; #$o = undef, array is still empty unshift(array,5); #array =(5) unshift array,4; #array = (4,5) others = 13; unshift array, others; #array =(1,2,3,4,5),16,Data Types,List Subroutines: reverse fred = 6 10; barney = reverse (fred); #gets 10,9,8,7,6 sort rocks = qw/ bedrock slate rubble granite /; sorted = sort(rocks); #gets bedrock, granite, rubble, slate back = reverse sort rocks; #from slate to bedrock rocks = sort rocks; #gets sorted rocks numbers = sort 97 102; #gets 100,101,102,97,98,99 sort rocks; #error, the subroutine must have a return value split $info = “Caine:Micheal:Actor:14,Leafy Drive”; personal = split(/:/,$info); #personal = (“Caine“, “Michael“, “Actor“, “14, Leafy Drive“);,17,Data Types,Scalar/List Context 42 + something #something must be a scalar sort something #something must be a list people = qw( fred barney betty ); $number = people; # $number = 3 sorted = sort people; #list context:barney , betty, fred $number = 42 + people; #scalar context:42+3, $number = 45 backwards = reverse qw / yabba dabba doo /; #gets doo, dabba, yabba $backwards = reverse qw/ yabba dabba doo /; #gets oodabbadabbay ro
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号