perl语言讲义04

上传人:灯火****19 文档编号:142981351 上传时间:2020-08-25 格式:PDF 页数:7 大小:215.56KB
返回 下载 相关 举报
perl语言讲义04_第1页
第1页 / 共7页
perl语言讲义04_第2页
第2页 / 共7页
perl语言讲义04_第3页
第3页 / 共7页
perl语言讲义04_第4页
第4页 / 共7页
perl语言讲义04_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《perl语言讲义04》由会员分享,可在线阅读,更多相关《perl语言讲义04(7页珍藏版)》请在金锄头文库上搜索。

1、Spring, 2009 1 Perl 入门和提高Lesson 4 周晓方 coursesxfzhou.homeftp.org Spring, 2009 2 Statements - - -perlsyn Perl语法:查看perlsyn网页 复合语句,或叫块语句 语句;语句;语句; Comments in Perl # This is the perl style comments. /* C style */ is not for perl, nor / C+ sytle comments Comment out block of code if (0) some valid perl c

2、ode PoC (Plain old comments) # line 200 bzzzt # the previous # must on the first column die foo; #格式是 顶格的# 可选的空格 行号 文件名 foo at bzzzt line 201. Spring, 2009 3 倒装修饰的简单语句 Simple statement + modifier Simple_stat modifier Cond; #if/unless/while/until/foreach !Always evaluates Condition before execute Exp

3、ression! do BLOCK while cond; run BLOCK once before evaluate cond print $A is negitive! if $A 0; die SOS! if ($fail); $B=1/$A unless $A = 0; ($sum, $j) = (0, 1); do $sum += $j while +$j 10); print $J; # = 55 perl -e print while ; readme.txt Spring, 2009 4 Control Flow 1 False conditions: 0.000, 0, u

4、ndef, 0, (but 0.0 00 is true) If statement: if () # Multi-choice: # not else if # not elseif # but elsif if () elsif () elsif () elsif () else if () else # compare modified statement expression if ; expression unless ; Spring, 2009 5 Control Flow 2 while loopuntil loop nextjump to continue block and

5、 check condition redojump to beginning of loop, no continue block or cond check lastexit loop immediately # loop before # check do while (); # redo, next, last not allowed. while () redo if any; next if any; last if any; continue # run before # next # iteration # check # before loop while () # loop

6、before # check do until (); # similar to do while # check # before loop until () Spring, 2009 6 Control Flow 3 for loop for (start_exp; condition_exp; step_exp) for ($n=1, $sum=0; $sum方法(参数) 的一些例子: 调用模块 use Win32:Clipboard; 生成一个剪贴板对象 my $Clip = Win32:Clipboard(); 监视剪贴板变化 $Clip-WaitForChange() 判断是否是字

7、符格式 $Clip-IsText() 获得剪贴板的文本内容 $Clip-GetText() Spring, 2009 11 Subroutine I 参数在_中,直接修改_数组的元素 $_i 有副作用 子程序名称避免全大写 Declare all local variables using my( ) 如果预先申明的函数, 则在调用时可以省略括号 sub 函数名; use subs qw(函数名 函数名 函数名); Check context with wantarray( )确定调用的上下文 调用时,函数名可加前缀# pre declaire, 一般没有必要预先声明 # 但先作函数申明或定义

8、,调用函数时就可以省略括号 $res = name($a, $b);# call subroutine res = name $a, $b;# also can say # copy arguments my($arg2) = shift; my($result, result);# declare local variables wantarray() ? result : $result;# return result Spring, 2009 12 Subroutine II Copy arguments from _ (子程序先复制_的内容) Function with side ef

9、fect, _传递实际参数的别名 (直接修改_的元素,有副作用) 对_作shift,不改变数组的元素,无副作用 sub name # define subroutine my $arg1 = shift _; my $ary2 = shift; # the same as shift _ my($arg3, $arg4) = _; # copy arguments my(list) = _;# or in this format sub Side_effect $_0 = $_0 * 2; my $a = 5; Side_effect($a); # $a becomes 10 now. Sid

10、e_effect(5); # fatal run-time error. 5 is constant Spring, 2009 13 算pi的一个例子程序 #!/usr/bin/perl -w use strict; print 10:t , pi(10), n; print 100:t , pi(100), n; print 1000:t , pi(1000), n; print 10000:t , pi(10000), n; print 100000:t , pi(100000), n; print 1000000:t , pi(1000000), n; sub pi my($count)

11、 = $_0; my($inside); $inside += sqr(rand 1) + sqr(rand 1) 0; 4 / $_0 * $inside; sub sqr my($n) = shift; return $n * $n; 1; 蒙特卡洛单位圆 法近似计算 Spring, 2009 14 #!/usr/bin/perl -w use strict; print 10:t , pi(10), n; print 100:t , pi(100), n; print 1000:t , pi(1000), n; print 10000:t , pi(10000), n; print 100000:t , pi(100000), n; print 1000000:t , pi(1000000), n; sub pi my($count) = shift; my(distance) = map sqr(rand(1) + sqr(rand(1) 1, 1.$count; 4 / $count * scalar grep /1/, distance; sub sqr my($n) = shift; return $n * $n; 1; 算pi的又一个例子程序 算法相同 占用更多资源

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号