<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: use autodie;</title>
	<atom:link href="http://blog.bluefeet.net/2009/06/use-autodie/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.bluefeet.net/2009/06/use-autodie/</link>
	<description>Yet another Perl Hacker weblog</description>
	<lastBuildDate>Wed, 23 Jun 2010 03:52:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: racer</title>
		<link>http://blog.bluefeet.net/2009/06/use-autodie/comment-page-1/#comment-62</link>
		<dc:creator>racer</dc:creator>
		<pubDate>Mon, 20 Jul 2009 10:43:02 +0000</pubDate>
		<guid isPermaLink="false">http://blog.bluefeet.net/?p=80#comment-62</guid>
		<description>Chas.Owens, you forgot

&lt;code&gt; do &lt;/code&gt;

after that

&lt;code&gt; or &lt;/code&gt;

:)

unlink(&#039;testfile.txt&#039;) if -e &#039;testfile.txt&#039;;   is a race condition, testfile.txt could exist when -e &#039;testfile.txt&#039;; is called, but be deleted before unlink is called.

Not to worry, since autodie and $fh are lexically scoped, you could simply write

&lt;code&gt;
    use autodie;
    eval {
        open( my $fh, &#039;&lt;&#039;, &#039;testfile.txt&#039; );
        print $fh &quot;Hello world!\n&quot;;
        1;
    } or do {
        no autodie &#039;unlink&#039;;
        unlink &#039;testfile.txt&#039;;
        die;
    };
&lt;/code&gt;

or

&lt;code&gt;
    my $file = q&#039;testfile.txt&#039;;
    eval {
        use autodie;
        open( my $fh, &#039;&lt;&#039;, $file );
        print $fh &quot;Hello world!\n&quot;;
        1;
    } or do {
        unlink $file;
        die
    };
&lt;/code&gt;

</description>
		<content:encoded><![CDATA[<p>Chas.Owens, you forgot</p>
<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;do</div></div>
<p>after that</p>
<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;or</div></div>
<p>:)</p>
<p>unlink(&#8216;testfile.txt&#8217;) if -e &#8216;testfile.txt&#8217;;   is a race condition, testfile.txt could exist when -e &#8216;testfile.txt&#8217;; is called, but be deleted before unlink is called.</p>
<p>Not to worry, since autodie and $fh are lexically scoped, you could simply write</p>
<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; use autodie;<br />
&nbsp; &nbsp; eval {<br />
&nbsp; &nbsp; &nbsp; &nbsp; open( my $fh, '&lt;', 'testfile.txt' );<br />
&nbsp; &nbsp; &nbsp; &nbsp; print $fh &quot;Hello world!\n&quot;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; 1;<br />
&nbsp; &nbsp; } or do {<br />
&nbsp; &nbsp; &nbsp; &nbsp; no autodie 'unlink';<br />
&nbsp; &nbsp; &nbsp; &nbsp; unlink 'testfile.txt';<br />
&nbsp; &nbsp; &nbsp; &nbsp; die;<br />
&nbsp; &nbsp; };</div></div>
<p>or</p>
<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; my $file = q'testfile.txt';<br />
&nbsp; &nbsp; eval {<br />
&nbsp; &nbsp; &nbsp; &nbsp; use autodie;<br />
&nbsp; &nbsp; &nbsp; &nbsp; open( my $fh, '&lt;', $file );<br />
&nbsp; &nbsp; &nbsp; &nbsp; print $fh &quot;Hello world!\n&quot;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; 1;<br />
&nbsp; &nbsp; } or do {<br />
&nbsp; &nbsp; &nbsp; &nbsp; unlink $file;<br />
&nbsp; &nbsp; &nbsp; &nbsp; die<br />
&nbsp; &nbsp; };</div></div>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chas. Owens</title>
		<link>http://blog.bluefeet.net/2009/06/use-autodie/comment-page-1/#comment-9</link>
		<dc:creator>Chas. Owens</dc:creator>
		<pubDate>Tue, 30 Jun 2009 15:39:20 +0000</pubDate>
		<guid isPermaLink="false">http://blog.bluefeet.net/?p=80#comment-9</guid>
		<description>Your eval version can be a little cleaner:

my $file = &quot;testfile.txt&quot;;
eval {
    open my $fh, &quot;&gt;&quot;, $file;
    print {$fh} &quot;Hello world!\n&quot;;
    close $fh;
} or {
    warn $@;
    unlink $file if -e $file;
    exit 255;
}

Since close will return true if it succeeds, we can use that return to build a try/catch like system that is not dependent on $@ (which could be changed by an errant DESTROY method that failed to localize it).  Also, warning about the error means that if the unlink fails it won&#039;t mask the error from the eval.</description>
		<content:encoded><![CDATA[<p>Your eval version can be a little cleaner:</p>
<p>my $file = &#8220;testfile.txt&#8221;;<br />
eval {<br />
    open my $fh, &#8220;&gt;&#8221;, $file;<br />
    print {$fh} &#8220;Hello world!\n&#8221;;<br />
    close $fh;<br />
} or {<br />
    warn $@;<br />
    unlink $file if -e $file;<br />
    exit 255;<br />
}</p>
<p>Since close will return true if it succeeds, we can use that return to build a try/catch like system that is not dependent on $@ (which could be changed by an errant DESTROY method that failed to localize it).  Also, warning about the error means that if the unlink fails it won&#8217;t mask the error from the eval.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
