﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-小小程序员-随笔分类-脚本语言</title><link>http://www.blogjava.net/yuoveyu/category/45390.html</link><description>小小程序员</description><language>zh-cn</language><lastBuildDate>Mon, 21 Jun 2010 02:29:11 GMT</lastBuildDate><pubDate>Mon, 21 Jun 2010 02:29:11 GMT</pubDate><ttl>60</ttl><item><title>perl记事本</title><link>http://www.blogjava.net/yuoveyu/archive/2010/05/06/320202.html</link><dc:creator>余坚</dc:creator><author>余坚</author><pubDate>Thu, 06 May 2010 06:29:00 GMT</pubDate><guid>http://www.blogjava.net/yuoveyu/archive/2010/05/06/320202.html</guid><wfw:comment>http://www.blogjava.net/yuoveyu/comments/320202.html</wfw:comment><comments>http://www.blogjava.net/yuoveyu/archive/2010/05/06/320202.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/yuoveyu/comments/commentRss/320202.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yuoveyu/services/trackbacks/320202.html</trackback:ping><description><![CDATA[<div style="margin: 6px; padding: 0px; font-family: Verdana; font-size: 10pt; color: rgb(0, 0, 0); min-height: 1100px; counter-reset: __goog_page__ 0; line-height: normal; background-color: rgb(255, 255, 255);">
<div style="margin-top: 0px; margin-bottom: 0px;">perl记事本<br />
<br />
1. 'yu' x 3&nbsp;&nbsp;&nbsp; #&nbsp; print yuyuyu<br />
<br />
2.print "yu jian ${age}s" # 加{}&nbsp; 促使变量不会变成$ages<br />
<br />
3. 4 ** 2&nbsp; #4的平方<br />
<br />
4.(1..5) # &nbsp;--&gt;(1,2,3,4,5)<br />
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">5.qw ( yu jian's book) &nbsp;# 简洁，更少输入</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">6.($yu,$jian)=($jian,$yu) # 值互换</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">7.($yu,$jian)=qw(yu jian) # 赋值</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">8.@array=qw/yu jian/; #数组&nbsp;</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">9.@array=5..9;</div>
<div style="margin-top: 0px; margin-bottom: 0px;">$yu=pop(@array) &nbsp;# $yu=9 &nbsp;and @array=(5,6,7,8)</div>
$yu=pop @array &nbsp;# $yu=8 &nbsp;and @array=(5,6,7)<br />
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">10.如果数组为空 pop不会报错，什么也不返回，直接返回undef<br />
<br />
</div>
11.push(@array,0) &nbsp;# @array=(5,6,7,0)<br />
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">12.pop和push都操作数组末尾</div>
<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">13.shift和unshift是对数组的开始端操作</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">14.$_ &nbsp;perl的默认变量</div>
<div style="margin-top: 0px; margin-bottom: 0px;">for(1...10){</div>
<div style="margin-top: 0px; margin-bottom: 0px;">&nbsp;&nbsp; &nbsp;print $_; &nbsp;# $_ = 1 or 2 or 3...</div>
<div style="margin-top: 0px; margin-bottom: 0px;">}</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">15.perl会正确处理上下文</div>
<div style="margin-top: 0px; margin-bottom: 0px;">@people=qw{yu gao};</div>
<div style="margin-top: 0px; margin-bottom: 0px;">@list=@people #得到列表</div>
<div style="margin-top: 0px; margin-bottom: 0px;">$n=@people #得到人数2</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">16.scalar @array #scalar 导致切换为标量上下文</div>
<div style="margin-top: 0px; margin-bottom: 0px;">@array =qw/yu jian gao su/;</div>
<div style="margin-top: 0px; margin-bottom: 0px;">print "love is ".@array."\n"; #love is 4</div>
<div style="margin-top: 0px; margin-bottom: 0px;">print "love is ",@array,"\n"; #love is yujiangaosu</div>
<div style="margin-top: 0px; margin-bottom: 0px;">print "love is ".scalar @array,"\n";#love is 4</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">17.@lines=&lt;STDIN&gt; #在列表上下文中读取标准输入</div>
<br />
<br />
18.chomp 去掉所有的换行符 chmop(@lines)<br />
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">19.调用自定义方法 &nbsp;&amp;methodName;</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">20.sub max{</div>
<div style="margin-top: 0px; margin-bottom: 0px;">&nbsp;&nbsp; &nbsp;if(@_!=2){ &nbsp;#判断参数个数</div>
<div style="margin-top: 0px; margin-bottom: 0px;">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print "waning!!!"；<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return -1;<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">&nbsp;&nbsp; &nbsp;}<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">&nbsp;&nbsp; &nbsp;my($m,$n); #创建新的所有变量<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">&nbsp;&nbsp; &nbsp;($m,$n)=@_; #将参数赋值给变量<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">&nbsp;&nbsp; &nbsp;if($m &gt; $n){$m}else{$n}</div>
<div style="margin-top: 0px; margin-bottom: 0px;">}<br />
<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">21.my($num)=@_ #获取数组的第一个元素</div>
<div style="margin-top: 0px; margin-bottom: 0px;">&nbsp;&nbsp; &nbsp;my $num=@_ #数组的数量</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">22.use strice ; #强制使用一些严格的良好的程序语言规则<br />
<br />
23.sub division{</div>
<div style="margin-top: 0px; margin-bottom: 0px;">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$_[0] / $_[1]; #第一个参数和第二个参数<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">}<br />
<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">24.@ARGV=qw# file1 file2 #;# 强制让砖石操作符读取这两个文件</div>
<div style="margin-top: 0px; margin-bottom: 0px;">while(&lt;&gt;){</div>
<div style="margin-top: 0px; margin-bottom: 0px;">&nbsp;&nbsp; &nbsp;chomp;<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">&nbsp;&nbsp; &nbsp;print "It was $_\n":<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">}<br />
<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">25.printf "Hello, %s; your password in %d days!\n",$user,$days;#格式化输出</div>
<div style="margin-top: 0px; margin-bottom: 0px;">%d 整数 %g 浮点数</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">27.die处理错误输出</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">28.读取文件</div>
<div style="margin-top: 0px; margin-bottom: 0px;">open CONFIG,"c:/test.txt";</div>
<div style="margin-top: 0px; margin-bottom: 0px;">while(&lt;CONFIG&gt;){</div>
<div style="margin-top: 0px; margin-bottom: 0px;">print $_."\n";</div>
<div style="margin-top: 0px; margin-bottom: 0px;">}</div>
<br />
29. say == print &nbsp;xxxx ."\n" &nbsp;# say &nbsp;相当 等于print 加换行符<br />
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">30.hash</div>
<div style="margin-top: 0px; margin-bottom: 0px;">$family_name{"fred"}="flint";</div>
<div style="margin-top: 0px; margin-bottom: 0px;">$family_name{"bar"} ="rubble";</div>
<br />
%family_name("fred","flint","bar","rubble");<br />
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">my %family_name=(</div>
<div style="margin-top: 0px; margin-bottom: 0px;">&nbsp;&nbsp; &nbsp;"fred" =&gt; "flint",<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">&nbsp;&nbsp; &nbsp;"bar" =&gt; "rubblle";<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">)</div>
<br />
my @k=keys %family_name;
<div style="margin-top: 0px; margin-bottom: 0px;">my @v=values %family_name; &nbsp;#return array</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">31.</div>
<div style="margin-top: 0px; margin-bottom: 0px;">each函数</div>
<div style="margin-top: 0px; margin-bottom: 0px;">while( ($key,$value)=each %family_name ){</div>
<div style="margin-top: 0px; margin-bottom: 0px;">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print "$key =&gt; $value\n";<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">}</div>
<div style="margin-top: 0px; margin-bottom: 0px;">exists函数</div>
<div style="margin-top: 0px; margin-bottom: 0px;">if(exists $family_name("dino")){</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">}</div>
<div style="margin-top: 0px; margin-bottom: 0px;">delete函数</div>
delete $family{"fred"};<br />
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">32.\1,\2使用反向引用</div>
<div style="margin-top: 0px; margin-bottom: 0px;">$_=""abba;</div>
<div style="margin-top: 0px; margin-bottom: 0px;">if(/(.)\1/){#匹配到 bb</div>
<div style="margin-top: 0px; margin-bottom: 0px;">&nbsp;&nbsp; &nbsp;print "it matched";<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">}</div>
<br />
33. \d = [0-9]
<div style="margin-top: 0px; margin-bottom: 0px;">\w = [A-Za-z0-9_]</div>
[^\d] 非数字
<div style="margin-top: 0px; margin-bottom: 0px;">[^\w]非词</div>
<div style="margin-top: 0px; margin-bottom: 0px;">[^\s]非空白字符</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">34./yes/i &nbsp;# /i 不区分大小写</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">35./s 匹配任意字符 ， 不包括换行</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">36./x运行加入空白，便于阅读理解</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">37.m//模式匹配搜索功能</div>
<div style="margin-top: 0px; margin-bottom: 0px;">S/// 模式匹配替换功能</div>
<div style="margin-top: 0px; margin-bottom: 0px;">/g 全局替换</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">38.@fields = split /separator/ , $string;</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">39.my $result = join $glue,@pieces;</div>
<div style="margin-top: 0px; margin-bottom: 0px;">my $x = join ":",4,5,6; # $x = "4:5:6";</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">40.*贪婪 *？非贪婪版本</div>
<br />
41.从命令行进行在线编辑
<div style="margin-top: 0px; margin-bottom: 0px;">perl -p -w -e "s/ran/ra/g" fred.dat</div>
<div style="margin-top: 0px; margin-bottom: 0px;">-p 打印 &nbsp;-n 去掉自动打印 &nbsp;-w 打开警告选项 &nbsp;-e 表示后面更得是代码，而不是普通的参数</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">42.last 类似break&nbsp;</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">43.my $stuff="Howdy world!";</div>
<div style="margin-top: 0px; margin-bottom: 0px;">my $where=index($stuff,"wor"); # $where = &nbsp;6<br />
<br />
</div>
<div style="margin-top: 0px; margin-bottom: 0px;">44.my $mineral=substr("Fred J. Flintstone",8,5); # 值为Flint</div>
<br />
<div style="margin-top: 0px; margin-bottom: 0px;">45.智能匹配对应不同德操作数，不同德处理方式<br />
%a ~~ %b&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 哈希的键是否一致<br />
%a ~~@b&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 至少%a中的一个键在列表@b之中<br />
%a ~~ /Fred/&nbsp;&nbsp;&nbsp;&nbsp; 至少一个键匹配给定的模式<br />
%a ~~ 'Fred'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 哈希中某一指定键$a{'Fred'} 是否存在<br />
@a ~~ /Fred/&nbsp;&nbsp;&nbsp;&nbsp; 有一个元素匹配给定的模式<br />
@a ~~ 123&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 至少有一个元素转化为数字后事123<br />
<br />
$name ~~ undef&nbsp; $name确实尚未定义<br />
<br />
46.system 启动子进程<br />
system "date";<br />
<br />
14.my $tarfile="some*.tar";<br />
my @dirs=qw(fred|flinet&lt;bar&amp;rubble&gt; betty);<br />
system "tar"."cvf",$tarfile,@dirs;&nbsp; 带参数的系统命令调用<br />
<br />
47.system的所有语法对exec都适用<br />
执行exec类似goto语句，跳转到另个进程进行执行，当前perl进程会关闭<br />
chdir "/tmp" or die "cannot chdir /tmp: #!";<br />
ecec "bedrock","-o","args1",@ARGV;<br />
<br />
48.$ENV{'PATH'}="/home/rootbeer/bin:$ENV{'PATH'}";<br />
delete $ENV{'IFS'};<br />
my $make_result= system "make";<br />
改修改并不能影响shell或者其他父进程<br />
<br />
49.my $now =`date`;<br />
print "The time is now $now";<br />
魔力反引号调用进程<br />
<br />
50.perl -p -i.old -e "s/try//g" test.txt 替换文件中的内容,重新写入文件， 原文件备份为old后缀的文件.<br />
windows下要用双引号<br />
<br />
51.<br />
<br />
</div>
</div>
<img src ="http://www.blogjava.net/yuoveyu/aggbug/320202.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yuoveyu/" target="_blank">余坚</a> 2010-05-06 14:29 <a href="http://www.blogjava.net/yuoveyu/archive/2010/05/06/320202.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>