<?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>Amoeba Solution Kiosk &#187; Perl Programming</title> <atom:link href="http://ask.amoeba.co.in/topics/perl-programming/feed/" rel="self" type="application/rss+xml" /><link>http://ask.amoeba.co.in</link> <description>Providing solutions for PHP, MySQL, Flash, Flex, Action Script, Javascript, YUI, JQuery, CSS, XHTML problems.</description> <lastBuildDate>Fri, 30 Dec 2011 18:12:55 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.2.1</generator> <item><title>Error Handling in Perl &amp; EVAL Block</title><link>http://ask.amoeba.co.in/error-handling-in-perl/</link> <comments>http://ask.amoeba.co.in/error-handling-in-perl/#comments</comments> <pubDate>Thu, 28 Jan 2010 17:32:25 +0000</pubDate> <dc:creator>Aneeska</dc:creator> <category><![CDATA[Perl Programming]]></category> <category><![CDATA[Programming Techniques]]></category> <category><![CDATA[directory existance check]]></category> <category><![CDATA[error handling]]></category> <category><![CDATA[error handling in perl]]></category> <category><![CDATA[ftp directory exists]]></category> <category><![CDATA[ftp in perl]]></category> <category><![CDATA[Net:FTP]]></category> <category><![CDATA[perl]]></category> <guid
isPermaLink="false">http://ask.amoeba.co.in/?p=76</guid> <description><![CDATA[This article covers two things. 1) Error handling in Perl using EVAL Block 2) Using the EVAL Block feature to check whether a directory exists on a remote FTP location. The first time ever I had to work in Perl was last month when I was asked to look in to an issue with sending [...]]]></description> <content:encoded><![CDATA[<p>This article covers two things.</p><p>1) Error handling in Perl using EVAL Block<br
/> 2) Using the EVAL Block feature to check whether a directory exists on a remote FTP location.</p><p>The first time ever I had to work in <strong>Perl </strong>was last month when I was asked to look in to an issue with sending a file to a remote FTP from the server. Basically, the perl script fetches a file from the server and put it into a remote <strong>FTP</strong> location. Before copying the file to FTP, the script changes the current working directory of the remote destination using the <strong>$ftp-cwd</strong> command. We are using <strong>Net:FTP</strong> library to work with FTP. If the folder doesn&#8217;t exist on the FTP, perl needs to create the folder and FTP the file there. We faced basically two problems. <strong>1) Using Net:FTP, there is no way to know that a folder exists or not. 2) The old versions of Perl doesn&#8217;t feature a dedicated error handling mechanism. </strong></p><p>Since we don&#8217;t have a way to know whether a folder exists on the FTP or not, the only other way is to try changing the directory and then catch the error if the directory didn&#8217;t exist.  We can use &#8220;<strong>or die</strong>&#8221; with the <strong>$ftp-&gt;cwd</strong> command to terminate the script if the director doesn&#8217;t exist. But that doesn&#8217;t solve our problem. The script should not be terminated, and it should create the directory and upload the file.  All I wanted was something like<strong> </strong>a<strong> try{} catch(){}</strong> which is available in most of the programming languages.</p><p>Finally we found an alternative way of implementing an error catching mechanism in perl. That is using the EVAL block.</p><p>Here is a sample use of<strong> EVAL Blcok.</strong></p><pre class="brush: php; html-script: true; title: ; notranslate">
# Sample code to demonstrate EVAL usage
eval {
$ftp-&gt;cwd($ftpdir) or die &quot;Directory doesn't exist. &quot;.$ftp-&gt;message;  //the script doesn't get terminated here because its in an eval block.
}
if ($@) {
$ftp-&gt;mkdir($ftpdir);
$ftp-&gt;cwd($ftpdir);
}
#Continue your script
</pre><p>If the CWD command caused an error &#8220;$@&#8221; will hold the string value passed to the die command. If &#8220;$@&#8221; is null, we know that there is no error and just don&#8217;t do anything to handle the error. What I found out was we really need the die command inside the block and without the die command &#8220;$@&#8221; returned null though there was an error.</p><p>Thanks,<br
/> Anees</p><div
class="bottomcontainerBox" style=""><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <iframe
src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fask.amoeba.co.in%2Ferror-handling-in-perl%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div><div
style="float:left; width:65px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <g:plusone size="medium" href="http://ask.amoeba.co.in/error-handling-in-perl/"></g:plusone></div><div
style="float:left; width:100px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://ask.amoeba.co.in/error-handling-in-perl/"  data-text="Error Handling in Perl &#038; EVAL Block" data-count="horizontal" data-via="aneesme">Tweet</a></div><div
style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://ask.amoeba.co.in/error-handling-in-perl/" data-counter="right"></script></div><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://ask.amoeba.co.in/error-handling-in-perl/"></script></div></div><div
style="clear:both"></div><div
style="padding-bottom:4px;"></div>]]></content:encoded> <wfw:commentRss>http://ask.amoeba.co.in/error-handling-in-perl/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
