<?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>Artful Code &#187; perl</title>
	<atom:link href="http://www.artfulcode.net/tags/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artfulcode.net</link>
	<description>Resources and tips for dynamic, interactive languages.</description>
	<lastBuildDate>Fri, 09 Sep 2011 02:15:45 +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>goto statement considered</title>
		<link>http://www.artfulcode.net/articles/goto-statement-considered/</link>
		<comments>http://www.artfulcode.net/articles/goto-statement-considered/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 03:09:40 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Soap box]]></category>
		<category><![CDATA[goto]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/?p=816</guid>
		<description><![CDATA[Everyone knows not to use goto, right?  Dijkstra's words on the subject have risen to the level of gospel in the programming community. Not that I do not completely agree... in most cases.]]></description>
			<content:encoded><![CDATA[<p>Everyone knows not to use goto, right?  Dijkstra&#8217;s words on the subject have risen to the level of gospel in the programming community. Not that I do not completely agree&#8230; in most cases.<span id="more-816"></span></p>
<p>I have to admit that I occasionally resort to goto myself. This week was one such an occasion. But before you pick up your torch and farming implement of choice and join the mob heading for my castle gates, please consider that  Dijkstra&#8217;s letter was complaining about unrestricted goto, which was a popular feature of languages at the time. It was an unstructured precursor of the subroutine and structured programming, which put goto on a leash and taught it not to bark at the neighbors. In restricting goto to local jumps, as most modern languages supporting the feature do, the beast became much less fearsome. In fact, a while loop is just a structured goto, as is try/catch (albeit with a little extra magic.)</p>
<p>Perl, of course, does not impose such restrictions on a programmer (although it does prohibit jumping into blocks that require initialization, such as other functions.) With a little discipline, though, goto is a feature and quite useful. It can be used to make extremely complex loops explicit and easy to follow as well as allow a loop to break to any outer loop in the current call stack.</p>
<p>As an example, I recently wrote a function to increment a string of arbitrary alphanumeric characters, where letters must maintain their case but increment as base-26 numbers and numbers increment normally (base 10). For example, if a letter increments past z, it becomes a and the next character to the left is then incremented. The increment logic fit into a function that took two of these strings and calculated all possible values in between to support some limited set operations against two alphanumeric ranges.</p>
<p>My original code for the increment logic was grisly at best. It took a series of nested while loops and state variables that were incremented or decremented as needed. I avoided recursion because the sequences were of arbitrary length. Breaking the code into subroutines made it more complex to maintain loop state. Using goto allowed me to break apart the logic into blocks without the overhead or state-passing necessary to shoehorn the logic into subroutines. I was able to label each part clearly and jump between them as needed to increment or &#8220;carry the 1.&#8221; I reduced the size of the code to just a couple dozen lines, including safety checks, and found that the goto code was much easier to read and reason about.</p>
<p>That said, I want to emphasize strongly that goto should be kept local. Jumping to outer function calls makes the logic of a program difficult to reason about and changes scope in confusing ways. But as a way of structuring complex loops and iterative logic, you just can&#8217;t beat goto.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fgoto-statement-considered%2F&amp;title=goto+statement+considered" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fgoto-statement-considered%2F&amp;title=goto+statement+considered" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=goto+statement+considered&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fgoto-statement-considered%2F&amp;title=goto+statement+considered" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fgoto-statement-considered%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fgoto-statement-considered%2F&amp;title=goto+statement+considered" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fgoto-statement-considered%2F&amp;title=goto+statement+considered" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fgoto-statement-considered%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+goto+statement+considered+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fgoto-statement-considered%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/goto-statement-considered/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Why Ruby is an acceptable Perl</title>
		<link>http://www.artfulcode.net/articles/why-ruby-acceptable-perl/</link>
		<comments>http://www.artfulcode.net/articles/why-ruby-acceptable-perl/#comments</comments>
		<pubDate>Fri, 01 Jun 2007 01:56:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Soap box]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/why-ruby-acceptable-perl/</guid>
		<description><![CDATA[Everyone has read this article. It explains how Ruby&#8217;s enumerable effectively makes it a lisp. Like all we arrogant lispers, I of course disagree. That&#8217;s not to say that Ruby isn&#8217;t a good language, though. It&#8217;s just that it&#8217;s more idiomatic of Perl than Lisp. It does have its big selling point, enumerable. Enumerable is [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone has read <a href="http://www.randomhacks.net/articles/2005/12/03/why-ruby-is-an-acceptable-lisp">this article</a>.  It explains how Ruby&#8217;s enumerable effectively makes it a lisp.  Like all we arrogant lispers, I of course disagree.  That&#8217;s not to say that Ruby isn&#8217;t a good language, though.  It&#8217;s just that it&#8217;s more idiomatic of Perl than Lisp.<span id="more-60"></span></p>
<p>It does have its big selling point, enumerable.  Enumerable is nice.  Blocks are nice, although they are not as powerful as lambdas.  One big selling point that you rarely see mentioned, though, is Perl-style regexp.</p>
<p>Nearly every language can do regular expression matching using Perl-style regular expressions.  However, few have built-in operators for it &#8211; =~ to match an expression and s/foo/bar to modify an expression.  PHP&#8217;s functions, by comparison, are a huge code overhead.  The same is true for Python.  For example, in PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;hello world&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/world/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Jeff&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&#8230;not to mention having to first import the regex library into Python:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
regex = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'world'</span><span style="color: black;">&#41;</span>
x = <span style="color: #483d8b;">&quot;hello world&quot;</span>
x = regex.<span style="color: black;">sub</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Jeff&quot;</span>, x<span style="color: black;">&#41;</span></pre></div></div>

<p>Whereas in Perl you just have:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">$x</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;hello world&quot;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$x</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/world/Jeff/</span><span style="color: #339933;">;</span></pre></div></div>

<p>Ruby has similar syntax and a powerful regexp library.  It also has Perl&#8217;s syntactic freedom &#8211; that is, things can be written in many different ways.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">i = <span style="color:#996600;">&quot;Hello world&quot;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> i == <span style="color:#996600;">&quot;Hello world&quot;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> i
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>or&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> i <span style="color:#9966CC; font-weight:bold;">if</span> i == <span style="color:#996600;">&quot;Hello world&quot;</span></pre></div></div>

<p>Functions also do not require parenthesis if the meaning is clear:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span><span style="color:#006600; font-weight:bold;">&#40;</span>i<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>or&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">puts i</pre></div></div>

<p>This is often called &#8220;poetry mode&#8221; by Rubyists.  However, few Rubyists like to compare their language with Perl because Ruby is religiously object oriented, and Perl is more of an example on how not to implement OOP.</p>
<p>But Ruby does have its origins in Perl.  It inherits a lot of features from languages like Smalltalk and lisp, but if you look at a large Ruby program, you begin to see how Perl-like it is.</p>
<p>And that is Ruby&#8217;s biggest problem as well.  As with Perl, syntactic freedom means that the neatness of code is often a product of the mood of the programmer or the deadline.  Coming back to a program after six months can mean hours of analyzing code, trying to figure out what on earth you were thinking when you wrote that.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwhy-ruby-acceptable-perl%2F&amp;title=Why+Ruby+is+an+acceptable+Perl" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwhy-ruby-acceptable-perl%2F&amp;title=Why+Ruby+is+an+acceptable+Perl" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Why+Ruby+is+an+acceptable+Perl&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwhy-ruby-acceptable-perl%2F&amp;title=Why+Ruby+is+an+acceptable+Perl" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwhy-ruby-acceptable-perl%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwhy-ruby-acceptable-perl%2F&amp;title=Why+Ruby+is+an+acceptable+Perl" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwhy-ruby-acceptable-perl%2F&amp;title=Why+Ruby+is+an+acceptable+Perl" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwhy-ruby-acceptable-perl%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Why+Ruby+is+an+acceptable+Perl+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwhy-ruby-acceptable-perl%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/why-ruby-acceptable-perl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

