<?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"
	>
<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>
	<pubDate>Tue, 06 Jan 2009 05:29:02 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Zachary Fox</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl#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's exactly what I'm looking for. I'm going to update the post with a second version taken from the comments. It will definitely be more "perly."</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-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'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 'my'

$n = &#60;STDIN&#62;         # $n here will be global which could end up being messy
my $n = &#60;STDIN&#62;   # $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's a newline.  This is a nice piece of idiot-proofing for those times when you're absolutely 100% positive your string ends with a newline, but you are nonetheless wrong ;)

- Don't use the &#38; when calling a sub (unless you understand what the &#38; does) - just include the parens at the end so the parser understands you're making a sub call.  Older Perl books/tutorials generally included the &#38; on the front of a sub call, but it's not necessary and can break things in ways you're not expecting if you don't do lots of Perl.

&#38;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't require any args, you just include empty parens

- The variables $a and $b are used by the builtin function sort().  You aren't using sort() in your code, so in this case your $a and $b variables aren't going to hurt anything, but it'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' values without using a temporary variable like so:

$a = 'one';
$b = 'two';
($a, $b) = ($b, $a);
print "$a -- $b";   #  'two -- one'

- Last, your main sub isn't necessary.  Any code that isn't wrapped in a sub becomes your program's mainline.  The little example below will run the first two lines of code even though they aren't in a specifically named and called sub.

#################
my $var = mysub();
print "$var\n";

sub mysub {
  return 'hi there';
}
#################



- OK - putting it all together we get this:

#!/usr/bin/perl

print "How many numbers of the sequence would you like? ";
chomp( my $n = &#60;STDIN&#62; );
print "\n";   # Just to visually separate the user's input from the program's output
fibonacci($n);

sub fibonacci {
  my $a = 0;
  my $b = 1;
  for( 0 .. ($_[0] - 1) ) {
    print "$a\n";
    ($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 - 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) - 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 - 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 - 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] - 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-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'll substitute the "less than" 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't used as commen as normal print, you can just write
print "$a\n"; , variables get substituted in"'s just like in php.

also: how about some golf:

++$b;for(0..lt&#62;-1){print"$a\n";($b)=($s,$a)=($a+$b,$b);}

works just as well :) (remember to replace the "lt"</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;&#8217;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>
