<?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>dangoldin &#187; Perl</title>
	<atom:link href="http://www.dangoldin.com/category/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dangoldin.com</link>
	<description>My thoughts and musings</description>
	<lastBuildDate>Sun, 09 Jan 2011 00:05:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Interesting Perl Behavior</title>
		<link>http://www.dangoldin.com/2008/05/30/interesting-perl-behavior/</link>
		<comments>http://www.dangoldin.com/2008/05/30/interesting-perl-behavior/#comments</comments>
		<pubDate>Fri, 30 May 2008 18:56:59 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.dangoldin.com/blog/?p=17</guid>
		<description><![CDATA[I ran into this problem a while back and wanted to share it. It was a bit unintuitive but documentd so I guess I shouldn&#8217;t be surprised by the results. Hopefully this will help someone else avoid this pitfall. It looks as if declaring a variable with the &#8220;my&#8221; statement but then guarded with an [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into this problem a while back and wanted to share it. It was a bit unintuitive but documentd so I guess I shouldn&#8217;t be surprised by the results. Hopefully this will help someone else avoid this pitfall.</p>
<p>It looks as if declaring a variable with the &#8220;my&#8221; statement but then guarded with an &#8220;if&#8221; statement causes the scope of the variable to be global &#8211; note that the &#8220;use strict &#8216;vars&#8217;;&#8221; pragma does not give an error in this case.</p>
<blockquote><p>#!/usr/bin/perl -w<br />
use strict  &#8216;vars&#8217;;</p>
<p>sub foo{<br />
my $val = 0 if (0);<br />
$val = 1 unless defined($val);<br />
print &#8220;Val: $val\n&#8221;;<br />
$val = 2;<br />
}</p>
<p>foo();<br />
foo();</p></blockquote>
<p>The output of this call gives:<br />
Val: 1<br />
Val: 2</p>
<p>Although the expected result would seem to be:<br />
Val: 1<br />
Val: 1</p>
<p>Using Google, I found the following nugget from perlsyn:</p>
<blockquote><p>NOTE: The behaviour of a my statement modified with a statement modifier conditional or loop construct (e.g. my $x if &#8230; ) is undefined. The value of the my variable may be undef, any previously assigned value, or possibly anything else. Don&#8217;t rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.<br />
<a href="http://perldoc.perl.org/perlsyn.html#Statement-Modifiers">http://perldoc.perl.org/perlsyn.html#Statement-Modifiers</a></p></blockquote>
<p><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.dangoldin.com%2F2008%2F05%2F30%2Finteresting-perl-behavior%2F&amp;linkname=Interesting%20Perl%20Behavior" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.dangoldin.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_friendfeed" href="http://www.addtoany.com/add_to/friendfeed?linkurl=http%3A%2F%2Fwww.dangoldin.com%2F2008%2F05%2F30%2Finteresting-perl-behavior%2F&amp;linkname=Interesting%20Perl%20Behavior" title="FriendFeed" rel="nofollow" target="_blank"><img src="http://www.dangoldin.com/wp-content/plugins/add-to-any/icons/friendfeed.png" width="16" height="16" alt="FriendFeed"/></a><a class="a2a_button_digg" href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fwww.dangoldin.com%2F2008%2F05%2F30%2Finteresting-perl-behavior%2F&amp;linkname=Interesting%20Perl%20Behavior" title="Digg" rel="nofollow" target="_blank"><img src="http://www.dangoldin.com/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fwww.dangoldin.com%2F2008%2F05%2F30%2Finteresting-perl-behavior%2F&amp;linkname=Interesting%20Perl%20Behavior" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://www.dangoldin.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a><a class="a2a_button_yahoo_buzz" href="http://www.addtoany.com/add_to/yahoo_buzz?linkurl=http%3A%2F%2Fwww.dangoldin.com%2F2008%2F05%2F30%2Finteresting-perl-behavior%2F&amp;linkname=Interesting%20Perl%20Behavior" title="Yahoo Buzz" rel="nofollow" target="_blank"><img src="http://www.dangoldin.com/wp-content/plugins/add-to-any/icons/buzz.png" width="16" height="16" alt="Yahoo Buzz"/></a><a class="a2a_button_google_reader" href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fwww.dangoldin.com%2F2008%2F05%2F30%2Finteresting-perl-behavior%2F&amp;linkname=Interesting%20Perl%20Behavior" title="Google Reader" rel="nofollow" target="_blank"><img src="http://www.dangoldin.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.dangoldin.com%2F2008%2F05%2F30%2Finteresting-perl-behavior%2F&amp;linkname=Interesting%20Perl%20Behavior" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.dangoldin.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.dangoldin.com%2F2008%2F05%2F30%2Finteresting-perl-behavior%2F&amp;title=Interesting%20Perl%20Behavior" id="wpa2a_2"><img src="http://www.dangoldin.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dangoldin.com/2008/05/30/interesting-perl-behavior/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

