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

<channel>
	<title>answersy.com Blog &#187; PHP/Perl</title>
	<atom:link href="http://answersy.com/zchen/index.php/category/it-related/phpperl/feed/" rel="self" type="application/rss+xml" />
	<link>http://answersy.com/zchen</link>
	<description>Got questions? Got answers!</description>
	<lastBuildDate>Thu, 20 Oct 2011 04:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to Use LWP::UserAgent with Socks Proxy?</title>
		<link>http://answersy.com/zchen/2008/01/03/how-to-use-lwpuseragent-with-socks-proxy/</link>
		<comments>http://answersy.com/zchen/2008/01/03/how-to-use-lwpuseragent-with-socks-proxy/#comments</comments>
		<pubDate>Thu, 03 Jan 2008 06:56:39 +0000</pubDate>
		<dc:creator>zchen</dc:creator>
				<category><![CDATA[IT Related]]></category>
		<category><![CDATA[Linux/Unix]]></category>
		<category><![CDATA[PHP/Perl]]></category>

		<guid isPermaLink="false">http://answersy.com/zchen/2008/01/03/how-to-use-lwpuseragent-with-socks-proxy/</guid>
		<description><![CDATA[To use so you need LWP::Protocol::http::SocksChain. There is a number of such protocol packages, but usually, installing any one of them requires a number of other prerequisites: Here is a process I have tested, if you do have super user privilege, you can ignore where "/path/to/install" is mentioned 0. $ sudo -s 0.1 setenv PERL5LIB [...]]]></description>
			<content:encoded><![CDATA[<p>To use so you need <strong>LWP::Protocol::http::SocksChain</strong>.</p>
<p>There is a number of such protocol packages, but usually, installing any one of them requires a number of other prerequisites:</p>
<p>Here is a process I have tested, if you do have super user privilege, you can ignore where "<strong>/path/to/install</strong>" is mentioned</p>
<pre class="perl"><span style="color: #cc66cc;">0</span>. $ sudo -<a href="http://perldoc.perl.org/functions/s.html"><span style="color: #000066;">s</span></a>
<span style="color: #cc66cc;">0.1</span> setenv PERL5LIB /path/to/install/lib/site_perl
&nbsp;
<span style="color: #cc66cc;">1</span>. INSTALL OPENSSL
<span style="color: #cc66cc;">1.1</span> http://www.openssl.org/source/,
download http://www.openssl.org/source/openssl<span style="color: #cc66cc;">-0.9</span>.8g.tar.gz
<span style="color: #cc66cc;">1.2</span> Unzip openssl<span style="color: #cc66cc;">-0.9</span>.8g.tar.gz
<span style="color: #cc66cc;">1.3</span> Read INSTALL
<span style="color: #cc66cc;">1.4</span> $ ./config
<span style="color: #cc66cc;">1.5</span> $ make
<span style="color: #cc66cc;">1.6</span> $ make test
<span style="color: #cc66cc;">1.6</span> $ make install
&nbsp;
<span style="color: #cc66cc;">2</span>. INSTALL Net::<span style="color: #006600;">SSLeay</span>
<span style="color: #cc66cc;">2.1</span> Download it from http://search.cpan.org/~flora/Net-SSLeay<span style="color: #cc66cc;">-1.32</span>/,
download http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/Net-SSLeay<span style="color: #cc66cc;">-1.32</span>.tar.gz
<span style="color: #cc66cc;">2.2</span> Unzip Net-SSLeay<span style="color: #cc66cc;">-1.32</span>.tar.gz
<span style="color: #cc66cc;">2.3</span> $ perl Makefile.PL PREFIX=/path/to/install
<span style="color: #cc66cc;">2.4</span> $ make install <span style="color: #808080; font-style: italic;"># the Makefile requires -lz, on certain system, you can remove it</span>
<span style="color: #cc66cc;">2.5</span> $ cd examples
<span style="color: #cc66cc;">2.6</span> $ get_page.pl www.cryptsoft.com <span style="color: #cc66cc;">443</span> /
&nbsp;
<span style="color: #cc66cc;">3</span>. INSTALL IO::<span style="color: #006600;">Socket</span>::<span style="color: #006600;">SSL</span>
<span style="color: #cc66cc;">3.1</span> Download it from http://search.cpan.org/dist/IO-Socket-SSL/,
download http://search.cpan.org/CPAN/authors/id/S/SU/SULLR/IO-Socket-SSL<span style="color: #cc66cc;">-1.12</span>.tar.gz
<span style="color: #cc66cc;">3.2</span> Unzip IO-Socket-SSL<span style="color: #cc66cc;">-1.12</span>.tar.gz
<span style="color: #cc66cc;">3.3</span> $ perl Makefile.PL PREFIX=/path/to/install
<span style="color: #cc66cc;">3.4</span> $ make
<span style="color: #cc66cc;">3.5</span> $ make test
<span style="color: #cc66cc;">3.6</span> $ make install
&nbsp;
<span style="color: #cc66cc;">4</span>. INSTALL Net::<span style="color: #006600;">SC</span>
<span style="color: #cc66cc;">4.1</span> Download it from http://search.cpan.org/~gosha/Net-SC<span style="color: #cc66cc;">-1.20</span>/,
download http://search.cpan.org/CPAN/authors/id/G/GO/GOSHA/Net-SC<span style="color: #cc66cc;">-1.20</span>.tar.gz
<span style="color: #cc66cc;">4.2</span> Unzip Net-SC<span style="color: #cc66cc;">-1.20</span>.tar.gz
<span style="color: #cc66cc;">4.3</span> $ perl Makefile.PL PREFIX=/path/to/install
<span style="color: #cc66cc;">4.4</span> $ make
<span style="color: #cc66cc;">4.5</span> $ make test
<span style="color: #cc66cc;">4.6</span> $ make install
&nbsp;
<span style="color: #cc66cc;">5</span>. INSTALL
<span style="color: #cc66cc;">5.1</span> Download it from http://search.cpan.org/~gosha/LWP-Protocol-http-SocksChain<span style="color: #cc66cc;">-1.4</span>/,
download http://search.cpan.org/CPAN/authors/id/G/GO/GOSHA/LWP-Protocol-http-SocksChain<span style="color: #cc66cc;">-1.4</span>.tar.gz
<span style="color: #cc66cc;">5.2</span> Unzip LWP-Protocol-http-SocksChain<span style="color: #cc66cc;">-1.4</span>.tar.gz
<span style="color: #cc66cc;">5.3</span> $ perl Makefile.PL PREFIX=/path/to/install
<span style="color: #cc66cc;">5.4</span> $ make
<span style="color: #cc66cc;">5.5</span> $ make test
<span style="color: #cc66cc;">5.6</span> $ make install</pre>
<p>Now you can use LWPGet</p>
<pre class="perl"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl -w</span>
<a href="http://perldoc.perl.org/functions/require.html"><span style="color: #000066;">require</span></a> <span style="color: #cc66cc;">5.8</span><span style="color: #cc66cc;">.0</span>;
<span style="color: #000000; font-weight: bold;">use</span> strict;
<span style="color: #000000; font-weight: bold;">use</span> lib <span style="color: #ff0000;">&quot;/path/to/install/lib/site_perl/5.8.7&quot;</span>;
<span style="color: #000000; font-weight: bold;">use</span> LWP::<span style="color: #006600;">Simple</span>; <span style="color: #000000; font-weight: bold;">use</span> LWP::<span style="color: #006600;">UserAgent</span>;
<span style="color: #000000; font-weight: bold;">use</span> LWP::<span style="color: #006600;">Protocol</span>::<span style="color: #006600;">http</span>::<span style="color: #006600;">SocksChain</span>;
LWP::<span style="color: #006600;">Protocol</span>::<span style="color: #006600;">implementor</span><span style="color: #66cc66;">&#40;</span> http =&gt; <span style="color: #ff0000;">'LWP::Protocol::http::SocksChain'</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">@LWP</span>::<span style="color: #006600;">Protocol</span>::<span style="color: #006600;">http</span>::<span style="color: #006600;">SocksChain</span>::<span style="color: #006600;">EXTRA_SOCK_OPTS</span> = <span style="color: #66cc66;">&#40;</span>
Chain_Len    =&gt; <span style="color: #cc66cc;">1</span>,
Debug        =&gt; <span style="color: #cc66cc;">0</span>,
Random_Chain =&gt; <span style="color: #cc66cc;">1</span>,
Chain_File_Data =&gt; <span style="color: #66cc66;">&#91;</span>
<span style="color: #ff0000;">'ip_of_socks_proxy:port:::5'</span>,
<span style="color: #66cc66;">&#93;</span>,
Auto_Save    =&gt; <span style="color: #cc66cc;">0</span>,
Restore_Type =&gt; <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ua</span> = <span style="color: #000000; font-weight: bold;">new</span> LWP::<span style="color: #006600;">UserAgent</span> <span style="color: #0000ff;">$ua</span>-&gt;<span style="color: #006600;">agent</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Mozilla/4.0 (compatible; MSIE 6.0; Windws NT 5.1)'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$Url</span> = <span style="color: #0000ff;">$ARGV</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$response</span> = <span style="color: #0000ff;">$ua</span>-&gt;<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$Url</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$response</span>-&gt;<span style="color: #006600;">is_success</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$page</span> = <span style="color: #0000ff;">$response</span>-&gt;<span style="color: #006600;">content</span>;
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$page</span>, <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>n&quot;</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #000000; font-weight: bold;">STDERR</span> <span style="color: #ff0000;">&quot;Fail to get $Url<span style="color: #000099; font-weight: bold;">\\</span>n&quot;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fanswersy.com%2Fzchen%2F2008%2F01%2F03%2Fhow-to-use-lwpuseragent-with-socks-proxy%2F';
  addthis_title  = 'How+to+Use+LWP%3A%3AUserAgent+with+Socks+Proxy%3F';
  addthis_pub    = 'zchen050815';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://answersy.com/zchen/2008/01/03/how-to-use-lwpuseragent-with-socks-proxy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Maintain Software Config File with Perl?</title>
		<link>http://answersy.com/zchen/2007/12/21/how-to-maintain-software-config-file-with-perl/</link>
		<comments>http://answersy.com/zchen/2007/12/21/how-to-maintain-software-config-file-with-perl/#comments</comments>
		<pubDate>Fri, 21 Dec 2007 22:32:41 +0000</pubDate>
		<dc:creator>zchen</dc:creator>
				<category><![CDATA[IT Related]]></category>
		<category><![CDATA[PHP/Perl]]></category>

		<guid isPermaLink="false">http://answersy.com/zchen/2007/12/21/how-to-maintain-software-config-file-with-perl/</guid>
		<description><![CDATA[The original post can be found at http://www.perlmonks.org/?node_id=464358 I have made slight changes (1) Put the following in a file "cfg.cfg" %CFG = &#40; 'servers' =&#62; &#123; 'SRV1' =&#62; &#123; 'IP' =&#62; '99.32.4.0', 'user' =&#62; 'aname', 'pswd' =&#62; 'p4ssw0rd', 'status' =&#62; 'unavailable' &#125;, 'SRV2' =&#62; &#123; 'IP' =&#62; '129.99.10.5', 'user' =&#62; 'guest', 'pswd' =&#62; 'guest', [...]]]></description>
			<content:encoded><![CDATA[<p>The original post can be found at <a href="http://www.perlmonks.org/?node_id=464358">http://www.perlmonks.org/?node_id=464358</a></p>
<p>I have made slight changes</p>
<p>(1) Put the following in a file "cfg.cfg"</p>
<pre class="perl"><span style="color: #0000ff;">%CFG</span> = <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'servers'</span> =&gt; <span style="color: #66cc66;">&#123;</span>
<span style="color: #ff0000;">'SRV1'</span> =&gt; <span style="color: #66cc66;">&#123;</span>
<span style="color: #ff0000;">'IP'</span>     =&gt; <span style="color: #ff0000;">'99.32.4.0'</span>,
<span style="color: #ff0000;">'user'</span>   =&gt; <span style="color: #ff0000;">'aname'</span>,
<span style="color: #ff0000;">'pswd'</span>   =&gt; <span style="color: #ff0000;">'p4ssw0rd'</span>,
<span style="color: #ff0000;">'status'</span> =&gt; <span style="color: #ff0000;">'unavailable'</span>
<span style="color: #66cc66;">&#125;</span>,
<span style="color: #ff0000;">'SRV2'</span> =&gt; <span style="color: #66cc66;">&#123;</span>
<span style="color: #ff0000;">'IP'</span>     =&gt; <span style="color: #ff0000;">'129.99.10.5'</span>,
<span style="color: #ff0000;">'user'</span>   =&gt; <span style="color: #ff0000;">'guest'</span>,
<span style="color: #ff0000;">'pswd'</span>   =&gt; <span style="color: #ff0000;">'guest'</span>,
<span style="color: #ff0000;">'status'</span> =&gt; <span style="color: #ff0000;">'unavailable'</span>,
<span style="color: #66cc66;">&#125;</span>,
<span style="color: #66cc66;">&#125;</span>,
<span style="color: #66cc66;">&#41;</span>;</pre>
<p>(2) The following will load the config file and print out SVR1's IP</p>
<pre class="perl"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict;
<span style="color: #b1b100;">our</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">%CFG</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;"># Read a configuration file</span>
<span style="color: #000000; font-weight: bold;">sub</span> ReadCfg
<span style="color: #66cc66;">&#123;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$file</span> = <span style="color: #0000ff;">$_</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
<span style="color: #b1b100;">our</span> <span style="color: #0000ff;">$err</span>;
&nbsp;
<span style="color: #66cc66;">&#123;</span>   <span style="color: #808080; font-style: italic;"># Put config data into a separate namespace</span>
<a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> CFG_PKG;
&nbsp;
<span style="color: #808080; font-style: italic;"># Process the contents of the config file</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$rc</span> = <span style="color: #b1b100;">do</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$file</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;"># Check for errors</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>$@<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
$::<span style="color: #006600;">err</span> = <span style="color: #ff0000;">&quot;ERROR: Failure compiling '$file' - $@&quot;</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elsif</span> <span style="color: #66cc66;">&#40;</span>! <a href="http://perldoc.perl.org/functions/defined.html"><span style="color: #000066;">defined</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$rc</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
$::<span style="color: #006600;">err</span> = <span style="color: #ff0000;">&quot;ERROR: Failure reading '$file' - $!&quot;</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elsif</span> <span style="color: #66cc66;">&#40;</span>! <span style="color: #0000ff;">$rc</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
$::<span style="color: #006600;">err</span> = <span style="color: #ff0000;">&quot;ERROR: Failure processing '$file'&quot;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$err</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Get our configuration information</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$err</span> = ReadCfg<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'cfg.cfg'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">STDERR</span> <span style="color: #0000ff;">$err</span>, <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>n&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;SRV1's IP: &quot;</span>, <span style="color: #0000ff;">$CFG_PKG</span>::<span style="color: #006600;">CFG</span><span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">'servers'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">'SRV1'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">'IP'</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>n&quot;</span>;</pre>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fanswersy.com%2Fzchen%2F2007%2F12%2F21%2Fhow-to-maintain-software-config-file-with-perl%2F';
  addthis_title  = 'How+to+Maintain+Software+Config+File+with+Perl%3F';
  addthis_pub    = 'zchen050815';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://answersy.com/zchen/2007/12/21/how-to-maintain-software-config-file-with-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Count Occurrence of a Character in a String (with Perl)?</title>
		<link>http://answersy.com/zchen/2007/12/12/how-to-count-occurrence-of-a-character-in-a-string-perl/</link>
		<comments>http://answersy.com/zchen/2007/12/12/how-to-count-occurrence-of-a-character-in-a-string-perl/#comments</comments>
		<pubDate>Wed, 12 Dec 2007 06:07:34 +0000</pubDate>
		<dc:creator>zchen</dc:creator>
				<category><![CDATA[IT Related]]></category>
		<category><![CDATA[PHP/Perl]]></category>

		<guid isPermaLink="false">http://answersy.com/zchen/2007/12/12/how-to-count-occurrence-of-a-character-in-a-string-perl/</guid>
		<description><![CDATA[To count occurrence of a character, say "\", in a string: #!/usr/bin/perl -w use strict; &#160; my $url='http://www.aaa.com/bbb/ccc/ddd/eee/fff/aaa'; my $k = &#40;$url =~ tr/\\//\\//&#41;; print &#34;cnt = $k\\n&#34;; addthis_url = 'http%3A%2F%2Fanswersy.com%2Fzchen%2F2007%2F12%2F12%2Fhow-to-count-occurrence-of-a-character-in-a-string-perl%2F'; addthis_title = 'How+to+Count+Occurrence+of+a+Character+in+a+String+%28with+Perl%29%3F'; addthis_pub = 'zchen050815';]]></description>
			<content:encoded><![CDATA[<p>To count occurrence of a character, say "\", in a string:</p>
<pre class="perl"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl -w</span>
<span style="color: #000000; font-weight: bold;">use</span> strict;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$url</span>=<span style="color: #ff0000;">'http://www.aaa.com/bbb/ccc/ddd/eee/fff/aaa'</span>;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$k</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$url</span> =~ <a href="http://perldoc.perl.org/functions/tr.html"><span style="color: #000066;">tr</span></a>/\\//\\//<span style="color: #66cc66;">&#41;</span>;
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;cnt = $k<span style="color: #000099; font-weight: bold;">\\</span>n&quot;</span>;</pre>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fanswersy.com%2Fzchen%2F2007%2F12%2F12%2Fhow-to-count-occurrence-of-a-character-in-a-string-perl%2F';
  addthis_title  = 'How+to+Count+Occurrence+of+a+Character+in+a+String+%28with+Perl%29%3F';
  addthis_pub    = 'zchen050815';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://answersy.com/zchen/2007/12/12/how-to-count-occurrence-of-a-character-in-a-string-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Fork and Exec a Background Process in Perl?</title>
		<link>http://answersy.com/zchen/2007/10/31/how-to-fork-and-run-a-background-process-in-perl/</link>
		<comments>http://answersy.com/zchen/2007/10/31/how-to-fork-and-run-a-background-process-in-perl/#comments</comments>
		<pubDate>Wed, 31 Oct 2007 05:11:50 +0000</pubDate>
		<dc:creator>zchen</dc:creator>
				<category><![CDATA[IT Related]]></category>
		<category><![CDATA[PHP/Perl]]></category>

		<guid isPermaLink="false">http://answersy.com/zchen/2007/10/31/how-to-fork-and-run-a-background-process-in-perl/</guid>
		<description><![CDATA[Sometimes we need to start a background process to do a long-running job, while the parent process may die before the child dies. To do so in Perl, you can use the following code: #!/usr/bin/perl -w use strict; use POSIX qw&#40;:sys_wait_h&#41;; &#160; my $kidpid; if &#40;!defined&#40;$kidpid = fork&#40;&#41;&#41;&#41; &#123; # fork returned undef, so failed [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes we need to start a background process to do a long-running job, while the parent process may die before the child dies. To do so in Perl, you can use the following code:</p>
<pre class="perl"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl -w</span>
<span style="color: #000000; font-weight: bold;">use</span> strict;
<span style="color: #000000; font-weight: bold;">use</span> POSIX <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #66cc66;">&#40;</span>:sys_wait_h<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$kidpid</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<a href="http://perldoc.perl.org/functions/defined.html"><span style="color: #000066;">defined</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$kidpid</span> = <a href="http://perldoc.perl.org/functions/fork.html"><span style="color: #000066;">fork</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #808080; font-style: italic;"># fork returned undef, so failed</span>
<a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Failed to fork&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elsif</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$kidpid</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #808080; font-style: italic;"># fork returned 0, so this branch is child</span>
<a href="http://perldoc.perl.org/functions/exec.html"><span style="color: #000066;">exec</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;./longjob.pl&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #808080; font-style: italic;"># parent</span>
<span style="color: #0000ff;">$SIG</span><span style="color: #66cc66;">&#123;</span>CHLD<span style="color: #66cc66;">&#125;</span> = <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span> <a href="http://perldoc.perl.org/functions/waitpid.html"><span style="color: #000066;">waitpid</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">-1</span>, WNOHANG<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#125;</span>;
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Parent exited<span style="color: #000099; font-weight: bold;">\\</span>n&quot;</span>;
<a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>If the parent is a web server process and writes back a html page, it might not return immediately. The solution is to close the STDIN, STDOUT, and STDERR in the child process.</p>
<pre class="perl"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl -w</span>
<span style="color: #000000; font-weight: bold;">use</span> strict;
<span style="color: #000000; font-weight: bold;">use</span> POSIX <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #66cc66;">&#40;</span>:sys_wait_h<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$kidpid</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<a href="http://perldoc.perl.org/functions/defined.html"><span style="color: #000066;">defined</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$kidpid</span> = <a href="http://perldoc.perl.org/functions/fork.html"><span style="color: #000066;">fork</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #808080; font-style: italic;"># fork returned undef, so failed</span>
<a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Failed to fork&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elsif</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$kidpid</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #808080; font-style: italic;"># fork returned 0, so this branch is child</span>
<a href="http://perldoc.perl.org/functions/close.html"><span style="color: #000066;">close</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">STDIN</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://perldoc.perl.org/functions/close.html"><span style="color: #000066;">close</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">STDOUT</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://perldoc.perl.org/functions/close.html"><span style="color: #000066;">close</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">STDERR</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://perldoc.perl.org/functions/exec.html"><span style="color: #000066;">exec</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;./longjob.pl&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #808080; font-style: italic;"># parent</span>
<span style="color: #0000ff;">$SIG</span><span style="color: #66cc66;">&#123;</span>CHLD<span style="color: #66cc66;">&#125;</span> = <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span> <a href="http://perldoc.perl.org/functions/waitpid.html"><span style="color: #000066;">waitpid</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">-1</span>, WNOHANG<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#125;</span>;
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Parent exited<span style="color: #000099; font-weight: bold;">\\</span>n&quot;</span>;
<a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a>;
<span style="color: #66cc66;">&#125;</span></pre>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fanswersy.com%2Fzchen%2F2007%2F10%2F31%2Fhow-to-fork-and-run-a-background-process-in-perl%2F';
  addthis_title  = 'How+to+Fork+and+Exec+a+Background+Process+in+Perl%3F';
  addthis_pub    = 'zchen050815';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://answersy.com/zchen/2007/10/31/how-to-fork-and-run-a-background-process-in-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to get all the CGI Parameters in Perl?</title>
		<link>http://answersy.com/zchen/2007/10/26/how-to-get-all-the-cgi-parameters-in-perl/</link>
		<comments>http://answersy.com/zchen/2007/10/26/how-to-get-all-the-cgi-parameters-in-perl/#comments</comments>
		<pubDate>Fri, 26 Oct 2007 03:24:35 +0000</pubDate>
		<dc:creator>zchen</dc:creator>
				<category><![CDATA[IT Related]]></category>
		<category><![CDATA[PHP/Perl]]></category>

		<guid isPermaLink="false">http://answersy.com/zchen/2007/10/26/how-to-get-all-the-cgi-parameters-in-perl/</guid>
		<description><![CDATA[Use CGI package: #!/usr/bin/perl -w use strict; use CGI qw/:standard/; my $cgivars = new CGI; my @names = $cgivars-&#62;param; foreach &#40;@names&#41; &#123; print &#34;$_ =&#62; &#34;, $cgivars-&#62;param&#40;$_&#41;, &#34;\\n&#34;; &#125; addthis_url = 'http%3A%2F%2Fanswersy.com%2Fzchen%2F2007%2F10%2F26%2Fhow-to-get-all-the-cgi-parameters-in-perl%2F'; addthis_title = 'How+to+get+all+the+CGI+Parameters+in+Perl%3F'; addthis_pub = 'zchen050815';]]></description>
			<content:encoded><![CDATA[<p>Use CGI package:</p>
<pre class="perl"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl -w</span>
<span style="color: #000000; font-weight: bold;">use</span> strict;
<span style="color: #000000; font-weight: bold;">use</span> CGI <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a>/:standard/;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cgivars</span> = <span style="color: #000000; font-weight: bold;">new</span> CGI;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@names</span> = <span style="color: #0000ff;">$cgivars</span>-&gt;<span style="color: #006600;">param</span>;
<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">@names</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;$_ =&gt; &quot;</span>, <span style="color: #0000ff;">$cgivars</span>-&gt;<span style="color: #006600;">param</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>n&quot;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fanswersy.com%2Fzchen%2F2007%2F10%2F26%2Fhow-to-get-all-the-cgi-parameters-in-perl%2F';
  addthis_title  = 'How+to+get+all+the+CGI+Parameters+in+Perl%3F';
  addthis_pub    = 'zchen050815';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://answersy.com/zchen/2007/10/26/how-to-get-all-the-cgi-parameters-in-perl/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>How to Define and Use &#8220;Global&#8221; Variables in Perl Modules?</title>
		<link>http://answersy.com/zchen/2007/10/10/how-to-define-and-use-global-variables-in-perl-modules/</link>
		<comments>http://answersy.com/zchen/2007/10/10/how-to-define-and-use-global-variables-in-perl-modules/#comments</comments>
		<pubDate>Wed, 10 Oct 2007 06:52:35 +0000</pubDate>
		<dc:creator>zchen</dc:creator>
				<category><![CDATA[IT Related]]></category>
		<category><![CDATA[PHP/Perl]]></category>

		<guid isPermaLink="false">http://answersy.com/zchen/2007/10/10/how-to-define-and-use-global-variables-in-perl-modules/</guid>
		<description><![CDATA[my_pkg.pm package my_pkg; use warnings; use strict; our &#40;@ISA, @EXPORT&#41;; @ISA = qw&#40;Exporter&#41;; @EXPORT = qw&#40;subName $varName&#41;; our $varName = &#34;XXXXX&#34;; sub subName&#40;$&#41; &#123; ...... &#125; my_caller.pl #!/usr/bin/perl -w use strict; use my_pkg; print &#34;$varName&#34;; subName&#40;$varName&#41;; addthis_url = 'http%3A%2F%2Fanswersy.com%2Fzchen%2F2007%2F10%2F10%2Fhow-to-define-and-use-global-variables-in-perl-modules%2F'; addthis_title = 'How+to+Define+and+Use+%26%238220%3BGlobal%26%238221%3B+Variables+in+Perl+Modules%3F'; addthis_pub = 'zchen050815';]]></description>
			<content:encoded><![CDATA[<p>my_pkg.pm</p>
<pre class="perl"><a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> my_pkg;
<span style="color: #000000; font-weight: bold;">use</span> warnings;
<span style="color: #000000; font-weight: bold;">use</span> strict;
<span style="color: #b1b100;">our</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">@ISA</span>, <span style="color: #0000ff;">@EXPORT</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">@ISA</span> = <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #66cc66;">&#40;</span>Exporter<span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">@EXPORT</span> = <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #66cc66;">&#40;</span>subName <span style="color: #0000ff;">$varName</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">our</span> <span style="color: #0000ff;">$varName</span> = <span style="color: #ff0000;">&quot;XXXXX&quot;</span>;
<span style="color: #000000; font-weight: bold;">sub</span> subName<span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
......
<span style="color: #66cc66;">&#125;</span></pre>
<p>my_caller.pl</p>
<pre class="perl"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl -w</span>
<span style="color: #000000; font-weight: bold;">use</span> strict;
<span style="color: #000000; font-weight: bold;">use</span> my_pkg;
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;$varName&quot;</span>;
subName<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$varName</span><span style="color: #66cc66;">&#41;</span>;</pre>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fanswersy.com%2Fzchen%2F2007%2F10%2F10%2Fhow-to-define-and-use-global-variables-in-perl-modules%2F';
  addthis_title  = 'How+to+Define+and+Use+%26%238220%3BGlobal%26%238221%3B+Variables+in+Perl+Modules%3F';
  addthis_pub    = 'zchen050815';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://answersy.com/zchen/2007/10/10/how-to-define-and-use-global-variables-in-perl-modules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.813 seconds -->

