﻿<?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-Java天空 任我翱翔-随笔分类-perl</title><link>http://www.blogjava.net/persister/category/37147.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 09 Sep 2010 04:41:43 GMT</lastBuildDate><pubDate>Thu, 09 Sep 2010 04:41:43 GMT</pubDate><ttl>60</ttl><item><title>Perl学习六（练习题）</title><link>http://www.blogjava.net/persister/archive/2009/09/15/295141.html</link><dc:creator>persister</dc:creator><author>persister</author><pubDate>Tue, 15 Sep 2009 06:16:00 GMT</pubDate><guid>http://www.blogjava.net/persister/archive/2009/09/15/295141.html</guid><wfw:comment>http://www.blogjava.net/persister/comments/295141.html</wfw:comment><comments>http://www.blogjava.net/persister/archive/2009/09/15/295141.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/persister/comments/commentRss/295141.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/persister/services/trackbacks/295141.html</trackback:ping><description><![CDATA[下面的习题解答：<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #800000;">1</span><span style="color: #000000;">&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;!/usr/bin/perl&nbsp;-w</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">2</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">3</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">@list</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">STDIN</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">4</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">@list</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">sort</span><span style="color: #000000;">(</span><span style="color: #800080;">@list</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">5</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">@list</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">reverse</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">@list</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">6</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">7</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">\n</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">8</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">9</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">@list</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">10</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">11</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">\n</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">12</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">input&nbsp;the&nbsp;number:\n</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">13</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">$num</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">STDIN</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">14</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">$list</span><span style="color: #000000;">[</span><span style="color: #800080;">$num</span><span style="color: #000000;">];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">15</span><span style="color: #000000;"> </span>srand;<br />
<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">16</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">$list</span><span style="color: #000000;">[</span><span style="color: #0000ff;">rand</span><span style="color: #000000;">(</span><span style="color: #800080;">@list</span><span style="color: #000000;">)];<br />
</span></div>
<br />
发现第四行 @list[$num]的结果也是一样的，不必是$list[$num]，难道是版本问题？<br />
书上说srand是进行随机数初始化的，但是发现如果删除这行也可以<br />
<br />
<img src ="http://www.blogjava.net/persister/aggbug/295141.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/persister/" target="_blank">persister</a> 2009-09-15 14:16 <a href="http://www.blogjava.net/persister/archive/2009/09/15/295141.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Perl学习笔记五</title><link>http://www.blogjava.net/persister/archive/2009/09/14/295074.html</link><dc:creator>persister</dc:creator><author>persister</author><pubDate>Mon, 14 Sep 2009 14:10:00 GMT</pubDate><guid>http://www.blogjava.net/persister/archive/2009/09/14/295074.html</guid><wfw:comment>http://www.blogjava.net/persister/comments/295074.html</wfw:comment><comments>http://www.blogjava.net/persister/archive/2009/09/14/295074.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/persister/comments/commentRss/295074.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/persister/services/trackbacks/295074.html</trackback:ping><description><![CDATA[1.The -x File Tests<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">where?&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #800080;">$filename</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">STDIN</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">3</span>&nbsp;<span style="color: #0000ff;">chomp</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">$filename</span><span style="color: #000000;">;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;toss&nbsp;pesky&nbsp;newline</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">4</span>&nbsp;<span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(</span><span style="color: #000000;">-</span><span style="color: #000000;">r&nbsp;</span><span style="color: #800080;">$filename</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">w&nbsp;</span><span style="color: #800080;">$filename</span><span style="color: #000000;">)&nbsp;{<br />
</span><span style="color: #008080;">5</span>&nbsp;<span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;file&nbsp;exists,&nbsp;and&nbsp;I&nbsp;can&nbsp;read&nbsp;and&nbsp;write&nbsp;it</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">6</span>&nbsp;<span style="color: #000000;"><img src="http://www.blogjava.net/Images/dot.gif"  alt="" /></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">7</span>&nbsp;<span style="color: #000000;">}</span></div>
<br />
Table 10.1: File Tests and Their Meanings<br />
File Test Meaning<br />
-r File or directory is readable<br />
-w File or directory is writable<br />
-x File or directory is executable<br />
-o File or directory is owned by user<br />
-R File or directory is readable by real user, not effective user (differs from -r for setuid<br />
programs)<br />
-W File or directory is writable by real user, not effective user (differs from -w for setuid<br />
programs)<br />
-X File or directory is executable by real user, not effective user (differs from -x for setuid<br />
programs)<br />
-O File or directory is owned by real user, not effective user (differs from -o for setuid<br />
programs)<br />
-e File or directory exists<br />
-z File exists and has zero size (directories are never empty)<br />
-s File or directory exists and has nonzero size (the value is the size in bytes)<br />
-f Entry is a plain file<br />
-d Entry is a directory<br />
-l Entry is a symlink<br />
-S Entry is a socket<br />
-p Entry is a named pipe (a "fifo")<br />
-b Entry is a block-special file (like a mountable disk)<br />
-c Entry is a character-special file (like an I/O device)<br />
-u File or directory is setuid<br />
-g File or directory is setgid<br />
-k File or directory has the sticky bit set<br />
-t isatty() on the filehandle is true<br />
-T File is "text"<br />
-B File is "binary"<br />
-M Modification age in days<br />
-A Access age in days<br />
-C Inode-modification age in days<br />
<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">foreach</span><span style="color: #000000;">&nbsp;(</span><span style="color: #800080;">@some_list_of_filenames</span><span style="color: #000000;">)&nbsp;{<br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">$_&nbsp;is&nbsp;readable\n</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">r;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;same&nbsp;as&nbsp;-r&nbsp;$_</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;">}</span></div>
<br />
2.The stat and lstat Functions<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #000000;">(</span><span style="color: #800080;">$dev</span><span style="color: #000000;">,</span><span style="color: #800080;">$ino</span><span style="color: #000000;">,</span><span style="color: #800080;">$mode</span><span style="color: #000000;">,</span><span style="color: #800080;">$nlink</span><span style="color: #000000;">,</span><span style="color: #800080;">$uid</span><span style="color: #000000;">,</span><span style="color: #800080;">$gid</span><span style="color: #000000;">,</span><span style="color: #800080;">$rdev</span><span style="color: #000000;">,</span><span style="color: #000000;"><br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #800080;">$size</span><span style="color: #000000;">,</span><span style="color: #800080;">$atime</span><span style="color: #000000;">,</span><span style="color: #800080;">$mtime</span><span style="color: #000000;">,</span><span style="color: #800080;">$ctime</span><span style="color: #000000;">,</span><span style="color: #800080;">$blksize</span><span style="color: #000000;">,</span><span style="color: #800080;">$blocks</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">stat</span><span style="color: #000000;">(</span><span style="color: #000000;"><img src="http://www.blogjava.net/Images/dot.gif"  alt="" /></span><span style="color: #000000;">)</span></div>
<br />
<img src ="http://www.blogjava.net/persister/aggbug/295074.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/persister/" target="_blank">persister</a> 2009-09-14 22:10 <a href="http://www.blogjava.net/persister/archive/2009/09/14/295074.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Perl学习笔记四</title><link>http://www.blogjava.net/persister/archive/2009/09/11/294737.html</link><dc:creator>persister</dc:creator><author>persister</author><pubDate>Fri, 11 Sep 2009 07:23:00 GMT</pubDate><guid>http://www.blogjava.net/persister/archive/2009/09/11/294737.html</guid><wfw:comment>http://www.blogjava.net/persister/comments/294737.html</wfw:comment><comments>http://www.blogjava.net/persister/archive/2009/09/11/294737.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/persister/comments/commentRss/294737.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/persister/services/trackbacks/294737.html</trackback:ping><description><![CDATA[1. subroutine<br />
&nbsp;&nbsp; <br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">sub</span><span style="color: #000000;">&nbsp;say&nbsp;{<br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">$_[0],&nbsp;$_[1]!</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">n</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">;<br />
</span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000; font-weight: bold;">&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000; font-weight: bold;">&nbsp;&nbsp;&nbsp;say(</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">hello</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">,</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">world</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">);&nbsp;#&nbsp;hello&nbsp;world,&nbsp;once&nbsp;again<br />
</span><span style="color: #008080;">5</span>&nbsp;<span style="color: #000000; font-weight: bold;">&nbsp;&nbsp;&nbsp;say(</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">goodbye</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">,</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">cruel&nbsp;world</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">);&nbsp;#&nbsp;silent&nbsp;movie&nbsp;lament<br />
</span><span style="color: #008080;">6</span>&nbsp;</div>
2.Private Variables in Functions<br />
&nbsp; <br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #0000ff;">sub</span><span style="color: #000000;">&nbsp;bigger_than&nbsp;{<br />
</span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">my</span><span style="color: #000000;">(</span><span style="color: #800080;">$n</span><span style="color: #000000;">,</span><span style="color: #800080;">@values</span><span style="color: #000000;">);&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;create&nbsp;some&nbsp;local&nbsp;variables</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;(</span><span style="color: #800080;">$n</span><span style="color: #000000;">,</span><span style="color: #800080;">@values</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">@_</span><span style="color: #000000;">;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;split&nbsp;args&nbsp;into&nbsp;limit&nbsp;and&nbsp;values</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">my</span><span style="color: #000000;">(</span><span style="color: #800080;">@result</span><span style="color: #000000;">);&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;temporary&nbsp;for&nbsp;holding&nbsp;the&nbsp;return&nbsp;value</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">foreach</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">$_</span><span style="color: #000000;">&nbsp;(</span><span style="color: #800080;">@values</span><span style="color: #000000;">)&nbsp;{&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;step&nbsp;through&nbsp;the&nbsp;arg&nbsp;list</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(</span><span style="color: #800080;">$_</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">$n</span><span style="color: #000000;">)&nbsp;{&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;is&nbsp;it&nbsp;eligible?</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">push</span><span style="color: #000000;">(</span><span style="color: #800080;">@result</span><span style="color: #000000;">,</span><span style="color: #800080;">$_</span><span style="color: #000000;">);&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;add&nbsp;it</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">@result</span><span style="color: #000000;">;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;return&nbsp;the&nbsp;final&nbsp;list</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;}</span></div>
# some invocations:<br />
@this = bigger_than(5,1,5,15,30); # @this gets (15,30)<br />
<br />
3.File-Level my( ) Variables<br />
&nbsp; The my() operator can also be used at the outermost level of your program, outside of <br />
&nbsp; any subroutines or blocks. While this isn't really a "local" variable in the sense <br />
&nbsp; defined above, it's actually rather useful,especially when used in conjunction with a <br />
&nbsp; Perl pragma:<br />
&nbsp;&nbsp;&nbsp; use strict;<br />
&nbsp; A pragma is a compiler directive. Other directives include those to set up integer<br />
&nbsp; arithmetic, overload numeric operators, or request more verbose warnings and error<br />
&nbsp; messages. <br />
&nbsp; If you place this pragma at the beginning of your file, you will no longer be able <br />
&nbsp; to use variables (scalars,arrays, and hashes) until you have first "declared" them. <br />
&nbsp; And you declare them with my(), like so:<br />
&nbsp; <br />
&nbsp; <br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">use</span><span style="color: #000000;">&nbsp;strict;<br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">my</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">$a</span><span style="color: #000000;">;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;starts&nbsp;as&nbsp;undef</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">my</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">@b</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;qw(fred&nbsp;barney&nbsp;betty);&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;give&nbsp;initial&nbsp;value</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #000000;"><img src="http://www.blogjava.net/Images/dot.gif" alt="" /></span><span style="color: #000000;"><br />
</span><span style="color: #008080;">5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">push</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">@b</span><span style="color: #000000;">,</span><span style="color: #000000;">&nbsp;qw(wilma);&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;cannot&nbsp;leave&nbsp;her&nbsp;out</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #800080;">@c</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">sort</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">@b</span><span style="color: #000000;">;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;WILL&nbsp;NOT&nbsp;COMPILE</span></div>
<br />
&nbsp; That last statement will be flagged at compile time as an error, because it referred <br />
&nbsp; to a variable that had not previously been declared with my (that is, @c). In other <br />
&nbsp; words,your program won't even start running unless every single variable being used <br />
&nbsp; has been declared. The advantages of forcing variable declarations are twofold:<br />
&nbsp; 1.&nbsp; Your programs will run slightly faster (variables created with my are accessed <br />
&nbsp; slightly faster than ordinary variables).<br />
&nbsp; <br />
&nbsp; Variables created with my() are not found in any package.<br />
&nbsp; 2. You'll catch mistakes in typing much faster, because you'll no longer be able <br />
&nbsp;&nbsp;&nbsp;&nbsp; to accidentally reference a nonexisting variable named $freed when you wanted $fred.<br />
<br />
4.The last Statement<br />
&nbsp; <br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #0000ff;">while</span><span style="color: #000000;">&nbsp;(something)&nbsp;{<br />
</span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;something;<br />
</span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;something;<br />
</span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;something;<br />
</span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(somecondition)&nbsp;{<br />
</span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;somethingorother;<br />
</span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;somethingorother;<br />
</span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">last</span><span style="color: #000000;">;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;break&nbsp;out&nbsp;of&nbsp;the&nbsp;while&nbsp;loop</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;morethings;<br />
</span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;morethings;<br />
</span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;last&nbsp;comes&nbsp;here</span></div>
5.The next Statement<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">&nbsp;(something)&nbsp;{<br />
</span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;firstpart;<br />
</span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;firstpart;<br />
</span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;firstpart;<br />
</span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(somecondition)&nbsp;{<br />
</span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;somepart;<br />
</span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;somepart;<br />
</span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">next</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;}<br />
</span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;otherpart;<br />
</span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;otherpart;<br />
</span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;next&nbsp;comes&nbsp;here</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;}</span></div>
<br />
<br />
6.The redo Statement<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #0000ff;">while</span><span style="color: #000000;">&nbsp;(somecondition)&nbsp;{<br />
</span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;redo&nbsp;comes&nbsp;here</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;something;<br />
</span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;something;<br />
</span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;something;<br />
</span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(somecondition)&nbsp;{<br />
</span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;somestuff;<br />
</span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;somestuff;<br />
</span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">redo</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">}<br />
</span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">morething;<br />
</span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">morething;<br />
</span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">morething;<br />
</span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">}</span></div>
<br />
7.Expression Modifiers<br />
&nbsp; <br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #000000;">exp2&nbsp;</span><span style="color: #0000ff;">unless</span><span style="color: #000000;">&nbsp;exp1;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;like:&nbsp;unless&nbsp;(exp1)&nbsp;{&nbsp;exp2;&nbsp;}</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">exp2&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">&nbsp;exp1;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;like:&nbsp;while&nbsp;(exp1)&nbsp;{&nbsp;exp2;&nbsp;}</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;">exp2&nbsp;</span><span style="color: #0000ff;">until</span><span style="color: #000000;">&nbsp;exp1;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;like:&nbsp;until&nbsp;(exp1)&nbsp;{&nbsp;exp2;&nbsp;}</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000;">some_expression&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;control_expression;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;like&nbsp;if&nbsp;(control_expression)&nbsp;{&nbsp; </span><span style="color: #000000;">some_expression;</span><span style="color: #000000;">}</span></div>
<br />
8.&amp;&amp; and || as Control Structures<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(this)&nbsp;{&nbsp;that;&nbsp;}&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;one&nbsp;way</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">that&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;this;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;another&nbsp;way<br />
</span><span style="color: #008080;">3</span>&nbsp;<span style="color: #008000;"><br />
</span><span style="color: #008080;">4</span>&nbsp;<span style="color: #008000;">#Here's&nbsp;a&nbsp;third&nbsp;(and&nbsp;believe&nbsp;it&nbsp;or&nbsp;not,&nbsp;there&nbsp;are&nbsp;still&nbsp;others):</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">5</span>&nbsp;<span style="color: #000000;">this&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;that;<br />
</span>6 #Likewise, the logical-or works like the unless statement (or unless modifier). So you can replace:<br />
7 unless (this) { that; }<br />
8 #with:<br />
9 this || that;<br />
</div>
<br />
9.Opening and Closing a Filehandle<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">open</span><span style="color: #000000;">(FILEHANDLE</span><span style="color: #000000;">,</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">somename</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">);&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">opens&nbsp;the&nbsp;filehandle&nbsp;for&nbsp;reading</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;"><br />
</span><span style="color: #008080;">3</span>&nbsp;<span style="color: #0000ff;">open</span><span style="color: #000000;">(OUT</span><span style="color: #000000;">,</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">&gt;outfile</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">);&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">open&nbsp;a&nbsp;file&nbsp;for&nbsp;writing</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000;"><br />
</span><span style="color: #008080;">5</span>&nbsp;<span style="color: #0000ff;">open</span><span style="color: #000000;">(LOGFILE</span><span style="color: #000000;">,</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">&gt;&gt;mylogfile</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">);&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">you&nbsp;can&nbsp;open&nbsp;a&nbsp;file&nbsp;for&nbsp;appending<br />
6 </span><span style="color: #2835ff;">close</span>(LOGFILE);</div>
<br />
10.A Slight Diversion: die<br />
&nbsp;<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">open</span><span style="color: #000000;">(DATAPLACE</span><span style="color: #000000;">,</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">&gt;/tmp/dataplace</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;"><br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #0000ff;">die</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">Sorry,&nbsp;I&nbsp;couldn't&nbsp;create&nbsp;/tmp/dataplace\n</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;"><br />
</span><span style="color: #008080;">4</span>&nbsp;<span style="color: #0000ff;">die</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">you&nbsp;gravy-sucking&nbsp;pigs</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">prints&nbsp;the&nbsp;file&nbsp;and&nbsp;line&nbsp;number</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">5</span>&nbsp;<span style="color: #000000;"><br />
</span><span style="color: #008080;">6</span>&nbsp;<span style="color: #0000ff;">die</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">you&nbsp;gravy-sucking&nbsp;pigs\n</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">print&nbsp;either</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">7</span>&nbsp;<span style="color: #000000;"><br />
</span><span style="color: #008080;">8</span>&nbsp;<span style="color: #0000ff;">open</span><span style="color: #000000;">(</span><span style="color: #0000ff;">LOG</span><span style="color: #000000;">,</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">&gt;&gt;logfile</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">die</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">cannot&nbsp;append:&nbsp;$!</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;&nbsp;$!: </span><span style="color: #000000;">most&nbsp;recent&nbsp;operating&nbsp;</span><span style="color: #0000ff;">system</span><span style="color: #000000;">&nbsp;error</span></div>
<br />
11.Using Filehandles<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">open</span><span style="color: #000000;">&nbsp;(EP</span><span style="color: #000000;">,</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">/etc/passwd</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">);<br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #0000ff;">while</span><span style="color: #000000;">&nbsp;(</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">EP</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">)&nbsp;{<br />
</span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">chomp</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">I&nbsp;saw&nbsp;$_&nbsp;in&nbsp;the&nbsp;password&nbsp;file!\n</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">5</span>&nbsp;<span style="color: #000000;">}<br />
6 close(EP);<br />
</span></div>
<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">open</span><span style="color: #000000;">(OUT</span><span style="color: #000000;">,</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">&gt;outfile</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">);&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">open&nbsp;a&nbsp;file&nbsp;for&nbsp;writing</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;OUT&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">What's&nbsp;going&nbsp;on&nbsp;in&nbsp;this&nbsp;file<img src="http://www.blogjava.net/Images/dot.gif" alt="" /></span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">3</span>&nbsp;<span style="color: #0000ff;">close</span><span style="color: #000000;">(OUT);<br />
</span></div>
<img src ="http://www.blogjava.net/persister/aggbug/294737.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/persister/" target="_blank">persister</a> 2009-09-11 15:23 <a href="http://www.blogjava.net/persister/archive/2009/09/11/294737.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>perl学习笔记三</title><link>http://www.blogjava.net/persister/archive/2009/03/18/260591.html</link><dc:creator>persister</dc:creator><author>persister</author><pubDate>Wed, 18 Mar 2009 14:00:00 GMT</pubDate><guid>http://www.blogjava.net/persister/archive/2009/03/18/260591.html</guid><wfw:comment>http://www.blogjava.net/persister/comments/260591.html</wfw:comment><comments>http://www.blogjava.net/persister/archive/2009/03/18/260591.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/persister/comments/commentRss/260591.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/persister/services/trackbacks/260591.html</trackback:ping><description><![CDATA[<p>1、Anchoring Patterns<br />
&nbsp;&nbsp;&nbsp;&nbsp;\b意味着字符边界。代码：<br />
&nbsp;&nbsp;&nbsp;&nbsp;print "what's your name?";<br />
&nbsp;&nbsp;&nbsp;&nbsp;$name=&lt;STDIN&gt;;<br />
&nbsp;&nbsp;&nbsp;&nbsp;chomp($name);<br />
&nbsp;&nbsp;&nbsp;&nbsp;if($name=~ /^randl\b/i){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "matched\n";<br />
&nbsp;&nbsp;&nbsp;&nbsp;}else{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "not matched\n";<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;如果输入randl*,&nbsp; *是字符、数字和下划线，那么都会打印not matched<br />
&nbsp;&nbsp; 而输入"randl%"、"randl fa"等则matched。<br />
<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;/fred\b/; # matches fred, but not frederick<br />
&nbsp;&nbsp;&nbsp;&nbsp;/\bmo/; # matches moe and mole, but not Elmo<br />
&nbsp;&nbsp;&nbsp;&nbsp;/\bFred\b/; # matches Fred but not Frederick or alFred<br />
&nbsp;&nbsp;&nbsp;&nbsp;/\b\+\b/; # matches "x+y" but not "++" or " + "<br />
&nbsp;&nbsp;&nbsp;&nbsp;/abc\bdef/; # never matches (impossible for a boundary there)<br />
&nbsp;&nbsp; word boundary按字面来看是一种边界，一个位置。在这个位置两边的字符一个与\w匹配，一个与<br />
&nbsp;&nbsp; \W匹配，这是一种情况；还有一种情况就是，一个字符与\w匹配，而另一个字符嘛，要么是<br />
&nbsp;&nbsp; 字符串的打头，要么是结尾。而\b这个anchor恰恰需要在它出现的位置是一个word boundary。<br />
&nbsp;&nbsp; 再看来/abc\bdef/，\b两边一个是c，一个是d，都与\w匹配，这已不满足上述的第一种情况，<br />
&nbsp;&nbsp; 至于第二种情况，更加不会是字符串的打头或末尾了。所以\b出现的地方不可能是word boundary。</p>
<p><br />
</p>
<p>附Learning Perl中的片段说明：<br />
</p>
<p>if ($name =~ /^Randal/) {<br />
## yes, it matches<br />
} else {<br />
## no, it doesn't<br />
}<br />
Note that the regular expression is delimited by slashes. Within the slashes, spaces and other whitespace<br />
are significant, just as they are within strings.<br />
This almost does it, but it doesn't handle selecting randal or rejecting Randall. To accept randal,<br />
we add the ignore-case option, a small i appended after the closing slash. To reject Randall, we add a<br />
word boundary special marker (similar to vi and some versions of grep) in the form of "b in the regular<br />
expression. This ensures that the character following the first l in the regular expression is not another<br />
letter. This changes the regular expression to be /^randal"b/i, which means "randal at the<br />
beginning of the string, no letter or digit following, and OK to be in either case."<br />
<br />
</p>
<img src ="http://www.blogjava.net/persister/aggbug/260591.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/persister/" target="_blank">persister</a> 2009-03-18 22:00 <a href="http://www.blogjava.net/persister/archive/2009/03/18/260591.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>perl连接mysql数据库</title><link>http://www.blogjava.net/persister/archive/2009/03/16/260100.html</link><dc:creator>persister</dc:creator><author>persister</author><pubDate>Mon, 16 Mar 2009 13:46:00 GMT</pubDate><guid>http://www.blogjava.net/persister/archive/2009/03/16/260100.html</guid><wfw:comment>http://www.blogjava.net/persister/comments/260100.html</wfw:comment><comments>http://www.blogjava.net/persister/archive/2009/03/16/260100.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/persister/comments/commentRss/260100.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/persister/services/trackbacks/260100.html</trackback:ping><description><![CDATA[1、查看系统上是否已经安装好了DBI，perl -e "use DBI"，如果没有出现错误且没有退出，表明已经安装<br />
&nbsp;&nbsp;&nbsp;&nbsp;此模块。<br />
2、如果未安装，到<a href="http://www.cpan.org">www.cpan.org</a>进行下载两个模块，DBI-1.607.tar.gz和DBD-mysql-4.010进行安装。<br />
&nbsp;&nbsp;&nbsp;&nbsp;tar -xzvf DBI-1.607.tar.gz<br />
&nbsp;&nbsp;&nbsp;&nbsp;cd DBI-1.607<br />
&nbsp;&nbsp;&nbsp;&nbsp;perl Makefile.PL<br />
&nbsp;&nbsp;&nbsp;&nbsp;make<br />
&nbsp;&nbsp;&nbsp;&nbsp;make test<br />
&nbsp;&nbsp;&nbsp;&nbsp;make install<br />
&nbsp;&nbsp;&nbsp;&nbsp;DBD-myql-4.010也同样的方式进行安装。<br />
3、通过下面的perl脚本连接数据库：<br />
&nbsp;&nbsp;&nbsp;&nbsp;my $dbh = DBI-&gt;connect("DBI:mysql:database=test;host=localhost","root","123456");<br />
&nbsp;&nbsp;&nbsp;&nbsp;my $sth = $dbh-&gt;("select * from clients");<br />
&nbsp;&nbsp;&nbsp;&nbsp;$sth-&gt;execute();<br />
&nbsp;&nbsp;&nbsp;&nbsp;while(my $ref = $sth-&gt;fetchrow_hashref())<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "cid=$ref-&gt;{'cid'} and cname=$ref-&gt;{'cname'}\n";<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;$sth-&gt;finish();<br />
&nbsp;&nbsp;&nbsp;&nbsp;$dbh-&gt;disconnect();
<img src ="http://www.blogjava.net/persister/aggbug/260100.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/persister/" target="_blank">persister</a> 2009-03-16 21:46 <a href="http://www.blogjava.net/persister/archive/2009/03/16/260100.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>apache的安装和perl测试</title><link>http://www.blogjava.net/persister/archive/2009/03/16/259958.html</link><dc:creator>persister</dc:creator><author>persister</author><pubDate>Mon, 16 Mar 2009 02:54:00 GMT</pubDate><guid>http://www.blogjava.net/persister/archive/2009/03/16/259958.html</guid><wfw:comment>http://www.blogjava.net/persister/comments/259958.html</wfw:comment><comments>http://www.blogjava.net/persister/archive/2009/03/16/259958.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/persister/comments/commentRss/259958.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/persister/services/trackbacks/259958.html</trackback:ping><description><![CDATA[1.到官方网站下载最新apache<br />
2. 下载后的文件形式是httpd-2.2.11.tar.gz.<br />
&nbsp;&nbsp;&nbsp;&nbsp;1)利用命令tar xvfz *.tar.gz将apache解压.<br />
&nbsp;&nbsp;&nbsp;&nbsp;2)cd httpd-2.2.11&nbsp;&nbsp;&nbsp;&nbsp;3)利用./configure --help查看帮助信息,看看需要配置哪些选项.由于我要装mysql和php,所以选择了以下<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;几个选项:./configure --prefix=/usr/local/apache2 --enable-rewrite&nbsp;&nbsp; --with-mpm=worker&nbsp; --enable-so<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 最后一个很重要,如果要装php,一定要写,前面两个不是必需的.&lt;如果不明白选项的含义,去google.&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;4)make<br />
&nbsp;&nbsp;&nbsp;&nbsp;5)make install<br />
3. 启动Apache：<br />
&nbsp;&nbsp;&nbsp;&nbsp;#cd /usr/local/apache2/bin<br />
&nbsp;&nbsp;&nbsp;&nbsp;#./apachectl start
<p>&nbsp;&nbsp;&nbsp;停掉apache是<br />
&nbsp;&nbsp;&nbsp;&nbsp;#cd /usr/local/apache2/bin<br />
&nbsp;&nbsp;&nbsp;&nbsp;#./apachectl stop<br />
4.测试安装是否成功，浏览器输入<a href="http://localhost/">http://localhost</a>, 页面显示it works，表明apache安装成功.<br />
5.测试perl.文件为cgitest.pl,放在/usr/local/apache2/cgi-bin目录中，并且chmod a+x cgitest.pl使其可执行<br />
：</p>
&nbsp;&nbsp;&nbsp;&nbsp;#!/usr/bin/perl -w<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;use CGI;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my $q = new CGI;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print $q-&gt;header(),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $q-&gt;start_html("hello,perl"),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $q-&gt;h1('hello perl'),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $q-&gt;end_html();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
6.在浏览器中输入：<a href="http://localhost/cgi-bin/cgitest.pl">http://localhost/cgi-bin/cgitest.pl</a>&nbsp;即可看到hello perl。<br />
<br />
7.自定义文件存放位置：<br />
&nbsp;&nbsp;&nbsp; # ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"<br />
&nbsp;&nbsp;&nbsp; # ScriptAlias /cgi/ "/usr/local/www/"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ScriptAlias / "/usr/local/www/"<br />
&nbsp; <br />
&nbsp; #&lt;Directory "/usr/local/apache2/cgi-bin"&gt;<br />
&nbsp; &lt;Directory "/usr/local/www"&gt;<br />
这样就可以将网页文件存放在/usr/local/www目录<br />
访问的时候：<a href="http://localhost/cgi-bin/cgitest.pl">http://localhost/cgitest.pl</a>&nbsp;即可。&nbsp;
<img src ="http://www.blogjava.net/persister/aggbug/259958.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/persister/" target="_blank">persister</a> 2009-03-16 10:54 <a href="http://www.blogjava.net/persister/archive/2009/03/16/259958.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Perl学习笔记二</title><link>http://www.blogjava.net/persister/archive/2009/03/14/259774.html</link><dc:creator>persister</dc:creator><author>persister</author><pubDate>Sat, 14 Mar 2009 15:33:00 GMT</pubDate><guid>http://www.blogjava.net/persister/archive/2009/03/14/259774.html</guid><wfw:comment>http://www.blogjava.net/persister/comments/259774.html</wfw:comment><comments>http://www.blogjava.net/persister/archive/2009/03/14/259774.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/persister/comments/commentRss/259774.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/persister/services/trackbacks/259774.html</trackback:ping><description><![CDATA[<p>1.the string abc as a regular expression by enclosing the string in slashes:<br />
&nbsp;&nbsp;&nbsp;&nbsp;while (&lt;&gt;) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (/abc/) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print $_;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
2.dot "." pattern<br />
&nbsp;&nbsp;&nbsp;This matches any single character except newline (\n).&nbsp; For example, the pattern /a./ matches any two-letter sequence <br />
&nbsp;&nbsp; that starts with a and is not "a\n".<br />
3.A pattern-matching character class <br />
&nbsp;&nbsp;&nbsp;&nbsp; open and close square brackets and a list of characters between the brackets. One and only one of these characters <br />
&nbsp;&nbsp;&nbsp;&nbsp; must be present at the corresponding part of the string for the pattern to match. For example,<br />
&nbsp;&nbsp;&nbsp;&nbsp; /[abcde]/<br />
&nbsp;&nbsp;&nbsp; [0123456789] # match any single digit<br />
&nbsp;&nbsp;&nbsp; [0-9] # same thing<br />
&nbsp;&nbsp;&nbsp; [0-9\-] # match 0-9, or minus<br />
&nbsp;&nbsp;&nbsp;&nbsp;[a-z0-9] # match any single lowercase letter or digit<br />
&nbsp;&nbsp;&nbsp;&nbsp;[a-zA-Z0-9_] # match any single letter, digit, or underscore<br />
&nbsp;&nbsp;&nbsp;&nbsp;There's also a negated character class, which is the same as a character class, but has a leading up-arrow<br />
&nbsp;&nbsp;&nbsp;&nbsp;(or caret: ^) immediately after the left bracket. （if caret ^ is not right after the left bracket,then it means starting with）This character class matches any single character that is<br />
&nbsp;&nbsp;&nbsp;&nbsp;not in the list. For example:<br />
&nbsp;&nbsp;&nbsp;&nbsp;[^0-9] # match any single non-digit<br />
&nbsp;&nbsp;&nbsp;&nbsp;[^aeiouAEIOU] # match any single non-vowel<br />
&nbsp;&nbsp;&nbsp;&nbsp;[^\^] # match single character except an up-arrow<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;Predefined Character Class Abbreviations<br />
&nbsp;&nbsp;&nbsp;&nbsp;Construct&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Equivalent Class&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Negated Construct&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Equivalent Negated Class<br />
&nbsp;&nbsp;&nbsp;&nbsp;\d (a digit)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[0-9]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\D (digits, not!)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[^0-9]<br />
&nbsp;&nbsp;&nbsp;&nbsp;\w (word char)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[a-zA-Z0-9_]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\W (words, not!)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[^a-zA-Z0-9_]<br />
&nbsp;&nbsp;&nbsp;&nbsp;\s (space char)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ \r\t\n\f]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;\S (space, not!)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; [^ \r\t\n\f] <br />
4. Multipliers<br />
&nbsp;&nbsp;&nbsp;&nbsp;asterisk (*) as a grouping pattern. The asterisk indicates zero or more of the<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;immediately previous character (or character class).<br />
&nbsp;&nbsp;&nbsp;&nbsp;Two other grouping patterns that work like this are the plus sign (+), meaning one or more of the<br />
&nbsp;&nbsp;&nbsp;&nbsp;immediately previous character, and the question mark (?), meaning zero or one of the immediately<br />
&nbsp;&nbsp;&nbsp;&nbsp;previous character. For example, the regular expression /fo+ba?r/ matches an f followed by one or<br />
&nbsp;&nbsp;&nbsp;&nbsp;more o's followed by a b, followed by an optional a, followed by an r.<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;the general multiplier. The general multiplier consists of a pair of matching curly braces with one or two numbers<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inside, as in /x{5,10}/<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;We could dispense with *, +, and ? entirely, since they are completely equivalent to {0,}, {1,}, and<br />
&nbsp;&nbsp;&nbsp;&nbsp;{0,1}. But it's easier to type the equivalent single punctuation character, and more familiar as well.<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;If two multipliers occur in a single expression, the greedy rule is augmented with "leftmost is greediest."<br />
&nbsp;&nbsp;&nbsp;&nbsp;For example:<br />
&nbsp;&nbsp;&nbsp;&nbsp;$_ = "a xxx c xxxxxxxx c xxx d";<br />
&nbsp;&nbsp;&nbsp;&nbsp;/a.*c.*d/;<br />
&nbsp;&nbsp;&nbsp;&nbsp;In this case, the first ".*" in the regular expression matches all characters up to the second c, even<br />
&nbsp;&nbsp;&nbsp;&nbsp;though matching only the characters up to the first c would still allow the entire regular expression to<br />
&nbsp;&nbsp;&nbsp;&nbsp;match. Right now, this doesn't make any difference (the pattern would match either way), but later when<br />
&nbsp;&nbsp;&nbsp;&nbsp;we can look at parts of the regular expression that matched, it'll matter quite a bit.<br />
&nbsp;&nbsp;&nbsp;&nbsp;We can force any multiplier to be nongreedy (or lazy) by following it with a question mark:<br />
&nbsp;&nbsp;&nbsp;&nbsp;$_ = "a xxx c xxxxxxxx c xxx d";<br />
&nbsp;&nbsp;&nbsp;&nbsp;/a.*?c.*d/;<br />
&nbsp;&nbsp;&nbsp;&nbsp;Here, the a.*?c now matches the fewest characters between the a and c, not the most characters. This<br />
&nbsp;&nbsp;&nbsp;&nbsp;means the leftmost c is matched, not the rightmost. You can put such a question-mark modifier after any<br />
&nbsp;&nbsp;&nbsp;&nbsp;of the multipliers (?,+,*, and {m,n}).<br />
&nbsp;&nbsp;&nbsp;&nbsp;What if the string and regular expression were slightly altered, say, to:<br />
&nbsp;&nbsp;&nbsp;&nbsp;$_ = "a xxx ce xxxxxxxx ci xxx d";<br />
&nbsp;&nbsp;&nbsp;&nbsp;/a.*ce.*d/;<br />
&nbsp;&nbsp;&nbsp;&nbsp;In this case, if the .* matches the most characters possible before the next c, the next regular expression<br />
&nbsp;&nbsp;&nbsp;&nbsp;character (e) doesn't match the next character of the string (i). In this case, we get automatic<br />
&nbsp;&nbsp;&nbsp;&nbsp;backtracking: the multiplier is unwound and retried, stopping at someplace earlier (in this case, at the<br />
&nbsp;&nbsp;&nbsp;&nbsp;earlier c, next to the e).[2] A complex regular expression may involve many such levels of backtracking,<br />
&nbsp;&nbsp;&nbsp;&nbsp;leading to long execution times. In this case, making that match lazy (with a trailing "?") will actually<br />
&nbsp;&nbsp;&nbsp;&nbsp;simplify the work that Perl has to perform, so you may want to consider that.<br />
&nbsp;&nbsp;&nbsp;&nbsp;[2] Well, technically there was a lot of backtracking of the * operator to find the c's in the<br />
&nbsp;&nbsp;&nbsp;&nbsp;first place. But that's a little trickier to describe, and it works on the same principle. <br />
5.Parentheses as memory</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;To recall a memorized part of a string, you must include a backslash followed by an integer. This pattern<br />
&nbsp;&nbsp;&nbsp;&nbsp;construct represents the same sequence of characters matched earlier in the same-numbered pair of<br />
&nbsp;&nbsp;&nbsp;&nbsp;parentheses (counting from one). For example,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/fred(.)barney\1/;<br />
&nbsp;&nbsp;&nbsp;&nbsp;matches a string consisting of fred, followed by any single non-newline character, followed by<br />
&nbsp;&nbsp;&nbsp;&nbsp;barney, followed by that same single character. So, it matches fredxbarneyx, but not<br />
&nbsp;&nbsp;&nbsp;&nbsp;fredxbarneyy. Compare that with<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/fred.barney./;</p>
&nbsp;&nbsp;&nbsp;&nbsp;/a(.)b(.)c\2d\1/;&nbsp;&nbsp;&nbsp;#&nbsp;it matches axbycydx, for example<br />
&nbsp;&nbsp;&nbsp; /a(.*)b\1c/; matches an a, followed by any number of characters (even zero) followed by b, followed by that same<br />
&nbsp;&nbsp;&nbsp;&nbsp;sequence of characters followed by c. So, it would match aFREDbFREDc, or even abc, but not aXXbXXXc.
<img src ="http://www.blogjava.net/persister/aggbug/259774.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/persister/" target="_blank">persister</a> 2009-03-14 23:33 <a href="http://www.blogjava.net/persister/archive/2009/03/14/259774.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Perl学习笔记一</title><link>http://www.blogjava.net/persister/archive/2009/01/06/250113.html</link><dc:creator>persister</dc:creator><author>persister</author><pubDate>Tue, 06 Jan 2009 07:42:00 GMT</pubDate><guid>http://www.blogjava.net/persister/archive/2009/01/06/250113.html</guid><wfw:comment>http://www.blogjava.net/persister/comments/250113.html</wfw:comment><comments>http://www.blogjava.net/persister/archive/2009/01/06/250113.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/persister/comments/commentRss/250113.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/persister/services/trackbacks/250113.html</trackback:ping><description><![CDATA[1、Perl的Hello World版，vi编辑文件hello，在此文件中输入以下内容：<br />
&nbsp;&nbsp; <br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #008000;">#</span><span style="color: #008000;">!/usr/bin/perl</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;(</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">Hello,&nbsp;world!\n</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">);</span></div>
&nbsp;&nbsp; 即打印Hello World！<br />
2、变量定义：<br />
&nbsp; <br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #008000;">#</span><span style="color: #008000;">!/usr/bin/perl&nbsp;-w</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">What&nbsp;is&nbsp;your&nbsp;name?&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #800080;">$name</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">STDIN</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">定义一个变量采用符号$</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">chomp</span><span style="color: #000000;">&nbsp;(</span><span style="color: #800080;">$name</span><span style="color: #000000;">);&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">删除本字符串最后的"\n"，即回车符</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">Hello,&nbsp;$name!\n</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;</span></div>
3、在perl中，只区分字符串和数字这两种数据类型，而且两者之间还可以互换。字符串分单引号字符串和<br />
&nbsp;&nbsp; 双引号字符串，数字分整型和浮点型。<br />
4、string repetition operator：<br />
&nbsp;&nbsp; "fred" x 3 # is "fredfredfred"<br />
5、"." operator<br />
&nbsp;&nbsp; "hello" . "world" # same as "helloworld"<br />
6、chop and chomp Functions<br />
&nbsp; <br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #800080;">$x</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">hello&nbsp;world</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">chop</span><span style="color: #000000;">(</span><span style="color: #800080;">$x</span><span style="color: #000000;">);&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;$x&nbsp;is&nbsp;now&nbsp;"hello&nbsp;worl"</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;"><br />
</span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #800080;">$a</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">hello&nbsp;world\n</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">chomp</span><span style="color: #000000;">&nbsp;(</span><span style="color: #800080;">$a</span><span style="color: #000000;">);&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;$a&nbsp;is&nbsp;now&nbsp;"hello&nbsp;world"</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">chomp</span><span style="color: #000000;">&nbsp;(</span><span style="color: #800080;">$a</span><span style="color: #000000;">);&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;aha!&nbsp;no&nbsp;change&nbsp;in&nbsp;$a</span></div>
7、variable interpolation：<br />
&nbsp; <br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #800080;">$a</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">fred</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #800080;">$b</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">some&nbsp;text&nbsp;$a</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;$b&nbsp;is&nbsp;now&nbsp;"some&nbsp;text&nbsp;fred"</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;"><br />
</span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #800080;">$fred</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">'</span><span style="color: #000000; font-weight: bold;">hi</span><span style="color: #000000; font-weight: bold;">'</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #800080;">$barney</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">a&nbsp;test&nbsp;of&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">.</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">'</span><span style="color: #000000; font-weight: bold;">$fred</span><span style="color: #000000; font-weight: bold;">'</span><span style="color: #000000;">;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;literally:&nbsp;'a&nbsp;test&nbsp;of&nbsp;$fred'</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #800080;">$barney2</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">a&nbsp;test&nbsp;of&nbsp;\$fred</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;same&nbsp;thing</span></div>
8、list literal<br />
&nbsp;&nbsp; 1,2,3) # array of three values 1, 2, and 3<br />
&nbsp;&nbsp; ("fred",4.5) # two values, "fred" and 4.5<br />
9、list constructor operator<br />
&nbsp;&nbsp; (1 .. 5) # same as (1, 2, 3, 4, 5)<br />
&nbsp;&nbsp; (1.2 .. 5.2) # same as (1.2, 2.2, 3.2, 4.2, 5.2)<br />
&nbsp;&nbsp; (1.3 .. 6.1) # same as (1.3,2.3,3.3,4.3,5.3)<br />
10、"quote word" function<br />
&nbsp;&nbsp; @a = ("fred","barney","betty","wilma"); # ugh!<br />
&nbsp;&nbsp; @a = qw(fred barney betty wilma); # better!<br />
11、Array Element Access<br />
&nbsp;&nbsp; @fred = (7,8,9);<br />
&nbsp;&nbsp; $b = $fred[0]; # give 7 to $b (first element of @fred)<br />
&nbsp;&nbsp; $fred[0] = 5; # now @fred = (5,8,9)<br />
&nbsp;&nbsp; @fred[0,1]; # same as ($fred[0],$fred[1])<br />
12、push and pop Functions<br />
&nbsp;&nbsp; @mylist = (1,2,3);<br />
&nbsp;&nbsp; push(@mylist,4,5,6); # @mylist = (1,2,3,4,5,6)<br />
&nbsp;&nbsp; pop(@mylist); # @mylist = (1,2,3,4,5);<br />
13、shift and unshift Functions<br />
&nbsp;&nbsp; @fred = (5,6,7);<br />
&nbsp;&nbsp; unshift(@fred,2,3,4); # @fred is now (2,3,4,5,6,7)<br />
&nbsp;&nbsp; $x = shift(@fred); # $x gets 2, @fred is now (3,4,5,6,7)<br />
14、reverse Function<br />
&nbsp;&nbsp; @a = (7,8,9);<br />
&nbsp;&nbsp; @b = reverse(@a); # gives @b the value of (9,8,7)<br />
&nbsp;&nbsp; @b = reverse(7,8,9); # same thing<br />
<br />
14.1 &lt;STDIN&gt; as an Array:<br />
@a = &lt;STDIN&gt;; # read standard input in a list context<br />
<br />
15、The if/unless Statement，unless其实就是if not<br />
&nbsp;&nbsp; <br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">how&nbsp;old&nbsp;are&nbsp;you?&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #800080;">$a</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">STDIN</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">chomp</span><span style="color: #000000;">(</span><span style="color: #800080;">$a</span><span style="color: #000000;">);<br />
</span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">unless</span><span style="color: #000000;">&nbsp;(</span><span style="color: #800080;">$a</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">18</span><span style="color: #000000;">)&nbsp;{<br />
</span><span style="color: #008080;">5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">Old&nbsp;enough!&nbsp;Cool!&nbsp;So&nbsp;go&nbsp;vote!\n</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800080;">$voter</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;}</span></div>
16、true or false：<br />
&nbsp;&nbsp; In Perl, the rules are slightly weird, but they give you the expected<br />
&nbsp;&nbsp; results. The control expression is evaluated for a string value in scalar <br />
&nbsp;&nbsp; context (if it's already a string, no change, but if it's a number, it is converted<br />
&nbsp;&nbsp; to a string).If this string is either the empty string (with a length of zero), or a <br />
&nbsp;&nbsp; string consisting of the single character "0" (the digit zero), then the value of <br />
&nbsp;&nbsp; the expression is false. Anything else is true automatically.Some examples below：<br />
<br />
&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # converts to "0", so false<br />
&nbsp;&nbsp; 1-1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # computes to 0, then converts to "0", so false<br />
&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # converts to "1", so true<br />
&nbsp;&nbsp; ""&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # empty string, so false<br />
&nbsp;&nbsp; "1"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # not "" or "0", so true<br />
&nbsp;&nbsp; "00"&nbsp;&nbsp;&nbsp;&nbsp; # not "" or "0", so true (this is weird, watch out)<br />
&nbsp;&nbsp; "0.000"&nbsp; # also true for the same reason and warning<br />
&nbsp;&nbsp; undef&nbsp; &nbsp; # evaluates to "", so false<br />
17、elsif（not elseif or elif）<br />
&nbsp;&nbsp; <br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(some_expression_one)&nbsp;{<br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;one_true_statement_1;<br />
</span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">elsif</span><span style="color: #000000;">&nbsp;(some_expression_two)&nbsp;{<br />
</span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;two_true_statement_1;<br />
</span><span style="color: #008080;">5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{<br />
</span><span style="color: #008080;">6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;all_false_statement_1;<br />
</span><span style="color: #008080;">7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;}</span></div>
18、while/until Statement<br />
&nbsp;&nbsp; <br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">1</span>&nbsp;<span style="color: #800080;">$stops</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">0</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">do</span><span style="color: #000000;">&nbsp;{<br />
</span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800080;">$stops</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000; font-weight: bold;">Next&nbsp;stop?&nbsp;</span><span style="color: #000000; font-weight: bold;">"</span><span style="color: #000000;">;<br />
</span><span style="color: #008080;">5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">chomp</span><span style="color: #000000;">(</span><span style="color: #800080;">$location</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">STDIN</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">);<br />
</span><span style="color: #008080;">6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">until</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">$stops</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">5</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">$location</span><span style="color: #000000;">&nbsp;eq&nbsp;</span><span style="color: #000000; font-weight: bold;">'</span><span style="color: #000000; font-weight: bold;">home</span><span style="color: #000000; font-weight: bold;">'</span><span style="color: #000000;">;</span></div>
19、for Statement<br />
&nbsp;&nbsp; for ( initial_exp; test_exp; re-init_exp ) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; statement_1;<br />
&nbsp;&nbsp; }<br />
20、foreach Statement<br />
&nbsp;&nbsp; @a = (1,2,3,4,5);<br />
&nbsp;&nbsp; foreach $b (reverse @a) {<br />
&nbsp;&nbsp;&nbsp;&nbsp; print $b;<br />
&nbsp;&nbsp; }<br />
&nbsp;&nbsp; or<br />
&nbsp;&nbsp; foreach (reverse @a) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print;&nbsp; # $_ variable is used as a default for many of Perl's operations<br />
&nbsp;&nbsp; }<br />
21、Hash<br />
&nbsp;&nbsp; %smooth = ("aaa","bbb","234.5",456.7);<br />
22、keys Function<br />
&nbsp;&nbsp; $fred{"aaa"} = "bbb";<br />
&nbsp;&nbsp; $fred{234.5} = 456.7;<br />
&nbsp;&nbsp; @list = keys(%fred); # @list gets ("aaa",234.5) or (234.5,"aaa")<br />
23、values function<br />
&nbsp;&nbsp; %lastname = (); # force %lastname empty<br />
&nbsp;&nbsp; $lastname{"fred"} = "flintstone";<br />
&nbsp;&nbsp; $lastname{"barney"} = "rubble";<br />
&nbsp;&nbsp; @lastnames = values(%lastname); # grab the values<br />
24、each Function<br />
&nbsp;&nbsp; while (($first,$last) = each(%lastname)) {<br />
&nbsp;&nbsp;&nbsp;&nbsp; print "The last name of $first is $last\n";<br />
&nbsp;&nbsp; }<br />
25、delete Function<br />
&nbsp;&nbsp; %fred = ("aaa","bbb",234.5,34.56); # give %fred two elements<br />
&nbsp;&nbsp; delete $fred{"aaa"};&nbsp; //# %fred is now just one key-value pair<br />
26、Hash Slices<br />
&nbsp;&nbsp; @score{"fred","barney","dino"} = (205,195,30);<br />
&nbsp;&nbsp; Hash slices can also be used to merge a smaller hash into a larger one. In this example, <br />
&nbsp;&nbsp; the smaller hash takes precedence in the sense that if there are duplicate keys, <br />
&nbsp;&nbsp; the value&nbsp;&nbsp;&nbsp; from the smaller hash is used: <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %league{keys %score} = values %score;<br />
&nbsp;&nbsp;&nbsp; Here, the values of %score are merged into the %league hash. This is equivalent to <br />
&nbsp;&nbsp;&nbsp; the much slower operation:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %league = (%league, %score); # merge %score into %league<br />
<br />
27、Input from STDIN<br />
&nbsp;&nbsp; $a = &lt;STDIN&gt;; # read the next line<br />
&nbsp;&nbsp; @a = &lt;STDIN&gt;;<br />
&nbsp;&nbsp; <br />
&nbsp;&nbsp; Typically, one thing you want to do is read all lines one at a time and do something <br />
&nbsp;&nbsp; with each line. One common way to do this is:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (defined($line = &lt;STDIN&gt;)) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # process $line here<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
28、Diamond Operator<br />
&nbsp;&nbsp; If you don't specify any filenames on the command line, the diamond operator reads <br />
&nbsp;&nbsp; from standard input automatically.<br />
&nbsp;&nbsp; #!/usr/bin/perl<br />
&nbsp;&nbsp; while (&lt;&gt;) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print $_;<br />
&nbsp;&nbsp; }<br />
<br />
&nbsp;&nbsp; or<br />
&nbsp;&nbsp; @ARGV = ("aaa","bbb","ccc");<br />
&nbsp;&nbsp; while (&lt;&gt;) { # process files aaa, bbb, and ccc<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "this line is: $_";&nbsp; //print every line in these files aaa,bbb and ccc<br />
&nbsp;&nbsp; }<br />
29、printf for Formatted Output<br />
&nbsp;&nbsp; printf "%15s %5d %10.2f\n", $s, $n, $r;<br />
<br />
30、if (($words{$somename} || "groucho") eq $someguess) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1; # return value is true<br />
&nbsp;&nbsp;&nbsp; 注意上面的||不是或运算符，而是说如果左边的值为空的话，那么就是用右边的值。<br />
<br />
<img src ="http://www.blogjava.net/persister/aggbug/250113.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/persister/" target="_blank">persister</a> 2009-01-06 15:42 <a href="http://www.blogjava.net/persister/archive/2009/01/06/250113.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>