<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>My Crusade &#187; Perl</title>
	<atom:link href="http://www.jarodwang.cn/tag/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jarodwang.cn</link>
	<description>For the future we believe in.</description>
	<lastBuildDate>Tue, 07 Sep 2010 05:42:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>3 Tips in Perl Programming</title>
		<link>http://www.jarodwang.cn/2009/05/09/three-tips-in-perl-programming/</link>
		<comments>http://www.jarodwang.cn/2009/05/09/three-tips-in-perl-programming/#comments</comments>
		<pubDate>Sat, 09 May 2009 09:20:31 +0000</pubDate>
		<dc:creator>jarodwang</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://www.jarodwang.cn/?p=232</guid>
		<description><![CDATA[1. 获取当前时间 Perl 有一个函数 localtime 可以用来实现这个功能。在具体使用的过程中需要注意的是，它返回的月份数值是以 0 为基准的，而年份数值是在减掉 1900 之后的值，所以很多时候都需要进行相应的调整： # construct log file name my &#40;$sec, $min, $hour, $day, $month, $year&#41; = &#40;localtime&#41; &#91;0, 1, 2, 3, 4, 5&#93;; $month = $month + 1; $year = $year + 1900; &#160; my $local_node; $local_node = qx&#123;hostname&#125;; chomp $local_node; &#160; my $filename = sprintf&#40;&#34;%04d%02d%02d%02d%02d%02d&#34;, $year, $month, [...]]]></description>
			<content:encoded><![CDATA[<p>1. 获取当前时间<br />
Perl 有一个函数 localtime 可以用来实现这个功能。在具体使用的过程中需要注意的是，它返回的月份数值是以 0 为基准的，而年份数值是在减掉 1900 之后的值，所以很多时候都需要进行相应的调整：</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># construct log file name</span>
<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sec</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$min</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$hour</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$day</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$month</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$year</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">localtime</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$month</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$month</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$year</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$year</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1900</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$local_node</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$local_node</span> <span style="color: #339933;">=</span> <span style="color: #000066;">qx</span><span style="color: #009900;">&#123;</span>hostname<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">chomp</span> <span style="color: #0000ff;">$local_node</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #000066;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%04d%02d%02d%02d%02d%02d&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$year</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$month</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$day</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$hour</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$min</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$sec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;kill_crsd_&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$local_node</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;_&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$filename</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;.log&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$KILL_CRSD_LOG</span> <span style="color: #339933;">=</span> catfile<span style="color: #009900;">&#40;</span>curdir<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>2. 一个写日志文件的函数<br />
经常跑一些小脚本去循环做一些破坏性测试，由于有些操作具有重启节点的潜在可能性，所以这样一个需求也就随之而来。下面就是一个我用来写日志文件的函数的实现：</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Write trace file</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># trace(@content)</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #000000; font-weight: bold;">sub</span> trace<span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sec</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$min</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$hour</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$day</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$month</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$year</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">localtime</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$month</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$month</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$year</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$year</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1900</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span>LOGFILE<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;&gt;&gt;$KILL_CRSD_LOG&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;ERROR: Can't open $KILL_CRSD_LOG: $!&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span> LOGFILE <span style="color: #ff0000;">&quot;%04d-%02d-%02d %02d:%02d:%02d: @_<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$year</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$month</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$day</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$hour</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$min</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$sec</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">close</span> <span style="color: #009900;">&#40;</span>LOGFILE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>有了这样一个函数之后，可以在进行某个操作之前调用它来记录一下，这样就不怕因为节点重启而丢失脚本的运行信息了。</p>
<p>3. 高于秒级精度的休眠<br />
要让脚本休眠一段时间，我常用的是 Perl 中的 sleep 函数，但是遇到需要进行高于秒级精度的休眠时，这个函数就无能为力了。这时可以使用 select 操作符来实现：</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">select</span> <span style="color: #000066;">undef</span><span style="color: #339933;">,</span> <span style="color: #000066;">undef</span><span style="color: #339933;">,</span> <span style="color: #000066;">undef</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4.75</span><span style="color: #339933;">;</span></pre></div></div>

<p>上面的语句将使你的脚本休眠 4.75 秒。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jarodwang.cn/2009/05/09/three-tips-in-perl-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Perl Debugger</title>
		<link>http://www.jarodwang.cn/2008/12/23/the-perl-debugger/</link>
		<comments>http://www.jarodwang.cn/2008/12/23/the-perl-debugger/#comments</comments>
		<pubDate>Tue, 23 Dec 2008 12:30:13 +0000</pubDate>
		<dc:creator>jarodwang</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[debugger]]></category>
		<category><![CDATA[ScribeFire]]></category>

		<guid isPermaLink="false">http://www.jarodwang.cn/2008/12/23/the-perl-debugger/</guid>
		<description><![CDATA[今天同事老铁问了我几个关于 Perl 的编程的小问题，包括 @ARGV 和 Getopt::Long 的使用。其间还使用到了 Perl 自带的 debugger，其实这个工具我也不是第一次使用了，但是平时用得少，印象中存留的就剩下 s 和 x 命令了。在调试一个对 Getopt::Long::GetOptions 的调用的时候，用 s 进入了函数的内部半天没有能够出来，后来 Google 了一下，找到了一个 Perl Debugger Quick Reference Card 的 PDF 文档，用 r 命令快速地来到了 GetOptions 的结尾。 另外，这篇文章是我第一次尝试使用 ScribeFire 这个 FireFox 插件来写 Blog，顺便试试能不能同步到 jarodwang.cn 上面去。]]></description>
			<content:encoded><![CDATA[<p>今天同事老铁问了我几个关于 Perl 的编程的小问题，包括 <a href="http://perldoc.perl.org/perlvar.html#Predefined-Names">@ARGV</a> 和 <a href="http://perldoc.perl.org/Getopt/Long.html">Getopt::Long</a> 的使用。其间还使用到了 Perl 自带的 <a href="http://perldoc.perl.org/perldebug.html">debugger</a>，其实这个工具我也不是第一次使用了，但是平时用得少，印象中存留的就剩下 s 和 x 命令了。在调试一个对 Getopt::Long::GetOptions 的调用的时候，用 s 进入了函数的内部半天没有能够出来，后来 Google 了一下，找到了一个 <a href="http://refcards.com/docs/forda/perl-debugger/perl-debugger-refcard-a4.pdf">Perl Debugger Quick Reference Card</a> 的 PDF 文档，用 r 命令快速地来到了 GetOptions 的结尾。</p>
<p>另外，这篇文章是我第一次尝试使用 <a href="https://addons.mozilla.org/en-US/firefox/addon/1730">ScribeFire</a> 这个 FireFox 插件来写 Blog，顺便试试能不能同步到 <a href="http://jarodwang.cn/">jarodwang.cn</a> 上面去。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jarodwang.cn/2008/12/23/the-perl-debugger/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
