﻿<?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>IT博客-金鳞-文章分类-perl</title><link>http://www.cnitblog.com/chenxin9821/category/7386.html</link><description>目标-&gt;软件测试架构师</description><language>zh-cn</language><lastBuildDate>Mon, 26 Sep 2011 19:05:17 GMT</lastBuildDate><pubDate>Mon, 26 Sep 2011 19:05:17 GMT</pubDate><ttl>60</ttl><item><title>用perl分析apache日志</title><link>http://www.cnitblog.com/chenxin9821/articles/58863.html</link><dc:creator>金鳞</dc:creator><author>金鳞</author><pubDate>Wed, 27 May 2009 07:56:00 GMT</pubDate><guid>http://www.cnitblog.com/chenxin9821/articles/58863.html</guid><wfw:comment>http://www.cnitblog.com/chenxin9821/comments/58863.html</wfw:comment><comments>http://www.cnitblog.com/chenxin9821/articles/58863.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/chenxin9821/comments/commentRss/58863.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/chenxin9821/services/trackbacks/58863.html</trackback:ping><description><![CDATA[原文出处：<a href="http://hi.baidu.com/smallfish_xy/blog/item/2bc4d4232d23e0ac4723e822.html">http://hi.baidu.com/smallfish_xy/blog/item/2bc4d4232d23e0ac4723e822.html<br></a><br><strong>功能简述<br><br></strong>&nbsp;&nbsp;&nbsp; 统计出日志里一个或多个页面总共访问的次数，比如aa.jsp, bb.jsp这样页面分别多少次。<br><br><strong>实现简述</strong><br><br>&nbsp;&nbsp;&nbsp; Apache单个日志文件800M。<br><br>&nbsp;&nbsp;&nbsp; 最初程序使用Python编写，按行来统计，分别使用in(最慢)和index方法去查找，然后使用了正则匹配，程序运行时间从最初的1分50多秒优化到1分10秒左右，参考了<strong><a href="http://www.dup2.org/node/1006" target=_blank><u><font color=#800080>qyb</font></u></a></strong>博客中提到的gc.disable()，有了一定的提升，最终还是需要1分左右。<br><br>&nbsp;&nbsp;&nbsp;&nbsp; 然后随意用了Perl写了一个，用了最土鳖的&lt;LOG&gt;这样的按行分析，最后正则匹配，然后++，速度竟然在40-50秒之间，惊叹！后来经过<strong><a href="http://shucho.org/blog/" target=_blank><u><font color=#800080>shucho</font></u></a></strong>指点，在正则部分采用了预编译，效果那是相当惊人！800M文件只用了7秒左右。<br><br><strong>程序片段</strong><br><br># --------------------------------------------------------------------<br>use strict;<br>use Benchmark;<br><br>my $LOG_FILE = '/usr/local/apache/logs/access.log';<br><span style="COLOR: red">#</span> <font color=#ff0000>下面qr部分起了关键作用，预编译了表达式。</font><br>my @EXT_LIST = map {qr/$_/} qw{<br>aaServlet<br>bbServlet<br>};<br><span style="COLOR: red">#qr表示建立正则；map映射，功能可以理解为foreach。<br></span><br>my $startime = new Benchmark;<br>my %result;<br>map {$result{$_} = 0} @EXT_LIST; <span style="COLOR: red">#赋初值<br></span>open LOG_FILE, $LOG_FILE;<br>while (&lt;LOG_FILE&gt;){ <br>&nbsp;&nbsp;&nbsp; foreach my $ext (@EXT_LIST) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { $result{$ext}++ if $_ =~ /$ext/; }<br>}<br>close LOG_FILE;<br><br>while (my ($key, $value) = each(%result)){ <br>&nbsp;&nbsp;&nbsp; $key =~ s/\(\?-xism:(.*?)\)/$1/g;<br>&nbsp;&nbsp;&nbsp; print "$key:\t$value\n"; <br>}<br><br>printf "** %s\n\n", timestr(timediff(new Benchmark, $startime));<br>
<img src ="http://www.cnitblog.com/chenxin9821/aggbug/58863.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/chenxin9821/" target="_blank">金鳞</a> 2009-05-27 15:56 <a href="http://www.cnitblog.com/chenxin9821/articles/58863.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>通过脚本学Perl语句</title><link>http://www.cnitblog.com/chenxin9821/articles/52540.html</link><dc:creator>金鳞</dc:creator><author>金鳞</author><pubDate>Thu, 11 Dec 2008 07:48:00 GMT</pubDate><guid>http://www.cnitblog.com/chenxin9821/articles/52540.html</guid><wfw:comment>http://www.cnitblog.com/chenxin9821/comments/52540.html</wfw:comment><comments>http://www.cnitblog.com/chenxin9821/articles/52540.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/chenxin9821/comments/commentRss/52540.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/chenxin9821/services/trackbacks/52540.html</trackback:ping><description><![CDATA[<div style="FONT-SIZE: 10pt">自：<a href="http://www.builder.com.cn/2002/0517/48549.shtml">http://www.builder.com.cn/2002/0517/48549.shtml</a><br><br>&nbsp;作为一名程序员我得应付各种五花八门的客户问题。最近做一个活的时候，编程中要求从某种应用程序中导出用符号分隔的普通文件并且对其进行格式化，这样才能把它导入另一种应用程序。这个文件中的数据包含了3000个数据项，而且项目描述采用了变长字段，我得设法把这种格式转换成3个30字符长的字段。方法有二：使用文本编辑器编辑3000个条目或者写一个Perl脚本来完成这一任务。显然，我当然得选择第2种方案。
<h5>脚本</h5>
<p style="FONT-SIZE: 10pt"><strong><a href="http://builder.com.com/utils/sidebar.jhtml?id=u00220020514HXS01.htm&amp;index=1" target=_target>清单A</a></strong> </span>就是我编写的脚本。这里我也不拿什么算法和程序设计说事，也就直接带你阅读这个脚本程序来掌握Perl 5的主要语句。这个脚本是在Windows 98计算机上用<a href="http://www.activestate.com/">ActiveState</a></span>开发的<a href="http://www.activestate.com/Products/Download/Get.plex?id=ActivePerl">ActivePerl</a></span>编写的。编写和测试脚本用了三个小时，确定基本算法用了大约一个小时，另两个小时是程序的反复精练。<br><br>这是一个&#8220;快餐型&#8221;的脚本程序，不过话又说回来，就说明Perl的关键的功能和构造而言还是足够了</p>
<p style="FONT-SIZE: 10pt" align=left><font color=#000099>$input = &#8220;Inventory.txt&#8221;; #设置$input为符号分隔文件名<br>$output = &#8220;&gt;inv.txt&#8221;; #设置$output为输出文件名</font><br><br>以上的代码显示了Perl的字符串变量定义方式（比如，<em>$variable_name = &#8220;string&#8221;;</em>）。Perl语句用分号结束（;）。用#符号表示注释。</p>
<p style="FONT-SIZE: 10pt" align=left><font color=#000099>open (INV, $input); #打开$openme 供读取数据<br>open (OUTFILE, $output); #打开Inv.txt 供写入数据</font></p>
<p style="FONT-SIZE: 10pt" align=left>Open语句用来打开文件以实现读、写或添加操作。Open语句的第1个参数是将为其他语句作为参数使用的文件句柄（filehandle）的名字（文件句柄是一种数据结构，Perl脚本通过它可以访问文件）。第1个参数是包含文件名字符串的变量。在我定义 $output 时我用 &gt; 告诉解释器打开Inv.txt写入数据。要是为添加数据而打开文件的话就要用到 &gt;&gt; 了。</p>
<p style="FONT-SIZE: 10pt" align=left>while语句在执行花括号内的代码之前会对执行条件进行测试。当条件测试为false时即退出循环。在这种情况下，循环会连续执行直到到达文件末尾。</p>
<p style="FONT-SIZE: 10pt"><font color=#000099>$offset =0; #最初的30字符字段偏移<br>$TheLine = $_;&nbsp; #设置$TheLine为当前行</font><br><br>接下来的这两行代码声明和初始化输出字符串格式化所需要的两个整数变量。第二行把输入文件的当前行赋给$TheLine变量。Perl用$_当作默认的输入和模式匹配变量。在这个例子中，它的值是由while语句赋予的。</p>
<p style="FONT-SIZE: 10pt"><font color=#000099>chomp($TheLine); #删除换行符</font><br><br>chomp语句用来删除行终止符，例如换行符等。</p>
<p style="FONT-SIZE: 10pt" align=left><font color=#000099>@cols = split(" ",$TheLine); #占位符分隔</font><br><br>以上语句介绍了Perl编程中两个不可或缺的方面。Perl语言中的数组以@打头而且被分配了一列元素。split语句有两个参数：将被分隔的字符和被分隔的字符串。Split返回元素列表。</p>
<p style="FONT-SIZE: 10pt" align=left><font color=#000099>$splitme = $cols[6];&nbsp; #把第7列的字符串赋给$splitme（项目描述）</font><br><br>上句的作用是指向数组中的元素。数组按照从0到(N-1)的序列索引，其中的N是元素总数。</p>
<p style="FONT-SIZE: 10pt" align=left><font color=#000099>@splitup = split('',$splitme); #把字符串转换为字符数组<br>if ($#splitup &gt;= 30) { #如果超过30个字符</font><br><br>以上代码的第二行介绍了第二种条件表达式：if语句。如果If语句括号内的条件得到满足则执行相应的代码。Perl用$#array_name在数组中存储最后一个元素的索引。<br><br><font color=#000099>for($i = 0; $i &lt;= 10; $i++) { #找出最后10个字符中的第1个空格</font><br><br>现在介绍for循环。Perl语言中的for循环结构类同C、C++或Java等编程语言中的for循环。For循环的语法是这样的：for(initial condition; exit condition; increment statement).</p>
<p style="FONT-SIZE: 10pt" align=left><font color=#000099>if ($splitup[(30 - $i)]=~ /W/) { #检查空格</font><br><br>这条 <em>if</em>语句中的条件又是一个新概念。这里，我采用了所谓的正则表达式，具体地说就是模式匹配。如果字符串包含空格该语句将返回true。模式匹配条件的语法如下所示：<font color=#000099>$string =~ /pattern/</font>.<br><br><strong><a href="http://builder.com.com/utils/sidebar.jhtml?id=u00220020514HXS01.htm&amp;index=2" target=_target>清单B</a></strong> </span>中的最后一行代码演示了 <em>join</em>函数。<em>Join</em>差不多就是<em>split</em>的反面。<em>Join</em>也有两个参数：分隔符和元素数组。<em>Join</em>会返回一个字符串，这个字符串是通过连接数组元素并在每两个元素之间放置分隔符之后构成的。<br><br>在<strong><a href="http://builder.com.com/utils/sidebar.jhtml?id=u00220020514HXS01.htm&amp;index=3" target=_target>清单C</a></span></strong>结尾你还会发现一个 <em>else</em>语句。如果 <em>if</em>语句的条件无法满足就会执行<em>else</em>语句牵涉的代码。<br><br><font color=#000099>if ($#splitup &lt;=0 ){ $newguy = " ";} #如果没有说明就分配3个字段<br>$cols[6] = $newguy; #在数组中插入 3 个字段描述<br>$newline = join (" ",@cols); #创建输出字符串<br>print OUTFILE $newline . " "; #把输出字符串写入文件</font><br><br>现在我们到了写数据的阶段。Print函数用来打印字符串。如果没有给定文件句柄该函数会把结果打印到标准 I/O设备。在示例语句中，只要创建一行数据我就给输出文件写入一行。你可以在字符串之后加上句号。<br><font color=#000099>}#结束while循环<br>close OUTFILE; #关闭输出文件</font><br><br>这里，循环执行到文件的末尾，于是我们关闭输出文件以便它从内存获取数据存储到磁盘。close语句只有一个参数，也就是它要关闭的文件句柄。</p>
<h5 align=left>小结</h5>
<p style="FONT-SIZE: 10pt" align=left>下面就是我在编写以上脚本中用到的Perl知识：</p>
</div>
<ul>
    <li>
    <div style="FONT-SIZE: 10pt" align=left>变量（数值变量和数组）</div>
    <li>
    <div style="FONT-SIZE: 10pt" align=left>流程控制（while和for循环以及if/else语句）</div>
    <li>
    <div style="FONT-SIZE: 10pt" align=left>正则表达式</div>
    <li>
    <div style="FONT-SIZE: 10pt" align=left>函数（chomp、join和split）</div>
    <li>
    <div style="FONT-SIZE: 10pt" align=left>文件输入/输出</div>
    </li>
</ul>
<div style="FONT-SIZE: 10pt">
<p style="FONT-SIZE: 10pt">Perl是一种很容易在掌握的脚本编程语言，网上有很多学习资源可以帮助你获得有用的指导信息和相关资料，比如<a class=content href="http://learn.perl.org/" target=_blank>learn.perl.org</a> </span>和O&#8217;Reilly的<a class=content href="http://www.perl.com/" target=_blank>perl.com</a></span>都是相当不错的站点。</p>
</div>
<img src ="http://www.cnitblog.com/chenxin9821/aggbug/52540.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/chenxin9821/" target="_blank">金鳞</a> 2008-12-11 15:48 <a href="http://www.cnitblog.com/chenxin9821/articles/52540.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>各种平台下Perl模块的安装方法</title><link>http://www.cnitblog.com/chenxin9821/articles/52459.html</link><dc:creator>金鳞</dc:creator><author>金鳞</author><pubDate>Wed, 10 Dec 2008 08:40:00 GMT</pubDate><guid>http://www.cnitblog.com/chenxin9821/articles/52459.html</guid><wfw:comment>http://www.cnitblog.com/chenxin9821/comments/52459.html</wfw:comment><comments>http://www.cnitblog.com/chenxin9821/articles/52459.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/chenxin9821/comments/commentRss/52459.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/chenxin9821/services/trackbacks/52459.html</trackback:ping><description><![CDATA[<p><font style="COLOR: #000000; FONT-FAMILY: Courier" color=#cccccc>Perl到了第五版增加了模块的概念，用来提供面向对象编程的能力。这是Perl语言发展史上　 <br>的一个里程碑。此后，广大自由软件爱好者开发了大量功能强大、构思精巧的Perl模块，极大地　 <br>扩展了Perl语言的功能。CPAN（Comprehensive　Perl　Archive　Network）是internet上Perl模块　 <br>最大的集散地，包含了现今公布的几乎所有的perl模块。　 <br><br>　　　　几个主要的CPAN站点有：　 <br>　　　　国内：ftp://freesoft.cgi.gov.cn/pub/languages/perl/CPAN　 <br>　　　　　　　　　　http://cpan.qz.fj.cn/　 <br>　　　　国外：http://www.cpan.org/　 <br>　　　　　　　　　　http://www.perl.com/CPAN-local/　 <br><br>　　　　我在这里介绍一下各种平台下perl模块的安装方法。以安装DBI模块为例。　 <br><br>1、Linux/Unix下的安装方法：　 <br><br>　　　　Linux/Unix下安装Perl模块有两种方法：手工安装和自动安装。第一种方法是从CPAN上下载　 <br>您需要的模块，手工编译、安装。第二种方法是联上internet，使用一个叫做CPAN的模块自动完　 <br>成下载、编译、安装的全过程。　 <br><br>a、手工安装的步骤：　 <br><br>从CPAN下载了DBI模块1.13版的压缩文件DBI-1.13.tar.gz，假设放在/usr/local/src/下。　 <br>cd　/usr/local/src　 <br>解压缩这个文件：　 <br>tar　xvzf　DBI-1.13.tar.gz　 <br>这时会新建一个DBI-1.13的目录。　 <br>cd　DBI-1.13　 <br><br>生成makefile：　 <br>perl　Makefile.PL　 <br>建立模块　 <br>make　 <br>测试模块　 <br>make　test　 <br>如果测试结果报告&#8220;all　test　ok&#8221;，您就可以放心地安装编译好的模块了。安装模块前，先要　 <br>确保您对perl5安装目录有可写权限（通常以su命令获得），执行：　 <br>make　install　 <br><br>现在，写个程序试试吧。　 <br><br>#!/usr/bin/perl　-w　 <br>use　strict;　 <br>use　DBI;　 <br>..　 <br>　　 <br><br>　　　　上述步骤适合于Linux/Unix下绝大多数的Perl模块。可能还有少数模块的安装方法略有差别，　 <br>所以最好先看看安装目录里的README或INSTALL。另外，上述过程是针对动态链接的Perl编译　 <br>器（所有Linux下预安装的Perl都是动态链接的），如果您在使用一个静态链接的Perl，您需要将　 <br>新的模块静态链接到perl编译器中，可能还需要重启机器。　 <br>　　 <br><br>b、使用CPAN模块自动安装：　 <br><br>安装前需要先联上线，并且您需要取得root权限。　 <br>perl　-MCPAN　-e　shell　 <br>初次运行CPAN时需要做一些设置，如果您的机器是直接与internet相联（拨号上网、专线，etc.），　 <br>那么一路回车就行了，只需要在最后选一个离您最近的CPAN镜像站点。例如我选的是位于国内的　 <br>中国自由软件库ftp://freesoft.cgi.gov.cn/pub/languages/perl/CPAN　。否则，如果您的机器　 <br>位于防火墙之后，还需要设置ftp代理或http代理。　 <br><br>获得帮助　 <br>cpan&gt;h　 <br><br>列出CPAN上所有模块的列表　 <br>cpan&gt;m　 <br><br>安装模块　 <br>cpan&gt;install　DBI　 <br>自动完成DBI模块从下载到安装的全过程。　 <br><br>退出　 <br>cpan&gt;q　 <br>　　 <br>　　 <br><br>2、Win32下的安装方法：　 <br><br>　　　　在Win32下，建议使用ActiveState的Perl　for　Win32，即ActivePerl。ActivePerl的主页在：　 <br>http://www.activestate.com/　。在Win32下，最好也使用专门为ActivePerl定制的Perl模块。从　 <br>CPAN下载的Perl模块不能很好地在ActivePerl下使用。类似于CPAN模块，ActiveState也开发　 <br>了一个自动安装工具叫做PPM（Perl　Package　Manager）。　 <br><br>最通常的安装方法是：　 <br>首先联上线　 <br>在dos命令行下启动ppm　 <br>ppm　 <br><br>获得帮助　 <br>PPM&gt;h　 <br><br>列出ActiveState站点上所有为Perl模块的清单。　 <br>PPM&gt;search　 <br><br>安装模块　 <br>PPM&gt;install　DBI　 <br>自动完成DBI模块从下载到安装的全过程。　 <br><br>退出　 <br>PPM&gt;q　 <br><br>如果您的机器位于防火墙之后，通过http代理上网，那么可以使用下面的方法。　 <br><br>对于老的基于perl　5.005的ActivePerl版本（ActivePerl　522以下版本，不包括ActivePerl　522），从　 <br>http://www.ActiveState.com/ppmpackages/5.005/zips/下载您需要的Perl模块，例如DBI.zip。　 <br>将其解压缩在C:\TEMP\DBI下　 <br>cd　\TEMP\DBI　 <br>ppm　install　DBI.PPD　 <br><br>最新的基于perl　5.6的ActivePerl版本（ActivePerl　613及以上版本）的安装方法略有不同。安　 <br>装ActivePerl　613前需要先安装Microsoft的Windows　Installer。　 <br>Windows　Installer从这里下载：　 <br>http://activestate.com/download/contrib/Microsoft/9x/InstMsi.exe　 <br>ActivePerl　613从这里下载：　 <br>http://activestate.com/download/ActivePerl/Windows/5.6/ActivePerl-5.6.0.613.msi　 <br><br>对于基于perl　5.6的ActivePerl版本，从　 <br>http://www.activestate.com/PPMPackages/5.6/　 <br>下载您需要的Perl模块的安装描述文件，例如DBI.PPD　 <br>安装模块前需要先联上线　 <br>ppm　install　DBI.PPD。　 <br>　　 <br>　　 <br><br>　　　　以上较为详细地描述了各种平台下Perl模块的安装方法。希望能够帮助国内Perl爱好者们通过　 <br>使用现有大量优秀的Perl模块，避免重复劳动，提高效率，更好发挥Perl语言的强大威力。　 <br>　　 <br><!-- 正文end --><br>(<a href="http://www.fanqiang.com/">http://www.fanqiang.com</a>)<br><font style="COLOR: #000000" color=#999999 size=2>来源:http://www.linuxforum.net David lee </font></font></p>
<img src ="http://www.cnitblog.com/chenxin9821/aggbug/52459.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/chenxin9821/" target="_blank">金鳞</a> 2008-12-10 16:40 <a href="http://www.cnitblog.com/chenxin9821/articles/52459.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>小骆驼9.2习题</title><link>http://www.cnitblog.com/chenxin9821/articles/51527.html</link><dc:creator>金鳞</dc:creator><author>金鳞</author><pubDate>Sun, 16 Nov 2008 04:17:00 GMT</pubDate><guid>http://www.cnitblog.com/chenxin9821/articles/51527.html</guid><wfw:comment>http://www.cnitblog.com/chenxin9821/comments/51527.html</wfw:comment><comments>http://www.cnitblog.com/chenxin9821/articles/51527.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/chenxin9821/comments/commentRss/51527.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/chenxin9821/services/trackbacks/51527.html</trackback:ping><description><![CDATA[<p>题目：<br></p>
写一个程序，它可以得到当前文本文件的一个拷贝。在拷贝的文件中，字符串Fred(大小写无关)将被Larry 替换掉。<br>（因此，&#8220;Manfred Mann&#8221;将变成&#8220;ManLarry Mann&#8221;.）输入的文件名已经在命令行中指定（不需要询问用户），输出的<br>文件名是对应的输入文件名后面加上.out。<br><br>答案：<br>#!/usr/bin/perl<br>my $in = $ARGV[0];<br>if(not defined $in){<br>die"Usage:$0 filename";<br>}<br>my $out = $in;<br>$out =~s/\.\w+$/.out/;<br>unless (open IN,"&lt;$in"){<br>die"cant open '$in':$!";<br>}<br>unless (open OUT,"&gt;$out"){<br>die"cant write '$out':$!";<br>}<br>while (&lt;IN&gt;) {<br>s/Fred/Larry/gi;<br>print OUT $_;<br>} <br><br>命令行下输入：C:\Documents and Settings\1&gt;perl D:\study\perl\aixiaoxin.pl ttt.txt回车，即可在相应路径下生成ttt.out文件。
<img src ="http://www.cnitblog.com/chenxin9821/aggbug/51527.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/chenxin9821/" target="_blank">金鳞</a> 2008-11-16 12:17 <a href="http://www.cnitblog.com/chenxin9821/articles/51527.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Install perl on windows</title><link>http://www.cnitblog.com/chenxin9821/articles/41162.html</link><dc:creator>金鳞</dc:creator><author>金鳞</author><pubDate>Wed, 02 Jul 2008 01:35:00 GMT</pubDate><guid>http://www.cnitblog.com/chenxin9821/articles/41162.html</guid><wfw:comment>http://www.cnitblog.com/chenxin9821/comments/41162.html</wfw:comment><comments>http://www.cnitblog.com/chenxin9821/articles/41162.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/chenxin9821/comments/commentRss/41162.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/chenxin9821/services/trackbacks/41162.html</trackback:ping><description><![CDATA[<p>1、首先，去网站上下载安装程序。<br><a href="http://www.activestate.com/Products/?_x=1"><span style="FONT-SIZE: 18pt">http://www.activestate.com/Products/?_x=1</span></a><br><br>2、其次，执行安装（按提示一步一步走）。<br><br>3、再次，敲cmd进入终端，<br>&nbsp;&nbsp;&nbsp;&nbsp; 如果当前目录是c盘，而perl安装在d盘perl文件夹下，那么这时敲入d:，之后回车，就可以从c盘路径进入d盘路径了。<br><br>4、Next，敲入perl -v，当出现<br>&#8220;This is perl, version 5.005_02 built for MSWin32-x86-object<br>&nbsp;&nbsp; (with 1 registered patch, see perl -V for more detail)<br>&nbsp;&nbsp;&nbsp; Copyright 1987-1998, Larry Wall&#8221;提示时，表示perl安装成功了。<br><br>5、最后，举个例子，运行一次perl。<br><br>&nbsp;&nbsp;&nbsp;&nbsp; 1】在记事本或editplus中，写一段perl程序，如<br>&nbsp;&nbsp; &#8220;#! /usr/bin/perl</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; use strict;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; use warnings;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;my $greeting = "Hello, World!";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print $greeting,"\n";&nbsp; # Say hello&#8221;<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2】文件要保存成.pl结尾的文件，将文件保存在D:\Perl路径下，取名为 tt.pl 。 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3】在当前目录D:\&gt;下输入D:\Perl\tt.pl，回车即可出现结果。<br><br>感谢&nbsp; 幻灵游侠和沉默ing&nbsp; 的耐心指导...</p>
<img src ="http://www.cnitblog.com/chenxin9821/aggbug/41162.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/chenxin9821/" target="_blank">金鳞</a> 2008-07-02 09:35 <a href="http://www.cnitblog.com/chenxin9821/articles/41162.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>三种遍历文件夹方法比较 （转载）</title><link>http://www.cnitblog.com/chenxin9821/articles/46211.html</link><dc:creator>金鳞</dc:creator><author>金鳞</author><pubDate>Wed, 02 Jul 2008 01:27:00 GMT</pubDate><guid>http://www.cnitblog.com/chenxin9821/articles/46211.html</guid><wfw:comment>http://www.cnitblog.com/chenxin9821/comments/46211.html</wfw:comment><comments>http://www.cnitblog.com/chenxin9821/articles/46211.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/chenxin9821/comments/commentRss/46211.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/chenxin9821/services/trackbacks/46211.html</trackback:ping><description><![CDATA[<p>&nbsp;</p>
<table style="BORDER-COLLAPSE: collapse; WORD-WRAP: break-word" cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td align=middle>
            <table style="BORDER-COLLAPSE: collapse; WORD-WRAP: break-word" cellSpacing=0 cellPadding=0 width="100%" border=0>
                <tbody>
                    <tr>
                        <td width="100%">
                        <div id=art style="MARGIN: 15px" width="100%"><span style="FONT-SIZE: 13px">本贴对三种遍历文件夹方法比较。<br>1. 使用File::Find;<br>2. 递归遍历。(遍历函数为lsr)<br>3. 使用队列或栈遍历。(遍历函数为lsr_s)<br><br>1.use File::Find<br><br>
                        <div class=smalltxt style="FONT-WEIGHT: bold; MARGIN-LEFT: 2em; MARGIN-RIGHT: 2em">
                        <div style="FLOAT: left">CODE:</div>
                        <div style="FLOAT: right; TEXT-ALIGN: right"><a class=smalltxt onclick="copycode(findobj('code0'));" href="http://bbs.chinaunix.net/viewthread.php?tid=907555#">[Copy to clipboard]</a></div>
                        </div>
                        <div class=altbg2 id=code0 style="CLEAR: both; BORDER-RIGHT: rgb(105,140,195) 1px solid; PADDING-RIGHT: 10px; BORDER-TOP: rgb(105,140,195) 1px solid; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; MARGIN: 3px 2em 2em; BORDER-LEFT: rgb(105,140,195) 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: rgb(105,140,195) 1px solid; FONT-FAMILY: fixedsys">#!/usr/bin/perl -W<br>#<br># File: find.pl<br># Author:&nbsp;&nbsp;路小佳<br># License: GPL-2<br><br>use strict;<br>use warnings;<br>use File::Find;<br><br>my ($size, $dircnt, $filecnt) = (0, 0, 0);<br><br>sub process {<br>&nbsp; &nbsp; my $file = $File::Find::name;<br>&nbsp; &nbsp; #print $file, "\n";<br>&nbsp; &nbsp; if (-d $file) {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$dircnt++;<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; else {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$filecnt++;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$size += -s $file;<br>&nbsp; &nbsp; }<br>}<br><br>find(\&amp;process, '.');<br>print "$filecnt files, $dircnt directory. $size bytes.\n";</div>
                        2. lsr递归遍历<br><br>
                        <div class=smalltxt style="FONT-WEIGHT: bold; MARGIN-LEFT: 2em; MARGIN-RIGHT: 2em">
                        <div style="FLOAT: left">CODE:</div>
                        <div style="FLOAT: right; TEXT-ALIGN: right"><a class=smalltxt onclick="copycode(findobj('code1'));" href="http://bbs.chinaunix.net/viewthread.php?tid=907555#">[Copy to clipboard]</a></div>
                        </div>
                        <div class=altbg2 id=code1 style="CLEAR: both; BORDER-RIGHT: rgb(105,140,195) 1px solid; PADDING-RIGHT: 10px; BORDER-TOP: rgb(105,140,195) 1px solid; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; MARGIN: 3px 2em 2em; BORDER-LEFT: rgb(105,140,195) 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: rgb(105,140,195) 1px solid; FONT-FAMILY: fixedsys">#!/usr/bin/perl -W<br>#<br># File: lsr.pl<br># Author: 路小佳<br># License: GPL-2<br><br>use strict;<br>use warnings;<br><br>sub lsr($) {<br>&nbsp; &nbsp; sub lsr;<br>&nbsp; &nbsp; my $cwd = shift;<br><br>&nbsp; &nbsp; local *DH;<br>&nbsp; &nbsp; if (!opendir(DH, $cwd)) {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;warn "Cannot opendir $cwd: $! $^E";<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return undef;<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; foreach (readdir(DH)) {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if ($_ eq '.' || $_ eq '..') {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;next;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;my $file = $cwd.'/'.$_;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if (!-l $file &amp;&amp; -d _) {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$file .= '/';<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;lsr($file);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;process($file, $cwd);<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; closedir(DH);<br>}<br><br>my ($size, $dircnt, $filecnt) = (0, 0, 0);<br><br>sub process($$) {<br>&nbsp; &nbsp; my $file = shift;<br>&nbsp; &nbsp; #print $file, "\n";<br>&nbsp; &nbsp; if (substr($file, length($file)-1, 1) eq '/') {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$dircnt++;<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; else {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$filecnt++;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$size += -s $file;<br>&nbsp; &nbsp; }<br>}<br><br>lsr('.');<br>print "$filecnt files, $dircnt directory. $size bytes.\n";</div>
                        3. lsr_s栈遍历<br><br>
                        <div class=smalltxt style="FONT-WEIGHT: bold; MARGIN-LEFT: 2em; MARGIN-RIGHT: 2em">
                        <div style="FLOAT: left">CODE:</div>
                        <div style="FLOAT: right; TEXT-ALIGN: right"><a class=smalltxt onclick="copycode(findobj('code2'));" href="http://bbs.chinaunix.net/viewthread.php?tid=907555#">[Copy to clipboard]</a></div>
                        </div>
                        <div class=altbg2 id=code2 style="CLEAR: both; BORDER-RIGHT: rgb(105,140,195) 1px solid; PADDING-RIGHT: 10px; BORDER-TOP: rgb(105,140,195) 1px solid; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; MARGIN: 3px 2em 2em; BORDER-LEFT: rgb(105,140,195) 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: rgb(105,140,195) 1px solid; FONT-FAMILY: fixedsys">#!/usr/bin/perl -W<br>#<br># File: lsr_s.pl<br># Author: 路小佳<br># License: GPL-2<br><br>use strict;<br>use warnings;<br><br>sub lsr_s($) {<br>&nbsp; &nbsp; my $cwd = shift;<br>&nbsp; &nbsp; my @dirs = ($cwd.'/');<br><br>&nbsp; &nbsp; my ($dir, $file);<br>&nbsp; &nbsp; while ($dir = pop(@dirs)) {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;local *DH;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if (!opendir(DH, $dir)) {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;warn "Cannot opendir $dir: $! $^E";<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;next;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;foreach (readdir(DH)) {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if ($_ eq '.' || $_ eq '..') {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; next;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;$file = $dir.$_;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (!-l $file &amp;&amp; -d _) {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $file .= '/';<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; push(@dirs, $file);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;process($file, $dir);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;closedir(DH);<br>&nbsp; &nbsp; }<br>}<br><br>my ($size, $dircnt, $filecnt) = (0, 0, 0);<br><br>sub process($$) {<br>&nbsp; &nbsp; my $file = shift;<br>&nbsp; &nbsp; print $file, "\n";<br>&nbsp; &nbsp; if (substr($file, length($file)-1, 1) eq '/') {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$dircnt++;<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; else {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$filecnt++;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$size += -s $file;<br>&nbsp; &nbsp; }<br>}<br><br>lsr_s('.');<br>print "$filecnt files, $dircnt directory. $size bytes.\n";</div>
                        对我的硬盘/dev/hda6的测试结果。<br><br>1: File::Find<br><br>
                        <div class=smalltxt style="FONT-WEIGHT: bold; MARGIN-LEFT: 2em; MARGIN-RIGHT: 2em">
                        <div style="FLOAT: left">CODE:</div>
                        <div style="FLOAT: right; TEXT-ALIGN: right"><a class=smalltxt onclick="copycode(findobj('code3'));" href="http://bbs.chinaunix.net/viewthread.php?tid=907555#">[Copy to clipboard]</a></div>
                        </div>
                        <div class=altbg2 id=code3 style="CLEAR: both; BORDER-RIGHT: rgb(105,140,195) 1px solid; PADDING-RIGHT: 10px; BORDER-TOP: rgb(105,140,195) 1px solid; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; MARGIN: 3px 2em 2em; BORDER-LEFT: rgb(105,140,195) 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: rgb(105,140,195) 1px solid; FONT-FAMILY: fixedsys">26881 files, 1603 directory. 9052479946 bytes.<br><br>real&nbsp; &nbsp; 0m9.140s<br>user&nbsp; &nbsp; 0m3.124s<br>sys&nbsp; &nbsp;&nbsp;&nbsp;0m5.811s</div>
                        2: lsr<br><br>
                        <div class=smalltxt style="FONT-WEIGHT: bold; MARGIN-LEFT: 2em; MARGIN-RIGHT: 2em">
                        <div style="FLOAT: left">CODE:</div>
                        <div style="FLOAT: right; TEXT-ALIGN: right"><a class=smalltxt onclick="copycode(findobj('code4'));" href="http://bbs.chinaunix.net/viewthread.php?tid=907555#">[Copy to clipboard]</a></div>
                        </div>
                        <div class=altbg2 id=code4 style="CLEAR: both; BORDER-RIGHT: rgb(105,140,195) 1px solid; PADDING-RIGHT: 10px; BORDER-TOP: rgb(105,140,195) 1px solid; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; MARGIN: 3px 2em 2em; BORDER-LEFT: rgb(105,140,195) 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: rgb(105,140,195) 1px solid; FONT-FAMILY: fixedsys">26881 files, 1603 directory. 9052479946 bytes.<br><br>real&nbsp; &nbsp; 0m8.266s<br>user&nbsp; &nbsp; 0m2.686s<br>sys&nbsp; &nbsp;&nbsp;&nbsp;0m5.405s</div>
                        3: lsr_s<br><br>
                        <div class=smalltxt style="FONT-WEIGHT: bold; MARGIN-LEFT: 2em; MARGIN-RIGHT: 2em">
                        <div style="FLOAT: left">CODE:</div>
                        <div style="FLOAT: right; TEXT-ALIGN: right"><a class=smalltxt onclick="copycode(findobj('code5'));" href="http://bbs.chinaunix.net/viewthread.php?tid=907555#">[Copy to clipboard]</a></div>
                        </div>
                        <div class=altbg2 id=code5 style="CLEAR: both; BORDER-RIGHT: rgb(105,140,195) 1px solid; PADDING-RIGHT: 10px; BORDER-TOP: rgb(105,140,195) 1px solid; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; MARGIN: 3px 2em 2em; BORDER-LEFT: rgb(105,140,195) 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: rgb(105,140,195) 1px solid; FONT-FAMILY: fixedsys">26881 files, 1603 directory. 9052479946 bytes.<br><br>real&nbsp; &nbsp; 0m6.532s<br>user&nbsp; &nbsp; 0m2.124s<br>sys&nbsp; &nbsp;&nbsp;&nbsp;0m3.952s</div>
                        测试时考虑到cache所以要多测几次取平均, 也不要同时打印文件名， 因为控制台是慢设备， 会形成瓶颈。<br>lsr_s之所以用栈而不是队列来遍历，是因为Perl的push shift pop操作是基于数组的， push pop这样成对操作可能有优化。内存和cpu占用大小顺序也是1&gt;2&gt;3.<br><br>
                        <div class=smalltxt style="FONT-WEIGHT: bold; MARGIN-LEFT: 2em; MARGIN-RIGHT: 2em">
                        <div style="FLOAT: left">CODE:</div>
                        <div style="FLOAT: right; TEXT-ALIGN: right"><a class=smalltxt onclick="copycode(findobj('code6'));" href="http://bbs.chinaunix.net/viewthread.php?tid=907555#">[Copy to clipboard]</a></div>
                        </div>
                        <div class=altbg2 id=code6 style="CLEAR: both; BORDER-RIGHT: rgb(105,140,195) 1px solid; PADDING-RIGHT: 10px; BORDER-TOP: rgb(105,140,195) 1px solid; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; MARGIN: 3px 2em 2em; BORDER-LEFT: rgb(105,140,195) 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: rgb(105,140,195) 1px solid; FONT-FAMILY: fixedsys">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;CPU load&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; memory<br>use File::Find&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 97%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;4540K<br>lsr&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;95%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;3760K<br>lsr_s&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 95%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;3590K</div>
                        结论: 强烈推荐使用lsr_s来遍历文件夹。<br><br>=============再罗嗦几句======================<br>从执行效率上来看，find.pl比lsr.pl的差距主要在user上， 原因是File::Find模块选项较多，条件判断费时较多，而lsr_s.pl比lsr.pl在作系统调用用时较少， 是因为递归时程序还在保存原有的文件句柄和函数恢复现场的信息，所以sys费时较多。 所以lsr_s在sys与user上同时胜出是不无道理的。</span> </div>
                        </td>
                    </tr>
                </tbody>
            </table>
            <p style="MARGIN: 5px; LINE-HEIGHT: 150%"></p>
            </td>
        </tr>
        <tr>
            <td height=25>&nbsp;<font color=#000099><strong>原文地址</strong></font> <a href="http://bbs.chinaunix.net/viewthread.php?tid=907555" target=_blank>http://bbs.chinaunix.net/viewthread.php?tid=907555</a> </td>
        </tr>
    </tbody>
</table>
<img src ="http://www.cnitblog.com/chenxin9821/aggbug/46211.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/chenxin9821/" target="_blank">金鳞</a> 2008-07-02 09:27 <a href="http://www.cnitblog.com/chenxin9821/articles/46211.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>