<?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: Fibonacci in Perl</title>
	<atom:link href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl/feed" rel="self" type="application/rss+xml" />
	<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl</link>
	<description></description>
	<lastBuildDate>Sat, 22 Oct 2011 10:44:23 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Zachary Fox</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl/comment-page-1#comment-141</link>
		<dc:creator>Zachary Fox</dc:creator>
		<pubDate>Fri, 04 Jan 2008 16:15:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl#comment-141</guid>
		<description>Simon and Tim,

Thanks! That&#039;s exactly what I&#039;m looking for. I&#039;m going to update the post with a second version taken from the comments. It will definitely be more &quot;perly.&quot;</description>
		<content:encoded><![CDATA[<p>Simon and Tim,</p>
<p>Thanks! That&#8217;s exactly what I&#8217;m looking for. I&#8217;m going to update the post with a second version taken from the comments. It will definitely be more &#8220;perly.&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl/comment-page-1#comment-139</link>
		<dc:creator>Tim</dc:creator>
		<pubDate>Fri, 04 Jan 2008 07:29:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl#comment-139</guid>
		<description>Your Perl version runs fine, but you&#039;ve got a number of conventions that, while not exactly wrong, are a bit dated (looks like you were 
using a pretty old Perl tutorial/book/whatever).  Here are some explanations followed by a more Perl-y bit of code:

- Scope your variables with &#039;my&#039;

$n = &lt;STDIN&gt;         # $n here will be global which could end up being messy
my $n = &lt;STDIN&gt;   # $n here is lexical - it falls out of scope at the end of the sub

- Use chomp() instead of chop().  chop() removes the last character from a string regardless of what the character is; chomp() only removes the last character if it&#039;s a newline.  This is a nice piece of idiot-proofing for those times when you&#039;re absolutely 100% positive your string ends with a newline, but you are nonetheless wrong ;)

- Don&#039;t use the &amp; when calling a sub (unless you understand what the &amp; does) - just include the parens at the end so the parser understands you&#039;re making a sub call.  Older Perl books/tutorials generally included the &amp; on the front of a sub call, but it&#039;s not necessary and can break things in ways you&#039;re not expecting if you don&#039;t do lots of Perl.

&amp;fibonacci(5);   # Generally considered bad, though it works and hurts nothing in your example
fibonacci(5);     # works just fine, regardless of where you defined the sub
fibonacci();       # if your sub didn&#039;t require any args, you just include empty parens

- The variables $a and $b are used by the builtin function sort().  You aren&#039;t using sort() in your code, so in this case your $a and $b variables aren&#039;t going to hurt anything, but it&#039;s generally a good idea to get in the habit of staying away from those particular variable names - use $x and $y or whatever instead.  If your code or any external code (a module or library) that you might be using uses sort anywhere and your code includes $a and $b, you can occasionally get hard-to-track-down bugs.

- In Perl, you can swap two variables&#039; values without using a temporary variable like so:

$a = &#039;one&#039;;
$b = &#039;two&#039;;
($a, $b) = ($b, $a);
print &quot;$a -- $b&quot;;   #  &#039;two -- one&#039;

- Last, your main sub isn&#039;t necessary.  Any code that isn&#039;t wrapped in a sub becomes your program&#039;s mainline.  The little example below will run the first two lines of code even though they aren&#039;t in a specifically named and called sub.

#################
my $var = mysub();
print &quot;$var\n&quot;;

sub mysub {
  return &#039;hi there&#039;;
}
#################



- OK - putting it all together we get this:

#!/usr/bin/perl

print &quot;How many numbers of the sequence would you like? &quot;;
chomp( my $n = &lt;STDIN&gt; );
print &quot;\n&quot;;   # Just to visually separate the user&#039;s input from the program&#039;s output
fibonacci($n);

sub fibonacci {
  my $a = 0;
  my $b = 1;
  for( 0 .. ($_[0] - 1) ) {
    print &quot;$a\n&quot;;
    ($a, $b) = ($b, ($a + $b));
  }
}</description>
		<content:encoded><![CDATA[<p>Your Perl version runs fine, but you&#8217;ve got a number of conventions that, while not exactly wrong, are a bit dated (looks like you were<br />
using a pretty old Perl tutorial/book/whatever).  Here are some explanations followed by a more Perl-y bit of code:</p>
<p>- Scope your variables with &#8216;my&#8217;</p>
<p>$n = &lt;STDIN&gt;         # $n here will be global which could end up being messy<br />
my $n = &lt;STDIN&gt;   # $n here is lexical &#8211; it falls out of scope at the end of the sub</p>
<p>- Use chomp() instead of chop().  chop() removes the last character from a string regardless of what the character is; chomp() only removes the last character if it&#8217;s a newline.  This is a nice piece of idiot-proofing for those times when you&#8217;re absolutely 100% positive your string ends with a newline, but you are nonetheless wrong <img src='http://www.zacharyfox.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>- Don&#8217;t use the &amp; when calling a sub (unless you understand what the &amp; does) &#8211; just include the parens at the end so the parser understands you&#8217;re making a sub call.  Older Perl books/tutorials generally included the &amp; on the front of a sub call, but it&#8217;s not necessary and can break things in ways you&#8217;re not expecting if you don&#8217;t do lots of Perl.</p>
<p>&amp;fibonacci(5);   # Generally considered bad, though it works and hurts nothing in your example<br />
fibonacci(5);     # works just fine, regardless of where you defined the sub<br />
fibonacci();       # if your sub didn&#8217;t require any args, you just include empty parens</p>
<p>- The variables $a and $b are used by the builtin function sort().  You aren&#8217;t using sort() in your code, so in this case your $a and $b variables aren&#8217;t going to hurt anything, but it&#8217;s generally a good idea to get in the habit of staying away from those particular variable names &#8211; use $x and $y or whatever instead.  If your code or any external code (a module or library) that you might be using uses sort anywhere and your code includes $a and $b, you can occasionally get hard-to-track-down bugs.</p>
<p>- In Perl, you can swap two variables&#8217; values without using a temporary variable like so:</p>
<p>$a = &#8216;one&#8217;;<br />
$b = &#8216;two&#8217;;<br />
($a, $b) = ($b, $a);<br />
print &#8220;$a &#8212; $b&#8221;;   #  &#8216;two &#8212; one&#8217;</p>
<p>- Last, your main sub isn&#8217;t necessary.  Any code that isn&#8217;t wrapped in a sub becomes your program&#8217;s mainline.  The little example below will run the first two lines of code even though they aren&#8217;t in a specifically named and called sub.</p>
<p>#################<br />
my $var = mysub();<br />
print &#8220;$var\n&#8221;;</p>
<p>sub mysub {<br />
  return &#8216;hi there&#8217;;<br />
}<br />
#################</p>
<p>- OK &#8211; putting it all together we get this:</p>
<p>#!/usr/bin/perl</p>
<p>print &#8220;How many numbers of the sequence would you like? &#8220;;<br />
chomp( my $n = &lt;STDIN&gt; );<br />
print &#8220;\n&#8221;;   # Just to visually separate the user&#8217;s input from the program&#8217;s output<br />
fibonacci($n);</p>
<p>sub fibonacci {<br />
  my $a = 0;<br />
  my $b = 1;<br />
  for( 0 .. ($_[0] &#8211; 1) ) {<br />
    print &#8220;$a\n&#8221;;<br />
    ($a, $b) = ($b, ($a + $b));<br />
  }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon Stroh</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl/comment-page-1#comment-137</link>
		<dc:creator>Simon Stroh</dc:creator>
		<pubDate>Fri, 04 Jan 2008 05:21:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl#comment-137</guid>
		<description>hm, it seams part of my comment has been filtered out.
what I meant: i&#039;ll substitute the &quot;less than&quot; sign with lt.

I really like what you’re doing here and think it’s a great idea, but you do use practically the same code everywhere. I’d like to see more language specific construct, for example, it’s not common to write
for ($i=0;$i lt $n; $i++)
but instead:
for(0...$n-1)
also, i think printf isn&#039;t used as commen as normal print, you can just write
print &quot;$a\n&quot;; , variables get substituted in&quot;&#039;s just like in php.

also: how about some golf:

++$b;for(0..lt&gt;-1){print&quot;$a\n&quot;;($b)=($s,$a)=($a+$b,$b);}

works just as well :) (remember to replace the &quot;lt&quot;</description>
		<content:encoded><![CDATA[<p>hm, it seams part of my comment has been filtered out.<br />
what I meant: i&#8217;ll substitute the &#8220;less than&#8221; sign with lt.</p>
<p>I really like what you’re doing here and think it’s a great idea, but you do use practically the same code everywhere. I’d like to see more language specific construct, for example, it’s not common to write<br />
for ($i=0;$i lt $n; $i++)<br />
but instead:<br />
for(0&#8230;$n-1)<br />
also, i think printf isn&#8217;t used as commen as normal print, you can just write<br />
print &#8220;$a\n&#8221;; , variables get substituted in&#8221;&#8216;s just like in php.</p>
<p>also: how about some golf:</p>
<p>++$b;for(0..lt&gt;-1){print&#8221;$a\n&#8221;;($b)=($s,$a)=($a+$b,$b);}</p>
<p>works just as well <img src='http://www.zacharyfox.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  (remember to replace the &#8220;lt&#8221;</p>
]]></content:encoded>
	</item>
</channel>
</rss>

