﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-gyn-随笔分类-perl</title><link>http://www.cnitblog.com/gyn/category/2447.html</link><description>Win32下的Perl，无用的select，停滞的Tk，结束吧....</description><language>zh-cn</language><lastBuildDate>Wed, 28 Sep 2011 07:00:33 GMT</lastBuildDate><pubDate>Wed, 28 Sep 2011 07:00:33 GMT</pubDate><ttl>60</ttl><item><title>Perl程序系统服务的实现办法</title><link>http://www.cnitblog.com/gyn/archive/2008/12/19/52775.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Fri, 19 Dec 2008 01:12:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/12/19/52775.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/52775.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/12/19/52775.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/52775.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/52775.html</trackback:ping><description><![CDATA[
		<p class="p0" style="MARGIN: 0cm 0cm 0pt">
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">有人想要一个可以远程关机的程序，想了下似乎相当地简单，无非是一个</span>
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">cs</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">的</span>
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">socket</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">，发一个命令过去，然后调用关机指令“</span>
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">shutdown -s -t</font> 0</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">”，于是机器就关掉了。很快就写完了，即使行而已的小玩意儿，转念一想总不好让要被关的机器整天开着个黑乎乎的命令行窗口吧，所以自然想到了要把它做成一个系统服务。</span>
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?>
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="p0" style="MARGIN: 0cm 0cm 0pt">
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">说起来，</span>
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">pdk</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">的更新算是挺勤快的，一转眼到</span>
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">7.3</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">了。其中有一个功能就是用来创建</span>
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">win</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">下的系统服务的，但首先必须要遵循一些规则。</span>
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="p0" style="MARGIN: 0cm 0cm 0pt">
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">具体的可以在帮助文档里找到，这里只是简要地说明一些必要的东西，并举一个例子来说明。有三个东西时一定要定义的，分别是</span>
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">$PerlSvc::Name</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">、</span>
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">$PerlSvc::DisplayName</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">和</span>
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">PerlSvc::startup()</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">，即服务名称、控制面板中显示的服务名称和主程序（也就是服务启动时要运行的程序）。其他的都是可有可无的，对于这么一个简单的东西，完全没有必要。</span>
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">package PerlSvc;<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<o:p>
								<font face="Courier New"> </font>
						</o:p>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">our %Config;<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<o:p>
								<font face="Courier New"> </font>
						</o:p>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">sub Startup {<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>while (ContinueRun()) {<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span> <span style="mso-spacerun: yes"> </span> $local_addr=pack('SnC4x8', 2, 32080, 127,0,0,1);<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>socket(FILE_TRANS_SERV, 2, 1, getprotobyname('tcp')) or die("socket failed for $!");<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>bind(FILE_TRANS_SERV, $local_addr) or die("bind failed for $!");<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>listen(FILE_TRANS_SERV,3);<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>$remote_addr = '';<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>for(; $remote_addr=accept(FILE_TRANS_CLIENT,FILE_TRANS_SERV); close(FILE_TRANS_CLIENT)){<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-tab-count: 3">           </span>$in = &lt;FILE_TRANS_CLIENT&gt;;<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-tab-count: 3">           </span>while (&lt;FILE_TRANS_CLIENT&gt;) {}<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-tab-count: 3">           </span>if ($in =~ 'shutdown') { <o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-tab-count: 4">              </span>close(FILE_TRANS_CLIENT); close(FILE_TRANS_SERV);<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-tab-count: 4">              </span>system "shutdown -s -t 0";<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-tab-count: 3">           </span>}<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>}<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-tab-count: 2">       </span>close(FILE_TRANS_SERV);<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-tab-count: 1">    </span>}<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">}<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<o:p>
								<font face="Courier New"> </font>
						</o:p>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">sub Pause { }<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">sub Continue { }<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">sub Interactive { }<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">sub Help { }<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<o:p>
								<font face="Courier New"> </font>
						</o:p>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">sub Install {<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-spacerun: yes">    </span>$Config{ServiceName} = 'MyShutdownService';<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-spacerun: yes">    </span>$Config{DisplayName} = 'My Shutdown Service';<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">}<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<o:p>
								<font face="Courier New"> </font>
						</o:p>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">sub Remove {<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">
								<span style="mso-spacerun: yes">    </span>$Config{ServiceName} = 'MyShutdownService';<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">}<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<o:p>
								<font face="Courier New"> </font>
						</o:p>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">package main;<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<o:p>
								<font face="Courier New"> </font>
						</o:p>
				</span>
		</p>
		<p class="p0" style="BACKGROUND: #d9d9d9; MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">1;<o:p></o:p></font>
				</span>
		</p>
		<p class="p0" style="MARGIN: 0cm 0cm 0pt">
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">在</span>
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">startup</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">中，我开了一个监听</span>
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">socket</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">，等待接收关机指令<span style="FONT-SIZE: 10.5pt; FONT-FAMILY: '黑体'; mso-spacerun: 'yes'"><font face="黑体"><span style="FONT-SIZE: 10.5pt; FONT-FAMILY: 黑体; mso-bidi-font-size: 11.0pt; mso-bidi-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA">，另外最好将<span lang="EN-US">socket</span>的建立和注销过程写在一个<span lang="EN-US">while</span>循环中，避免不必要的逻辑错误</span><span style="FONT-SIZE: 10.5pt; FONT-FAMILY: 黑体; mso-bidi-font-size: 11.0pt; mso-bidi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA">。</span></font></span>在</span>
				<span lang="EN-US" style="mso-fareast-font-family: 黑体">
						<font face="Courier New">Install</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Courier New'">中定义了两个服务名称。</span>
		</p>
<img src ="http://www.cnitblog.com/gyn/aggbug/52775.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-12-19 09:12 <a href="http://www.cnitblog.com/gyn/archive/2008/12/19/52775.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Perl程序转换为Tcl/Tk实例一则</title><link>http://www.cnitblog.com/gyn/archive/2008/10/11/50093.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sat, 11 Oct 2008 08:40:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/10/11/50093.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/50093.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/10/11/50093.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/50093.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/50093.html</trackback:ping><description><![CDATA[
		<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" />
		<!--[if gte mso 9]><xml>
 <w:WordDocument>
  <w:View>Normal</w:View>
  <w:Zoom>0</w:Zoom>
  <w:PunctuationKerning/>
  <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>
  <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
  <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
  <w:ValidateAgainstSchemas/>
  <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
  <w:IgnoreMixedContent>false</w:IgnoreMixedContent>
  <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
  <w:Compatibility>
   <w:SpaceForUL/>
   <w:BalanceSingleByteDoubleByteWidth/>
   <w:DoNotLeaveBackslashAlone/>
   <w:ULTrailSpace/>
   <w:DoNotExpandShiftReturn/>
   <w:AdjustLineHeightInTable/>
   <w:BreakWrappedTables/>
   <w:SnapToGridInCell/>
   <w:WrapTextWithPunct/>
   <w:UseAsianBreakRules/>
   <w:DontGrowAutofit/>
   <w:UseFELayout/>
  </w:Compatibility>
  <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
 </w:WordDocument>
</xml><![endif]-->
		<!--[if gte mso 9]><xml>
 <w:LatentStyles DefLockedState="false" LatentStyleCount="156">
 </w:LatentStyles>
</xml><![endif]-->
		<style>
				<!--
 /* Font Definitions */
 @font-face
	{font-family:黑体;
	panose-1:2 1 6 0 3 1 1 1 1 1;
	mso-font-alt:SimHei;
	mso-font-charset:134;
	mso-generic-font-family:auto;
	mso-font-pitch:variable;
	mso-font-signature:1 135135232 16 0 262144 0;}
@font-face
	{font-family:"\@黑体";
	panose-1:2 1 6 0 3 1 1 1 1 1;
	mso-font-charset:134;
	mso-generic-font-family:auto;
	mso-font-pitch:variable;
	mso-font-signature:1 135135232 16 0 262144 0;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{mso-style-parent:"";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	mso-pagination:none;
	font-size:10.5pt;
	mso-bidi-font-size:12.0pt;
	font-family:Arial;
	mso-fareast-font-family:黑体;
	mso-bidi-font-family:"Times New Roman";
	mso-font-kerning:1.0pt;}
 /* Page Definitions */
 @page
	{mso-page-border-surround-header:no;
	mso-page-border-surround-footer:no;}
@page Section1
	{size:612.0pt 792.0pt;
	margin:72.0pt 90.0pt 72.0pt 90.0pt;
	mso-header-margin:36.0pt;
	mso-footer-margin:36.0pt;
	mso-paper-source:0;}
div.Section1
	{page:Section1;}
-->
		</style>
		<!--[if gte mso 10]>
<style>
 /* Style Definitions */
 table.MsoNormalTable
	{mso-style-name:普通表格;
	mso-tstyle-rowband-size:0;
	mso-tstyle-colband-size:0;
	mso-style-noshow:yes;
	mso-style-parent:"";
	mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
	mso-para-margin:0cm;
	mso-para-margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	font-size:10.0pt;
	font-family:"Times New Roman";
	mso-fareast-font-family:"Times New Roman";
	mso-ansi-language:#0400;
	mso-fareast-language:#0400;
	mso-bidi-language:#0400;}
</style>
<![endif]-->
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<span style="font-family: 黑体;">每种语言都有其擅长和不足的地方，在某项工作甚至工作的某一部分中选择合适的语言往往可以达到事半功倍的效果。所以，多掌握一些知识是很有必要的，当然了也可以用一些大语言，比如说</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">C</span>
				<span style="font-family: 黑体;">或者</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Java</span>
				<span style="font-family: 黑体;">，它们几乎是可以通吃一切问题的，但是付出的代价一样高昂，一些可以用几行</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Perl</span>
				<span style="font-family: 黑体;">或者</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Python</span>
				<span style="font-family: 黑体;">实现的功能，换作</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Java</span>
				<span style="font-family: 黑体;">可能需要花费几倍的工作量，所以这里就不谈了。</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<span style="font-family: 黑体;">在很久以前，为了方便工作人员录制电视节目，我用</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Perl</span>
				<span style="font-family: 黑体;">写了一个基于</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Tk</span>
				<span style="font-family: 黑体;">的设置界面。界面如下图：</span>
		</p>
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<span style="font-family: 黑体;">
						<img src="http://www.cnitblog.com/images/cnitblog_com/gyn/tkcl.PNG" alt="tkcl.PNG" width="592" border="0" height="48" />
						<br />
				</span>
		</p>
		<p class="MsoNormal" style="text-indent: 17.95pt;">
		</p>
		<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" />
		<!--[if gte mso 9]><xml>
 <w:WordDocument>
  <w:View>Normal</w:View>
  <w:Zoom>0</w:Zoom>
  <w:PunctuationKerning/>
  <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>
  <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
  <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
  <w:ValidateAgainstSchemas/>
  <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
  <w:IgnoreMixedContent>false</w:IgnoreMixedContent>
  <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
  <w:Compatibility>
   <w:SpaceForUL/>
   <w:BalanceSingleByteDoubleByteWidth/>
   <w:DoNotLeaveBackslashAlone/>
   <w:ULTrailSpace/>
   <w:DoNotExpandShiftReturn/>
   <w:AdjustLineHeightInTable/>
   <w:BreakWrappedTables/>
   <w:SnapToGridInCell/>
   <w:WrapTextWithPunct/>
   <w:UseAsianBreakRules/>
   <w:DontGrowAutofit/>
   <w:UseFELayout/>
  </w:Compatibility>
  <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
 </w:WordDocument>
</xml><![endif]-->
		<!--[if gte mso 9]><xml>
 <w:LatentStyles DefLockedState="false" LatentStyleCount="156">
 </w:LatentStyles>
</xml><![endif]-->
		<style>
				<!--
 /* Font Definitions */
 @font-face
	{font-family:黑体;
	panose-1:2 1 6 0 3 1 1 1 1 1;
	mso-font-alt:SimHei;
	mso-font-charset:134;
	mso-generic-font-family:auto;
	mso-font-pitch:variable;
	mso-font-signature:1 135135232 16 0 262144 0;}
@font-face
	{font-family:"\@黑体";
	panose-1:2 1 6 0 3 1 1 1 1 1;
	mso-font-charset:134;
	mso-generic-font-family:auto;
	mso-font-pitch:variable;
	mso-font-signature:1 135135232 16 0 262144 0;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{mso-style-parent:"";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	mso-pagination:none;
	font-size:10.5pt;
	mso-bidi-font-size:12.0pt;
	font-family:Arial;
	mso-fareast-font-family:黑体;
	mso-bidi-font-family:"Times New Roman";
	mso-font-kerning:1.0pt;}
 /* Page Definitions */
 @page
	{mso-page-border-surround-header:no;
	mso-page-border-surround-footer:no;}
@page Section1
	{size:612.0pt 792.0pt;
	margin:72.0pt 90.0pt 72.0pt 90.0pt;
	mso-header-margin:36.0pt;
	mso-footer-margin:36.0pt;
	mso-paper-source:0;}
div.Section1
	{page:Section1;}
-->
		</style>
		<!--[if gte mso 10]>
<style>
 /* Style Definitions */
 table.MsoNormalTable
	{mso-style-name:普通表格;
	mso-tstyle-rowband-size:0;
	mso-tstyle-colband-size:0;
	mso-style-noshow:yes;
	mso-style-parent:"";
	mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
	mso-para-margin:0cm;
	mso-para-margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	font-size:10.0pt;
	font-family:"Times New Roman";
	mso-fareast-font-family:"Times New Roman";
	mso-ansi-language:#0400;
	mso-fareast-language:#0400;
	mso-bidi-language:#0400;}
</style>
<![endif]-->
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<span style="font-family: 黑体;">大致上就是设置一个时间，然后在该时间点自动启动一个录制程序。当时由于时间仓促，本着够用就好的精神，很快就写好了，所以难免留下一些</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">bug</span>
				<span style="font-family: 黑体;">。比如说设置完之后，没有将按钮重置为退出。这还是小事，最主要的还是一旦录制程序启动，整个界面就被阻塞，这是</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">windows</span>
				<span style="font-family: 黑体;">下</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Perl</span>
				<span style="font-family: 黑体;">的老毛病了，几乎无药可救。</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<span style="font-family: 黑体;">其实即便如此，这个程序还是可以顺利使用的，但是作为一个解决长时间运算阻塞界面的办法，将它移植到</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Tcl</span>
				<span style="font-family: 黑体;">上，还是有一些意义的。在上一篇随笔中，我大致讲到了</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">filevent</span>
				<span style="font-family: 黑体;">和</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">select</span>
				<span style="font-family: 黑体;">在</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">windows</span>
				<span style="font-family: 黑体;">下的差别，正好可以在这里验证一下，同时通过移植的过程，也可以更清楚地了解和区分两种语言在某些功能上的不同实现办法。最终实现的工具如下图</span>
				<span style="font-family: 黑体;">：</span>
		</p>
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<span style="font-family: 黑体;">
						<img src="http://www.cnitblog.com/images/cnitblog_com/gyn/chonglu.PNG" alt="chonglu.PNG" width="649" border="0" height="186" />
						<br />
				</span>
		</p>
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<br />
		</p>
		<p class="MsoNormal" style="text-indent: 17.95pt;">
		</p>
		<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" />
		<!--[if gte mso 9]><xml>
 <w:WordDocument>
  <w:View>Normal</w:View>
  <w:Zoom>0</w:Zoom>
  <w:PunctuationKerning/>
  <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>
  <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
  <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
  <w:ValidateAgainstSchemas/>
  <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
  <w:IgnoreMixedContent>false</w:IgnoreMixedContent>
  <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
  <w:Compatibility>
   <w:SpaceForUL/>
   <w:BalanceSingleByteDoubleByteWidth/>
   <w:DoNotLeaveBackslashAlone/>
   <w:ULTrailSpace/>
   <w:DoNotExpandShiftReturn/>
   <w:AdjustLineHeightInTable/>
   <w:BreakWrappedTables/>
   <w:SnapToGridInCell/>
   <w:WrapTextWithPunct/>
   <w:UseAsianBreakRules/>
   <w:DontGrowAutofit/>
   <w:UseFELayout/>
  </w:Compatibility>
  <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
 </w:WordDocument>
</xml><![endif]-->
		<!--[if gte mso 9]><xml>
 <w:LatentStyles DefLockedState="false" LatentStyleCount="156">
 </w:LatentStyles>
</xml><![endif]-->
		<style>
				<!--
 /* Font Definitions */
 @font-face
	{font-family:黑体;
	panose-1:2 1 6 0 3 1 1 1 1 1;
	mso-font-alt:SimHei;
	mso-font-charset:134;
	mso-generic-font-family:auto;
	mso-font-pitch:variable;
	mso-font-signature:1 135135232 16 0 262144 0;}
@font-face
	{font-family:"\@黑体";
	panose-1:2 1 6 0 3 1 1 1 1 1;
	mso-font-charset:134;
	mso-generic-font-family:auto;
	mso-font-pitch:variable;
	mso-font-signature:1 135135232 16 0 262144 0;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{mso-style-parent:"";
	margin:0cm;
	margin-bottom:.0001pt;
	text-align:justify;
	text-justify:inter-ideograph;
	mso-pagination:none;
	font-size:10.5pt;
	mso-bidi-font-size:12.0pt;
	font-family:Arial;
	mso-fareast-font-family:黑体;
	mso-bidi-font-family:"Times New Roman";
	mso-font-kerning:1.0pt;}
 /* Page Definitions */
 @page
	{mso-page-border-surround-header:no;
	mso-page-border-surround-footer:no;}
@page Section1
	{size:595.3pt 841.9pt;
	margin:72.0pt 90.0pt 72.0pt 90.0pt;
	mso-header-margin:42.55pt;
	mso-footer-margin:49.6pt;
	mso-paper-source:0;
	layout-grid:15.6pt;}
div.Section1
	{page:Section1;}
-->
		</style>
		<!--[if gte mso 10]>
<style>
 /* Style Definitions */
 table.MsoNormalTable
	{mso-style-name:普通表格;
	mso-tstyle-rowband-size:0;
	mso-tstyle-colband-size:0;
	mso-style-noshow:yes;
	mso-style-parent:"";
	mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
	mso-para-margin:0cm;
	mso-para-margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	font-size:10.0pt;
	font-family:"Times New Roman";
	mso-fareast-font-family:"Times New Roman";
	mso-ansi-language:#0400;
	mso-fareast-language:#0400;
	mso-bidi-language:#0400;}
</style>
<![endif]-->
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<span style="font-family: 黑体;">这里将代码拆分开来进行分析。</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<span style="font-family: 黑体;">首先是界面部分，这一部分其实是最简单的，所以在实现上几乎一样，只不过是语法上的差别而已。有一点值得注意的是</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Tcl</span>
				<span style="font-family: 黑体;">中是不需要的</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">mainloop</span>
				<span style="font-family: 黑体;">这样的命令来显式地启动主循环。</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<span style="font-family: 黑体;">第二，因为定时启动需要计算启动时间与当前事件的差值，所以需要处理一些时间相关的参数。</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Perl</span>
				<span style="font-family: 黑体;">中可以使用</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">localtime</span>
				<span style="font-family: 黑体;">来得到年份、月、日和秒等共</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">9</span>
				<span style="font-family: 黑体;">个值，供组合使用。在</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Tcl</span>
				<span style="font-family: 黑体;">中，有一个</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">clock</span>
				<span style="font-family: 黑体;">命令，通过</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">format</span>
				<span style="font-family: 黑体;">可以实现类似的功能，感觉上</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Tcl</span>
				<span style="font-family: 黑体;">的更方便一些。相同的感觉也处理时间差值时也存在，因为</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Perl</span>
				<span style="font-family: 黑体;">中需要拆分一个某种形式的时间表达式，提取必要参数给</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">timelocal</span>
				<span style="font-family: 黑体;">计算总秒数，而</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Tcl</span>
				<span style="font-family: 黑体;">的</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">clock</span>
				<span style="font-family: 黑体;">有</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">scan</span>
				<span style="font-family: 黑体;">可以直接生成。</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<span style="font-family: 黑体;">第三，在像</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Perl</span>
				<span style="font-family: 黑体;">的</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">BrowseEntry</span>
				<span style="font-family: 黑体;">和</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Tcl</span>
				<span style="font-family: 黑体;">的</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">ComboBox</span>
				<span style="font-family: 黑体;">的下拉框中，经常会插入一串连续的数字。</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Perl</span>
				<span style="font-family: 黑体;">可以使用形如“（</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">1..10</span>
				<span style="font-family: 黑体;">）”来表示</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">1-10</span>
				<span style="font-family: 黑体;">这</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">10</span>
				<span style="font-family: 黑体;">个数字，但是在</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Tcl</span>
				<span style="font-family: 黑体;">里没有类似的命令，所以当遇到大数组表达式的时候，最好是写一个方法来模拟</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Perl</span>
				<span style="font-family: 黑体;">中的这种功能。</span>
		</p>
		<div class="Section0" style="">
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">proc listOfNum {from to {step 1}} {</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   	</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">set lst [list ]</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   	</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">set index $from</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   	</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">while {$index &lt;= $to} {</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   	</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   	</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">lappend lst $index</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   	</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   	</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">set index [expr $index + $step]</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   	</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">}</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   	</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">return $lst</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">}</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
		</div>
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<span style="font-family: 黑体;">第四，为了确保录制任务在制定的时间点启动，需要有检查是否超时。在</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Tcl</span>
				<span style="font-family: 黑体;">里这个实现因为有</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">after</span>
				<span style="font-family: 黑体;">变得很轻松，只要指定一个需要等待时间戳，</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Tcl</span>
				<span style="font-family: 黑体;">就会在过晚这段时间之后启动需要执行的指令，而</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Perl</span>
				<span style="font-family: 黑体;">就麻烦多了。在</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Tk</span>
				<span style="font-family: 黑体;">的</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">mainloop</span>
				<span style="font-family: 黑体;">中，使用</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">repeat(1000, sub {…})</span>
				<span style="font-family: 黑体;">使其每隔一秒钟执行一次检查，看是否超时。一旦检查到超时则停止检查并启动录制；若没有则提示继续下次检查。</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<span style="font-family: 黑体;">第五，在如何启动录制这点上，两者使用了不同的手段。</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Tcl</span>
				<span style="font-family: 黑体;">使用管道来启动进程，使得自身不被阻塞（不过在使用producer的时候还是会阻塞，用ping就没问题，这个很奇怪），并且使用</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">filevent</span>
				<span style="font-family: 黑体;">监测打开句柄的状态。而</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">Perl</span>
				<span style="font-family: 黑体;">因为不可靠的</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">select</span>
				<span style="font-family: 黑体;">，我们只能使用</span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">system</span>
				<span style="font-family: 黑体;">，通过阻塞的办法来获知任务是否完成，这也是一个无可奈何的事情了。</span>
		</p>
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<span style="font-family: 黑体;">（<a href="/Files/gyn/chonglu.rar">代码下载</a>）<br /></span>
				<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<br />
		<span style="font-family: 黑体;">
		</span>
		<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">
				<o:p>
				</o:p>
		</span>
		<br />
		<span style="font-family: 黑体;">
		</span>
		<span style="font-family: &quot;Courier New&quot;;" lang="EN-US">
				<o:p>
				</o:p>
		</span>
<img src ="http://www.cnitblog.com/gyn/aggbug/50093.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-10-11 16:40 <a href="http://www.cnitblog.com/gyn/archive/2008/10/11/50093.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Perl与Tcl/Tk事件驱动的比较</title><link>http://www.cnitblog.com/gyn/archive/2008/09/20/49317.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sat, 20 Sep 2008 14:08:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/09/20/49317.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/49317.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/09/20/49317.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/49317.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/49317.html</trackback:ping><description><![CDATA[
		<div class="Section0" style="">
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">因为在用</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> Perl-Tk </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">或者</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> Socket </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">时，经常会需要监视某些打开的句柄而不至于阻塞主循环，</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<font face="宋体">对于这些 win32 </font>
						</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">下的</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Perl</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">表现远不如</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Linux</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">下来得得心应手，但这时候我基本没有什么办法。这和</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">windows</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的底层</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">select</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">实现有关，而</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Perl</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">恰恰是调用它的，但我一直认为肯定有别的替代办法，因为</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Tcl/Tk</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">fileevent</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">就表现得很好。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">下面是一个例子，用来显示</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> ping </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的结果。这里有两个组件，一个是按钮，用来开始和停止</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> ping </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">；另一个是</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> text </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">用来显示</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> ping </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的结果。分别用</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> Perl </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">和</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> Tcl/Tk </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">代码来实现，看看他们有什么不同。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">use Tk;</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">$count = 4;</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">$host = '127.0.01';</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">$mw = MainWindow-&gt;new;</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">$bt = $mw-&gt;Button(-text =&gt; 'Clink to start ping', -command =&gt; \&amp;RunPing);</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">$bt-&gt;pack(-fill =&gt; 'x');</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">$tx = $mw-&gt;Text(-width =&gt; 50, -height =&gt; 30);</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">$tx-&gt;pack;</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">MainLoop;</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sub RunPing {</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   open $fd, "ping -n $count $host|";</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   $mw-&gt;fileevent($fd, 'readable' =&gt; sub {</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">      sysread $fd, $buf, 30;</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">      $tx-&gt;insert('end', $buf);</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">      });</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   $bt-&gt;configure(-text =&gt; 'close', -command =&gt; \&amp;ClosePing);</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">}</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sub ClosePing {</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   close $fd;</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   undef $fd;</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   $bt-&gt;configure(-text =&gt; 'Clink to start ping', -command =&gt; \&amp;RunPing);</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">}</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">运行的结果很让人失望，在</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> readable </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的事件中加入</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> print </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">后而已更清楚地看到，事实上</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> readable </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">事件是没有被触发过的。这里的</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> fileevent </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">是</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> Tk </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">中处理事件触发的函数，它为句柄的两个状态（</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> readable </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> writable </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">）设定触发函数，其本质上使用了系统的</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> select </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">或者</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> poll </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，换句话说即便是用</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> Perl </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> select </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">或者</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> IO::Seelct </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">也是于事无补。文档里的原话佐证了这点“</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> A file is considered to be readable on same basis as the system's select() (or poll()). </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">”。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">所以可以这么讲，</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> win32 </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">下的</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> Perl </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">是不大适合处理事件响应的，因而也就很难处理好处理网络相关或者界面需要运行后台长时间操作的情况。接下来换作是使用</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> Tcl/Tk </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">来实现这一功能，值得一提的是在</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> Perl </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">下</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> 50 </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">宽度的</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> button </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">和</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> text </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">是不等长的，所以索性在</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> pack </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">里用</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> fill </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">自动拓宽了，而在</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> Tcl/Tk </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">中没有这个问题。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">package require Tk</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">set pingcount 4</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">set host {127.0.0.1}</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">button .btn -text {Clink to start ping} -command "RunPing $host $pingcount" -width 50</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">text .text -width 50 -height 30</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">pack .btn .text</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">proc RunPing {host count} {</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   set fd [open "|ping -n $count $host"]</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   ;#fconfigure $fd -blocking 0</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   fileevent $fd readable "onPingRead $fd $count $host"</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   .btn configure -text "close" -command "closeFd $fd $count $host"</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">}</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">proc onPingRead {fd count host} {</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   if {[eof $fd]} {</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">      closeFd $fd $count $host</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   } else {</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">      set buf [gets $fd]</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">      .text insert {end} "$buf\n"</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   }</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">}</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">proc closeFd {fd count host} {</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   close $fd</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   .btn configure -text {Clink to start ping} -command "RunPing $host $count"</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; text-align: justify; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">}</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">为了使用同样条件的管道，屏蔽了设置非阻塞的代码行，但这么作</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">因为系统不得不等待管道中的进程运行结束，所以</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">也使得</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> close </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">没有起到应有的作用。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: '黑体';">运行的结果很令人满意，</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> test </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">没有被阻塞，在显示</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> ping </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">结果的同时，依然可以在</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> text </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">中执行输入，而且</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> button </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">也可以被顺利按下。这么看来</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> Tcl/Tk </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">比之</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> Perl </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">在</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> win32 </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">下，显然在事件处理上占有优势，而且这个占优的方面是极其重要的。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
		</div>
<img src ="http://www.cnitblog.com/gyn/aggbug/49317.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-09-20 22:08 <a href="http://www.cnitblog.com/gyn/archive/2008/09/20/49317.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>对POE事件队列顺序的一次亲身感受</title><link>http://www.cnitblog.com/gyn/archive/2008/09/18/49185.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Thu, 18 Sep 2008 01:00:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/09/18/49185.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/49185.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/09/18/49185.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/49185.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/49185.html</trackback:ping><description><![CDATA[
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">众所周知</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">POE</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">的消息触发机制会维护一个事件的队列，我经常会开几个</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">component</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">，然后来回</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">post</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">或者</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">call</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">。一直没有在意，</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">call</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">和</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">post</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">的区别，虽然我是知道这些区别的，但是实际需要用到的时候，这些区别对我而言几乎没有什么意义，所以经常随便用。前两天，因为需要用到网络连接，我决定把一个写得差不多了的</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">Tk</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">程序搬到</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">POE</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">上。主要的工作一是把变量存在</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">$_[HEAP]</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">中，二是将函数改为事件回调。仔细一点的话，一切还是很顺利的，但调试的时候还是出现了一些状况。这个程序差不多是一个在线的文稿系统，在切换文稿的时候，会对为保存信息进行提示，提醒用户保存。但是事情是，对于已保存的文件，在切换的时候，依然会跳出提醒对话框。我怀疑是字体更改标记被修改了，所以对其进行了跟踪，神奇的是在进入保存函数前标记为</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">0</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">，可一出函数永远为</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">1</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">。在保存函数中，有一个对字体修改事件的回调，该事件会在设置完字体之后将字体更改标记设置为</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">1</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">，表示已更改。在保存万之后，需要将该标记重置为</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">0</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">，但是这个重置的过程似乎没有起作用。细想之下，才恍然大悟，原来我用了</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">post</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">来回调字体设置事件，因此该事件是被放置在事件队列的尾部的，也就是说在保存函数执行完毕之后</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">POE</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">才会运行它，那么自然字体更改标记永远会被设置为</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">0</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">了。改为</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">call</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">之后就一切</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">OK</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">了，因为</span>
				<span lang="EN-US" style="mso-bidi-font-family: 'Courier New'">
						<font face="Courier New">call</font>
				</span>
				<span style="FONT-FAMILY: 黑体; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'">是立即执行该事件的。</span>
		</p>
<img src ="http://www.cnitblog.com/gyn/aggbug/49185.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-09-18 09:00 <a href="http://www.cnitblog.com/gyn/archive/2008/09/18/49185.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>在Perl中使用打印机打印格式化文档</title><link>http://www.cnitblog.com/gyn/archive/2008/09/06/48781.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sat, 06 Sep 2008 06:29:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/09/06/48781.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/48781.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/09/06/48781.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/48781.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/48781.html</trackback:ping><description><![CDATA[
		<div class="Section0" style="">
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left;">
						<span style="font-size: 10.5pt; font-family: '黑体';">不得不说的是</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">google</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">上搜索出来的竟然都是《</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">perl</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">在</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">win32</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">平台上直接操作打印机》，这是非常令人抱歉的文章。很显然是初学者写的，如果连这个都不知道，我还真是佩服他怎么学</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">perl</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的了，这不是单指</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">win32</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">下的打印办法，哪里都一样。但是这太简陋了，不是我想要的。</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left;">
						<span style="font-size: 10.5pt; font-family: '黑体';">当然了，在</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">avtivePerl</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">中你可以使用</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">ppm</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">下载</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Printer</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">来打印，但是当你看见那可怜的文档后，也许会打消这个念头，或者打印一些</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">text</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">是再好不过了。或者使用</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">cpan</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">安装</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Win32::Word::Writer</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">来打印，我想它是非常好的，但前提是你能装的上它，而我在</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">win32</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">下装不上。</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left;">
						<span style="font-size: 10.5pt; font-family: '黑体';">这个问题事实上困扰了我很久，因为很多时候你写的</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">tk</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">界面脚本，需要将分析的数据打印出来，如果不将这个功能集成进脚本，便不得不求助于其他的工具，比方说先生成一个文档，然后用</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">openoffice</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">或者其他打开它，最后在其中实现打印，这非常麻烦，而且也不容易让使用者接受。</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left;">
						<span style="font-size: 10.5pt; font-family: '黑体';">既然没有特别好的针对性工具，我变依照上面的流程模拟这个过程，但这一切都是在内存中实现的，因此对于用户，不会知道里面的实现细节，这样很好。首先我用</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Win32::OLE</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">打开一个文档，要保证它是不被</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">visiable</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">。之后将需要打印的文档按照其中的格式，打入到这个文档中。最后打印出来。不过可能是</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">OLE</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">bug</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，这样做会在内存中保留一个到该文档的句柄，在关机的时候不有提示出错，而且如果在程序中直接使用</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">close</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的话，会弹出对话框询问是否保存，这就做不到隔离使用者的效果了。所以，使用</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">saveas</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">将文档存到</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">disk</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">里，这算是一个无奈的妥协。下面是我在集群文档编辑系统中的用于打印的一个函数：</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sub file_print {</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    my  $cur_style   =  ' a ' ;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    my  $word        =</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">      Win32 :: OLE -&gt; new( ' Word.Application ' ,  ' Quit ' ) or die $ ! ;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    $word -&gt; { ' Visible ' } =  0 ;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    my  $document    =  $word -&gt; Documents -&gt; Add;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">     my  $selection   =  $word -&gt; Selection;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    my  $style       =  $document -&gt; Styles -&gt; Add( $cur_style );</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    my  $style_font  =  $style -&gt; {Font};</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    $style_font -&gt; {Name}                         =  $family ;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    $style_font -&gt; {Size}                          =  $size ;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    $style_font -&gt; {Bold}                         =  $weight ;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    $style_font -&gt; {Italic}                       =  $slant eq ' italic '  ?  1  :  0 ;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    $style_font -&gt; {Underline}                    =  $underline ;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    $document -&gt; ActiveWindow -&gt; Selection -&gt; {Style} =  $cur_style ;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    $selection -&gt; TypeText( $textundo -&gt; get( ' 1.0 ' ,  ' end ' ) );</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    $word -&gt; ActiveDocument -&gt; PrintOut;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    $document -&gt; SaveAs( ' printout.doc ' );</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    $document -&gt; Close ;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    $word -&gt; Quit;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">}</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: '黑体';">因为</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">tk</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">下的</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">font</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">和</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">word</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">中的有些不兼容，所以做了一些变换，比如说对于</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">slant</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">tk</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">是用</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">‘italic’</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">和</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">‘roman’</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的，相对于</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">word</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">就是布尔值了。</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
										<br />
								</o:p>
						</span>
				</p>
		</div>
<img src ="http://www.cnitblog.com/gyn/aggbug/48781.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-09-06 14:29 <a href="http://www.cnitblog.com/gyn/archive/2008/09/06/48781.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>修改pTk组件两则小记（ToolBar, DateEntry）</title><link>http://www.cnitblog.com/gyn/archive/2008/09/02/48604.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Tue, 02 Sep 2008 00:11:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/09/02/48604.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/48604.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/09/02/48604.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/48604.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/48604.html</trackback:ping><description><![CDATA[
		<div class="Section0" style="">
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: '黑体';">这里讲的两个都是非标准组件，所以一般都是建立在标准的</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">pTk</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">组件之上的，由纯</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">perl</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">代码写成的，因此也便于修改。</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: '黑体';">第一个是</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">ToolBar</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，就是文件浏览器中的在菜单栏之下的，一般叫工具栏。</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">pTk</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">中的</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">ToolBar</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">标准支持</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Button</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Label</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">LabEntry</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">OptionMenu</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">和自带的一个</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">separator</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">（这个还是很有用的）。如果要加入其他的组件，比如</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">BrowseEntry</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">pack</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">之后会被</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">ToolBar</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">放到工具栏的第二行上，很不方便。现在我们来看看</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">ToolBar.pm</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，里面到底是怎么处理标准支持的。搜索到</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">ToolLabel</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sub</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，里面的代码如下：</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sub ToolLabel {</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   my $self = shift;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   my $l = $self-&gt;{CONTAINER}-&gt;Label(@_);</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   push @{$self-&gt;{WIDGETS}} =&gt; $l;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   $self-&gt;_packWidget($l);</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">   return $l;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">}</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: '黑体';">对比其他的几个</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sub</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，我可以很肯定，只要将第四行中的</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Label</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">改成</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">BrowseEntry</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">就可以了，这里缺少的一行是关于</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">tip</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的，对于</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">BrowseEntry</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">来说，没有必要。</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: '黑体';">第二个是</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">DateEntry</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">。就是一个日期选择器，很多时候我们会用到它。我再使用的时候发现了一个问题，就是在显示月份的地方，出现了乱码，打开</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">DateEntry.pm</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">一看，找到如下一段：</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">my $monthlabel = (defined &amp;strftime</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">         ? strftime($w-&gt;cget('-headingfmt'),0,0,0,1,</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">                $w-&gt;{_month}-1,$w-&gt;{_year}-1900)</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">         : $w-&gt;{_month} . "/" . $w-&gt;{_year}</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">        );</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: '黑体';">显然，它是用了</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">strftime</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">这个函数，去除选择，直接用冒号后的形式即可。</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: '黑体';">还有，如何让在日期控件中的日期按钮被按下的时候响应一个事件。作为一般的控件这是一个必要的功能，但是在</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">DateEntry</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">中却没有，查看了源代码，原来是回调了用于更新标签和关闭的</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">selectDay</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">函数。这么作是很不负责任的，如果不修改源代码，我们就不得不在此控件旁添加一个按钮用于触发事件，这样一来不美观，而来更重要的是不符合一般的使用习惯。为此需要在</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">ConfigSpec</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">中添加一个</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">-btncommand</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的标签，并在</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">selectDay</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">中回调。</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
										<br />
								</o:p>
						</span>
				</p>
		</div>
<img src ="http://www.cnitblog.com/gyn/aggbug/48604.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-09-02 08:11 <a href="http://www.cnitblog.com/gyn/archive/2008/09/02/48604.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于ARP病毒防护的一些想法</title><link>http://www.cnitblog.com/gyn/archive/2008/08/26/48363.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Mon, 25 Aug 2008 23:55:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/08/26/48363.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/48363.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/08/26/48363.html#Feedback</comments><slash:comments>13</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/48363.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/48363.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 前言						 																																										大约是在三年前的时候，第一次碰到						 						ARP						 						病毒，当时的情况现在依然历历在目。不断的有人断网，之后范围越来越扩大，重启了交换机之后好了一段时间，接着又开始不正常，周而复始。后来在						 		...&nbsp;&nbsp;<a href='http://www.cnitblog.com/gyn/archive/2008/08/26/48363.html'>阅读全文</a><img src ="http://www.cnitblog.com/gyn/aggbug/48363.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-08-26 07:55 <a href="http://www.cnitblog.com/gyn/archive/2008/08/26/48363.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>文件结构保护系统概述</title><link>http://www.cnitblog.com/gyn/archive/2008/08/25/48328.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Mon, 25 Aug 2008 01:27:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/08/25/48328.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/48328.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/08/25/48328.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/48328.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/48328.html</trackback:ping><description><![CDATA[
		<div class="Section0" style="">
				<p class="p0" style="margin-bottom: 13pt; margin-top: 13pt; text-align: center; line-height: 172%;">
						<span style="font-weight: bold; font-size: 16pt; font-family: '黑体';">引入文件结构保护的原因</span>
						<span style="font-weight: bold; font-size: 16pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">随着越来越多的工作、数据被迁移到服务器及存储上，对于这些作为业务基础的软硬件的安全性和可靠性，我们需要提高关注度，并在尽可能的范围内提高这种可靠性，以保障工作的平稳顺利开展。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">针 对不同的内容，我们可以采取不同的安全保障策略。比方说，对于服务器的灾难恢复，比较方便而合适的办法是用双机热备，备机通过监视主机运行状况，实现尽可 能快地恢复服务。而对于资料的存储，我们多是会选择一些实践证明了的比较可靠的数据备份和恢复方法，这些方法多是基于某种特定的数据库的，或者是数据库自 身提供的。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">以上两点内容是大多数管理者想得到并且可以做得不错的，我们常常会忽略一点是，作为提供某种服务的程序本身的安全性。打个比方说，提供</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">web</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">服务的</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">apache</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">， 其配置和一些附属的文件可能遭到破坏，而且即便是有操作系统锁级的保护，进程也可能被恶意停止，进而破坏文件。拓展开来讲，任何以文件或者文件树形式存储 的数据的安全性，一向被忽略。其结果是，我们不得不重新安装服务并配置参数，这不是一项轻松的工作，尤其是对于一些复杂的程序而言。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">虽然针对不同服务的保护各不相同，但一般都需要涉及到对于该服务的文件结构保护，在此基础上在做一些进程的恢复工作等。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="margin-bottom: 13pt; margin-top: 13pt; text-align: center; line-height: 172%;">
						<span style="font-weight: bold; font-size: 16pt; font-family: '黑体';">文件结构保护系统的架构</span>
						<span style="font-weight: bold; font-size: 16pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">除了必要的防杀毒软件之外，还需要一些更一般的保护手段，因为病毒库的更新总是有滞后性的。这些手段包括建立文件结构信息备份、实时监控和文件恢复等。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">首 先，在确定系统一切正常的前提下，对于需要进行保护的文件结构，我们需要采集能唯一标识该结构的必要信息，至少包括一个描述文件结构的数据结构和在其中每 一个文件的唯一标记。就前者而言，可以是一个多叉树或者向量，这个根据不同的工具的特性具体而定；文件唯一标记可以通过计算整个文件的</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">md5</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">码来得到，这也是现阶段在网络上发布文件尤其是大型文件时，保证文件完整性的一个通用做法，比如说</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">ubuntu</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的发布。除了多叉树或向量数据结构需要驻留本地之外，其他的必要信息连同文件结构中所有有效文件将存储到一个可以保证安全的位置，这个位置一般是一台被隔离的服务器，通过有限的端口与存在受保护文件结构的服务器相连接，也就是监控端。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">完 成了文件结构的备份之后，将建立服务器之间的监控关系。因为文件遍历的本地性特点，所以主逻辑即数据结构与文件结构之间的比较需要在被监控段实现。根据比 较之后的结果，包括被恶意添加或删除的文件，做出是否恢复或删除的决定。而具体到每一个文件的完整性检测，则需要被监测端将当前次文件遍历中得到的</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">md5</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">码，发送至监测端进行比较。因此，从最终的实现来看，服务器两端之间的关系并不能完全用监控来描述，事实上被监控端将负责起遍历文件结构的工作，而监控端将主要负责</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">md5</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">码比较和文件恢复的功能。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">最后在监控端，通过一定的途径连接监控程序和备份信息，实现文件按顺序有效地回传，这样就基本上形成了一个文件结构保护系统的框架。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="margin-bottom: 13pt; margin-top: 13pt; text-align: center; line-height: 172%;">
						<span style="font-weight: bold; font-size: 16pt; font-family: '黑体';">需要注意的一些细节</span>
						<span style="font-weight: bold; font-size: 16pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">文件结构信息的采集需要在系统建立之初就准备就绪，然后通过人工拷贝的方法移植到监控端。这个采集过程相对来说比较简单，只需要一次文件结构的遍历就可以实现。监控端的信息存储可以考虑使用数据库，而在存储文件的时候，根据具体的回传特点可以直接存储在文件系统之下或者以</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">blob</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的形式存入数据库。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">相比于略显简单的初始化，保护系统实现的关键环节在于文件结构与数据结构的比较以及如何准确地回传文件。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">首 先数据结构的建立需要在保护系统启动的初始化环节完成，之后它将作为整个系统运行的基础，循环地将文件结构与它进行对比。而该对比过程可以通过两种方法来 实现。第一：两次遍历，即数据结构和文件结构遍历。这么做的好处是实现难度不大，尤其是在文件结构遍历部分，可以任意选择深度或广度遍历。但是针对遍历中 每个节点遇到的文件或文件夹，不得不查询一次数据结构，对于设计不佳的数据结构，这么做的结果可能又是一次遍历，这就增加了运行的时长。第二：一次遍历， 即在数据结构和文件结构同时以一种相对协调的步调向下遍历。这里所说的向下即是说，需要在完成本层次遍历的基础上才能继续下一个层次，也就是层次优先遍 历。这样的做法需要在设计上保证数据结构与文件结构具有类似性，而对于层次的控制也完全依靠人工逻辑完成，但在运行上将获得比前一种方法快一个数量级以上 的优势。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">在文件回传方面，由于存在很大可能性是需要恢复的一个文件列表，所以应准确控制其中每一个文件恢复的启动与截至。可以采用架设</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">ftp</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">服务器的方法，在传输单文件时阻塞本地剩余传输队列中的文件。在这里采用了这种模式，但未直接使用</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">ftp</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，而是采用了一种简化的模拟方式：被监控端维护一个文件处理队列，通过阻塞的方式和先进先出的顺序来处理</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">文件句柄</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">；建立了两个</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">socket</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">连接，其中之一在监控端接受被监控端发出的指令并启动数据传输，另一个在被监控端接受来自监控端的文件传输完成信号并关闭本地文件句柄，然后通知启动下一个文件传输。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="margin-bottom: 13pt; margin-top: 13pt; text-align: center; line-height: 172%;">
						<span style="font-weight: bold; font-size: 16pt; font-family: 'Courier New';">Perl </span>
						<span style="font-weight: bold; font-size: 16pt; font-family: '黑体';">实现的几点建议</span>
						<span style="font-weight: bold; font-size: 16pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">一：</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">对于这种通过网络来连接的服务，整体的框架可以考虑使用</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">poe</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，这将极大地减少底层代码的维护量，而将精力主要集中到业务逻辑的实现上来。尤其是在合理利用</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">component</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">的情况下，一段几十行的</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">socket</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">代码可能会被压缩为短短几行。更为重要的是，</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">poe</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">在单线程内实现了多任务模式，虽然依旧会阻塞在长任务执行过程中，但这方便各个任务之间信息的传递和资源的共享。对于未来其他模块功能的增加，也</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">提供了</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">一个可扩展的基础。比如，需要为客户端提供一个可视的操作界面，只需引入</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">Tk</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">，</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">poe</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">会在变量表中自动添加根元素</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">$poe_main_window</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">二：</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">由于文件结构信息的量一般不是很大，因此在事先未安装其他数据库的情况下，信息的存储可以考虑使用轻量级的</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">sqlite</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">。它没有守护进程，一般以嵌入的形式</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">服务</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">于各个</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">任务</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">中。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">三：</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">体现文件结构逻辑的数据结构，可以使用</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">hash</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">表来实现，在这里</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">hash</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">表起得是多叉树的作用。打个比方，针对绝对路径</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; font-size: 10.5pt; font-family: 'Arial'; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">d:\sources\perl\frs\md5_check.pl</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">其数据结构将是</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">:</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; font-size: 10.5pt; font-family: 'Arial'; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">$base-&gt;{d}-&gt;{sources}-&gt;{perl}-&gt;{frs}-&gt;{md5_check.pl} = ’f’</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">也就是说任意的目录是一个</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">hash</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">引用，而最终的文件节点是一个内容为“</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">f</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">”的</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">text</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">变量。那么在遍历时，就可以通过</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">ref</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">节点类型来判断是目录还是文件。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: left; line-height: 125%;">
						<span style="font-size: 10.5pt; font-family: '黑体';">四：</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">在处理关闭文件传输的指令时，可以考虑使用</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Arial';">ikc</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: '黑体';">来远程调用本地指令，这实际上是简化了由远程发指令、本地翻译再到本地指令执行这一连串的过程，用在这里也方便未来丰富远程指令。</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="text-indent: 17.95pt; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
										<br />
								</o:p>
						</span>
				</p>
		</div>
<img src ="http://www.cnitblog.com/gyn/aggbug/48328.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-08-25 09:27 <a href="http://www.cnitblog.com/gyn/archive/2008/08/25/48328.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>perl and Net::Pcap</title><link>http://www.cnitblog.com/gyn/archive/2008/07/27/47179.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sun, 27 Jul 2008 12:49:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/07/27/47179.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/47179.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/07/27/47179.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/47179.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/47179.html</trackback:ping><description><![CDATA[
		<div class="Section0" style="">
				<p class="p0" style="margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: '黑体';">原文在http://www.perlmonks.org/index.pl?node_id=170648里，代码抄了一遍。</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">use Net::Pcap;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">use NetPacket::Ethernet;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">use NetPacket::IP;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">use NetPacket::TCP;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">use strict;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">my $err;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  Use network device passed in program argments or if no</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  argument is passed, determine an appropriate network</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  device fo packet sniffering using the</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  Net::Pcap::lookupdev method</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">my $dev = $ARGV[0];</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">unless (defined $dev) {</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    my @devs = Net::Pcap::findalldevs(\$err);</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    if (defined $err) { die 'Unable to determine network device for monitoring - ', $err; }</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    if (length(@devs) == 2) { $dev = $devs[1]; }</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    else { $dev = $devs[0]; }</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    }</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  Look up network address information about network</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  device using Net::Pcap::lookupnet - This also acts as a</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  check on bogus network device argument that may be</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  passed to the progeam as an argument</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">my ($adress, $netmask);</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">if (Net::Pcap::lookupnet($dev, \$address, \$netmask, \$err)) {</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    die 'Unable to look up device information for ', $dev, ' - ', $err;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    }</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  Create packet capture object on device</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">my $object;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">$object = Net::Pcap::open_live($dev, 1500, 0, 0, \$err);</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">unless (defined $object) {</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    die 'Unable to create packet capture on device ', $dev, ' - ', $err;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    }</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  Compile and set packet filter for packet capture</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  object - For the capture of TCP packes with the SYN</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  header falg set directed at the external interface of</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  the local host, the packet filter of '(dst ip) &amp;&amp; (tcp[13] &amp; 2 != 0)'</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  is used where IP is the IP address of</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  the external interface of the machine,  For</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  illustrative purposes, the IP address of 127.0.0.1 is used in thsis example.</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">my $filter;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Net::Pcap::compile(</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    $object,</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    \$filter,</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    '(dst 127.0.0.1) &amp;&amp; (tcp[13] &amp; 2 != 0)',</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    0,</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    $netmask</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">) &amp;&amp; die "Unable to compile packet capture filter";</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Net::Pcap::setfilter($object, $filter) &amp;&amp;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    die 'Unable to set packet capture filter';</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#  Set callback function and initialate packet capture loop</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Net::Pcap::loop($object, -1, \&amp;syn_packets, '') ||</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    die 'Unable to perform packet capture';</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">Net::Pcap::close($object);</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">sub syn_packets {</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    my ($user_data, $header, $packet) = @_;</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    #  Strip ethernet encapsulation of captures packet</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    my $ether_data = NetPacket::Ethernet::strip($packet);</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    #  Decode contents of TCP/IP packet contained within</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    #  captured ethernet packet</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    my $ip = NetPacket::IP-&gt;decode($ether_data);</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    my $tcp = NetPacket::TCP-&gt;decode($ip-&gt;{'data'});</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    #  Print all out where its coming from and where its going to!</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    print</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">        $ip-&gt;{'src_ip'}, ":", $tcp-&gt;{'src_port'}, " -&gt; ",</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">        $ip-&gt;{'dest_ip'}, ":", $tcp-&gt;{'dest_port'}, "\n";</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="padding: 0pt; background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 0pt; margin-top: 0pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    }</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';"> </span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="border: medium none ; padding: 1pt 4pt; margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
										<br />
								</o:p>
						</span>
				</p>
		</div>
<img src ="http://www.cnitblog.com/gyn/aggbug/47179.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-07-27 20:49 <a href="http://www.cnitblog.com/gyn/archive/2008/07/27/47179.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>检测系统可用磁盘</title><link>http://www.cnitblog.com/gyn/archive/2008/06/14/45755.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sat, 14 Jun 2008 14:37:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/06/14/45755.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/45755.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/06/14/45755.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/45755.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/45755.html</trackback:ping><description><![CDATA[
		<div class="Section0" style="">
				<p class="p0" style="margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<font face="黑体">只有一行代码。如下：</font>
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="margin-bottom: 0pt; margin-top: 0pt;">
						<span style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; font-size: 10.5pt; font-family: 'Courier New'; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">for ('a'..'z') { chdir "$_:" &amp;&amp; print "$_:\n"}</span>
						<span style="font-size: 10.5pt; font-family: 'Times New Roman';">
								<o:p>
								</o:p>
						</span>
				</p>
		</div>
<img src ="http://www.cnitblog.com/gyn/aggbug/45755.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-06-14 22:37 <a href="http://www.cnitblog.com/gyn/archive/2008/06/14/45755.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>IKC初探</title><link>http://www.cnitblog.com/gyn/archive/2008/05/31/44659.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sat, 31 May 2008 09:02:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/05/31/44659.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/44659.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/05/31/44659.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/44659.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/44659.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: What is IKC not KFC																																										Component的存在使得我们可以将大多数的精力放在业务逻辑上，但我认为这样同时也减低了程序员对代码的信任度。因为对于像IKC这样一个高级的应用，我们很难确切地理解其中的原理。很多时候我们不得不根据文档亦步亦趋，但始终还是存在那么一些难以定位的逻辑...&nbsp;&nbsp;<a href='http://www.cnitblog.com/gyn/archive/2008/05/31/44659.html'>阅读全文</a><img src ="http://www.cnitblog.com/gyn/aggbug/44659.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-05-31 17:02 <a href="http://www.cnitblog.com/gyn/archive/2008/05/31/44659.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>回声服务器的轮子代码解析</title><link>http://www.cnitblog.com/gyn/archive/2008/05/29/44519.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Thu, 29 May 2008 00:29:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/05/29/44519.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/44519.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/05/29/44519.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/44519.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/44519.html</trackback:ping><description><![CDATA[
		<div class="Section0" style="">
				<p class="p0" style="margin-bottom: 5pt; margin-top: 5pt;">
						<span style="font-size: 18pt; font-family: 'Courier New';">Tell me why</span>
						<span style="font-size: 18pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<font face="黑体">为什么要写这个有关轮子代码的解析呢？如果是用 IO::Socket配合select_read等写的代码，一来需要自己处理客户socket的连接，二来还要整理传入的数据，如此这般使得代码量变得 很巨大，对其解析可能会需要很大的工作量，事实上我也大致写过很长的一个。而且这种具有普遍意义的代码任放在哪个平台上其中的内容大多是不会发生变化的， 因而很多地方都可整合起来。在另一个极端上来说，如果使用像POE::Component::Server::TCP的组件，虽然可以将代码量控制在一个 很小的范围，但是同时也隐藏了绝大多数的细节，对于刚刚接触POE的初学者，这无异于一个神奇的盒子。所以要用夹在中间的轮子代码，先有一个大致的概念， 之后再去深究其中的细节。</font>
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 18pt; font-family: 'Courier New';">
								<font face="黑体">一拖n的结构</font>
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<font face="黑体">还是老样子，先把代码的大致结构讲一下。和大多数底层的POE程序差不多，一个session拖一屁股事件句柄。</font>
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 5pt; margin-top: 5pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">POE :: Session -&gt; create(</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    inline_states  =&gt;  {</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">        _start  =&gt;   \&amp; server_start ,</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">        server_accepted  =&gt;   \&amp; server_accepted ,</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">        client_input  =&gt;   \&amp; client_input ,</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">        client_error  =&gt;   \&amp; client_error ,</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">        server_error  =&gt;   \&amp; server_error ,</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">});</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="margin-bottom: 5pt; margin-top: 5pt;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">_start <font face="黑体">事件建立了一个服务socket，再用ListenAccept轮子监听它。当发现有客户socket连接时，触发了server_accept事件。在 该事件中，我们使用ReadWrite轮子来监视客户输入，并将其返回给客户。如果发生意外，根据事故的制造者，分别交予client_error或者 server_error处理。其中server_error将销毁ListenAccept轮子，从而停止整个程序。</font></span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<font face="黑体">所以，不一样关键在于有了ListenAccept和ReadWrite两个轮子。前者用来监听客户socket连接，后者用来处理传入的数据并将它发送回去。这在很大程度上节约了代码量，使我们可以集中更多的注意力在业务逻辑上，而又不至于对于其中的具体实现一头雾水。</font>
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 18pt; font-family: 'Courier New';">
								<font face="黑体">记忆点</font>
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<font face="黑体">对 于这两个轮子，需要牢记的有两点，首先是参数。ListenAccept需要至少三个，分别是Handle、AcceptEvent和 ErrorEvent。Handle是监视对象，AcceptEvent是接受连接时触发的事件，到处可见的ErrorEvent则用来处理错误。这三个 缺一不可，不然运行会发生错误，至少需要写一个空句柄。ReadWrite也差不多，只是InputEevnt代替了Listenaccept中的 AcceptEvent，用来处理客户输入。</font>
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<font face="黑体">第二点要注意的是ARG参数。可以这么认为，ARG0是与轮子具体功能相关的, ARG1则代表了轮子本身的ID。打个比方，因为ListenAccept是用来监听并接受客户连接的，那么ARG0就是被接受了的客户。由于对于轮子的 使用经常是跨事件的，所以必须保留对于轮子的引用。ID因其唯一性，所以可以在哈希表中作为键值以保存对轮子的引用。</font>
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 18pt; font-family: 'Courier New';">
								<font face="黑体">测试</font>
						</span>
						<span style="background: rgb(238, 238, 238) none repeat scroll 0% 0%; font-size: 9.5pt; font-family: 'Courier New'; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="background: rgb(217, 217, 217) none repeat scroll 0% 0%; margin-bottom: 5pt; margin-top: 5pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">#!/usr/bin/perl</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">use warnings;</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">use strict;</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">use POSIX;</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">use IO :: Socket;</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">my $sock = IO::Socket::INET-&gt;new(</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    PeerHost =&gt; '127.0.0.1',</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    PeerPort =&gt; 12345,</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">    ) or die "can't connect to server: $@\n";</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">my $word = "hello.\nmy name is Jinhao.\nwlecome to POE.</span>
						<span style="font-size: 10.5pt; font-family: '黑体';">a</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">\n";</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">$sock-&gt;send($word);</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">$buffer = '';</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">$sock-&gt;recv($buffer, POSIX::BUFSIZ);</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">print $buffer unless $buffer eq '';</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">$sock-&gt;shutdown(0);</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="margin-bottom: 5pt; margin-top: 5pt;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<font face="黑体">客 户端还是在本地，应用了一个IO::Socket来发送一段带“\n”的数据，之后接受返回并打印结果。可以印证一点，ReadWrite轮子只有在发现 换行才起作用，也就是说如果发送的数据的最后不是以“\n”结尾的，那么打印结果会少一段。至于为什么没有发现数据结尾，我想可能跟操作系统有关系。</font>
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<br />
						</span>
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
								</o:p>
						</span>
				</p>
				<p class="p0" style="margin-bottom: 0pt; margin-top: 0pt;">
						<span style="font-size: 10.5pt; font-family: 'Courier New';">
								<o:p>
										<br />
								</o:p>
						</span>
				</p>
		</div>
<img src ="http://www.cnitblog.com/gyn/aggbug/44519.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-05-29 08:29 <a href="http://www.cnitblog.com/gyn/archive/2008/05/29/44519.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>对于poe cookbook中Tk interface一例的解析</title><link>http://www.cnitblog.com/gyn/archive/2008/05/26/44366.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Mon, 26 May 2008 12:01:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/05/26/44366.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/44366.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/05/26/44366.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/44366.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/44366.html</trackback:ping><description><![CDATA[
		<h3 style="TEXT-ALIGN: center" align="center">
				<span style="FONT-SIZE: 15pt; LINE-HEIGHT: 173%; FONT-FAMILY: 黑体">起因<span lang="EN-US"><?xml:namespace prefix = o /?><o:p></o:p></span></span>
		</h3>
		<p class="MsoNormal" style="TEXT-INDENT: 17.95pt">
				<span lang="EN-US" style="FONT-FAMILY: 黑体">Perl-Tk</span>
				<span style="FONT-FAMILY: 黑体">在<span lang="EN-US">windows</span>下的表现着实让我头疼了很长一段时间，最无法忍受的一点是缺乏稳定的线程支持。比如，无法在一个界面上一边录制新闻，一边完成下一个录制时间的设定，整个程序被阻塞在那儿无法动弹。某些情况下可以使用非阻塞的本地<span lang="EN-US">socket</span>来实现进程间的通信，但这么做很费劲，而且使得程序像一个奇怪的网络服务器。<span lang="EN-US">POE</span>作为在单进程中实现并行的框架，提供了对<span lang="EN-US">Tk</span>的支持，很好地解决了组件间通信的问题。<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<h3 style="TEXT-ALIGN: center" align="center">
				<span style="FONT-SIZE: 15pt; LINE-HEIGHT: 173%; FONT-FAMILY: 黑体">大致的介绍<span lang="EN-US"><o:p></o:p></span></span>
		</h3>
		<p class="MsoNormal" style="TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 黑体">
						<span lang="EN-US">
						</span>
						<span lang="EN-US">
								<a title="" href="#_ftn1" name="_ftnref1">
										<span class="MsoFootnoteReference">
												<span>
														<span class="MsoFootnoteReference">
																<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: 黑体">
																</span>
														</span>
												</span>
										</span>
								</a>
						</span>
						<span lang="EN-US">
								<o:p>
								</o:p>
						</span>
				</span>
		</p>
		<p class="MsoNormal" style="TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 黑体">
				</span>
		</p>
		<div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="COLOR: rgb(0,128,0)">#</span>
				<span style="COLOR: rgb(0,128,0)">!/usr/bin/perl</span>
				<span style="COLOR: rgb(0,128,0)">
						<br />
				</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />
				</span>
				<span style="COLOR: rgb(0,0,255)">use</span>
				<span style="COLOR: rgb(0,0,0)"> warnings;<br /></span>
				<span style="COLOR: rgb(0,0,255)">use</span>
				<span style="COLOR: rgb(0,0,0)"> strict;<br /><br /></span>
				<span style="COLOR: rgb(0,0,255)">use</span>
				<span style="COLOR: rgb(0,0,0)"> Tk;<br /></span>
				<span style="COLOR: rgb(0,0,255)">use</span>
				<span style="COLOR: rgb(0,0,0)"> POE;<br /><br />POE</span>
				<span style="COLOR: rgb(0,0,0)">::</span>
				<span style="COLOR: rgb(0,0,0)">Session</span>
				<span style="COLOR: rgb(0,0,0)">-&gt;</span>
				<span style="COLOR: rgb(0,0,0)">create(<br />    inline_states </span>
				<span style="COLOR: rgb(0,0,0)">=&gt;</span>
				<span style="COLOR: rgb(0,0,0)"> {<br />        _start </span>
				<span style="COLOR: rgb(0,0,0)">=&gt;</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="COLOR: rgb(0,0,0)">\&amp;</span>
				<span style="COLOR: rgb(0,0,0)">ui_start</span>
				<span style="COLOR: rgb(0,0,0)">,</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />        ev_count </span>
				<span style="COLOR: rgb(0,0,0)">=&gt;</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="COLOR: rgb(0,0,0)">\&amp;</span>
				<span style="COLOR: rgb(0,0,0)">ui_count</span>
				<span style="COLOR: rgb(0,0,0)">,</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />        ev_clear </span>
				<span style="COLOR: rgb(0,0,0)">=&gt;</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="COLOR: rgb(0,0,0)">\&amp;</span>
				<span style="COLOR: rgb(0,0,0)">ui_clear</span>
				<span style="COLOR: rgb(0,0,0)">,</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />    }<br />);<br /><br /></span>
				<span style="COLOR: rgb(128,0,128)">$poe_kernel</span>
				<span style="COLOR: rgb(0,0,0)">-&gt;</span>
				<span style="COLOR: rgb(0,0,0)">run();<br /></span>
				<span style="COLOR: rgb(0,0,255)">exit</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="COLOR: rgb(128,0,0)">0</span>
				<span style="COLOR: rgb(0,0,0)">;<br /><br /></span>
				<span style="COLOR: rgb(0,0,255)">sub</span>
				<span style="COLOR: rgb(0,0,0)"> ui_start {<br />    </span>
				<span style="COLOR: rgb(0,0,255)">my</span>
				<span style="COLOR: rgb(0,0,0)"> ( </span>
				<span style="COLOR: rgb(128,0,128)">$kernel</span>
				<span style="COLOR: rgb(0,0,0)">,</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="COLOR: rgb(128,0,128)">$session</span>
				<span style="COLOR: rgb(0,0,0)">,</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="COLOR: rgb(128,0,128)">$heap</span>
				<span style="COLOR: rgb(0,0,0)"> ) </span>
				<span style="COLOR: rgb(0,0,0)">=</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="COLOR: rgb(128,0,128)">@_</span>
				<span style="COLOR: rgb(0,0,0)">[ KERNEL</span>
				<span style="COLOR: rgb(0,0,0)">,</span>
				<span style="COLOR: rgb(0,0,0)"> SESSION</span>
				<span style="COLOR: rgb(0,0,0)">,</span>
				<span style="COLOR: rgb(0,0,0)"> HEAP ];<br />    <br />    </span>
				<span style="COLOR: rgb(128,0,128)">$poe_main_window</span>
				<span style="COLOR: rgb(0,0,0)">-&gt;</span>
				<span style="COLOR: rgb(0,0,0)">Label( </span>
				<span style="COLOR: rgb(0,0,0)">-</span>
				<span style="COLOR: rgb(0,0,0)">text </span>
				<span style="COLOR: rgb(0,0,0)">=&gt;</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">"</span>
				<span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">Counter</span>
				<span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">"</span>
				<span style="COLOR: rgb(0,0,0)"> )</span>
				<span style="COLOR: rgb(0,0,0)">-&gt;</span>
				<span style="COLOR: rgb(0,0,255)">pack</span>
				<span style="COLOR: rgb(0,0,0)">;<br />    </span>
				<span style="COLOR: rgb(128,0,128)">$poe_main_window</span>
				<span style="COLOR: rgb(0,0,0)">-&gt;</span>
				<span style="COLOR: rgb(0,0,0)">Label( </span>
				<span style="COLOR: rgb(0,0,0)">-</span>
				<span style="COLOR: rgb(0,0,0)">textvariable </span>
				<span style="COLOR: rgb(0,0,0)">=&gt;</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="COLOR: rgb(0,0,0)">\</span>
				<span style="COLOR: rgb(128,0,128)">$heap</span>
				<span style="COLOR: rgb(0,0,0)">-&gt;</span>
				<span style="COLOR: rgb(0,0,0)">{count} )</span>
				<span style="COLOR: rgb(0,0,0)">-&gt;</span>
				<span style="COLOR: rgb(0,0,255)">pack</span>
				<span style="COLOR: rgb(0,0,0)">;<br />    <br />    </span>
				<span style="COLOR: rgb(128,0,128)">$poe_main_window</span>
				<span style="COLOR: rgb(0,0,0)">-&gt;</span>
				<span style="COLOR: rgb(0,0,0)">Button(<br />        </span>
				<span style="COLOR: rgb(0,0,0)">-</span>
				<span style="COLOR: rgb(0,0,0)">text </span>
				<span style="COLOR: rgb(0,0,0)">=&gt;</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">"</span>
				<span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">Clear</span>
				<span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">"</span>
				<span style="COLOR: rgb(0,0,0)">,</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />        </span>
				<span style="COLOR: rgb(0,0,0)">-</span>
				<span style="COLOR: rgb(0,0,0)">command </span>
				<span style="COLOR: rgb(0,0,0)">=&gt;</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="COLOR: rgb(128,0,128)">$session</span>
				<span style="COLOR: rgb(0,0,0)">-&gt;</span>
				<span style="COLOR: rgb(0,0,0)">postback(</span>
				<span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">"</span>
				<span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">ev_clear</span>
				<span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">"</span>
				<span style="COLOR: rgb(0,0,0)">)</span>
				<span style="COLOR: rgb(0,0,0)">,</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />    )</span>
				<span style="COLOR: rgb(0,0,0)">-&gt;</span>
				<span style="COLOR: rgb(0,0,255)">pack</span>
				<span style="COLOR: rgb(0,0,0)">;<br />    <br />    </span>
				<span style="COLOR: rgb(128,0,128)">$kernel</span>
				<span style="COLOR: rgb(0,0,0)">-&gt;</span>
				<span style="COLOR: rgb(0,0,0)">yield(</span>
				<span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">"</span>
				<span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">ev_count</span>
				<span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">"</span>
				<span style="COLOR: rgb(0,0,0)">);<br />}<br /><br /></span>
				<span style="COLOR: rgb(0,0,255)">sub</span>
				<span style="COLOR: rgb(0,0,0)"> ui_count {<br />    </span>
				<span style="COLOR: rgb(128,0,128)">$_</span>
				<span style="COLOR: rgb(0,0,0)">[HEAP]</span>
				<span style="COLOR: rgb(0,0,0)">-&gt;</span>
				<span style="COLOR: rgb(0,0,0)">{count}</span>
				<span style="COLOR: rgb(0,0,0)">++</span>
				<span style="COLOR: rgb(0,0,0)">;<br />        </span>
				<span style="COLOR: rgb(128,0,128)">$_</span>
				<span style="COLOR: rgb(0,0,0)">[KERNEL]</span>
				<span style="COLOR: rgb(0,0,0)">-&gt;</span>
				<span style="COLOR: rgb(0,0,0)">delay( </span>
				<span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">"</span>
				<span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">ev_count</span>
				<span style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">"</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="COLOR: rgb(0,0,0)">=&gt;</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="COLOR: rgb(0,0,0)">.</span>
				<span style="COLOR: rgb(128,0,0)">5</span>
				<span style="COLOR: rgb(0,0,0)">);<br /><br />    </span>
				<span style="COLOR: rgb(0,128,0)">#</span>
				<span style="COLOR: rgb(0,128,0)"> $_[KERNEL]-&gt;yield("ev_count");</span>
				<span style="COLOR: rgb(0,128,0)">
						<br />
				</span>
				<span style="COLOR: rgb(0,0,0)">}<br /><br /></span>
				<span style="COLOR: rgb(0,0,255)">sub</span>
				<span style="COLOR: rgb(0,0,0)"> ui_clear {<br />    </span>
				<span style="COLOR: rgb(128,0,128)">$_</span>
				<span style="COLOR: rgb(0,0,0)">[HEAP]</span>
				<span style="COLOR: rgb(0,0,0)">-&gt;</span>
				<span style="COLOR: rgb(0,0,0)">{count} </span>
				<span style="COLOR: rgb(0,0,0)">=</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="COLOR: rgb(128,0,0)">0</span>
				<span style="COLOR: rgb(0,0,0)">;<br />}<br /></span>
		</div>
		<br />
		<p>
		</p>
		<p class="MsoNormal" style="TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 黑体">很容易可以看出来一个不一样的地方，比如说用<span lang="EN-US">$poe_kernel</span>代替了一贯的<span lang="EN-US">POE::Kernel</span>，但在实际运行中会发现<span lang="EN-US">$poe_kernel</span>是对<span lang="EN-US">POE:;Kernel</span>的引用，最终起作用的依然是<span lang="EN-US">POE::Kernel</span>。好了，除了这一点外，其他还是与传统的<span lang="EN-US">POE</span>程序差不多。<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<p class="MsoNormal" style="TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 黑体">首先建立了一个包含了内置<span lang="EN-US">_start</span>和两个自定义（<span lang="EN-US">ev_count</span>，<span lang="EN-US">ev_clear</span>）事件的<span lang="EN-US">session</span>。之后在<span lang="EN-US">_start</span>事件中，在已初始化了的<span lang="EN-US">$poe_main_window</span>上建立一个内容为“<span lang="EN-US">Counter</span>”的标签，内容为变量<span lang="EN-US">$_[HEAP]-&gt;{count}</span>的标签和一个用来清零的按钮。至于<span lang="EN-US">ev_count</span>和<span lang="EN-US">ev_clear</span>事件不过是自增和清零<span lang="EN-US">$_[HEAP]-&gt;{count}</span>而已，其中<span lang="EN-US">ev_count</span>事件会递归回调自身以实现不断自增的过程。<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<p class="MsoNormal" style="TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 黑体">如果一切顺利，运行之后的结果如下图所示：</span>
		</p>
		<p class="MsoNormal" style="TEXT-INDENT: 17.95pt; TEXT-ALIGN: center" align="center">
				<span lang="EN-US" style="FONT-FAMILY: 黑体">
						<?xml:namespace prefix = v /?>
						<v:shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600">
								<v:stroke joinstyle="miter">
								</v:stroke>
								<v:formulas>
										<v:f eqn="if lineDrawn pixelLineWidth 0">
										</v:f>
										<v:f eqn="sum @0 1 0">
										</v:f>
										<v:f eqn="sum 0 0 @1">
										</v:f>
										<v:f eqn="prod @2 1 2">
										</v:f>
										<v:f eqn="prod @3 21600 pixelWidth">
										</v:f>
										<v:f eqn="prod @3 21600 pixelHeight">
										</v:f>
										<v:f eqn="sum @0 0 1">
										</v:f>
										<v:f eqn="prod @6 1 2">
										</v:f>
										<v:f eqn="prod @7 21600 pixelWidth">
										</v:f>
										<v:f eqn="sum @8 21600 0">
										</v:f>
										<v:f eqn="prod @7 21600 pixelHeight">
										</v:f>
										<v:f eqn="sum @10 21600 0">
										</v:f>
								</v:formulas>
								<v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f">
								</v:path>
								<o:lock aspectratio="t" v:ext="edit">
								</o:lock>
						</v:shapetype>
						<v:shape id="_x0000_i1025" style="WIDTH: 90.75pt; HEIGHT: 69.75pt" type="#_x0000_t75">
								<v:imagedata o:title="" src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image001.png">
								</v:imagedata>
						</v:shape>
						<img height="93" alt="poe_tk.JPG" src="http://www.cnitblog.com/images/cnitblog_com/gyn/poe_tk.JPG" width="121" border="0" /> <br /><o:p></o:p></span>
		</p>
		<p class="MsoNormal" style="TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 黑体">单击<span lang="EN-US">Clear</span>按钮，中间的数字将从零开始继续飞速自增。如果想让它自增得慢一点，或者说可以看得清，可以在<span lang="EN-US">ev_count</span>事件中<span lang="EN-US">delay</span>自身回调一小段时间。需要注意的一点是不要指望用<span lang="EN-US">sleep</span>，因为它会让整个<span lang="EN-US">POE</span>崩溃。</span>
		</p>
		<h3 style="TEXT-ALIGN: center" align="center">
				<span style="FONT-SIZE: 15pt; LINE-HEIGHT: 173%; FONT-FAMILY: 黑体">一些细节<span lang="EN-US"><o:p></o:p></span></span>
		</h3>
		<p class="MsoNormal" style="TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 黑体">下面来讲一下隐藏在这一切背后的一些小细节。事实上，这些细节危险重重，任何细小的错误哪怕是位置的改变都可能导致程序的失败，而原文中的某些注释也比较晦涩或者是存在误导，需要详细说明一下。<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<p class="MsoNormal" style="TEXT-INDENT: 17.95pt">
				<span lang="EN-US" style="FONT-FAMILY: 黑体">$poe_main_window</span>
				<span style="FONT-FAMILY: 黑体">在<span lang="EN-US">strict</span>环境中出现，这让我很吃惊。一般情况下，这样的非<span lang="EN-US">my</span>变量是无法通过检测的。唯一的解释是<span lang="EN-US">use POE</span>在背后干了很多事情，多得连<span lang="EN-US">POE::Kernel</span>都增加了叫一个<span lang="EN-US">$poe_kernel</span>的引用，当然这是为了保证<span lang="EN-US">POE::Kernel</span>不被自动垃圾回收。在这里不得不说的是，<span lang="EN-US">use Tk</span>必须出现在<span lang="EN-US">use POE</span>之前，否则<span lang="EN-US">use POE</span>不会初始化生成<span lang="EN-US">$poe_main_window</span>。所以可以这么认为，<span lang="EN-US">use POE</span>会通过检测其之前的模块调用来决定之后的框架行为，这点在其他模块的应用中不是很多见，至少在我是第一次看见。<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<p class="MsoNormal" style="TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 黑体">原文中有这么一句话“<span lang="EN-US">Widgets we need to work with later, such as the counter display,must be stored somewhere.The heap is a convenient place for them.</span>”意思是说“要在之后用到的组件，比如说显示自增计数的标签，必须存储在某个地方，而堆栈就是一个方便的场所。”这也让我感到很困惑，至少在这个例子中，之后没有显式地用到这个计数显示标签。如果非要说用到，那么就是<span lang="EN-US">ev_count</span>事件中的<span lang="EN-US">$_[HEAP]-&gt;{counter}</span>以<span lang="EN-US">textvariable</span>的身份影响到其宿主，但事实上这非常隐晦，而且为此保存一个引用似乎也毫无意义。后来的实验证明了我的猜测，在注释掉这个引用之后程序依然可以正常运行，也就是说原文这句话并非针对于本例子或者说毫不相干，其中的“<span lang="EN-US">such as</span>”极易引人误入歧途。它真正想说的是，为了可以在某个事件中改变组件的行为，需要一个全局可见的引用，而<span lang="EN-US">$_[HEAP]</span>正是恰当的选择，其实用一个全局哈希表就可以得到相同的效果，只是这么多就显得多余了。<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<p class="MsoNormal" style="TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 黑体">最后的一点是有关于<span lang="EN-US">POE</span>本身的机制。在<span lang="EN-US">ev_count</span>中为了实现不断自增，在其事件处理句柄中有一句自身回调，而在<span lang="EN-US">ev_clear</span>中仅仅是将计数清零了而已。一开始我有些担心自增行为会中断，因此在<span lang="EN-US">ev_clear</span>中增加了一句触发<span lang="EN-US">ev_count</span>事件的命令，但这么做是多余的。首先<span lang="EN-US">POE</span>中的每一个事件句柄都是并行的，因此<span lang="EN-US">ev_clear</span>不会中断<span lang="EN-US">ev_count</span>的行为；第二，作为两个事件之间关联的全局变量<span lang="EN-US">$_[HEAP]-&gt;{counter},POE</span>为其提供了锁保护，不必担心其中一个事件的行为导致另外一个崩溃。<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<h3 style="TEXT-ALIGN: center" align="center">
				<span style="FONT-SIZE: 15pt; LINE-HEIGHT: 173%; FONT-FAMILY: 黑体">几句话<span lang="EN-US"><o:p></o:p></span></span>
		</h3>
		<p class="MsoNormal" style="TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 黑体">因为不是正儿八经地写论文，所以遣词造句就显得随性了一些。倒不是说我可以不认真了，相反地，对于一些细节倒是表现得更自然。<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<p class="MsoNormal" style="TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 黑体">这个例子非常简单，简单得几乎没有实用价值，但是说明了问题。我认为一个好的<span lang="EN-US">tutorial</span>就应该这样，简单而能说明问题。简单就是说容易让人看懂，说明问题则是把注意事项讲清楚了，之后全凭各人兴趣和悟性了。<span lang="EN-US">POE Cookbook</span>是一本非官方的技术文集，没有出版物，甚至说如果你写了一个好的例子，也可以被收录其中。但是每个例子都很通俗易懂，从最简单的入手，由浅入深，最后形成一个系统。反观国内的一些技术出版物，往往枯燥理论先讲一大堆，吓跑一批人；再举一两个可以堪称作者代表作的复杂例子，又闷掉一批人，结果是弄得是曲高和寡。<span lang="EN-US"><o:p></o:p></span></span>
		</p>
<img src ="http://www.cnitblog.com/gyn/aggbug/44366.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-05-26 20:01 <a href="http://www.cnitblog.com/gyn/archive/2008/05/26/44366.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>POE状态机入门与进阶</title><link>http://www.cnitblog.com/gyn/archive/2008/05/09/43537.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Fri, 09 May 2008 03:20:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/05/09/43537.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/43537.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/05/09/43537.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/43537.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/43537.html</trackback:ping><description><![CDATA[一．前言<br />至于POE的应用，我不想多说什么，因为需要使用状态机的地方太多。举一个极端的例子，windows下的perl-tk对于多线程的支持极不稳定，如果在其中加入一个大数据量的处理应用，结果往往会是一个无法动弹的程序。这时除了使用POE，也许没有更好的解决办法了。<br />另外，python中有叫twisted的类似框架，被广泛地应用在网络服务中，具体的使用方法可以参考Oreilly出版的《Python Twisted Network Programming Essentials》。某种意义上，也说明了状态机的重要性。<br /><br />二．基本原理与概念<br /><br />在详细说明POE服务器的建立步骤之前，需要对POE的原理做一个大致的了解。在这个阶段中，我们将描述这方面的不同概念，以及它们在POE中的应用。<br /><br />1. 事件与事件句柄<br />POE 是一个为网络工作和并行任务服务的事件驱动框架。首先作为前提，需要掌握事件和事件驱动编程的意义。<br />在抽象意义上，事件就是真实世界中发生的一件事情。比如说：早晨打铃、面包从烤机里弹出、茶煮好了等。而在用户界面上最常见的事件则是鼠标移动、按钮点击和键盘敲打等等。<br />具体到程序软件事件，则往往是一些抽象的事件。也就是说，它不仅包括了发送给程序的外部活动，而且也包括了一些在操作系统内部运行的事件。比如说，计时器到点了，socket建立了连接，下载完成等。<br />在事件驱动程序中，中心分配器的作用是将事件分配给不同的处理程序。这些处理程序就是事件句柄，顾名思义，它们的任务就是处理相应事件。<br />POE的事件句柄之间的关系是合作性质的。没有两个句柄会同时运行，每一个句柄在被激发运行期间将独占程序。通过尽可能快地返回来保证程序的其它部分得以顺畅运行，这就是事件句柄之间的合作方式。<br /><br />2. POE程序的组成部分<br />最简单的POE程序包括两个模块和一些用户代码：它们分别是POE::Kernel，POE::Session以及一些事件句柄。<br /><br />a. POE::Kernel：<br />POE::Kernel提供了基于事件的操作系统核心服务。包括I/O事件、警报和其它计时事件、信号事件和一些不被人意识到的事件。POE::Kernel提供不同的方法对这些事件进行设置，比如select_read(), delay()和sig()。<br />POE::Kernel还能够跟踪事件发生源和与之相关的任务之间的关系。之所以能够这么做，是因为当事件发生时，它将跟踪哪个任务被激活了。于是它便知道了哪个任务调用方法来使用了这些资源，而这些都是自动完成的。<br />POE::Kernel也知道何事需将任务销毁。它检测任务以确定是否还有事件需要处理，或者是哪个事需要释放占用的资源。当任务没有事件可以触发的时候，POE::Kernel就自动销毁该资源。<br />POE::Kernel会在最后一个session停止以后终止运行。<br /><br />b. POE::Session：<br />POE::Session实例就是上面所讲的由POE::Kernel管理的“任务”。（以下的章节中为了便于识别将使用“session”）<br />每一个session都有一个自己私有的存储空间，叫“heap”。存储在当前session的heap中的数据很难被一个外部session得到。<br />每一个session还拥有自己的资源和事件句柄。这些资源为拥有它们的session生成事件，而事件只被指派到其所处的session中。举例说明，有多个session都可以设置相同的警报，并且任何一个都能接受其所请求的计时事件。但所有其他session不会在意发生在它们之外的事情。<br /><br />c. 事件句柄：<br />事件句柄就是Perl程序。它们因为使用了POE::Kernel传递的参数而不同于一般的perl程序。<br />POE::Kernel是通过@_来传递参数。该数组的前七个成员定义了发生该事件的session的上下文。它包括了一个指向POE::Kernel运行实例的引用、事件自身的名字、指向私有heap的引用以及指向发出事件的session的引用。<br />@_中剩下的成员属于事件自身，其中的具体内容依照被指派的事件类型而定。举例说明：对于I/O事件，包括两个参数：一个是缓冲文件句柄，另一个是用来说明采取何种行为（input、output或者异常）的标记。<br />POE不强求程序员为每一个事件句柄分配所有的参数，要不然这将变成一件非常烦人的工作，因为它们中的一些参数是不常被用到的。而POE::Session会自动为@_输出剩余的常量，这样就能使我们相对比较轻松地将注意力放在重要的参数上，而让POE来处理不必需的参数。<br />POE还允许改变参数的顺序和数量，而不会对程序造成影响。比如说，KERNEL，HEAP和ARG0分别是POE::Kernel实例、当前session的堆栈和事件的第一个用户参数。它们可以一个个直接从@_被导出。<br />my $kernel = $_[KERNEL];<br />my $heap = $_[HEAP];<br />my $thingy = $_[ARG0];<br />或者一次性以队列片段的形式赋值给程序参数。<br />my ( $kernel, $heap, $thingy ) = @_[KERNEL, HEAP, ARG0];<br />当然在事件句柄中我们也可以直接使用$_[KERNEL]，$_[HEAP]和$_[AG0]。但是因为诸如ARG0的参数很难从字面上知道它在事件中代表的真实意义, 所以我们不提倡直接使用这种做法。<br /><br />三．简单的POE例子<br />现在大致知道了POE编程的概念，我们将举若干例子来了解它到底是怎么运行的。<br /><br />1. 一个单session的例子<br />简单的POE程序包括三个部分：一个用来加载模块和配置条件的前端，初始化并且运行一个或者多个session的主体和用来描述事件句柄的具体程序。<br /><br />a. 前端：<br />#!/usr/bin/perl<br /><br />use warnings;<br />use strict;<br />use POE;<br />引入POE模块的过程的背后隐藏了一些细节，事实上这么做还加载了一些诸如POE::Kernel和POE:Sesson的模块并相应地做了一些初始化，而通常在每个POE程序中我们都会用到这些隐藏模块。<br />在POE::Kernel第一次被引入时，它生成了一个将贯穿整个程序POE::Kernel实例。POE::Session会根据不同的事件输出默认常量给事件句柄的某些参数，如：KERNEL，HEAP，ARG0等等。<br />所以一个简单的use POE为程序做了大量的初始化工作。<br /><br />b. 主体session：<br />当所有的条件都准备好之后，为了保证POE::Kernel的有效运行，我们必须建立至少一个session。不然的话，运行程序意味着无事可做。<br />在这个例子里，我们将建立一个包含_start，_stop和count这三个事件的任务。POE::Session将每个事件与一个句柄联系在一起。<br />POE::Session-&gt;create{<br />    Inline_states =&gt; {<br />        _start =&gt; \&amp;session_start,<br />        _stop =&gt; \&amp;session_stop,<br />        count =&gt; \&amp;session_count,<br />    }<br />};<br />前两个事件是由POE::Kernel自身所提供的。它们分别表示该session的启动和销毁。最后一个事件是用户自定义事件，它被用于程序的逻辑之中。<br />我们之所以没有为session保留一个引用的原因是因为该session会自动被注册到POE::Kernel中，并接收它的管理，而我们在程序是很少直接使用该session的。<br />事实上，保存一个session的应用是存在危险的。因为如果存在显式的引用，Perl将不会自动销毁session对象或者重新为其分配内存。<br />接着我们启动POE::Kernel，由此便建立了一个用来探测并分派事件的主循环。在此示例程序中，为了使运行结果更加明确，我们将注明POE::Kernel运行的开始处和结束点。<br />print “Starting POE::Kernel.\n”;<br />POE::Kernel-&gt;run();<br />print “POE::Kernle’s run method returned.\n”;<br />exit;<br />Kernel的run方法只有在所有session返回之后才会停止循环。之后，我们调用一个表示程序结束的提示符的exit系统方法来表示程序被终止，而在实际的应用中这么做是不必要的。<br /><br />c. 事件句柄：<br />下面我们来了解一下事件句柄的应用，首先从_start开始。_start的句柄将在sesson初始化完成之后开始运行，session在其自身的上下文中使用它来实现输入引导。比如初始化heap中的值，或者分配一些必要的资源等等。<br />在该句柄中我们建立了一个累加器，并且发出了“count”事件以触发相应的事件句柄。<br />sub session_start {<br />    print "Session ", $_[SESSION]-&gt;ID, " has started.\n";<br />    $_[HEAP]-&gt;{count} = 0;<br />    $_[KERNEL]-&gt;yield("count");<br />}<br />一些熟悉线程编程的人可能会对yield方法在这里的使用产生困惑。事实上，它并不是用来中止session运行的，而是将一个事件放入fifo分派队列的末尾处，当队列中在其之前的事件被处理完毕之后，该事件将被触发以运行相应的事件句柄。这个概念在多任务环境下更容易被理解。我们可以通过在调用yield方法之后立即返回的办法，来清晰地体现yield方法的行为。<br />接下来的是_stop句柄。POE::Kernel将在所有session再无事件可触发之后，并且是在自身被销毁之前调用它。<br />sub session_stop {<br />    print "Session ", $_[SESSION]-&gt;ID, " has stopped.\n";<br />}<br />在_stop中设置一个事件是无用的。销毁session的过程本身包括清理与之相关的资源，而事件就是资源的组成部分。所以对于所有在_stop中的事件，在其能够被分派之前都是将被清理的。<br />最后讲一下count事件句柄。该函数用来增加heap中的累加器计数，并打印累加结果。我们可以使用一个while来完成这件工作，但是用yield方法一来可以使得程序更短小精悍，二来还能够加深对POE事件处理原理的理解。<br />sub session_count {<br />    my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];<br />    my $session_id = $_[SESSION]-&gt;ID;<br /><br />    my $count = ++$heap-&gt;{count};<br />    print "Session $session_id has counted to $count.\n";<br /><br />    $kernel-&gt;yield("count") if $count &lt; 10;<br />}<br />该函数的最后一句表示：只要累加器计数未超过10，session将再yield一个count事件。因为不断地触发了session_count句柄，使得当前session可以继续得以生存而不会被POE::Kernel清理。<br />当计数器到10时，便不再调用yield命令，session也将停止。一旦POE::Kernel检测到该session再没有事件句柄可被激发，便在调用_stop事件句柄之后将其清理销毁。<br />以下是运行的结果：<br />  Session 2 has started.<br />  Starting POE::Kernel.<br />  Session 2 has counted to 1.<br />  Session 2 has counted to 2.<br />  Session 2 has counted to 3.<br />  Session 2 has counted to 4.<br />  Session 2 has counted to 5.<br />  Session 2 has counted to 6.<br />  Session 2 has counted to 7.<br />  Session 2 has counted to 8.<br />  Session 2 has counted to 9.<br />  Session 2 has counted to 10.<br />  Session 2 has stopped.<br />  POE::Kernel's run() method returned.<br />对于该结果有几点需要解释一下。<br />?    为什么在运行结果中session的id是2。因为通常情况下，POE::Kernel是最先被创建的，它的id号会是1。接下来创建session的id号依次被累加。<br />?    因为当运行POE::Session-&gt;create时就会分派_start事件，所以_start事件句柄的激发是在POE::Kernel运行之前的。<br />?    第一个count事件句柄并没有被立即处理。这是因为该事件被Kernel放入了分派队列之中。<br />?    导致session停止的原因除了再没有事件可触发而之外，外部的终止信号也可以用来停止session。<br /><br />2．多任务的POE例子<br /><br />可以将以上的这个计数程序做成多任务的形式，使每一个session将在其自身的heap中保存累加器。各个session的事件被依次传送到POE::Kernel的事件队列中，并以先进先出的形式进行处理，以保证这些事件将轮流被执行。<br />为了演示这个结果，我们将复制以上程序中的session部分，其它部分保持原样不变。<br />for ( 1 .. 2 ) {<br />    POE::Session-&gt;create(<br />        inline_states =&gt; {<br />            _start =&gt; \&amp;session_start,<br />            _stop  =&gt; \&amp;session_stop,<br />            count  =&gt; \&amp;session_count,<br />          }<br />    );<br />}<br />以下便是修改后的程序的运行结果：<br />  Session 2 has started.<br />  Session 3 has started.<br />  Starting POE::Kernel.<br />  Session 2 has counted to 1.<br />  Session 3 has counted to 1.<br />  Session 2 has counted to 2.<br />  Session 3 has counted to 2.<br />  Session 2 has counted to 3.<br />  Session 3 has counted to 3.<br />  Session 2 has counted to 4.<br />  Session 3 has counted to 4.<br />  Session 2 has counted to 5.<br />  Session 3 has counted to 5.<br />  Session 2 has counted to 6.<br />  Session 3 has counted to 6.<br />  Session 2 has counted to 7.<br />  Session 3 has counted to 7.<br />  Session 2 has counted to 8.<br />  Session 3 has counted to 8.<br />  Session 2 has counted to 9.<br />  Session 3 has counted to 9.<br />  Session 2 has counted to 10.<br />  Session 2 has stopped.<br />  Session 3 has counted to 10.<br />  Session 3 has stopped.<br />  POE::Kernel's run() method returned.<br />每一个session是在自身heap中保存计数数据的，这与我们建立的session实例数量无关。POE轮次处理每一个事件，每次只有一个事件句柄被运行。当事件句柄运行的时候，POE::Kernel自身也将被中断，在事件句柄返回之前，没有事件被分派。<br />当各个session的事件被传送到主程序事件队列后，位于队列头部的事件被首先处理，新来的事件将被放置在队列的尾部。以此保证队列的轮次处理。<br />POE::Kernek的run方法在最后一个session停止之后返回。<br /><br />四．回声服务器<br />最后我们将用IO::Select建立一个非派生的回声服务器，然后再利用多个抽象层的概念将它移植到POE上。<br /><br />1. 一个简单的select()服务器<br />这个非派生的服务器的原型来自于《Perl Cookbook》中的17.13章节。为了保持简洁并且也是为了更方便于移植到POE上，对其做了一些修改。同时为了增加可读性，还给该服务器设定一些小的目的和功能。<br />首先，需要引入所需的模块并初始化一些数据结构。<br />#!/usr/bin/perl<br /><br />use warnings;<br />use strict;<br /><br />use IO::Socket;<br />use IO::Select;<br />use Tie::RefHash;<br /><br />my %inbuffer  = ();<br />my %outbuffer = ();<br />my %ready = ();<br /><br />tie %ready, "Tie::RefHash";<br />接下来，我们要建立一个服务器socket。为了不阻塞单进程的服务器，这个socket被设置为非阻塞状态。<br />my $server = IO::Socket::INET-&gt;new<br />  ( LocalPort =&gt; 12345,<br />    Listen =&gt; 10,<br />  ) or die "can't make server socket: $@\n";<br /><br />$server-&gt;blocking(0);<br />然后建立主循环。我们制造一个IO::Socket对象用以监视socket上的活动。无论何时，当有一个事件发生在socket上，都会有相应的程序来处理它。<br />my $select = IO::Select-&gt;new($server);<br /><br />while (1) {<br />    foreach my $client ( $select-&gt;can_read(1) ) {<br />        handle_read($client);<br />    }<br /><br />    foreach my $client ( keys %ready ) {<br />        foreach my $request ( @{ $ready{$client} } ) {<br />            print "Got request: $request";<br />            $outbuffer{$client} .= $request;<br />        }<br />        delete $ready{$client};<br />    }<br /><br />    foreach my $client ( $select-&gt;can_write(1) ) {<br />        handle_write($client);<br />    }<br />}<br /><br />exit;<br />以上的主循环对整个程序做了一个大致的总结。下面是用于处理socket不同行为的几个函数。<br />第一个函数用来处理可读状态的socket。如果这个准备就绪的socket是服务器的socket，我们再接收一个新的连接，并将它注册到IO::Socket对象中。如果这是一个存在输入数据的客户端socket，我们读取数据并对其进行处理，并将处理的结果添加到%ready数据结构中。主循环会捕获在%ready中的数据，并将它们回传给客户端。<br />sub handle_read {<br />    my $client = shift;<br /><br />    if ( $client == $server ) {<br />        my $new_client = $server-&gt;accept();<br />        $new_client-&gt;blocking(0);<br />        $select-&gt;add($new_client);<br />        return;<br />    }<br /><br />    my $data = "";<br />    my $rv   = $client-&gt;recv( $data, POSIX::BUFSIZ, 0 );<br /><br />    unless ( defined($rv) and length($data) ) {<br />        handle_error($client);<br />        return;<br />    }<br /><br />    $inbuffer{$client} .= $data;<br />    while ( $inbuffer{$client} =~ s/(.*\n)// ) {<br />        push @{ $ready{$client} }, $1;<br />    }<br />}<br />接下来是一个处理可写状态的socket的函数。等待被发送到客户端的数据将被写到这个socket中，之后被从输出缓冲中删除。<br />sub handle_write {<br />    my $client = shift;<br /><br />    return unless exists $outbuffer{$client};<br /><br />    my $rv = $client-&gt;send( $outbuffer{$client}, 0 );<br />    unless ( defined $rv ) {<br />        warn "I was told I could write, but I can't.\n";<br />        return;<br />    }<br /><br />    if ( $rv == length( $outbuffer{$client} ) or<br />        $! == POSIX::EWOULDBLOCK<br />      ) {<br />        substr( $outbuffer{$client}, 0, $rv ) = "";<br />        delete $outbuffer{$client} unless length $outbuffer{$client};<br />        return;<br />    }<br /><br />    handle_error($client);<br />}<br />最后我们需要一个程序来处理客户socket在读取和发送数据时产生的错误。它会为发生错误的socket做一些清理工作，并保证它们被正确关闭。<br />sub handle_error {<br />    my $client = shift;<br /><br />    delete $inbuffer{$client};<br />    delete $outbuffer{$client};<br />    delete $ready{$client};<br /><br />    $select-&gt;remove($client);<br />    close $client;<br />}<br />短短130行代码，我们就有了一个简单的回声服务器。不算太坏，但是我们可以做得更好。<br /><br />2. 将服务器移植到POE上<br />为了把IO::Socket服务器移植到POE上，需要使用到某些POE的底层特征。为了详细说明的需要，我们竟可能地不省略细节，而最终的程序也将保留其中的大部分代码。<br />事实上，以上的IO::Socket服务器本身就是由事件驱动的，在其中包含了一个用于检测并分派之间的主循环，配以处理这些事件的相应事件句柄。从这一点上来说，与POE的原理和架构有异曲同工的意思。<br />新的服务器程序需要一个如下所示的POE空框架。用于具体功能实现的代码将被添加到这个框架之中。<br />#!/usr/bin/perl<br /><br />use warnings;<br />use strict;<br /><br />use POSIX;<br />use IO::Socket;<br />use POE;<br /><br />POE::Session-&gt;create<br />  ( inline_states =&gt;<br />      {<br />      }<br />  );<br /><br />POE::Kernel-&gt;run();<br />exit;<br />在继续完成接下来的程序之前，为了勾勒出程序的大致框架结构，必须明确哪些事件的出现是必要的。<br />?    服务器启动，完成初始化。<br />?    服务器socket准备就绪，可以接收连接。<br />?    客户socket处于可读取状态，服务器读取数据并对其进行处理。<br />?    客户socket处于可写状态，服务器对其写入一些数据。<br />?    客户socket发生错误，需要将其关闭。<br />一旦知道了需要做些什么，就可以建立这些事件的名称，并为这些事件编写相应的事件处理句柄，从而快速地完成POE::Session的构造。<br />POE::Session-&gt;create<br />  ( inline_states =&gt;<br />      { _start =&gt; \&amp;server_start,<br />        event_accept =&gt; \&amp;server_accept,<br />        event_read   =&gt; \&amp;client_read,<br />        event_write  =&gt; \&amp;client_write,<br />        event_error  =&gt; \&amp;client_error,<br />      }<br />  );<br />现在是真正将IO::Select代码移植过来的时候了！和IO::Select服务器相同，需要为客户socket提供输入和输出缓冲，而且由于这两个缓冲对socket句柄的重要性并且不存在冲突，它们将被保持为程序的全局变量。另外，在这里将不再使用%ready哈希表。<br />my %inbuffer  = ();<br />my %outbuffer = ();<br />紧接着是引入IO::Select的程序片段，因为每一段都是上面指定的事件所触发的，因此这些片段将被移植入相应事件的处理句柄中。<br />在_start事件的处理句柄中，需要建立一个服务器的监听socket，并用select_read为其分配一个事件发生器。句柄中用到的POE::Kernel模块中的select_read方法接收两个参数：第一个是需要监视的socket，第二个是当该socket处于可读状态时所触发的处理句柄。<br />sub server_start {<br />    my $server = IO::Socket::INET-&gt;new<br />      ( LocalPort =&gt; 12345,<br />        Listen =&gt; 10,<br />        Reuse  =&gt; "yes",<br />      ) or die "can't make server socket: $@\n";<br /><br />    $_[KERNEL]-&gt;select_read( $server, "event_accept" );<br />}<br />注意一点，我们并没有保存服务器socket。因为POE::Kernel会对其进行跟踪，并将其作为一个参数传递给event_accept事件句柄。只有在需要特殊用途的情况下，我们才会保存一个该socket的拷贝。<br />再回顾POE::Session构造器，事件event_accept会激发server_accept事件句柄。该句柄接收一个新的客户socket，并对其分配一个监视器。<br />sub server_accept {<br />    my ( $kernel, $server ) = @_[ KERNEL, ARG0 ];<br /><br />    my $new_client = $server-&gt;accept();<br />    $kernel-&gt;select_read( $new_client, "event_read" );<br />}<br />之后我们在client_read句柄中处理处理来自客户的数据。当新连接的客户socket处于可读状态时，句柄被触发。该句柄中的第一个客户参数即为所连接的客户socket，因此我们无需再为其保留一个拷贝。<br />在内容上，句柄client_read与IO::Select服务器中的handle_read几乎一样。而由于handle_read的accept部分的内容被移植到了server_accept句柄中，相应地就不再需要%ready哈希表了。<br />如果接收过程发生错误，客户socket通过POE::Kernel的yield方法将被传送到event_error句柄中。因为该yield方法是根据程序员的具体要求发送事件的，所以需要在yield中将发生错误的客户socket作为某个参数，而此socket在处理该event_error的事件句柄client_error中被赋值给$_[ARG0]。<br />接着，如果在client_read中发现输出缓存存在数据，我们将检测以保证当该客户socket处于可写状态时，及时触发事件处理句柄，将数据发送出去。<br />sub client_read {<br />    my ( $kernel, $client ) = @_[ KERNEL, ARG0 ];<br /><br />    my $data = "";<br />    my $rv   = $client-&gt;recv( $data, POSIX::BUFSIZ, 0 );<br /><br />    unless ( defined($rv) and length($data) ) {<br />        $kernel-&gt;yield( event_error =&gt; $client );<br />        return;<br />    }<br /><br />    $inbuffer{$client} .= $data;<br />    while ( $inbuffer{$client} =~ s/(.*\n)// ) {<br />        $outbuffer{$client} .= $1;<br />    }<br /><br />    if ( exists $outbuffer{$client} ) {<br />        $kernel-&gt;select_write( $client, "event_write" );<br />    }<br />}<br />在用于发送数据的事件句柄中，第一个客户参数依然是一个可用的socket。在该句柄中，如果输出缓存为空，则停止检测并迅速返回。否则，我们将试图将缓冲内的数据全部发出。如果所有数据均发送成功，该缓冲将被销毁。与client_read类似，client_write中也有相应的错误处理句柄。<br />sub client_write {<br />    my ( $kernel, $client ) = @_[ KERNEL, ARG0 ];<br /><br />    unless ( exists $outbuffer{$client} ) {<br />        $kernel-&gt;select_write($client);<br />        return;<br />    }<br /><br />    my $rv = $client-&gt;send( $outbuffer{$client}, 0 );<br />    unless ( defined $rv ) {<br />        warn "I was told I could write, but I can't.\n";<br />        return;<br />    }<br /><br />    if ( $rv == length( $outbuffer{$client} ) or<br />        $! == POSIX::EWOULDBLOCK<br />      ) {<br />        substr( $outbuffer{$client}, 0, $rv ) = "";<br />        delete $outbuffer{$client} unless length $outbuffer{$client};<br />        return;<br />    }<br /><br />    $kernel-&gt;yield( event_error =&gt; $client );<br />}<br />最后说明一下在以上两个句柄中被用到的错误处理句柄。我们首先删除了客户socket的输入输出缓存，再关闭建立在该socket上的所有监视，最后保证该socket被成功关闭。如此这般便有效地关闭了来自于客户的连接。<br />sub client_error {<br />    my ( $kernel, $client ) = @_[ KERNEL, ARG0 ];<br /><br />    delete $inbuffer{$client};<br />    delete $outbuffer{$client};<br /><br />    $kernel-&gt;select($client);<br />    close $client;<br />}<br />移植成功！<br /><img src ="http://www.cnitblog.com/gyn/aggbug/43537.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-05-09 11:20 <a href="http://www.cnitblog.com/gyn/archive/2008/05/09/43537.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>多看代码是提高能力的捷径</title><link>http://www.cnitblog.com/gyn/archive/2008/05/04/43297.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sun, 04 May 2008 08:36:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/05/04/43297.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/43297.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/05/04/43297.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/43297.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/43297.html</trackback:ping><description><![CDATA[最近一直有在学习POE，这是一个类似于状态机的网络框架，在python上有叫twisted的类似框架。为此也看了一些文章，并学习了一些例子。<br />对于SOCKET编程，也许是很久没有用了的缘故，看到相关的一段程序时，感觉相当地眼生。还因为之前一直是用perl原生的socket命令来写的，所以对IO::Socket没有做过多的了解。再去看书，或者将perldoc里的内容从头到尾地通读一遍，一来没有什么兴趣，二来就以往经验来看很多内容都是很少用到的，看了也容易忘记。所以就将这段代码抄了一遍，对于其中不了解或者有疑问的再去查perldoc，或者写一些测试代码来验证。事实证明收获也是不小，很快就了解了其中的主要内容，顺带地还将POSIX中的一些常量搞搞清楚。更重要的是在抄写这样规范的代码，对于自身编写程序的格式也会有潜移默化的影响。<br />这么做的关键在于，首先要有一段好的代码，第二不要抱着得过且过的心态放过一些细节。<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/perl</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">use</span><span style="color: rgb(0, 0, 0);"> warnings;<br /></span><span style="color: rgb(0, 0, 255);">use</span><span style="color: rgb(0, 0, 0);"> strict;<br /><br /></span><span style="color: rgb(0, 0, 255);">use</span><span style="color: rgb(0, 0, 0);"> POSIX;<br /></span><span style="color: rgb(0, 0, 255);">use</span><span style="color: rgb(0, 0, 0);"> IO</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 255);">Socket</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 0, 255);">use</span><span style="color: rgb(0, 0, 0);"> IO</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 255);">Select</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 0, 255);">use</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">Tie</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 0);">RefHash;<br /><br /></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Create the server socket.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$server</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> IO</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 255);">Socket</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 0);">INET</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">new(<br />        LocalPort </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">12345</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">Listen</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"><br />    ) or </span><span style="color: rgb(0, 0, 255);">die</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">can't make server socket: $@\n</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">;<br />    <br /></span><span style="color: rgb(128, 0, 128);">$server</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">blocking(</span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br /><br /></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Set structures to track input and output data.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">%inbuffer</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ();<br /></span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">%outbuffer</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ();<br /></span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">%ready</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ();<br /><br /></span><span style="color: rgb(0, 0, 255);">tie</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">%ready</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">Tie::RefHash</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">;<br /><br /></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> The select loop itself.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(128, 0, 128);">$my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> IO</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 255);">Select</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">new(</span><span style="color: rgb(128, 0, 128);">$server</span><span style="color: rgb(0, 0, 0);">);<br /><br /></span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">) {<br />    <br />    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Process sockets that are ready for reading.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">foreach</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">( </span><span style="color: rgb(128, 0, 128);">$select</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">can_read(</span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">) ) {<br />        handle_read(</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">);<br />    }<br />    <br />    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Process any complete requests. Echo the data back to the client,<br />    # by putting the ready lines into the client's output buffer.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">foreach</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);"> ( </span><span style="color: rgb(0, 0, 255);">keys</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">%ready</span><span style="color: rgb(0, 0, 0);"> ) {<br />        </span><span style="color: rgb(0, 0, 255);">foreach</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$request</span><span style="color: rgb(0, 0, 0);"> ( @{ </span><span style="color: rgb(128, 0, 128);">$ready</span><span style="color: rgb(0, 0, 0);">{</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">} } ) {<br />            </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">Got request: $request\n</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(128, 0, 128);">$outbuffer</span><span style="color: rgb(0, 0, 0);">{</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">} </span><span style="color: rgb(0, 0, 0);">.=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$request</span><span style="color: rgb(0, 0, 0);">;<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">delete</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$ready</span><span style="color: rgb(0, 0, 0);">{</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">};<br />    }<br />    <br />    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Process sockets that are ready for writing.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">foreach</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);"> ( </span><span style="color: rgb(128, 0, 128);">$server</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">can_write(</span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">) ) {<br />        handle_write(</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">);<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">exit</span><span style="color: rgb(0, 0, 0);">;<br /><br /></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Handle a socket that's ready to be read from.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">sub</span><span style="color: rgb(0, 0, 0);"> handle_read {<br />    </span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">shift</span><span style="color: rgb(0, 0, 0);">;<br />    <br />    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> If it's the server socket, accept a new client connection.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ( </span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$server</span><span style="color: rgb(0, 0, 0);"> ) {<br />        </span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$new_client</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$server</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 255);">accept</span><span style="color: rgb(0, 0, 0);">();<br />        </span><span style="color: rgb(128, 0, 128);">$new_client</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">blocking(</span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br />        </span><span style="color: rgb(128, 0, 128);">$select</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">add(</span><span style="color: rgb(128, 0, 128);">$new_client</span><span style="color: rgb(0, 0, 0);">);<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />    <br />    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Read from an established client socket.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$data</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">""</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$rv</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 255);">recv</span><span style="color: rgb(0, 0, 0);">( </span><span style="color: rgb(128, 0, 128);">$data</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> POSIX</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 0);">BUFSIZ</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);"> );<br />    <br />    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Handle socket errors.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">unless</span><span style="color: rgb(0, 0, 0);"> ( </span><span style="color: rgb(0, 0, 255);">defined</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 128);">$rv</span><span style="color: rgb(0, 0, 0);">) and </span><span style="color: rgb(0, 0, 255);">length</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 128);">$data</span><span style="color: rgb(0, 0, 0);">) ) {<br />        handle_error(</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">);<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />    <br />    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Successful read. Buffer the data we got, and parse it into lines.<br />    # Place the lines into %ready, where they will be processed later.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(128, 0, 128);">$inbuffer</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">.=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$data</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);"> ( </span><span style="color: rgb(128, 0, 128);">$inbuffer</span><span style="color: rgb(0, 0, 0);">{</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">} </span><span style="color: rgb(0, 0, 0);">=~</span><span style="color: rgb(0, 0, 0);"> s</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">.*\</span><span style="color: rgb(0, 0, 0);">n)</span><span style="color: rgb(0, 0, 0);">//</span><span style="color: rgb(0, 0, 0);"> ) {<br />        </span><span style="color: rgb(0, 0, 255);">push</span><span style="color: rgb(0, 0, 0);"> @{ </span><span style="color: rgb(128, 0, 128);">$ready</span><span style="color: rgb(0, 0, 0);">{</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">} }</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> $</span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />}<br /><br /></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Handle a socket that's ready to be write to.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">sub</span><span style="color: rgb(0, 0, 0);"> handle_write {<br />    </span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">shift</span><span style="color: rgb(0, 0, 0);">;<br />    <br />    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Skip this client if there's nothing write.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">unless</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">exists</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$outbuffer</span><span style="color: rgb(0, 0, 0);">{</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">};<br />    <br />    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Attempt to write pending data to the client.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$rv</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 255);">send</span><span style="color: rgb(0, 0, 0);">( </span><span style="color: rgb(128, 0, 128);">$outbuffer</span><span style="color: rgb(0, 0, 0);">{</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">}</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);"> );<br />    </span><span style="color: rgb(0, 0, 255);">unless</span><span style="color: rgb(0, 0, 0);"> ( </span><span style="color: rgb(0, 0, 255);">defined</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$rv</span><span style="color: rgb(0, 0, 0);"> ) {<br />        </span><span style="color: rgb(0, 0, 255);">warn</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">I was told I could write, but I can't.\n</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />    <br />    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Successful write. Remove what was sent from the output buffer.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ( </span><span style="color: rgb(128, 0, 128);">$rv</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">length</span><span style="color: rgb(0, 0, 0);">( </span><span style="color: rgb(128, 0, 128);">$outbuffer</span><span style="color: rgb(0, 0, 0);">{</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">} ) or $</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">POSIX</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 0);">EWORLDBLOCK ) {<br />        </span><span style="color: rgb(0, 0, 255);">substr</span><span style="color: rgb(0, 0, 0);">( </span><span style="color: rgb(128, 0, 128);">$outbuffer</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$rv</span><span style="color: rgb(0, 0, 0);"> ) </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">""</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">delete</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$outbuffer</span><span style="color: rgb(0, 0, 0);">{</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">} </span><span style="color: rgb(0, 0, 255);">unless</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">length</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$outbuffer</span><span style="color: rgb(0, 0, 0);">{</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">};<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />    <br />    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Otherwise there was an error.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    handle_error(</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">);<br />}<br /><br /></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Handle client error. Clean up after dead socket.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">sub</span><span style="color: rgb(0, 0, 0);"> handle_error {<br />    </span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">shift</span><span style="color: rgb(0, 0, 0);">;<br />    <br />    </span><span style="color: rgb(0, 0, 255);">delete</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$inbuffer</span><span style="color: rgb(0, 0, 0);">{</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">};<br />    </span><span style="color: rgb(0, 0, 255);">delete</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$outbuffer</span><span style="color: rgb(0, 0, 0);">{</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">};<br />    </span><span style="color: rgb(0, 0, 255);">delete</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$ready</span><span style="color: rgb(0, 0, 0);">{</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">};<br />    <br />    </span><span style="color: rgb(128, 0, 128);">$select</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">remove(</span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">);<br />    </span><span style="color: rgb(0, 0, 255);">close</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$client</span><span style="color: rgb(0, 0, 0);">;<br />}<br /></span></div><br /><img src ="http://www.cnitblog.com/gyn/aggbug/43297.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-05-04 16:36 <a href="http://www.cnitblog.com/gyn/archive/2008/05/04/43297.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一段视频重录的代码</title><link>http://www.cnitblog.com/gyn/archive/2008/04/07/42051.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Mon, 07 Apr 2008 03:36:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/04/07/42051.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/42051.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/04/07/42051.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/42051.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/42051.html</trackback:ping><description><![CDATA[网站会录制一档每晚播出的电视节目，但是有时候录制会失败，于是需要在第二天重播的时候再次录制。为此，需要调整各个文件的参数，由于参数比较多而且涉及到FTP传输，对于不是很熟悉这一过程的网管，如果一不留神很可能会导致再次失败。所以写了一个带界面的设置工具，完成录制和传输过程。<br /><img src="http://www.cnitblog.com/images/cnitblog_com/gyn/chonglu.JPG" alt="chonglu.JPG" border="0" height="48" width="592" /><br />代码：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 255);">use</span><span style="color: rgb(0, 0, 0);"> Win32</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 0);">GUI;<br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">use</span><span style="color: rgb(0, 0, 0);"> strict;<br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">use</span><span style="color: rgb(0, 0, 0);"> Encode;<br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">use</span><span style="color: rgb(0, 0, 0);"> Net</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 0);">FTP;<br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">use</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">Time</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 255);">Local</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">use</span><span style="color: rgb(0, 0, 0);"> Tk;<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">use</span><span style="color: rgb(0, 0, 0);"> Tk</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 0);">BrowseEntry;<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$hw</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Win32</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 0);">GUI</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 0);">GetPerlWindow();<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">Win32</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 0);">GUI</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 0);">Hide(</span><span style="color: rgb(128, 0, 128);">$hw</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(128, 0, 128);">$sec</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 128);">$min</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 128);">$hour</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 128);">$mday</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 128);">$mon</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 128);">$year</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 128);">$wday</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 128);">$yday</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 128);">$isdst</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">localtime</span><span style="color: rgb(0, 0, 0);">();<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">@t</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">localtime</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">time</span><span style="color: rgb(0, 0, 0);">() </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">86400</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$tm</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$bt</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$mw</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$signal</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$sd</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">sprintf</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">%d</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$t</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">1900</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 255);">sprintf</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">%.2d</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$t</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 255);">sprintf</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">%.2d</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$t</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]);<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$data</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">sprintf</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">%d</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$t</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">1900</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">-</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 255);">sprintf</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">%.2d</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$t</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">-</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 255);">sprintf</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">%.2d</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$t</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]);<br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$ta</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">sprintf</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">%d</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$year</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">1900</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">-</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 255);">sprintf</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">%.2d</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$mon</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">-</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(0, 0, 255);">sprintf</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">%.2d</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$mday</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">sub</span><span style="color: rgb(0, 0, 0);"> hanzi { </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> decode(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">gb2312</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">shift</span><span style="color: rgb(0, 0, 0);">); }<br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">sub</span><span style="color: rgb(0, 0, 0);"> shezhi {<br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(128, 0, 128);">$tm</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> timelocal(</span><span style="color: rgb(128, 0, 128);">$sec</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 128);">$min</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 128);">$hour</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 128);">$mday</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 128);">$mon</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 128);">$year</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(128, 0, 128);">$signal</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">open</span><span style="color: rgb(0, 0, 0);"> C</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">chonglu.rpjf</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">open</span><span style="color: rgb(0, 0, 0);"> T</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">&gt;temp</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">C</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">) { <br /></span><span style="color: rgb(0, 128, 128);">29</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">chomp</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">30</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(128, 0, 128);">$_</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=~</span><span style="color: rgb(0, 0, 0);"> s</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">.*</span><span style="color: rgb(0, 0, 0);">)([</span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(128, 0, 0);">9</span><span style="color: rgb(0, 0, 0);">]{</span><span style="color: rgb(128, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">})(</span><span style="color: rgb(0, 0, 0);">.*</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">$</span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(128, 0, 128);">$sd</span><span style="color: rgb(0, 0, 0);">$</span><span style="color: rgb(128, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">31</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(128, 0, 128);">$_</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=~</span><span style="color: rgb(0, 0, 0);"> s</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">.*</span><span style="color: rgb(0, 0, 0);">)([</span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(128, 0, 0);">9</span><span style="color: rgb(0, 0, 0);">]{</span><span style="color: rgb(128, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">}</span><span style="color: rgb(0, 0, 0);">\-</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(128, 0, 0);">9</span><span style="color: rgb(0, 0, 0);">]{</span><span style="color: rgb(128, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">}</span><span style="color: rgb(0, 0, 0);">\-</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(128, 0, 0);">9</span><span style="color: rgb(0, 0, 0);">]{</span><span style="color: rgb(128, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">})(</span><span style="color: rgb(0, 0, 0);">.*</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">$</span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(128, 0, 128);">$data</span><span style="color: rgb(0, 0, 0);">$</span><span style="color: rgb(128, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">32</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> T </span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">$_\n</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">33</span> <span style="color: rgb(0, 0, 0);">    }<br /></span><span style="color: rgb(0, 128, 128);">34</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">close</span><span style="color: rgb(0, 0, 0);"> C; </span><span style="color: rgb(0, 0, 255);">close</span><span style="color: rgb(0, 0, 0);"> T;<br /></span><span style="color: rgb(0, 128, 128);">35</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">system</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">del chonglu.rpjf</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">36</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">system</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">rename temp chonglu.rpjf</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">37</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(128, 0, 128);">$bt</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">configure(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">state </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">disable</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">38</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">39</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">sub</span><span style="color: rgb(0, 0, 0);"> monitor {<br /></span><span style="color: rgb(0, 128, 128);">40</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(128, 0, 128);">$signal</span><span style="color: rgb(0, 0, 0);">) { <br /></span><span style="color: rgb(0, 128, 128);">41</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">time</span><span style="color: rgb(0, 0, 0);">() </span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$tm</span><span style="color: rgb(0, 0, 0);">) {<br /></span><span style="color: rgb(0, 128, 128);">42</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(128, 0, 128);">$signal</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">43</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">system</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">producer -j "d:\shixian\chonglu.rpjf" -daw -lc "e,i"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">44</span> <span style="color: rgb(0, 0, 0);">        }<br /></span><span style="color: rgb(0, 128, 128);">45</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">e </span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">shixian$sd.rm</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">) {<br /></span><span style="color: rgb(0, 128, 128);">46</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">my</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$f</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Net</span><span style="color: rgb(0, 0, 0);">::</span><span style="color: rgb(0, 0, 0);">FTP</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">new(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">media.zsgd.com</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">) or </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">ftp failed</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">47</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(128, 0, 128);">$f</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">login(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">jh</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">2020038</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">48</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(128, 0, 128);">$f</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">put(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">shixian$sd.rm</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">49</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(128, 0, 128);">$f</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">quit();<br /></span><span style="color: rgb(0, 128, 128);">50</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0); font-weight: bold;">send complete\n</span><span style="color: rgb(0, 0, 0); font-weight: bold;">"</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">51</span> <span style="color: rgb(0, 0, 0);">        }<br /></span><span style="color: rgb(0, 128, 128);">52</span> <span style="color: rgb(0, 0, 0);">    }<br /></span><span style="color: rgb(0, 128, 128);">53</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">54</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">55</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(128, 0, 128);">$mw</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> MainWindow</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">new(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">title </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> hanzi(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">视线重录</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">));<br /></span><span style="color: rgb(0, 128, 128);">56</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(128, 0, 128);">$mw</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">Label(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">text </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$ta</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 255);">pack</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">side </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">left</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">expand </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">fill </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">x</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">57</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">foreach</span><span style="color: rgb(0, 0, 0);"> (([</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">小时</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">\</span><span style="color: rgb(128, 0, 128);">$hour</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">..</span><span style="color: rgb(128, 0, 0);">23</span><span style="color: rgb(0, 0, 0);">]]</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">分</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">\</span><span style="color: rgb(128, 0, 128);">$min</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">..</span><span style="color: rgb(128, 0, 0);">59</span><span style="color: rgb(0, 0, 0);">]]</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">秒</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">\</span><span style="color: rgb(128, 0, 128);">$sec</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">..</span><span style="color: rgb(128, 0, 0);">59</span><span style="color: rgb(0, 0, 0);">]])) {<br /></span><span style="color: rgb(0, 128, 128);">58</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(128, 0, 128);">$mw</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">BrowseEntry(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">label </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> hanzi(</span><span style="color: rgb(128, 0, 128);">$_</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">])</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">variable </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$_</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">choices </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$_</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">])</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 255);">pack</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">side </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">left</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">expand </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">fill </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">x</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">59</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">60</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(128, 0, 128);">$bt</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">$mw</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">Button(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">text </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> hanzi(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">设置</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">command </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">\&amp;</span><span style="color: rgb(0, 0, 0);">shezhi)</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 255);">pack</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">side </span><span style="color: rgb(0, 0, 0);">=&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0); font-weight: bold;">bottom</span><span style="color: rgb(0, 0, 0); font-weight: bold;">'</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">61</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(128, 0, 128);">$mw</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">resizable(</span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">62</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(128, 0, 128);">$mw</span><span style="color: rgb(0, 0, 0);">-&gt;</span><span style="color: rgb(0, 0, 0);">repeat(</span><span style="color: rgb(128, 0, 0);">1000</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">\&amp;</span><span style="color: rgb(0, 0, 0);">monitor);<br /></span><span style="color: rgb(0, 128, 128);">63</span> <span style="color: rgb(0, 0, 0);">MainLoop();<br /></span><span style="color: rgb(0, 128, 128);">64</span> <span style="color: rgb(0, 0, 0);"></span></div><br /><img src ="http://www.cnitblog.com/gyn/aggbug/42051.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-04-07 11:36 <a href="http://www.cnitblog.com/gyn/archive/2008/04/07/42051.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>检索mp3的脚本</title><link>http://www.cnitblog.com/gyn/archive/2008/04/05/41993.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sat, 05 Apr 2008 08:57:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2008/04/05/41993.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/41993.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2008/04/05/41993.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/41993.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/41993.html</trackback:ping><description><![CDATA[播出软件的音频解码单元无法处理一些特殊码率的mp3文件，为了保证播放的安全和流畅，需要将这些特殊码率的文件剔除掉。使用诸如千千静听和foobar等播放器都可以查看文件的码率，但是如果文件数量很多，这种人工查看的办法非常费时。所以用脚本来完成一项工作。<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080"> 1</span><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #0000ff">use</span><span style="COLOR: #000000"> strict;<br /></span><span style="COLOR: #008080"> 2</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">use</span><span style="COLOR: #000000"> File</span><span style="COLOR: #000000">::</span><span style="COLOR: #000000">Find;<br /></span><span style="COLOR: #008080"> 3</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">use</span><span style="COLOR: #000000"> MP3</span><span style="COLOR: #000000">::</span><span style="COLOR: #000000">Tag;<br /></span><span style="COLOR: #008080"> 4</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /></span><span style="COLOR: #008080"> 5</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">my</span><span style="COLOR: #000000"> (</span><span style="COLOR: #800080">$path</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$delete</span><span style="COLOR: #000000">) </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">@ARGV</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 6</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /></span><span style="COLOR: #008080"> 7</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />find(</span><span style="COLOR: #000000">\&amp;</span><span style="COLOR: #000000">wanted</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$path</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080"> 8</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /></span><span style="COLOR: #008080"> 9</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">sub</span><span style="COLOR: #000000"> wanted{<br /></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #000000">/\.</span><span style="COLOR: #000000">mp3$</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">do</span><span style="COLOR: #000000"> {<br /></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">my</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$mp3</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> MP3</span><span style="COLOR: #000000">::</span><span style="COLOR: #000000">Tag</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">new(</span><span style="FONT-WEIGHT: bold; COLOR: #000000">"</span><span style="FONT-WEIGHT: bold; COLOR: #000000">$_</span><span style="FONT-WEIGHT: bold; COLOR: #000000">"</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #0000ff">defined</span><span style="COLOR: #000000">(</span><span style="COLOR: #800080">$mp3</span><span style="COLOR: #000000">) </span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000"> is_normal_bitrate(</span><span style="COLOR: #800080">$mp3</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">bitrate_kbps())){<br /></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />            remove(</span><span style="COLOR: #800080">$_</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        }<br /></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    };<br /></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />}<br /></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">sub</span><span style="COLOR: #000000"> is_normal_bitrate{<br /></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">my</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$bitrate</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">shift</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (</span><span style="COLOR: #800000">64</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">128</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">192</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">256</span><span style="COLOR: #000000">){<br /></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #800080">$bitrate</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$_</span><span style="COLOR: #000000">) {<br /></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />            </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">1</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        }<br /></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    }<br /></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">0</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />}<br /></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">sub</span><span style="COLOR: #000000"> remove{<br /></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">my</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$filename</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">shift</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">my</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">@paths</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">split</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">/\//,</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$path</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">my</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$recycled</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$paths</span><span style="COLOR: #000000">[</span><span style="COLOR: #800000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">.</span><span style="FONT-WEIGHT: bold; COLOR: #000000">"</span><span style="FONT-WEIGHT: bold; COLOR: #000000">/$filename</span><span style="FONT-WEIGHT: bold; COLOR: #000000">"</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    <br /></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">open</span><span style="COLOR: #000000">(FILE</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$filename</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">open</span><span style="COLOR: #000000">(ANO</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #000000">"</span><span style="FONT-WEIGHT: bold; COLOR: #000000">&gt;</span><span style="FONT-WEIGHT: bold; COLOR: #000000">"</span><span style="COLOR: #000000">.</span><span style="COLOR: #800080">$recycled</span><span style="COLOR: #000000">) or </span><span style="COLOR: #0000ff">die</span><span style="COLOR: #000000"> $</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    <br /></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">binmode</span><span style="COLOR: #000000"> FILE;<br /></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">binmode</span><span style="COLOR: #000000"> ANO;<br /></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    <br /></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">my</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$offset</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">0</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">my</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$buffer</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">undef</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">my</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$number</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">0</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">((</span><span style="COLOR: #800080">$number</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">sysread</span><span style="COLOR: #000000">(FILE</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$buffer</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">1024</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$offset</span><span style="COLOR: #000000">)) </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">0</span><span style="COLOR: #000000">){<br /></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">syswrite</span><span style="COLOR: #000000">(ANO</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$buffer</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$number</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$offset</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #800080">$offset</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$number</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    }<br /></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    <br /></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">close</span><span style="COLOR: #000000"> FILE;<br /></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">close</span><span style="COLOR: #000000"> ANO;<br /></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    <br /></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #800080">$delete</span><span style="COLOR: #000000"> eq </span><span style="FONT-WEIGHT: bold; COLOR: #000000">"</span><span style="FONT-WEIGHT: bold; COLOR: #000000">-d</span><span style="FONT-WEIGHT: bold; COLOR: #000000">"</span><span style="COLOR: #000000">) {</span><span style="COLOR: #0000ff">unlink</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$_</span><span style="COLOR: #000000">;}<br /></span><span style="COLOR: #008080">51</span><span style="COLOR: #000000"><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />}</span></div><img src ="http://www.cnitblog.com/gyn/aggbug/41993.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2008-04-05 16:57 <a href="http://www.cnitblog.com/gyn/archive/2008/04/05/41993.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>基于串口心跳的双机热备Perl代码(下载)</title><link>http://www.cnitblog.com/gyn/archive/2007/11/29/37031.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Thu, 29 Nov 2007 06:07:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2007/11/29/37031.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/37031.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2007/11/29/37031.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/37031.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/37031.html</trackback:ping><description><![CDATA[
		<p>(下载<a title="serial_heartbeat.rxr" href="/Files/gyn/serial_heartbeat.rar">serial_heartbeat.rar</a>)压缩包里有两个文件夹，一个用于主机，一个用于备机。串口参数修改在serial.conf中；检测参数在parameters.txt中一般不需要修改；网卡参数设置在inetrface开头的文件中，其中interface_null不需要修改。运行时，请确保已安装了perl 和Win32::SerialPort模块。由于上载空间有限，不提供exe文件下载。有需要可电邮我<a href="mailto:jh@zsgd.com">jh@zsgd.com</a>。</p>
<img src ="http://www.cnitblog.com/gyn/aggbug/37031.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2007-11-29 14:07 <a href="http://www.cnitblog.com/gyn/archive/2007/11/29/37031.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>perl串口通信实例</title><link>http://www.cnitblog.com/gyn/archive/2007/11/27/36919.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Tue, 27 Nov 2007 06:40:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2007/11/27/36919.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/36919.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2007/11/27/36919.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/36919.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/36919.html</trackback:ping><description><![CDATA[
		<p>
				<font face="Arial">使用串口通信，在备机端使用如下脚本检测来自主机的心跳信号，一旦未接受次数超过指定记数，备机认为主机DOWN机，自动设置为主机的网络参数，顶替主机提供服务。</font>
		</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #008000">#</span>
				<span style="COLOR: #008000">! perl -w</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">use</span>
				<span style="COLOR: #000000"> strict;<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #0000ff">use</span>
				<span style="COLOR: #000000"> Win32</span>
				<span style="COLOR: #000000">::</span>
				<span style="COLOR: #000000">SerialPort;<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #0000ff">my</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800080">$port</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">COM2</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="COLOR: #000000">; </span>
				<span style="COLOR: #008000">#</span>
				<span style="COLOR: #008000"> serail port name registried in OS</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">my</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800080">$count</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800000">0</span>
				<span style="COLOR: #000000">; </span>
				<span style="COLOR: #008000">#</span>
				<span style="COLOR: #008000"> count number of heartbeat-receiving failture</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">my</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800080">$max_count</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800000">5</span>
				<span style="COLOR: #000000">; </span>
				<span style="COLOR: #008000">#</span>
				<span style="COLOR: #008000"> max fail count to be tolerated </span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">my</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800080">$interface</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /># ---------------------------------- <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /># 接口 IP 配置         <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /># ---------------------------------- <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />pushd interface ip<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /># "local" 的接口 IP  配置<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />set address name="local" source=static addr=192.168.6.185 mask=255.255.255.0<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />set address name="local" gateway=192.168.6.65 gwmetric=0<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />set dns name="local" source=static addr=192.168.6.112 register=PRIMARY<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />add dns name="local" addr=192.168.6.201 index=2<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />set wins name="local" source=static addr=none<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />popd<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /># 接口 IP 配置结束<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="COLOR: #000000">; </span>
				<span style="COLOR: #008000">#</span>
				<span style="COLOR: #008000"> net inetrface config information</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">sub</span>
				<span style="COLOR: #000000"> errlog {<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #008000">#</span>
				<span style="COLOR: #008000">log the failtrue occuring time</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">    <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #0000ff">open</span>
				<span style="COLOR: #000000"> ERR</span>
				<span style="COLOR: #000000">,</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">&gt;&gt;err.log</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="COLOR: #000000">;<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #0000ff">my</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800080">@time</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">localtime</span>
				<span style="COLOR: #000000">();<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #0000ff">my</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800080">$time</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">sprintf</span>
				<span style="COLOR: #000000">(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">%d</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="COLOR: #000000">,</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800080">$time</span>
				<span style="COLOR: #000000">[</span>
				<span style="COLOR: #800000">5</span>
				<span style="COLOR: #000000">] </span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800000">1900</span>
				<span style="COLOR: #000000">) <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />                        </span>
				<span style="COLOR: #000000">.</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">-</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="COLOR: #000000"> <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />                        </span>
				<span style="COLOR: #000000">.</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">sprintf</span>
				<span style="COLOR: #000000">(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">%d</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="COLOR: #000000">,</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800080">$time</span>
				<span style="COLOR: #000000">[</span>
				<span style="COLOR: #800000">4</span>
				<span style="COLOR: #000000">] </span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800000">1</span>
				<span style="COLOR: #000000">)<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />                        </span>
				<span style="COLOR: #000000">.</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">-</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />                        </span>
				<span style="COLOR: #000000">.</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">$time[3]</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />                        </span>
				<span style="COLOR: #000000">.</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />                        </span>
				<span style="COLOR: #000000">.</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">sprintf</span>
				<span style="COLOR: #000000">(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">%.2d</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="COLOR: #000000">,</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800080">$time</span>
				<span style="COLOR: #000000">[</span>
				<span style="COLOR: #800000">2</span>
				<span style="COLOR: #000000">])<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />                        </span>
				<span style="COLOR: #000000">.</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">:</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />                        </span>
				<span style="COLOR: #000000">.</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">sprintf</span>
				<span style="COLOR: #000000">(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">%.2d</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="COLOR: #000000">,</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800080">$time</span>
				<span style="COLOR: #000000">[</span>
				<span style="COLOR: #800000">1</span>
				<span style="COLOR: #000000">])<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />                        </span>
				<span style="COLOR: #000000">.</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">:</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />                        </span>
				<span style="COLOR: #000000">.</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">sprintf</span>
				<span style="COLOR: #000000">(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">%.2d</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="COLOR: #000000">,</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800080">$time</span>
				<span style="COLOR: #000000">[</span>
				<span style="COLOR: #800000">0</span>
				<span style="COLOR: #000000">]);<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #0000ff">print</span>
				<span style="COLOR: #000000"> ERR </span>
				<span style="COLOR: #800080">$time</span>
				<span style="COLOR: #000000">.</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">\n</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="COLOR: #000000">;<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #0000ff">close</span>
				<span style="COLOR: #000000"> ERR;<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />}<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #0000ff">sub</span>
				<span style="COLOR: #000000"> ipchange {<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #008000">#</span>
				<span style="COLOR: #008000"> change ip addrress</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">    <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #0000ff">open</span>
				<span style="COLOR: #000000"> TMP</span>
				<span style="COLOR: #000000">,</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">&gt;tmp</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="COLOR: #000000">;<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #0000ff">print</span>
				<span style="COLOR: #000000"> TMP </span>
				<span style="COLOR: #800080">$interface</span>
				<span style="COLOR: #000000">;<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #0000ff">close</span>
				<span style="COLOR: #000000"> TMP;<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #0000ff">eval</span>
				<span style="COLOR: #000000"> {<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #0000ff">system</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">netsh -f tmp</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="COLOR: #000000">;<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #0000ff">unlink</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">tmp</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="COLOR: #000000">;<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    };<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000"> ($@) {</span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800000">0</span>
				<span style="COLOR: #000000">;}<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800000">1</span>
				<span style="COLOR: #000000">;<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />}<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #0000ff">my</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800080">$ob</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> Win32</span>
				<span style="COLOR: #000000">::</span>
				<span style="COLOR: #000000">SerialPort</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">new(</span>
				<span style="COLOR: #800080">$port</span>
				<span style="COLOR: #000000">) or </span>
				<span style="COLOR: #0000ff">die</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">CANNOT OPEN $port</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="COLOR: #000000">;<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">#</span>
				<span style="COLOR: #008000"> open serial port </span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">eval</span>
				<span style="COLOR: #000000"> {<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #008000">#</span>
				<span style="COLOR: #008000"> set serial port properties</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">    <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #800080">$ob</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">baudrate(</span>
				<span style="COLOR: #800000">9600</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #800080">$ob</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">parity(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">none</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #800080">$ob</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">databits(</span>
				<span style="COLOR: #800000">8</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #800080">$ob</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">stopbits(</span>
				<span style="COLOR: #800000">1</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #800080">$ob</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">handshake(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">none</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />};<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000"> ($@) {</span>
				<span style="COLOR: #0000ff">die</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">SET FAILED</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="COLOR: #000000">;}<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #800080">$ob</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">write_settings or </span>
				<span style="COLOR: #0000ff">die</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">CANNOT WRITE $port DRIVER</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">"</span>
				<span style="COLOR: #000000">;<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">#</span>
				<span style="COLOR: #008000"> write to port driver to make it active</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">while</span>
				<span style="COLOR: #000000"> (</span>
				<span style="COLOR: #800000">1</span>
				<span style="COLOR: #000000">) {<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #008000">#</span>
				<span style="COLOR: #008000"> loop receiving heartbeat from remote machine<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    # change ip address after designated count of failture </span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">    <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #0000ff">my</span>
				<span style="COLOR: #000000"> (</span>
				<span style="COLOR: #800080">$length</span>
				<span style="COLOR: #000000">,</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800080">$result</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800080">$ob</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #0000ff">read</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #800000">10</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000"> (</span>
				<span style="COLOR: #800080">$result</span>
				<span style="COLOR: #000000">) { <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #800080">$count</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800000">0</span>
				<span style="COLOR: #000000">;<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    } </span>
				<span style="COLOR: #0000ff">elsif</span>
				<span style="COLOR: #000000"> (</span>
				<span style="COLOR: #800080">$count</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">==</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800080">$max_count</span>
				<span style="COLOR: #000000">) {<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        errlog();<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000"> (not ipchange()) { </span>
				<span style="COLOR: #0000ff">print</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">IP CHANGE FAILED</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">'</span>
				<span style="COLOR: #000000">; }<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #0000ff">last</span>
				<span style="COLOR: #000000">;<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    }    </span>
				<span style="COLOR: #0000ff">else</span>
				<span style="COLOR: #000000"> { <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #800080">$count</span>
				<span style="COLOR: #000000">++</span>
				<span style="COLOR: #000000">;<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    }<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #0000ff">sleep</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #800000">1</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />}<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #0000ff">undef</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #800080">$ob</span>
				<span style="COLOR: #000000">;</span>
		</div>
<img src ="http://www.cnitblog.com/gyn/aggbug/36919.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2007-11-27 14:40 <a href="http://www.cnitblog.com/gyn/archive/2007/11/27/36919.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>将参数文件转化为hash表的脚本</title><link>http://www.cnitblog.com/gyn/archive/2007/08/14/31778.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Tue, 14 Aug 2007 03:21:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2007/08/14/31778.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/31778.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2007/08/14/31778.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/31778.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/31778.html</trackback:ping><description><![CDATA[（<a href="/Files/gyn/tj.rar">点此下载</a>）在配置脚本的时候，经常需要读入一些参数，为了方便起见有时会把参数整理成参数文件。当这些参数是平行关系的时候，只需要按行读入即可；但是更一般的情况是，如果这些参数存在层次的关系，该怎么读入呢？<br />比方说，我要写一个带界面的数据库查询工具，该工具的界面大小由初始化参数决定，数据库的连接参数也写入在参数文件中。这样就存在了两类截然不相同类型的数据，最好将它们区分开来。具体形式如下：<br />size^<br />    x^<br />        500<br />    $<br />    y^<br />        500<br />    $<br />$<br />db^<br />    name^<br />        tj<br />    $<br />    host^<br />        localhost<br />    $<br />    port^<br />        5432<br />    $<br />    id^<br />        postgres<br />    $<br />    pwd^<br />        7697<br />    $<br />$<br />以上数据存成tj.param文件，再把它整成一个hash表，这样我要取数据库的端口的话就可以用“$param{'db'}-&gt;{'port'}-&gt;{'val'}”。<br />脚本如下：<br />sub getparam{<br />    my $param = shift;<br />    my @tmp = ();<br />    my $tmp = '';<br />    push @tmp, $param ;<br />    open PAR, 'tj.para' ;<br />    while (&lt;PAR&gt;) {<br />        s/^(\s+)([^\s]*)(\s+)$/$2/;<br />        chomp;<br />        if ($_ ne '') {<br />            s/\^// and do {<br />                my %tmp = ();<br />                $tmp[length(@tmp) - 1]-&gt;{"$_"} = \%tmp;<br />                push @tmp, \%tmp ;<br />                next;<br />            };<br />            s/\$// and do {<br />                if (@tmp&gt;0) {<br />                    pop @tmp;<br />                } else {<br />                    print "logic error: stack has been empty.\n";<br />                    return 0;<br />                }<br />                next;<br />            };<br />            $tmp[length(@tmp) - 1]-&gt;{"val"} = $_;<br />        }<br />    }<br />    close PAR;<br />    if (length(@tmp) != 0) { return 0; } <br />    else {return 1;}<br />}<br /><br />1;<br />存成tjpar.pl，测试一下看看：<br />use  strict;<br />require('tjpar.pl');<br /><br />my %param = ();<br />getparam(\%param);<br /><br />print $param{'db'}-&gt;{'port'}-&gt;{'val'};<br />会打印出来5432，就成功了;-)<br /><img src ="http://www.cnitblog.com/gyn/aggbug/31778.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2007-08-14 11:21 <a href="http://www.cnitblog.com/gyn/archive/2007/08/14/31778.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>监测网络并切换ip地址的脚本</title><link>http://www.cnitblog.com/gyn/archive/2007/07/22/30422.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sun, 22 Jul 2007 13:14:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2007/07/22/30422.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/30422.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2007/07/22/30422.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/30422.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/30422.html</trackback:ping><description><![CDATA[最近电信的线路总是受到攻击，一旦出现问题就得切换到联通的线路上去。这样的情况优势甚至出现在晚上下班以后，弄得人疲惫不堪。两条宽带线路接入的机房在另一部门的机房里，那儿有专人值班，所以一般物理上的切换只要打电话拜托几位兄弟就行了。但是服务器机房只有我在看管，所以其上的ip地址切换就不得不亲自前往，甚是麻烦。所以写了一个监测的脚本，每一分钟ping网管一次，如果遇到不通则将ip地址换为联通的。<br />use Net::Ping;<br />$count = 0;<br />$timer = 1;<br />print "start monitoring...\n";<br />while(1){<br />    $p = Net::Ping-&gt;new();<br />    if( not $p-&gt;ping('*.*.*.*', 3) ){ <br />        if ($count == 3){<br />            $count = 0;<br />            system 'netsh -f c:/interface';<br />            open FH, '&gt;&gt;c:/transfer_log.txt';<br />            @current = localtime();<br />            $index = 0;<br />            while($index &lt; 6){ $log = $log."\t".shift(@current); }<br />            print FH $log."\n";<br />            close FH;<br />            last;<br />        }else{<br />            print 'failed: '.$count."\n";<br />            $count++;<br />            sleep 15;<br />            next;<br />        }<br />    }<br />    print 'success: '.$timer."\n";<br />    $timer++;<br />    $p-&gt;close();<br />    sleep 60;<br />}<br />其中interface文件是用‘netsh -c interface ip dump &gt; c:/interface’命令导出的网卡信息，稍作修改即可使用。<br /><img src ="http://www.cnitblog.com/gyn/aggbug/30422.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2007-07-22 21:14 <a href="http://www.cnitblog.com/gyn/archive/2007/07/22/30422.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>单服务器升级至双机热备实例</title><link>http://www.cnitblog.com/gyn/archive/2007/07/08/29633.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sun, 08 Jul 2007 14:35:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2007/07/08/29633.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/29633.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2007/07/08/29633.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/29633.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/29633.html</trackback:ping><description><![CDATA[
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<span style="font-family: Arial;" lang="EN-US">摘要：介绍了基于网络的双机热备系统的设计思路以及通过在主备服务器中配置脚本程序，将阅文站服务器升级至双机热备系统的过程。<br />关键词：双机热备，心跳监测，同步<br />Abstract: This
paper introduce a design method of network based hot standby service system as
well as the procedure of upgrade from single document server to hot standby
service system by configuring script files on both active and standby server.<o:p></o:p><br />Keyword: hot
standby service, heartbeat monitor, synchronization</span>
		</p>
		<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
				<span style="font-family: Arial;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
				<br />
				<b style="">
						<span lang="EN-US">1</span>
				</b>
				<b style="">
						<span style="font-family: 宋体;">：升级方案建立的起因</span>
						<span lang="EN-US">
								<o:p>
								</o:p>
						</span>
				</b>
				<br />
				<span style="font-family: 宋体;">    目前的内部文件阅读服务器由于使用年代较长，经常出现无响应甚至死机的状况。由于内部文件阅读服务器是我台员工了解并获取规章制度内容和近期活动消息的一个重要途径，所以该服务器的稳定性显得十分重要。就目前的管理情况而言，除了定期的检查之外，一般都是通过阅读者的通报获知服务器处于无响应的状况，很难做到及时发现并处理。</span>
				<span style="font-family: 宋体;">
						<o:p>
						</o:p>
						<br />    单纯通过增加检查次数的方法很难收到理想的效果。一来即便达到一天十次检查的量，对于服务器而言仍然是小时单位的数量级，而且只能在工作时间内施行；再者，由于没有配套的同步备份方案，即便发现服务器崩溃，恢复重建所消耗的时间依然相当可观。长时间停机导致信息传达的滞后将直接影响到工作的开展，试想如果这种情况发生在关键性服务器如新闻文稿系统上的话，将造成不可估量的严重后果。</span>
				<span style="font-family: Arial;" lang="EN-US">
						<o:p> </o:p>
				</span>
				<br />
				<b style="">
						<span lang="EN-US">
								<br />2</span>
				</b>
				<b style="">
						<span style="font-family: 宋体;">：网络双机热备的思路</span>
						<span lang="EN-US">
								<o:p>
								</o:p>
						</span>
				</b>
				<br />
				<span style="font-family: 宋体;">    所要采用的采用双机热备的办法，双机热备即是目前通常所说的</span>
				<span style="font-family: Arial;" lang="EN-US">active/standby</span>
				<span style="font-family: 宋体;">方式。当</span>
				<span style="font-family: Arial;" lang="EN-US"> active</span>
				<span style="font-family: 宋体;">服务器出现故障的时候，通过软件诊测（一般是通过心跳诊断）将</span>
				<span style="font-family: Arial;" lang="EN-US">standby</span>
				<span style="font-family: 宋体;">机器激活，保证应用在短时间内完全恢复正常使用。数据同步的方案一般在设计双机热备服务器时制定，比方说使用共享存储器就是个简单方便的解决办法；更加有效的方法的是在编写配置与数据输入输出相关的服务程序，使数据同时写入主备服务器中相关的数据库。但是在本文的例子中，由于文件服务器在建立时是一个单机的形式，所以其中的程序和数据都是针对单机进行设置的，如果要实现传统意义上双机热备的数据同步，需要对原先的网站程序和存储方案做较大的的修改，除开</span>
				<span style="font-family: Arial;" lang="EN-US">standby</span>
				<span style="font-family: 宋体;">服务器的费用外，可能还需要另外购买存储设备。在对源文件无法进行修改或者预算有限的情况下，这种代价的方案并不是完全适用。</span>
				<span style="font-family: 宋体;">
						<o:p>
						</o:p>
						<br />    为了克服以上的几个局限，我们利用网络代替传统心跳线和传输数据来同步数据，不但利用了现有的网络资源来节省投入的成本，而且在必要的情况下通过建立在不同端口的监听，</span>
				<span style="font-family: Arial;" lang="EN-US">standby</span>
				<span style="font-family: 宋体;">服务器甚至可以同时为多台提供不同服务的</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器的备用支持。</span>
				<span style="font-family: 宋体;">
						<o:p>
						</o:p>
						<br />    首先，</span>
				<span style="font-family: Arial;" lang="EN-US">standby</span>
				<span style="font-family: 宋体;">服务器中配置监控脚本，通过循环心跳检测</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器的生存状况，达到实时监控的目的，一旦发现出错状况就可以采取预先设置的方案及时接管</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器服务。通常情况下，一旦服务器崩溃，其网络功能也将停止，所以可以简单采用</span>
				<span style="font-family: Arial;" lang="EN-US">ping</span>
				<span style="font-family: 宋体;">的方法监测</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器；但是更一般的情况是，服务器仅仅是停止了某一项服务，比如网站发布服务或者数据库服务，而其网络联通性依然良好，针对这种情况需要在主备双方配置监测特定服务的信息问答脚本。比方说，</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器提供数据库查询服务，在每次接受到来自备服务器的心跳包后，</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器执行一次特定的</span>
				<span style="font-family: Arial;" lang="EN-US">sql</span>
				<span style="font-family: 宋体;">查询并将查询所得与预期的比对，得到的结果以布尔值的形式传回备服务器，最后由备服务器决定是否接管服务。</span>
				<span style="font-family: 宋体;">
						<o:p>
						</o:p>
						<br />    接着，为了保持</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器与</span>
				<span style="font-family: Arial;" lang="EN-US">standby</span>
				<span style="font-family: 宋体;">服务器中数据的一致性，需要将</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器上的文件或数据库的更新内容，定期地通过一定的途径同步到备服务器中。对于数据处理不是很频繁的服务器，可以采用定期备份的方式。如需要采取更加细致的做法的话，在被服务器定期备份的基础上，</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器可以配置数据更新检测脚本，一旦检测到有数据更新就将其写入同步数据包中，当数据包达到规定大小之后就可用它来同步</span>
				<span style="font-family: Arial;" lang="EN-US">standby</span>
				<span style="font-family: 宋体;">服务器。同步数据包大小的设置视乎网络的通畅度和服务器的响应能力而定。在两个条件都较好的情况下，可以采用小数据包的形式，这样做的好处在于主备的同步度较高，</span>
				<span style="font-family: Arial;" lang="EN-US">standby</span>
				<span style="font-family: 宋体;">服务器在接管服务时可以最大限度再现</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器内容。而在有任何一个条件或者两者都不理想的情况下，需要适当增加数据包的大小。另外，由于目前一些大型的数据库普遍支持触发器，在同步这类数据库时我们可以利用触发器来保存添加、删除和更新的记录操作，很大程度上简化了同步的过程。</span>
				<span style="font-family: Arial;" lang="EN-US">
						<o:p> </o:p>
				</span>
				<br />
				<b style="">
						<span lang="EN-US">
								<br />3</span>
				</b>
				<b style="">
						<span style="font-family: 宋体;">：具体的升级方案和主要脚本分析</span>
						<span lang="EN-US">
								<o:p>
								</o:p>
								<br />3.1</span>
				</b>
				<b style="">
						<span style="font-family: 宋体;">：心跳监视</span>
						<span lang="EN-US">
								<o:p>
								</o:p>
						</span>
				</b>
				<span style="font-family: 宋体;">
						<br />    现在将以上的思路与具体的情况想结合，实现文稿阅读服务器升级至双机热备。由于该服务器提供的是</span>
				<span style="font-family: Arial;" lang="EN-US">http</span>
				<span style="font-family: 宋体;">的网页浏览服务，所以在</span>
				<span style="font-family: Arial;" lang="EN-US">standby</span>
				<span style="font-family: 宋体;">服务器中只需要配置定期具备获取网页功能的脚本即可以达到判断</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器服务是否正常，换一句换说在做这个特例中</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器的心跳应答是建立在已有的</span>
				<span style="font-family: Arial;" lang="EN-US">http</span>
				<span style="font-family: 宋体;">服务上的，无需再建立额外的监听端口。</span>
				<span style="font-family: 宋体;">
						<o:p>
						</o:p>
						<br />    以下是备份服务器上的心跳脚本</span>
				<span style="font-family: Arial;" lang="EN-US">heartbeat.bat</span>
				<span style="font-family: 宋体;">，心跳间隔为一个小时即</span>
				<span style="font-family: Arial;" lang="EN-US">3600</span>
				<span style="font-family: 宋体;">秒，</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器域名是</span>
				<span style="font-family: Arial;" lang="EN-US">file.zsgd.com</span>
				<span style="font-family: 宋体;">，</span>
				<span style="font-family: Arial;" lang="EN-US">standby</span>
				<span style="font-family: 宋体;">服务器为</span>
				<span style="font-family: Arial;" lang="EN-US">file-bak.zsgd.com</span>
				<span style="font-family: 宋体;">。</span>
				<span style="font-family: 宋体;">脚本首先建立了一个无穷循环，在该循环中每隔一个小时使用</span>
				<span style="font-family: Arial;" lang="EN-US">LWP::Simple<a style="" href="#_ftn1" name="_ftnref1" title=""><span class="MsoFootnoteReference"><span style=""><!--[if !supportFootnotes]--><span class="MsoFootnoteReference"><span style="font-size: 10.5pt; font-family: Arial;" lang="EN-US">[1]</span></span><!--[endif]--></span></span></a></span>
				<span style="font-family: 宋体;">下载一次阅文站首页的内容，如果下载失败则表示</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器</span>
				<span style="font-family: Arial;" lang="EN-US">http</span>
				<span style="font-family: 宋体;">服务失效，</span>
				<span style="font-family: Arial;" lang="EN-US">standby</span>
				<span style="font-family: 宋体;">服务器将调用</span>
				<span style="font-family: Arial;" lang="EN-US">iisweb.vbs<a style="" href="#_ftn2" name="_ftnref2" title=""><span class="MsoFootnoteReference"><span style=""><!--[if !supportFootnotes]--><span class="MsoFootnoteReference"><span style="font-size: 10.5pt; font-family: Arial;" lang="EN-US">[2]</span></span><!--[endif]--></span></span></a></span>
				<span style="font-family: 宋体;">来启动本身的</span>
				<span style="font-family: Arial;" lang="EN-US">iis</span>
				<span style="font-family: 宋体;">网站服务。然后用</span>
				<span style="font-family: Arial;" lang="EN-US">Net::FTP<a style="" href="#_ftn3" name="_ftnref3" title=""><span class="MsoFootnoteReference"><span style=""><!--[if !supportFootnotes]--><span class="MsoFootnoteReference"><span style="font-size: 10.5pt; font-family: Arial;" lang="EN-US">[3]</span></span><!--[endif]--></span></span></a></span>
				<span style="font-family: 宋体;">登录</span>
				<span style="font-family: Arial;" lang="EN-US">zsgd.com</span>
				<span style="font-family: 宋体;">主站的</span>
				<span style="font-family: Arial;" lang="EN-US">ftp</span>
				<span style="font-family: 宋体;">下载默认页面文件</span>
				<span style="font-family: Arial;" lang="EN-US">default.asp</span>
				<span style="font-family: 宋体;">，修改其中的阅文站链接，使其指向</span>
				<span style="font-family: Arial;" lang="EN-US">standby</span>
				<span style="font-family: 宋体;">服务器。最后将修改完的</span>
				<span style="font-family: Arial;" lang="EN-US">default.asp</span>
				<span style="font-family: 宋体;">上传至</span>
				<span style="font-family: Arial;" lang="EN-US">zsgd.com</span>
				<span style="font-family: 宋体;">主站</span>
				<span style="font-family: Arial;" lang="EN-US">ftp</span>
				<span style="font-family: 宋体;">，覆盖原先的文件。</span>
				<span style="font-family: Arial;" lang="EN-US">
						<o:p>
						</o:p>
						<br />
				</span>
		</p>
		<div style="border: 1pt solid windowtext; padding: 1pt 4pt;">
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">@rem = '--*-Perl-*--<br />@echo off<o:p></o:p><br />if
"%OS%" == "Windows_NT" goto WinNT<o:p></o:p><br />perl -x -S
"%0" %1 %2 %3 %4 %5 %6 %7 %8 %9<o:p></o:p><br />goto endofperl<o:p></o:p><br />:WinNT<o:p></o:p><br />perl -x -S %0
%*<o:p></o:p><br />if NOT
"%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto
endofperl<o:p></o:p><br />if
%errorlevel% == 9009 echo You do not have Perl in your PATH.<o:p></o:p><br />if errorlevel
1 goto script_failed_so_exit_with_non_zero_val 2&gt;nul<o:p></o:p><br />goto endofperl<o:p></o:p><br />@rem ';<o:p></o:p><br />#!perl<o:p></o:p><br />#line 15<o:p></o:p><br />use strict;<o:p></o:p><br />use
LWP::Simple;<o:p></o:p><br />use Net::FTP;<o:p></o:p><br />my ( $none,
$interval, $ftp, $backserver) = ( '', 3600, undef, 'file-bak.zsgd' );<o:p></o:p><br />my $content =
$none;<o:p></o:p><br />whil(1){<o:p></o:p><br /><span style="">       </span>$content = get 'http://file.zsgd.com/';<o:p></o:p><br /><span style="">       </span>if( $content eq $none ){ <o:p></o:p><br /><span style="">              </span>system ' cscript.exe %systemroot%/system32/iisweb.vbs
/stop file ';<o:p></o:p><br /><span style="">              </span>system 'cscript.exe %systemroot%/system32/iisweb.vbs
/start file'; <o:p></o:p><br /><span style="">              </span>$ftp = Net::FTP-&gt;new('file.zsgd.com',
debug =&gt; 0);<o:p></o:p><br /><span style="">              </span>$ftp-&gt;login('***', '***');<o:p></o:p><br /><span style="">              </span>$ftp-&gt;get('default.asp');<o:p></o:p><br /><span style="">              </span>open FL, '&lt;default.asp';<o:p></o:p><br /><span style="">              </span>open TMP, '&gt;tmp';<o:p></o:p><br /><span style="">              </span>while(&lt;FL&gt;){<o:p></o:p><br /><span style="">                     </span>chomp;<o:p></o:p><br /><span style="">                     </span>$_ =~
s/(.*)(file\.zsgd)(.*)/$1$backserver$3/;<o:p></o:p><br /><span style="">                     </span>print TMP
$_."\n";<o:p></o:p><br /><span style="">              </span>}<o:p></o:p><br /><span style="">              </span>close FL; close TMP;<o:p></o:p><br /><span style="">       </span><span style="">       </span>system
'<st1:place w:st="on"><st1:state w:st="on">del</st1:state></st1:place>
default.asp'; <o:p></o:p><br /><span style="">              </span>system 'ren tmp default.asp';<o:p></o:p><br /><span style="">              </span>$ftp-&gt;put('default.asp');<o:p></o:p><br /><span style="">              </span>$ftp-&gt;quit;<o:p></o:p><br /><span style="">       </span>}<o:p></o:p><br /><span style="">       </span>$content = $none;<o:p></o:p><br /><span style="">       </span>sleep $interval;<o:p></o:p><br />}<o:p></o:p><br />__END__<o:p></o:p><br />:endofperl<o:p></o:p></span>
				</p>
		</div>
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<span style="font-family: Arial;" lang="EN-US">heatbeat.bat<o:p></o:p></span>
				<br />
				<b style="">
						<span lang="EN-US">
								<br />3.2</span>
				</b>
				<b style="">
						<span style="font-family: 宋体;">：数据同步</span>
				</b>
				<b style="">
						<span style="font-size: 15pt; font-family: Arial;" lang="EN-US">
								<o:p>
								</o:p>
						</span>
				</b>
				<br />
				<span style="font-family: 宋体;">    在数据同步方面，</span>
				<span style="font-family: Arial;" lang="EN-US">msdn</span>
				<span style="font-family: 宋体;">上有关于基于</span>
				<span style="font-family: Arial;" lang="EN-US">Microsoft Jet</span>
				<span style="font-family: 宋体;">数据库引擎的</span>
				<span style="font-family: Arial;" lang="EN-US">internet</span>
				<span style="font-family: 宋体;">数据库同步的具体方案</span>
				<a style="" href="#_ftn4" name="_ftnref4" title="">
						<span class="MsoFootnoteReference">
								<span style="font-family: Arial;" lang="EN-US">
										<span style="">
												<!--[if !supportFootnotes]-->
												<span class="MsoFootnoteReference">
														<span style="font-size: 10.5pt; font-family: Arial;" lang="EN-US">[4]</span>
												</span>
												<!--[endif]-->
										</span>
								</span>
						</span>
				</a>
				<span style="font-family: 宋体;">，为</span>
				<span style="font-family: Arial;" lang="EN-US">windows</span>
				<span style="font-family: 宋体;">平台上大型数据库的同步提供了良好的途径。但是这次升级的服务器中配置的是小型</span>
				<span style="font-family: Arial;" lang="EN-US">access</span>
				<span style="font-family: 宋体;">数据库，而且数据上载周期较长，没有</span>
				<span style="font-family: 宋体;">频繁的删除或者更新数据项的操作。所以出于实验和学习的目的，这里通过配置脚本来实现</span>
				<span style="font-family: Arial;" lang="EN-US">internet</span>
				<span style="font-family: 宋体;">中</span>
				<span style="font-family: Arial;" lang="EN-US">access</span>
				<span style="font-family: 宋体;">数据库的监视与同步。</span>
				<span style="font-family: 宋体;">
						<o:p>
						</o:p>
						<br />    在</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器中配置</span>
				<span style="font-family: Arial;" lang="EN-US">datamonitor.bat</span>
				<span style="font-family: 宋体;">脚本监视数据库的更新情况。首先读入参数文件中的数据，该参数文件保存的是前一次脚本运行得到的</span>
				<span style="font-family: Arial;" lang="EN-US">access</span>
				<span style="font-family: 宋体;">数据库文件的最后修改时间和其中各个表的数据项</span>
				<span style="font-family: Arial;" lang="EN-US">id</span>
				<span style="font-family: 宋体;">值，通过比对本次脚本运行得到的数据库修改时间与最后一次修改时间即可判断数据库在两次脚本运行期间是否更新了数据。一旦发现有更新，脚本将使用</span>
				<span style="font-family: Arial;" lang="EN-US">DBD::ADO</span>
				<span style="font-family: 宋体;">连接</span>
				<span style="font-family: Arial;" lang="EN-US">access</span>
				<span style="font-family: 宋体;">数据库。首先，检测各个表中</span>
				<span style="font-family: Arial;" lang="EN-US">id</span>
				<span style="font-family: 宋体;">值大于前次查询最大</span>
				<span style="font-family: Arial;" lang="EN-US">id</span>
				<span style="font-family: 宋体;">的数据记录，这些就是新插入的数据项；再在小于最大</span>
				<span style="font-family: Arial;" lang="EN-US">id</span>
				<span style="font-family: 宋体;">值的</span>
				<span style="font-family: Arial;" lang="EN-US">id</span>
				<span style="font-family: 宋体;">中检索不重复项，这些是被删除的数据项；接着用本次查询所得的</span>
				<span style="font-family: Arial;" lang="EN-US">id</span>
				<span style="font-family: 宋体;">值替换参数文件中的内容；然后将这些数据分类打包，发送给</span>
				<span style="font-family: Arial;" lang="EN-US">standby</span>
				<span style="font-family: 宋体;">服务器的数据同步监听脚本；最后做一些必要清理和初始化工作，一个小时后进入下一个周期重复以上的步骤。</span>
				<span style="font-family: 宋体;">
						<o:p>
						</o:p>
						<br />    这么做的一个缺点是很难检测更新数据项，虽然理论上可以通过在表中设置更新标记的方法提示数据更新，但是更新标记的修改程序必须嵌入网站程序，这在加密的网页程序中将很难实现，退一步讲如果服务是基于编译了的程序的话，这样的修改将无从谈起。所以从某种程度上表明，重要的数据服务不要建立在文本型的数据库上，对于有同步要求的服务，选择的数据库至少要支持触发过程。在资金允许的前提下，最好使用如</span>
				<span style="font-family: Arial;" lang="EN-US">oracle</span>
				<span style="font-family: 宋体;">、</span>
				<span style="font-family: Arial;" lang="EN-US">db2</span>
				<span style="font-family: 宋体;">等大型的数据库系统，另外免费的</span>
				<span style="font-family: Arial;" lang="EN-US">mysql</span>
				<span style="font-family: 宋体;">和</span>
				<span style="font-family: Arial;" lang="EN-US">postgresql</span>
				<span style="font-family: 宋体;">也是不错的选择。</span>
				<span style="font-family: Arial;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<div style="border: 1pt solid windowtext; padding: 1pt 4pt;">
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">@rem =
'--*-Perl-*--<o:p></o:p><br />@echo off<o:p></o:p><br />if
"%OS%" == "Windows_NT" goto WinNT<o:p></o:p><br />perl -x -S
"%0" %1 %2 %3 %4 %5 %6 %7 %8 %9<o:p></o:p><br />goto endofperl<o:p></o:p><br />:WinNT<o:p></o:p><br />perl -x -S %0
%*<o:p></o:p><br />if NOT
"%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto
endofperl<o:p></o:p><br />if
%errorlevel% == 9009 echo You do not have Perl in your PATH.<o:p></o:p><br />if errorlevel
1 goto script_failed_so_exit_with_non_zero_val 2&gt;nul<o:p></o:p><br />goto endofperl<o:p></o:p><br />@rem ';<o:p></o:p><br />#!perl<o:p></o:p><br />#line 15<o:p></o:p><br />use strict;<o:p></o:p><br />use DBI;<o:p></o:p><br />my ($PF_INET,
$port, $SOCK_DGRAM) = (2, 2345, 2);<o:p></o:p><br />my
$remote_addr = pack('SnC4x8',$PF_INET,$port,192,168,138,228);<o:p></o:p><br />socket(CLIENT,
$PF_INET, $SOCK_DGRAM, getprotobyname('udp'));<o:p></o:p><br />my $dsn =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/temp/tp.mdb";<o:p></o:p><br />my $dbh =
DBI-&gt;connect("dbi:<st1:place w:st="on"><st1:city w:st="on">ADO</st1:city></st1:place>:$dsn",
{PrintError =&gt; 0});<o:p></o:p><br />my $tbh =
$dbh-&gt;prepare('select * from pic'); $tbh-&gt;execute;<o:p></o:p><br />my (@data,
@tbl_id, $max_tbl_id, $del_id, @insert_data_ref)= undef;<o:p></o:p><br />my ($index,
$id, $index_id) = (0, undef, undef);<o:p></o:p><br />while(1){<o:p></o:p><br /><span style="">       </span>open TBL, '&lt;tbl-pic';<o:p></o:p><br /><span style="">       </span>open TMP, '&gt;tmp';<o:p></o:p><br /><span style="">       </span>while(&lt;TBL&gt;){ chomp; push @tbl_id,
$_ ; } #$_ = atoi $_;<o:p></o:p><br /><span style="">       </span>close TBL;<o:p></o:p><br /><span style="">       </span>$max_tbl_id = $tbl_id[(scalar @tbl_id) -
1]; <o:p></o:p><br /><span style="">       </span>while(@data = $tbh-&gt;fetchrow_array()){<o:p></o:p><br /><span style="">              </span>$id = $data[0];<o:p></o:p><br /><span style="">              </span>print TMP $id."\n"<o:p></o:p><br /><span style="">              </span>if($id &lt;= $max_tbl_id){<o:p></o:p><br /><span style="">                     </span>$index_id =
$tbl_id[$index];<o:p></o:p><br /><span style="">                     </span>while($index_id &lt;= $id){
<o:p></o:p><br /><span style="">                            </span>if($index_id &lt;
$id){ $del_id .= $index_id."\t"; } <o:p></o:p><br /><span style="">                            </span>$index += 1;<o:p></o:p><br /><span style="">                            </span>if($index &lt;
(scalar @tbl_id)){ $index_id = $tbl_id[$index]; }else{ last; } <o:p></o:p><br /><span style="">                     </span>}<o:p></o:p><br /><span style="">              </span>}else{ my @temp = @data; push
@insert_data_ref, \@temp; }<o:p></o:p><br /><span style="">       </span>}<o:p></o:p><br /><span style="">       </span>close TMP; system '<st1:place w:st="on"><st1:state w:st="on">del</st1:state></st1:place> tbl-pic'; system 'ren tmp tbl-pic';<o:p></o:p><br /><span style="">       </span>undef $tbh;<o:p></o:p><br /><span style="">       </span>$dbh-&gt;disconnect;<o:p></o:p><br /><span style="">       </span>send(CLIENT,$del_id, 0, $remote_addr);<o:p></o:p><br /><span style="">       </span>foreach my $ref(@insert_data_ref){<o:p></o:p><br /><span style="">              </span>my $item = undef;<o:p></o:p><br /><span style="">              </span>foreach my $field(@$ref){ $item .=
$field."\t"; }<o:p></o:p><br /><span style="">              </span>send(UDP_CLIENT, $item, 0,
$remote_addr);<o:p></o:p><br /><span style="">       </span>}<o:p></o:p><br /><span style="">       </span>close CLIENT;<span style="">      </span><o:p></o:p><br /><span style="">       </span>sleep 3600;<o:p></o:p><br /><span style="">       </span>$dbh = DBI-&gt;connect("dbi:<st1:place w:st="on"><st1:city w:st="on">ADO</st1:city></st1:place>:$dsn", {PrintError
=&gt; 0});<o:p></o:p><br /><span style="">       </span>socket(CLIENT, $PF_INET, $SOCK_DGRAM,
getprotobyname('udp'));<o:p></o:p><br /><span style="">       </span>(@tbl_id, $del_id, @insert_data_ref) =
undef;<o:p></o:p><br /><span style="">       </span>$index = 0;<o:p></o:p><br />} <o:p></o:p><br />__END__<o:p></o:p><br />:endofperl<o:p></o:p></span>
				</p>
		</div>
		<p class="MsoNormal" style="text-indent: 17.95pt;">
				<span style="font-family: Arial;" lang="EN-US">tbl-pic-monitor.bat</span>
				<span style="font-family: 宋体;">
						<o:p>
						</o:p>
						<br />    在</span>
				<span style="font-family: Arial;" lang="EN-US">standby</span>
				<span style="font-family: 宋体;">服务器中配置监听服务脚本用以检查并接受来自</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器更新数据包，接着将接收到的数据中的前缀指示判断该数据是用于插入或是删除，从而再运行下一步的数据库更新操作，最后脚本进入下一个周期的等待监听。</span>
				<span style="font-family: Arial;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<div style="border: 1pt solid windowtext; padding: 1pt 4pt;">
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">@rem =
'--*-Perl-*--<o:p></o:p><br />@echo off<o:p></o:p><br />if
"%OS%" == "Windows_NT" goto WinNT<o:p></o:p><br />perl -x -S
"%0" %1 %2 %3 %4 %5 %6 %7 %8 %9<o:p></o:p><br />goto endofperl<o:p></o:p><br />:WinNT<o:p></o:p><br />perl -x -S %0
%*<o:p></o:p><br />if NOT
"%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto
endofperl<o:p></o:p><br />if
%errorlevel% == 9009 echo You do not have Perl in your PATH.<o:p></o:p><br />if errorlevel
1 goto script_failed_so_exit_with_non_zero_val 2&gt;nul<o:p></o:p><br />goto endofperl<o:p></o:p><br />@rem ';<o:p></o:p><br />#!perl<o:p></o:p><br />#line 15<o:p></o:p><br />my $dsn =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/temp/tp.mdb";<o:p></o:p><br />my ($dbh, $sign,
$content) = undef;<o:p></o:p><br />my ($buffer,
$PF_INET, $port, $SOCK_DGRAM) = (undef, 2, 2345, 2);<o:p></o:p><br />my $local_addr
= pack('SnC4x8', $PF_INET, $port, 192, 168, 138, 228);<o:p></o:p><br />socket(SERVER,
$PF_INET,$SOCK_DGRAM, getprotobyname('udp')) or die("$!");<o:p></o:p><br />bind(SERVER,
$local_addr) or die("$!");<o:p></o:p><br />listen(SERVER,
10);<o:p></o:p><br />while(1){<o:p></o:p><br /><span style="">       </span>next unless my $remote_addr =
recv(UDP_SERVER, $buffer, 100, 0);<o:p></o:p><br /><span style="">       </span>chomp($buffer);<o:p></o:p><br /><span style="">       </span>$dbh = DBI-&gt;connect("dbi:<st1:place w:st="on"><st1:city w:st="on">ADO</st1:city></st1:place>:$dsn",
{PrintError =&gt; 0});<o:p></o:p><span style=""><br />      </span>($sign, $content) = split $buffer, '::';<o:p></o:p><br /><span style="">       </span>if($sign eq 'add'){<o:p></o:p><br /><span style="">              </span>$dbh-&gt;prepare('insert into pic
values(?, ?, ?, ?)');<o:p></o:p><br /><span style="">              </span>$dbh-&gt;execute(split($content,
"\t"));<o:p></o:p><br /><span style="">              </span>$dbh-&gt;close;<o:p></o:p><br /><span style="">       </span>}else{<o:p></o:p><br /><span style="">              </span>$dbh-&gt;prepare('delete from pic
where id = ?');<o:p></o:p><br /><span style="">              </span>foreach my $tmp(split $content,
"\t"){ $dbh-&gt;execute($tmp); }<o:p></o:p><br /><span style="">       </span>}<o:p></o:p><br /><span style="">       </span>$dbh-&gt;disconnect;<o:p></o:p><br />}<o:p></o:p><br />__END__<o:p></o:p><br />:endofperl<o:p></o:p></span>
				</p>
		</div>
		<p class="MsoNormal">
				<span style="font-family: Arial;" lang="EN-US">tbl-pic-recv.bat<o:p> </o:p></span>
				<br />
				<b style="">
						<span lang="EN-US">
								<br />4</span>
				</b>
				<b style="">
						<span style="font-family: 宋体;">：建立跟踪系统运行的系统日志</span>
						<span lang="EN-US">
								<o:p>
								</o:p>
						</span>
				</b>
				<br />
				<span style="font-family: 宋体;">    一个成功服务器解决方案的产生，需要后期长时间的跟踪和调试。在建立了基于网络的双机热备系统之后，为了方便了解和检查</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">和</span>
				<span style="font-family: Arial;" lang="EN-US">standby</span>
				<span style="font-family: 宋体;">服务器中各个脚本的执行情况，需要对脚本做一些修改，使其能将执行的结果输出并存储到特定的日志文件中，方便管理员通过查询其中的内容来判断系统运行的状况。对于严重的状况，比如</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器</span>
				<span style="font-family: Arial;" lang="EN-US">down</span>
				<span style="font-family: 宋体;">机，系统还可以通过发送</span>
				<span style="font-family: Arial;" lang="EN-US">email</span>
				<span style="font-family: 宋体;">的方式通知管理员，使其能及时做出反应。</span>
				<span style="font-family: 宋体;">
						<o:p>
						</o:p>
						<br />    在心跳监测脚本中，当每个周期运行结束后，向日志文件里记录本周期检测启动成功与否、启动或失效的日期时间和检测得到的</span>
				<span style="font-family: Arial;" lang="EN-US">http</span>
				<span style="font-family: 宋体;">反馈内容，如果没有得到内容则发送</span>
				<span style="font-family: Arial;" lang="EN-US">email</span>
				<span style="font-family: 宋体;">给指定的管理员。对于数据同步，</span>
				<span style="font-family: Arial;" lang="EN-US">active</span>
				<span style="font-family: 宋体;">服务器中的脚本将记录本周期同步启动日期时间、数据库文件的最后一次修改时间、数据库连接状况、</span>
				<span style="font-family: Arial;" lang="EN-US">socket</span>
				<span style="font-family: 宋体;">客户端启动成功与否及数据发送的结果；</span>
				<span style="font-family: Arial;" lang="EN-US">standby</span>
				<span style="font-family: 宋体;">服务器中的脚本将记录收到更新数据的时间和所有的数据内容以便于在必要的时候恢复数据库。</span>
				<span style="font-family: Arial;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
				<br />
		</p>
		<p class="MsoNormal">
				<b style="">
						<span style="font-family: 宋体;">参考文献：</span>
				</b>
		</p>
		<p class="MsoNormal">
				<b style="">
						<span style="font-family: 宋体;">
						</span>
				</b>
				<b style="">
						<span style="font-family: Arial;" lang="EN-US">
								<o:p>
								</o:p>
						</span>
				</b>
				<span style="font-size: 10.5pt; font-family: Arial;" lang="EN-US">[1] perldoc5.8.8 <a href="http://www.perl.org/">www.perl.org</a>
2006</span>
				<!--[if !supportFootnotes]-->
				<br clear="all" />
		</p>
		<div style="">
				<hr align="left" size="1" width="33%" />
				<!--[endif]-->
				<div style="" id="ftn1">
						<p class="MsoFootnoteText">
								<a style="" href="#_ftnref1" name="_ftn1" title="">
										<span class="MsoFootnoteReference">
												<span lang="EN-US">
														<span style="">
																<!--[if !supportFootnotes]-->
																<span class="MsoFootnoteReference">
																		<span style="font-size: 9pt; font-family: &quot;Times New Roman&quot;;" lang="EN-US">[1]</span>
																</span>
																<!--[endif]-->
														</span>
												</span>
										</span>
								</a>
								<span lang="EN-US"> libwww-perl</span>
								<span style="font-family: 宋体;">的简称，提供一组用以访问</span>
								<span lang="EN-US">www</span>
								<span style="font-family: 宋体;">资源的简单而持久的</span>
								<span lang="EN-US">API</span>
								<span style="font-family: 宋体;">。</span>
								<br />
								<a style="" href="#_ftnref2" name="_ftn2" title="">
										<span class="MsoFootnoteReference">
												<span lang="EN-US">
														<span style="">
																<span class="MsoFootnoteReference">
																		<span style="font-size: 9pt; font-family: &quot;Times New Roman&quot;;" lang="EN-US">[2]</span>
																</span>
																<!--[endif]-->
														</span>
												</span>
										</span>
								</a>
								<span lang="EN-US"> IIS </span>
								<span style="font-family: 宋体;">网站管理脚本，在运行</span>
								<span lang="EN-US">IIS
6.0</span>
								<span style="font-family: 宋体;">的服务器上创建、删除、列出、启动、停止和暂停网站。</span>
								<br />
								<a style="" href="#_ftnref3" name="_ftn3" title="">
										<span class="MsoFootnoteReference">
												<span lang="EN-US">
														<span style="">
																<!--[if !supportFootnotes]-->
																<span class="MsoFootnoteReference">
																		<span style="font-size: 9pt; font-family: &quot;Times New Roman&quot;;" lang="EN-US">[3]</span>
																</span>
																<!--[endif]-->
														</span>
												</span>
										</span>
								</a>
								<span lang="EN-US">
								</span>
								<span style="font-family: 宋体;">一个提供</span>
								<span lang="EN-US"> ftp</span>
								<span style="font-family: 宋体;">客户端简单功能的类</span>
								<br />
								<a style="" href="#_ftnref4" name="_ftn4" title="">
										<span class="MsoFootnoteReference">
												<span lang="EN-US">
														<span style="">
																<!--[if !supportFootnotes]-->
																<span class="MsoFootnoteReference">
																		<span style="font-size: 9pt; font-family: &quot;Times New Roman&quot;;" lang="EN-US">[4]</span>
																</span>
																<!--[endif]-->
														</span>
												</span>
										</span>
								</a>
								<span lang="EN-US"> http://msdn2.microsoft.com/en-us/library/aa140025(office.10).aspx#intrjet4_distribrepset</span>
						</p>
				</div>
				<div style="" id="ftn2">
				</div>
				<div style="" id="ftn3">
				</div>
				<div style="" id="ftn4">
				</div>
		</div>
<img src ="http://www.cnitblog.com/gyn/aggbug/29633.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2007-07-08 22:35 <a href="http://www.cnitblog.com/gyn/archive/2007/07/08/29633.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>sqlite远程连接示例</title><link>http://www.cnitblog.com/gyn/archive/2006/12/14/20463.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Thu, 14 Dec 2006 01:29:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/12/14/20463.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/20463.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/12/14/20463.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/20463.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/20463.html</trackback:ping><description><![CDATA[
		<p class="MsoNormal" style="text-indent: 21pt;">
				<span style="font-family: Arial;" lang="EN-US">Sqlite</span>
				<span style="font-family: 宋体;">是一个只有几百</span>
				<span style="font-family: Arial;" lang="EN-US">k</span>
				<span style="font-family: 宋体;">大小的、</span>
				<span style="font-family: 宋体;">优秀的嵌入式数据库，本身不带有远程连接的功能。由于其身材小，速度快并且为众多的语言支持，所以在单机领域得到了广泛的使用。但由于天然不支持服务</span>
				<span style="font-family: Arial;" lang="EN-US">/</span>
				<span style="font-family: 宋体;">客户端的模式，使其在遇到小型规模数据库远程连接的情况时不得不借助于附加的编程。</span>
				<span style="font-family: Arial;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="text-indent: 21pt;">
				<span style="font-family: 宋体;">单位有三百多人，作为网管每天要接到很多电话，有很多一时无法想起对应的名字，翻查通讯录不仅速度慢而且容易漏过。为了方便起见，在</span>
				<span style="font-family: Arial;" lang="EN-US">sqlite</span>
				<span style="font-family: 宋体;">上建立了一个通讯录数据表，然后用</span>
				<span style="font-family: Arial;" lang="EN-US">perl</span>
				<span style="font-family: 宋体;">编写了一个查询脚本。效果不错的同时，也有同事希望可以得到一份拷贝。但是这时遇到一个问题，如何保证通讯录在不同人手中保持版本的统一性。最好的办法是建立一个服务</span>
				<span style="font-family: Arial;" lang="EN-US">/</span>
				<span style="font-family: 宋体;">客户端的模式，在本机上维护一个数据库，而其它人通过连接数据库得到相关的查询结果。</span>
				<span style="font-family: Arial;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="text-indent: 21pt;">
				<span style="font-family: 宋体;">具体的解决方法是这样的：在本机上维护一个到</span>
				<span style="font-family: Arial;" lang="EN-US">sqlite</span>
				<span style="font-family: 宋体;">数据库的连接并建立一个来自于客户端的</span>
				<span style="font-family: Arial;" lang="EN-US">socket</span>
				<span style="font-family: 宋体;">的监听，接受远程查询条件并回复查询结果；客户端志负责发送和接受这些最简单的任务。</span>
				<span style="font-family: Arial;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="text-indent: 21pt;">
				<span style="font-family: 宋体;">客户端的代码：</span>
				<span style="font-family: Arial;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<div style="border: 1pt solid windowtext; padding: 1pt 4pt;">
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">use strict;<br />my $in_buffer = undef;<br />my $PF_INET = 2;<br />my $port = 2345;<br />my $remote_addr =
pack('SnC4x8',$PF_INET,$port,192,168,138,228);<br />my $SOCK_DGRAM = 2;<br />socket(UDP_CLIENT, $PF_INET, $SOCK_DGRAM,
getprotobyname('udp'));<br />while(1){<br /><span style="">       </span>print("</span>
						<span style="font-family: 宋体;">输入名字或号码：</span>
						<span style="font-family: Arial;" lang="EN-US">");<br /><span style="">       </span>my
$out_buffer=&lt;STDIN&gt;;<br /><span style="">       </span>chomp($out_buffer);<br /><span style="">       </span>if($out_buffer
eq "exit"){last;}<br /><span style="">       </span>send(UDP_CLIENT,
$out_buffer, 0, $remote_addr);<br /><span style="">       </span>print("waiting
for reply...\n");<br /><span style="">       </span>recv(UDP_CLIENT,
$in_buffer, 100, 0);<br /><span style="">       </span>chomp($in_buffer);<br /><span style="">       </span>print("$in_buffer\n");<br />}<br />close(UDP_CLIENT);<o:p></o:p></span>
				</p>
		</div>
		<p class="MsoNormal">
				<span style="font-family: Arial;" lang="EN-US">
						<span style="">       </span>
				</span>
				<span style="font-family: 宋体;">服务器端的代码：</span>
				<span style="font-family: Arial;" lang="EN-US">
						<o:p>
						</o:p>
				</span>
		</p>
		<div style="border: 1pt solid windowtext; padding: 1pt 4pt;">
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">BEGIN{<br /><span style="">       </span>if( $^O
eq 'MSWin32' ){<br /><span style="">              </span>require
Win32::Console;<br /><span style="">              </span>Win32::Console::Free();<br /><span style="">       </span>}<br />}<o:p><br /></o:p></span>
				</p>
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">use strict;<br />use DBI;<o:p><br /></o:p></span>
				</p>
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">#database parameters<br />my $db_path = 'd:/src/cc/phones.db';<br />my $dbh =
DBI-&gt;connect("dbi:SQLite:$db_path", {PrintError =&gt; 0}) or die
$DBI::errstr;<br />my $sth = undef;<o:p><br /></o:p></span>
				</p>
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">#socket server parameters<br />my $in_buffer = undef;<br />my $out_buffer = undef;<br />my $PF_INET = 2;<br />my $port = 2345;<br />my $local_addr =
pack('SnC4x8',$PF_INET,$port,192,168,138,228);<br />my $SOCK_DGRAM = 2;<br />socket(UDP_SERVER, $PF_INET,$SOCK_DGRAM,
getprotobyname('udp')) or die("$!");<br />bind(UDP_SERVER, $local_addr) or die("$!");<br />listen(UDP_SERVER, 10);<o:p><br /></o:p></span>
				</p>
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">while(1){<br /><span style="">       </span>#receive
query then send result<br /><span style="">       </span>last
unless my $remote_addr = recv(UDP_SERVER,$in_buffer,100,0);<br /><span style="">       </span>chomp($in_buffer);<br /><span style="">       </span>if($in_buffer
=~ /^[0-9]{6}$/){<br /><span style="">              </span>$sth
= $dbh-&gt;prepare("select * from phones where number = $in_buffer");<br /><span style="">              </span>PROCEDURE:<br /><span style="">              </span>$sth-&gt;execute();<br /><span style="">              </span>my
@items = $sth-&gt;fetchrow_array();<br /><span style="">              </span>if(scalar(@items)){<br /><span style="">                     </span>$out_buffer
= $items[0].'</span>
						<span style="font-family: 宋体;">的虚拟网号码是</span>
						<span style="font-family: Arial;" lang="EN-US">'.$items[1];<br /><span style="">              </span>}else{<br /><span style="">                     </span>$out_buffer
= '</span>
						<span style="font-family: 宋体;">查无此人</span>
						<span style="font-family: Arial;" lang="EN-US">';<br /><span style="">              </span>}<br /><span style="">       </span>}else{<br /><span style="">              </span>$sth
= $dbh-&gt;prepare("select * from phones where name = '$in_buffer'");<br /><span style="">              </span>goto
PROCEDURE;<br /><span style="">       </span>}<span style=""><br />       </span>send(UDP_SERVER,$out_buffer,0,$remote_addr);<br />}<o:p><br /></o:p></span>
				</p>
				<p class="MsoNormal" style="border: medium none ; padding: 0cm;">
						<span style="font-family: Arial;" lang="EN-US">#disconnect from sqlite<br />$dbh-&gt;disconnect();<span style="">   </span><o:p></o:p></span>
				</p>
		</div>
<img src ="http://www.cnitblog.com/gyn/aggbug/20463.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-12-14 09:29 <a href="http://www.cnitblog.com/gyn/archive/2006/12/14/20463.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>分别用vbs、perl、python来操作文本</title><link>http://www.cnitblog.com/gyn/archive/2006/11/15/19177.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Wed, 15 Nov 2006 03:29:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/11/15/19177.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/19177.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/11/15/19177.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/19177.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/19177.html</trackback:ping><description><![CDATA[都是用来更新一个rm视频参数文件的。最早学asp的时候顺便用vbs写了一个，后来学perl就又写了一个，现在学python，凑活也写一个。基本上算是我的一个入门程序。用途就是用简单正则表达式找到时间段，再将它更新为当天日期，更新后的数据存在temp.txt中。perl最短，python比较干净。<br />vbs：<br />dim regOR<br />set regOR=new regexp<br />regOR.ignorecase=True<br />regOR.global=True<br />regOR.pattern="^([0-9]{1})$"<br />dim mytime<br />mytime=date<br />yy=year(mytime)<br />mm=month(mytime)<br />dd=day(mytime)<br />if regOR.test(mm) then<br />    mm=regOR.replace(mm,"0$1")<br />end if<br />if regOR.test(dd) then<br />    dd=regOR.replace(dd,"0$1")<br />end if<br />mytime=yy &amp; "-" &amp; mm &amp; "-" &amp; dd<br />dest=yy &amp; mm &amp; dd<br />pattern1="(.*[^0-9])([0-9]{4}\-[0-9]{2}\-[0-9]{2})([^0-9].*)"<br />pattern2="(.*[^0-9])([0-9]{8})([^0-9].*)"<br />regOR.pattern=pattern1<br />dim tempstring<br />Set fso = CreateObject("Scripting.FileSystemObject")<br />Set f = fso.CreateTextFile("d:\shixian\temp.txt", True)<br />Set fr = fso.GetFile("d:\shixian\shixian.rpjf")<br />Set ts = fr.OpenAsTextStream(1,-2)<br />do until ts.AtEndOfStream<br />    tempstring=ts.readline<br />    if regOR.test(tempstring) then<br />        tempstring=regOR.replace(tempstring,"$1" &amp; mytime &amp; "$3")<br />    end if<br />    regOR.pattern=pattern2<br />    if regOR.test(tempstring) then<br />        tempstring=regOR.replace(tempstring,"$1" &amp; dest &amp; "$3")<br />    end if<br />    regOR.pattern=pattern1<br />    f.writeline(tempstring)<br />loop<br />ts.close<br />f.close<br />set regOR=nothing<br />set ts=nothing<br />set fr=nothing<br />set f=nothing<br />set fso=nothing<br /><br />perl：<br />open(RPJF,"&lt;c:/code/10161.rpjf") or die("can not open because of $!\n");<br />open(TEMP,"&gt;c:/code/temp.txt") or die("can not open because of $!\n");<br />($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime();<br />$mday = sprintf("%.2d",$mday);<br />$mon = sprintf("%.2d",$mon+1);<br />$year += 1900;<br />$timerF = "$year"."$mon"."$mday";<br />$timerS = "$year"."-"."$mon"."-"."$mday";<br />while(&lt;RPJF&gt;){<br />    chomp();<br />    $_=~s/(.*)([0-9]{8})(.*)/$1$timerF$3/;<br />    $_=~s/(.*)([0-9]{4}\-[0-9]{2}\-[0-9]{2})(.*)/$1$timerS$3/;<br />    print TEMP "$_\n";<br />}<br />close(RPJF);<br />close(TEMP);<br /><br />python：<br />import re<br />import time<br /><br />pattern_name = re.compile(r'[0-9]{8}')<br />pattern_info = re.compile(r'[0-9]{4}\-[0-9]{2}\-[0-9]{2}')<br />(year, month, day) = time.localtime()[:3]<br />name = '%d%.2d%.2d' % (year, month, day)<br />info = '%d-%.2d-%.2d' % (year, month, day)<br /><br />try:<br />    file_rpjf = open('shixian.rpjf', 'r')<br />    file_temp = open('temp.txt', 'w')<br />    try:<br />        file_data = file_rpjf.readline()<br />        while file_data != '':<br />            file_data = pattern_name.sub(name, file_data)<br />            file_data = pattern_info.sub(info, file_data)<br />            file_temp.write(file_data)<br />            file_data = file_rpjf.readline()<br />    finally:<br />        file_rpjf.close()<br />        file_temp.close() <br />except IOError:<br />    pass<br /><img src ="http://www.cnitblog.com/gyn/aggbug/19177.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-11-15 11:29 <a href="http://www.cnitblog.com/gyn/archive/2006/11/15/19177.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>单位电脑信息采集程序</title><link>http://www.cnitblog.com/gyn/archive/2006/10/20/18199.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Fri, 20 Oct 2006 11:03:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/10/20/18199.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/18199.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/10/20/18199.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/18199.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/18199.html</trackback:ping><description><![CDATA[
		<img src="http://www.cnitblog.com/images/cnitblog_com/gyn/4001/r_gui.JPG" alt="r_gui.JPG" border="0" height="127" width="208" />
		<br />主要用于收集ip、mac、姓名、房间，后来又加入了维修记录的功能。服务器端接受数据并存入数据库中。<br />#############################<br />use strict;<br />use Tk;<br />use Encode;<br /><br />#SOCKE参数<br />my $PF_INET = 2;<br />my $port = 2345;<br />my $remote_addr = pack('SnC4x8',$PF_INET,$port,192,168,138,228);<br />my $SOCK_DGRAM = 2;<br /><br />#Frame<br />my ($label_room, $label_name, $label_ctrl, $label_notice);<br /><br />#确定、取消<br />my ($enter, $cancel);<br /><br />#房间、姓名变量<br />my ($room, $name);<br />$room = '';<br />$name = '';<br /><br />#主界面<br />my $mw = MainWindow-&gt;new(-title =&gt; hanzi('信息收集'));<br />$mw-&gt;minsize(qw/200 100/);<br />$mw-&gt;maxsize(qw/200 100/);<br /><br />#三个Frame<br />$label_room = $mw-&gt;Frame( qw/-borderwidth 2 -relief groove/ )-&gt;pack( qw/-side top -fill both/ );<br />$label_name = $mw-&gt;Frame( qw/-borderwidth 2 -relief groove/ )-&gt;pack( qw/-side top -fill both/ );<br />$label_ctrl = $mw-&gt;Frame( qw/-borderwidth 2 -relief groove/ )-&gt;pack( qw/-side top -fill both/ );<br /><br />#房间号码输入<br />$label_room-&gt;Label(-text =&gt; hanzi('房间号码'))-&gt;pack(qw/-side left -expand 1/);<br />$label_room-&gt;Entry(-textvariable =&gt; \$room, -relief =&gt; 'groove')-&gt;pack(qw/-side right -expand 1/);<br /><br />#姓名输入<br />$label_name-&gt;Label(-text =&gt; hanzi('姓名'))-&gt;pack(qw/-side left -expand 1/);<br />$label_name-&gt;Entry(-textvariable =&gt; \$name, -relief =&gt; 'groove')-&gt;pack(qw/-side right -expand 1/);<br /><br />#确定与重置<br />$enter = $label_ctrl-&gt;Button(-text =&gt; hanzi('确定'), -command =&gt; \&amp;enter)-&gt;pack(qw/-side left -expand 1/);<br />$cancel = $label_ctrl-&gt;Button(-text =&gt; hanzi('重置'), -command =&gt; \&amp;cancel)-&gt;pack(qw/-side right -expand 1/);<br /><br />#提示<br />$label_notice = $mw-&gt;Label(-text =&gt; hanzi('欢迎使用'), -relief =&gt; 'groove', -background =&gt; '#FFFF99')-&gt;pack(qw/-side bottom -fill x/);<br /><br />MainLoop();<br /><br />#汉字解码<br />sub hanzi{<br />    return decode('gb2312', shift);    <br />}<br /><br />#确定函数<br />sub    enter{<br />    chomp($room);<br />    chomp($name);<br />    $room =~ s/^\s+//;<br />    $name =~ s/^\s+//;<br />    if($room eq '' or $name eq ''){<br />        $label_notice-&gt;configure(-text =&gt; hanzi('输入不能为空')) ;<br />        return 0;<br />    }#if<br />    else{<br />        open(IPCF,'-|',"ipconfig -all");<br />        <br />        my ($mac_addr, $ip_addr, $out_buffer);<br />        while(&lt;IPCF&gt;){<br />            chomp;<br />            if($_ = ~s/(.*)(00(\-[0-9A-Z]{2}){5})(.*)/$2/){<br />                $mac_addr = join('', split(/-/,$_));<br />            }<br />            if($_ = ~/IP Address/){<br />                $_ = ~s/(.*)([0-9]{3}(\.[0-9]{1,3}){3})(.*)/$2/;<br />                $ip_addr = $_;<br />            }<br />        }#while<br />        $out_buffer = $room."\t".$mac_addr."\t".$ip_addr."\t".encode('utf8', $name);<br />        <br />        socket(UDP_CLIENT, $PF_INET, $SOCK_DGRAM, getprotobyname('udp'));<br />        send(UDP_CLIENT, $out_buffer, 0, $remote_addr);<br />        <br />        close(UDP_CLIENT);<br />        close(IPCF);<br />        $mw-&gt;destroy();<br />    }#else        <br />}<br /><br />#重置函数<br />sub cancel{<br />    $label_notice-&gt;configure(-text =&gt; hanzi('重置为空'));<br />    $room = '';<br />    $name = '';<br />}<br /><img src ="http://www.cnitblog.com/gyn/aggbug/18199.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-10-20 19:03 <a href="http://www.cnitblog.com/gyn/archive/2006/10/20/18199.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>源码：用perl写的基于postgresql的考勤数据管理软件</title><link>http://www.cnitblog.com/gyn/archive/2006/10/12/17888.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Thu, 12 Oct 2006 00:05:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/10/12/17888.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/17888.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/10/12/17888.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/17888.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/17888.html</trackback:ping><description><![CDATA[
		<br /> 
		已<url>停止下载（近一千行代码，用perl写真累.....，不比ms的，纯粹玩票）</url><br />大家看着玩玩儿吧～ ，单位在用了。:- ()<br />主要用到tk和dbi模块，因为喜欢用groove的效果（tk除了方便真没什么），所以比较土。有要学习tk，dbi的可以看看，蛮多代码都好重用的。<br /><img src ="http://www.cnitblog.com/gyn/aggbug/17888.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-10-12 08:05 <a href="http://www.cnitblog.com/gyn/archive/2006/10/12/17888.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>在perl中操作数据库中文字段</title><link>http://www.cnitblog.com/gyn/archive/2006/09/17/16955.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sun, 17 Sep 2006 02:48:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/09/17/16955.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/16955.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/09/17/16955.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/16955.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/16955.html</trackback:ping><description><![CDATA[
		<h3 style="MARGIN: 13pt 0cm; TEXT-ALIGN: center" align="center">
				<font size="5">
						<span style="FONT-FAMILY: 宋体">在</span>
						<span lang="EN-US">perl</span>
						<span style="FONT-FAMILY: 宋体">中操作数据库中文字段</span>
				</font>
		</h3>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US">Perl</span>
				<span style="FONT-FAMILY: 宋体">中对程序中的语句是按</span>
				<span lang="EN-US">utf8</span>
				<span style="FONT-FAMILY: 宋体">的编码处理的，对英文没有什么影响，但是中文就有一些麻烦。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 宋体">比方说，在</span>
				<span lang="EN-US">notepad</span>
				<span style="FONT-FAMILY: 宋体">中默认是以</span>
				<span lang="EN-US">gb2312</span>
				<span style="FONT-FAMILY: 宋体">的编码处理中文。如果在其中编写带有中文的</span>
				<span lang="EN-US">perl</span>
				<span style="FONT-FAMILY: 宋体">程序，一般都难令人如意。这种情况下，一般会拜托</span>
				<span lang="EN-US">encode</span>
				<span style="FONT-FAMILY: 宋体">模块，它是天然集成在</span>
				<span lang="EN-US">activeperl<?xml:namespace prefix = st1 /?><st1:chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899">5.8.8</st1:chsdate></span>
				<span style="FONT-FAMILY: 宋体">中的，如果没有找到可在</span>
				<span lang="EN-US">ppm</span>
				<span style="FONT-FAMILY: 宋体">中</span>
				<span lang="EN-US">search</span>
				<span style="FONT-FAMILY: 宋体">一下。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 宋体">通常只要用到一下两个函数即可：</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US" style="FONT-FAMILY: Wingdings">
						<span>l<span style="FONT: 7pt 'Times New Roman'; font-size-adjust: none; font-stretch: normal">           </span></span>
				</span>
				<span lang="EN-US">encode(‘</span>
				<span style="FONT-FAMILY: 宋体">编码</span>
				<span lang="EN-US">’, ‘</span>
				<span style="FONT-FAMILY: 宋体">文字</span>
				<span lang="EN-US">’)</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US" style="FONT-FAMILY: Wingdings">
						<span>l<span style="FONT: 7pt 'Times New Roman'; font-size-adjust: none; font-stretch: normal">           </span></span>
				</span>
				<span lang="EN-US">decode(‘</span>
				<span style="FONT-FAMILY: 宋体">编码</span>
				<span lang="EN-US">’, </span>
				<span style="FONT-FAMILY: 宋体">数据</span>
				<span lang="EN-US">)</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 宋体">其中</span>
				<span lang="EN-US">decode</span>
				<span style="FONT-FAMILY: 宋体">是将其中<span style="COLOR: red">编码</span>形式的<span style="COLOR: red">文字</span>转换为</span>
				<span lang="EN-US">utf8</span>
				<span style="FONT-FAMILY: 宋体">数据；而</span>
				<span lang="EN-US">encode</span>
				<span style="FONT-FAMILY: 宋体">则是将以</span>
				<span lang="EN-US">utf8</span>
				<span style="FONT-FAMILY: 宋体">编码的<span style="COLOR: red">数据</span>转换为<span style="COLOR: red">编码</span>形式的文字。举一个例子：</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: silver 0% 50%; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; moz-background-clip: -moz-initial; moz-background-origin: -moz-initial; moz-background-inline-policy: -moz-initial">
				<span lang="EN-US">$str = decode(‘gb<st1:chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="2312" unitname="’">2312’</st1:chmetcnv>, ‘</span>
				<span style="FONT-FAMILY: 宋体">你好</span>
				<span lang="EN-US">’);</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: silver 0% 50%; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; moz-background-clip: -moz-initial; moz-background-origin: -moz-initial; moz-background-inline-policy: -moz-initial">
				<span lang="EN-US">Print encode(‘gb<st1:chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="2312" unitname="’">2312’</st1:chmetcnv>, $str);</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 宋体">以上是可以正确打印出“你好”二字的。首先是告诉</span>
				<span lang="EN-US">perl</span>
				<span style="FONT-FAMILY: 宋体">“你好”是一个</span>
				<span lang="EN-US">gb2312</span>
				<span style="FONT-FAMILY: 宋体">编码的文字，</span>
				<span lang="EN-US">perl</span>
				<span style="FONT-FAMILY: 宋体">把它以</span>
				<span lang="EN-US">utf8</span>
				<span style="FONT-FAMILY: 宋体">的形式存在</span>
				<span lang="EN-US">$str</span>
				<span style="FONT-FAMILY: 宋体">中；在将</span>
				<span lang="EN-US">$str</span>
				<span style="FONT-FAMILY: 宋体">以</span>
				<span lang="EN-US">gb2312</span>
				<span style="FONT-FAMILY: 宋体">的形式转换为文字并打印出来。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US">
						<?xml:namespace prefix = o /?>
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 宋体">再说一下数据库。数据库都有自己的编码方式，比如说</span>
				<span lang="EN-US">utf8</span>
				<span style="FONT-FAMILY: 宋体">、</span>
				<span lang="EN-US">eu_cn</span>
				<span style="FONT-FAMILY: 宋体">、</span>
				<span lang="EN-US">gbk</span>
				<span style="FONT-FAMILY: 宋体">等等。一定编码方式的数据库只能接受相应编码的数据，一般从兼容性上考虑会使用</span>
				<span lang="EN-US">utf8</span>
				<span style="FONT-FAMILY: 宋体">编码的数据库。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 宋体">在</span>
				<span lang="EN-US">postgres8.1</span>
				<span style="FONT-FAMILY: 宋体">中，向一定编码的数据库中输入数据时，可以用</span>
				<span lang="EN-US">\encoding</span>
				<span style="FONT-FAMILY: 宋体">将</span>
				<span lang="EN-US">client encode</span>
				<span style="FONT-FAMILY: 宋体">设为与输入数据相同的形式。比方说在</span>
				<span lang="EN-US">psql</span>
				<span style="FONT-FAMILY: 宋体">中向</span>
				<span lang="EN-US">utf8</span>
				<span style="FONT-FAMILY: 宋体">编码的数据库插入</span>
				<span lang="EN-US">gbk</span>
				<span style="FONT-FAMILY: 宋体">编码的汉字，就可以：</span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: silver 0% 50%; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; moz-background-clip: -moz-initial; moz-background-origin: -moz-initial; moz-background-inline-policy: -moz-initial">
				<span lang="EN-US">\encoding ‘gbk’</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span style="FONT-FAMILY: 宋体">不然只会得到一个无法输入的提示。遗憾的是，</span>
				<span lang="EN-US">perl</span>
				<span style="FONT-FAMILY: 宋体">里没有像</span>
				<span lang="EN-US">\encoding</span>
				<span style="FONT-FAMILY: 宋体">这样的命令，于是又要拜托</span>
				<span lang="EN-US">encode</span>
				<span style="FONT-FAMILY: 宋体">模块了。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US">#------------------------------------</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US">use DBI;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US">use Encode;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US">use strict;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt">
				<span lang="EN-US">#连接数据库<br />my $dbh = DBI-&gt;connect("dbi:PgPP:dbname=</span>
				<span style="FONT-FAMILY: 宋体">
						<font color="#00cc00">数据库名</font>
				</span>
				<span lang="EN-US">:host=</span>
				<span style="FONT-FAMILY: 宋体">
						<font color="#00cc00">服务器地址</font>
				</span>
				<span lang="EN-US">:port=</span>
				<span style="FONT-FAMILY: 宋体">
						<font color="#00cc00">服务器端口</font>
				</span>
				<span lang="EN-US">",</span>
				<span style="FONT-FAMILY: 宋体">
						<font color="#00cc00">登录名</font>
				</span>
				<span lang="EN-US">,</span>
				<span style="FONT-FAMILY: 宋体">
						<font color="#00cc00">登录密码</font>
				</span>
				<span lang="EN-US">,{PrintError =&gt; 0})</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US">
						<span>
						</span>or die $DBI::errstr;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US">
						<o:p>#准备sql语句 </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US">my $inr = $dbh-&gt;prepare("insert into name values('encode('utf8', decode('gb2312','</span>
				<span style="FONT-FAMILY: 宋体">你好</span>
				<span lang="EN-US">'))')")</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US">
						<span>
						</span>or die $DBI::errstr;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US">
						<o:p>#执行sql语句 </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US">$inr-&gt;execute() or die $DBI::errstr;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US">
						<o:p>#打印成功信号 </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US">print "WELL DONE!";</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt">
				<span lang="EN-US">#---------------------------------------</span>
		</p>
<img src ="http://www.cnitblog.com/gyn/aggbug/16955.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-09-17 10:48 <a href="http://www.cnitblog.com/gyn/archive/2006/09/17/16955.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用perl写的简易ruby IDE：rubysgl</title><link>http://www.cnitblog.com/gyn/archive/2006/08/22/15749.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Tue, 22 Aug 2006 14:36:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/08/22/15749.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/15749.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/08/22/15749.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/15749.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/15749.html</trackback:ping><description><![CDATA[
		<p>    最近一直在学习Ruby on Rails，所以顺道一起把ruby给学了。有时因为要测试语法不得不在命令行中打入：</p>
		<p>    <font style="BACKGROUND-COLOR: #ffffff">ruby -e "comman line"</font></p>
		<p>    <font style="BACKGROUND-COLOR: #ffffff">打多了后甚感麻烦，难道就不能象python的IDEL？事实上是有一个fxri的，就是太难看了。偶不要边上的大堆注解，如果它做得象python的自省那也算了，而且慢得要死。索性自己写一个好了，要求能运行单行的程序就行，但发现ruby支持多“-e”输入，所以就把多行也一起支持了。</font></p>
		<p>    程序是用perl写的，30几行而已。</p>
		<p>use strict;<br /><br />print "RubySgl=&gt;";<br />my $cmdline="";<br />my $rsign="ruby";<br />my $esign=" -e";<br />my $sptr=" ' ";<br />my $sgl=&lt;STDIN&gt;;<br />chomp($sgl);</p>
		<p>while(!($sgl eq "<a>\\q</a>")){<br /><br />   if($sgl eq "<a>\\h</a>"){<br />      print "&gt; &gt; &gt; &gt; &gt;execute ruby single line programme\n";<br />   }#endif<br />   else{<br />      $cmdline=$rsign;<br />      do{<br />         $cmdline=$cmdline.$esign.$sptr.$sgl.$sptr;<br />         print "       =&gt;";<br />         $sgl=&lt;STDIN&gt;;<br />         chomp($sgl);<br />      }while(!($sgl eq "<a>\\e</a>"));</p>
		<p>      open(FOO,"-|",$cmdline);<br />      while(&lt;FOO&gt;){<br />         print "&gt; &gt; &gt; &gt; &gt;";<br />         chomp();<br />         print $_."\n";<br />      }#endwhile<br />      close(FOO);<br />   }#endelse<br /><br />   print "RubySgl=&gt;";<br />   $sgl=&lt;STDIN&gt;;<br />   chomp($sgl);<br />}<br /></p>
		<p> </p>
		<p>    然后再建立一个rubysgl.bat文件来启动：</p>
		<p>    @echo off<br />    perl d:\rubysgl.pl</p>
		<p>    试运行，正常，8--&gt;<br /><img height="159" alt="r_rubysgl.JPG" src="http://www.cnitblog.com/images/cnitblog_com/gyn/2469/r_rubysgl.JPG" width="352" border="0" /><br /><br />redited at 2006-08-25<br /><br />   做了一些细小的改进，增加了行号提示和清除所有输入的功能。本来还做了几个自省函数和清除特定行的功能，但程序变得巨大无比，自己都看不下去了，实在有违初衷，因此弃用。<br /><br />use strict;</p>
		<p>my $idx=1;<br />print "RubySgl&lt;".$idx."&gt;";<br />my $cmdline="";<br />my $rsign="ruby ";<br />my $esign="-e";<br />my $sptr=" ' ";<br />my $sgl=&lt;STDIN&gt;;<br />chomp($sgl);</p>
		<p>while(!($sgl eq "<a href="file://\\q">\\q</a>")){<br /> if($sgl eq "<a href="file://\\h">\\h</a>"){<br />  print " &gt; &gt; &gt; &gt; &gt;Execute ruby single line programme.\n"; #Display IDE informatrion<br /> }#endif<br /> else{<br />  $cmdline=$rsign;<br />  do{<br />   if($sgl =~ /^\\c\s*$/){<br />    $cmdline=$rsign;<br />    print " &gt; &gt; &gt; &gt; &gt;LINES ALL CLAERED!\n"; #Clear former inputings<br />    $idx=1;<br />   }  <br />   else{<br />    $cmdline=$cmdline.$esign.$sptr.$sgl.$sptr;<br />    $idx++;<br />   }#endelse</p>
		<p>   #continue to input<br />   print "       &lt;".$idx."&gt;";<br />   $sgl=&lt;STDIN&gt;;<br />   chomp($sgl);<br />  }while(!($sgl eq "<a href="file://\\e">\\e</a>") and !($sgl eq "<a href="file://\\q">\\q</a>"));</p>
		<p>  if($sgl eq "<a href="file://\\e">\\e</a>"){<br />   open(FOO,"-|",$cmdline);<br />   while(&lt;FOO&gt;){<br />    print " &gt; &gt; &gt; &gt; &gt;"; #Display outputings<br />    chomp();<br />    print $_."\n";<br />   }#endwhile<br />   close(FOO);<br />  }#endif<br />  elsif($sgl eq "<a href="file://\\q">\\q</a>"){ exit; }<br />  else{;}</p>
		<p> }#endelse<br /> <br /> print "----------another programme----------\n";<br /> $idx=1;<br /> print "RubySgl&lt;".$idx."&gt;"; #Restart programming<br /> $sgl=&lt;STDIN&gt;;<br /> chomp($sgl);<br />}<br /><img height="193" alt="r_fgafaf.JPG" src="http://www.cnitblog.com/images/cnitblog_com/gyn/2469/r_fgafaf.JPG" width="303" border="0" /><br /><br />redited at 2006-08-26<br /><br />修订了几个bug，程序改伟如下：<br />use strict;</p>
		<p>my $idx=1;<br />print "RubySgl&lt;".$idx."&gt;";<br />my $cmdline="";<br />my $rsign="ruby ";<br />my $esign="-e";<br />my $sptr=" ' ";<br />my $sgl=&lt;STDIN&gt;;<br />chomp($sgl);</p>
		<p>while(!($sgl eq "<a href="file://\\q">\\q</a>")){<br /> if($sgl eq "<a href="file://\\h">\\h</a>"){<br />  print " &gt; &gt; &gt; &gt; &gt;Execute ruby single line program.\n"; <br /> }<br /> else{<br />  $cmdline=$rsign;<br />  while(!($sgl eq "<a href="file://\\e">\\e</a>") and !($sgl eq "<a href="file://\\q">\\q</a>")){<br />   if($sgl =~ /^\\c\s*$/){<br />    $cmdline=$rsign;<br />    print " &gt; &gt; &gt; &gt; &gt;LINES ALL CLEARED!\n"; <br />    $idx=1;<br />   }  <br />   else{<br />    $cmdline=$cmdline.$esign.$sptr.$sgl.$sptr;<br />    $idx++;<br />   }</p>
		<p>   print "       &lt;".$idx."&gt;";<br />   $sgl=&lt;STDIN&gt;;<br />   chomp($sgl);<br />  }</p>
		<p>  if($sgl eq "<a href="file://\\e">\\e</a>"){<br />   if($idx==1){ goto RESTART; }<br />   else{<br />    open(FOO,"-|",$cmdline);<br />    while(&lt;FOO&gt;){<br />     print " &gt; &gt; &gt; &gt; &gt;"; <br />     chomp();<br />     print $_."\n";<br />    }<br />    close(FOO);<br />   }<br />  }<br />  elsif($sgl eq "<a href="file://\\q">\\q</a>"){ exit; }<br />  else{;}</p>
		<p> }#endelse</p>
		<p> RESTART:<br /> print "-----------another program-----------\n";<br /> $idx=1;<br /> print "RubySgl&lt;".$idx."&gt;"; <br /> $sgl=&lt;STDIN&gt;;<br /> chomp($sgl);<br />}<br /></p>
<img src ="http://www.cnitblog.com/gyn/aggbug/15749.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-08-22 22:36 <a href="http://www.cnitblog.com/gyn/archive/2006/08/22/15749.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>perl编程：尼科彻斯定理</title><link>http://www.cnitblog.com/gyn/archive/2006/05/08/10195.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Mon, 08 May 2006 06:01:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/05/08/10195.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/10195.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/05/08/10195.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/10195.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/10195.html</trackback:ping><description><![CDATA[
		<p>5月8日的电脑报上有一个小测试：<br />任何一个整数的立方都可以写成一串连续奇数的和。<br />想也没想，就写了：<br /><br />use strict;</p>
		<p>my $seed=&lt;STDIN&gt;;<br />$seed**=3;<br />my @stack=();<br />open(NKCS,'&gt;c:/code/nkcs.txt');</p>
		<p>for(my $init=1; $init&lt;=$seed; $init+=2){<br />   my $sum=$init;<br />   my $temp=$init;<br />   while($sum&lt;$seed){<br />      push(@stack,$temp);<br />      $temp+=2;<br />      $sum+=$temp;<br />   }<br />   if($sum==$seed){<br />      while(@stack){print NKCS shift(@stack)."\n" ;}<br />      close NKCS;<br />      exit;<br />   }<br />   undef @stack;<br />}<br /></p>
		<p>close NKCS;<br />die("nkcs failed");</p>
<img src ="http://www.cnitblog.com/gyn/aggbug/10195.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-05-08 14:01 <a href="http://www.cnitblog.com/gyn/archive/2006/05/08/10195.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>perl编程：一个链表类</title><link>http://www.cnitblog.com/gyn/archive/2006/04/29/9941.html</link><dc:creator>gyn_tadao</dc:creator><author>gyn_tadao</author><pubDate>Sat, 29 Apr 2006 11:21:00 GMT</pubDate><guid>http://www.cnitblog.com/gyn/archive/2006/04/29/9941.html</guid><wfw:comment>http://www.cnitblog.com/gyn/comments/9941.html</wfw:comment><comments>http://www.cnitblog.com/gyn/archive/2006/04/29/9941.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/gyn/comments/commentRss/9941.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/gyn/services/trackbacks/9941.html</trackback:ping><description><![CDATA[
		<p>package data_string;</p>
		<p>sub new{<br /> shift();<br /> my $val=undef;<br /> my $ref=undef; <br /> ($val,$ref)=@_;<br /> my $new_string={<br />  val =&gt; $val,<br />  ref =&gt; $ref<br />  };<br /> bless $new_string, data_string;<br /> return $new_string;<br />}</p>
		<p>#return a reference to the hash holding two incoming parameters which are value of the node and a #reference to a next node</p>
		<p>sub val{<br /> return shift()-&gt;{val};<br />}</p>
		<p>#return the value of the node</p>
		<p>sub ref{<br /> return shift()-&gt;{ref};<br />}</p>
		<p>#return a reference to the following node</p>
		<p>sub setval{<br /> my $tnode=shift();<br /> if(@_){<br />  my $tval=shift();<br />  $tnode-&gt;{val}=$tval;<br />  return 1;<br /> }<br /> return 0;<br />}</p>
		<p>#set the node with a new value</p>
		<p>sub setref{<br /> my $tnode=shift();<br /> my $tref=shift();<br /> if(CORE::ref($tnode)){<br />  $tnode-&gt;{ref}=$tref;<br />  return 1;<br /> }<br /> return 0;<br />}</p>
		<p>#set the node with a new reference</p>
		<p>sub setnull{<br /> undef %{shift()};<br /> return 1;<br />}</p>
		<p>#undefine the node</p>
		<p>sub length{<br /> my $count=0;<br /> my $head=shift();<br /> while( defined($head) ){ $count++; $head=$head-&gt;ref;}<br /> return $count;<br />}</p>
		<p>#return the length of a list whose head is the current node </p>
		<p>sub insert{<br /> my $head=shift();<br /> my $pos=undef;<br /> my $str=undef;<br /> if(@_==2){<br />  ($pos, $str)=@_;<br />  if($pos&gt;=$head-&gt;length &amp;&amp; $pos&lt;0)<br />   {return 0;}<br />  else<br />   {<br />   my $pre=$head;<br />   my $cur=$pre-&gt;ref;<br />   my $index=0;<br />   while($index!=$pos &amp;&amp; defined($cur)){<br />     $pre=$cur;<br />     $cur=$cur-&gt;ref;<br />     $index++;<br />   }#while<br />   $pre-&gt;setref(data_string-&gt;new($str, $cur)); <br />   return 1;<br />   }#else<br /> }<br /> return 0;<br />}</p>
		<p>#insert the current node leading list a new node taking the incoming parameter as the value</p>
		<p>sub delete{<br /> if($_[1]==0){<br />  if($_[0]-&gt;length==1){shift()-&gt;setnull; return 1;}<br />  else{<br />   my $fir=shift;<br />   my $sec=$fir-&gt;ref;<br />   $fir-&gt;setval($sec-&gt;val);<br />   $fir-&gt;setref($sec-&gt;ref);<br />   $sec-&gt;setnull;<br />   return 1;<br />  }<br /> }<br /> my $head=shift();<br /> if(@_){<br />  my $pos=shift();<br />  if($pos&gt;=$head-&gt;length &amp;&amp; $pos&lt;=0)<br />   {return 0;}#if<br />  else{<br />   my $pre=$head;<br />   my $cur=$pre-&gt;ref;<br />   my $index=1;<br />   while($index&lt;$pos){<br />    $pre=$cur;<br />    if(defined($cur)){$cur=$cur-&gt;ref;}<br />    $index++; <br />   }#while<br />   $pre-&gt;setref($cur-&gt;ref);<br />   $cur-&gt;setnull;<br />   return 1;<br />  }#else<br /> }#if<br /> return 0;<br />}</p>
		<p>#delete the current node leading list a node whose position is related to the incoming parameter</p>
		<p>1;<br /></p>
		<p>
				<br />#写了个测试程序：<br /><br />use data_string;</p>
		<p>$temp=undef;<br />$node=data_string-&gt;new(1, $temp);</p>
		<p>foreach (qw(2 3 4 5)){<br /> $ind=$_-2;<br /> if($ind&lt;$node-&gt;length){<br />  $node-&gt;insert($ind, $_);<br /> }<br />}</p>
		<p>$node-&gt;delete(3);</p>
		<p>$temp=$node;</p>
		<p>while(defined($temp)){<br /> print($temp-&gt;val."\n");<br /> $temp=$temp-&gt;ref;<br />}<br /><br /><img style="WIDTH: 512px; HEIGHT: 320px" height="413" alt="r_data_string.jpg" src="http://www.cnitblog.com/images/cnitblog_com/gyn/2469/r_data_string.jpg" width="640" border="0" /></p>
<img src ="http://www.cnitblog.com/gyn/aggbug/9941.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/gyn/" target="_blank">gyn_tadao</a> 2006-04-29 19:21 <a href="http://www.cnitblog.com/gyn/archive/2006/04/29/9941.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>