<?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>Labix Blog &#187; Perl</title>
	<atom:link href="http://blog.labix.org/tag/perl/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.labix.org</link>
	<description>by Gustavo Niemeyer</description>
	<lastBuildDate>Fri, 09 Jul 2010 19:15:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>The forgotten art of error checking</title>
		<link>http://blog.labix.org/2010/06/17/the-forgotten-art-of-error-checking</link>
		<comments>http://blog.labix.org/2010/06/17/the-forgotten-art-of-error-checking#comments</comments>
		<pubDate>Thu, 17 Jun 2010 15:15:59 +0000</pubDate>
		<dc:creator>Gustavo Niemeyer</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Go]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[Test]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=275</guid>
		<description><![CDATA[I was just rambling randomly yesterday, in the usual microblogging platforms, about how result checking seems to be ignored or done badly. The precise wording was: It&#8217;s really amazing how little attention error handling receives in most software development. Even &#8230; <a href="http://blog.labix.org/2010/06/17/the-forgotten-art-of-error-checking">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I was just rambling randomly yesterday, in the usual microblogging platforms, about how result checking seems to be ignored or done badly.  The precise wording was:</p>
<blockquote><p>
It&#8217;s really amazing how little attention error handling receives in most software development. Even *tutorials* often ignore it.
</p></blockquote>
<p>It indeed does amaze me.  It sometimes feels like we write code for theoretical perfect worlds.. <i>&#8220;If the processor executes exactly in this order, and the weather is calm, this program will work.&#8221;</i>.  There are countless examples of bad assumptions.. someday I will come with some statistics of the form <i>&#8220;Every N seconds someone forgets to check the result of write().&#8221;</i>.</p>
<p><span id="more-275"></span></p>
<p>If you are a teacher, or a developer that enjoys writing snippets of code to teach people, please join me in the quest of building a better future.  Do <i>not</i> tell us that you&#8217;re &#8220;avoiding result checking for terseness&#8221;, because that&#8217;s exactly what we people will do (terseness is good, right?).  On the contrary, take this chance to make us feel <i>bad</i> about avoiding result checking.  You might do this by putting a comment like &#8220;If you don&#8217;t do this, you&#8217;re a bad programmer.&#8221; right next to the logic which is handling the result, and might take this chance to teach people how proper result handling is done.</p>
<p>Of course, there&#8217;s another forgotten art related to result checking.  It sits on the other side of the fence.  If you are a library author, do think through about how you plan to make us check conditions which happen inside your library, and try to imagine how to make our lives easier.  If we suck at handling results when there are obvious ways to handle it, you can imagine what happens when you structure your result logic badly.</p>
<p>Here is a clear example of what <i>not</i> to do, coming straight from Python&#8217;s standard library, in the <i>imaplib</i> module:</p>
<pre>
    def login(self, user, password):
        typ, dat = self._simple_command('LOGIN', user, self._quote(password))
        if typ != 'OK':
            raise self.error(dat[-1])
        self.state = 'AUTH'
        return typ, dat
</pre>
<p>You see the problem there?  How do you handle errors from this library?  Should we catch the exception, or should we verify the result code? <i>&#8220;Both!&#8221;</i> is the right answer, unfortunately, because the author decided to do us a little favor and check the error condition himself in some arbitrary cases and raise the error, while letting it go through and end up in the result code in a selection of other arbitrary cases.</p>
<p>I may provide some additional advice on result handling in the future, but for now I&#8217;ll conclude with the following suggestion: please check the results from your actions, and help others to check theirs.  That&#8217;s a good life-encompassing recommendation, actually.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2010/06/17/the-forgotten-art-of-error-checking/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Using pyperl to build a python-like xchat interface for perl</title>
		<link>http://blog.labix.org/2003/10/27/using-pyperl-to-build-a-python-like-xchat-interface-for-perl</link>
		<comments>http://blog.labix.org/2003/10/27/using-pyperl-to-build-a-python-like-xchat-interface-for-perl#comments</comments>
		<pubDate>Mon, 27 Oct 2003 06:06:00 +0000</pubDate>
		<dc:creator>Gustavo Niemeyer</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Snippet]]></category>

		<guid isPermaLink="false">http://blog.labix.org/2003/10/27/using-pyperl-to-build-a-python-like-xchat-interface-for-perl/</guid>
		<description><![CDATA[My weekend was taken by boring academic stuff. More specifically, I&#8217;ve built a validator in C, including a tokenizer, a parser, and a scope system, to check for a small subset of the C language syntax. Anyway.. I was still &#8230; <a href="http://blog.labix.org/2003/10/27/using-pyperl-to-build-a-python-like-xchat-interface-for-perl">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>My weekend was taken by boring academic stuff. More specifically, I&#8217;ve built a validator in C, including a tokenizer, a parser, and a scope system, to check for a small subset of the C language syntax. Anyway.. I was still able to do something interesting after all. <img src='http://blog.labix.org/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Taking further my research about <a href="http://www.python.org/cgi-bin/moinmoin/PyPerl">pyperl</a>, I&#8217;ve wondered if it would be possible to wrap the <a href="https://moin.conectiva.com.br/XchatPython">XchatPython</a> plugin interface for perl usage. Actually, it turned out that besides being possible, it was easy. It was just a matter of putting the following wrapper in the <tt>~/.xchat2</tt> directory:</p>
<pre>
import xchat
import perl
perl.eval("$xchat = Python::eval('xchat')")
perl.eval("do '" + __file__[:-3] + ".pyl'")
__module_name__ = perl.eval("$__module_name__")
__module_version__ = perl.eval("$__module_version__")
__module_description__ = perl.eval("$__module_description__")
</pre>
<p>This wrapper will load a file with the same name as itself, but with a .pyl extension, which should contain perl code defining the plugin (I haven&#8217;t used the .pl extension because this would conflict with the internal perl plugin of xchat). That code may access the <tt>$xchat</tt> object to interface with xchat, using the same API as defined in XchatPython.</p>
<p>Here is a small sample module in perl, defining the /test command: </p>
<pre>
#!/usr/bin/perl
$__module_name__ = "perlwrap";
$__module_version__ = "1.0";
$__module_description__ = "Testing perlwrap.";

sub test {
    $xchat-&gt;prnt("Test command!");
    return $xchat-&gt;EAT_ALL;
}

$xchat-&gt;hook_command("test", \&amp;test);

$xchat-&gt;prnt("Plugin perlwrap loaded!\n");
</pre>
<p>Notice that the interface is exactly the same, including the meta-information. </p>
<p>Unfortunately, there seems to exist some bug in the MULTI_PERL support of pyperl which makes it lose track of function references once the python support commutes the thread states. To make this trick work, remove the MULTI_PERL support by deleting the MULTI_PERL file in the root of the pyperl distribution.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2003/10/27/using-pyperl-to-build-a-python-like-xchat-interface-for-perl/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Webmin and Perl inside Python</title>
		<link>http://blog.labix.org/2003/10/25/webmin-and-perl-inside-python</link>
		<comments>http://blog.labix.org/2003/10/25/webmin-and-perl-inside-python#comments</comments>
		<pubDate>Sat, 25 Oct 2003 04:56:00 +0000</pubDate>
		<dc:creator>Gustavo Niemeyer</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Snippet]]></category>

		<guid isPermaLink="false">http://blog.labix.org/2003/10/25/webmin-and-perl-inside-python/</guid>
		<description><![CDATA[Today I&#8217;ve been talking to Cavassin about Webmin, and wondering if it would be possible to write Python modules for it. I was sure that I wasn&#8217;t being original in that idea, and Cavassin also mentioned that he had read &#8230; <a href="http://blog.labix.org/2003/10/25/webmin-and-perl-inside-python">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;ve been talking to Cavassin about <a href="http://www.webmin.com">Webmin</a>, and wondering if it would be possible to write Python modules for it. I was sure that I wasn&#8217;t being original in that idea, and Cavassin also mentioned that he had read something about it somewhere, so I&#8217;ve looked in google for possible references.</p>
<p>Of course, we were right. I have found the <a href="http://www.cendio.se/~peter/python-webmin/">python-webmin</a> project. OTOH, I&#8217;ve looked in the code, and that&#8217;s not really what I had mind. This module reimplements the Webmin API in Python. This means that one will have to maintain it forever, and check for updates in every Webmin release, and even then, it won&#8217;t be able to do more advanced stuff which requires deeper interaction with the Webmin system.</p>
<p>The correct solution for that would be to somehow wrap the Webmin API in a way that Python calls the real methods, thus requiring little or no maintenance. It&#8217;d be even better if this system was generic and not bound to Webmin in any way. While looking for the solution, I found <a href="http://inline.perl.org/">Inline</a>, which did exactly what I had in mind, but reversed (Python inside Perl), and <a href="http://pyinline.sourceforge.net">PyInline</a>, which unfortunately doesn&#8217;t seem to support Perl yet. Then, I finally found what I was looking for: pyperl. </p>
<p>The <a href="http://www.python.org/cgi-bin/moinmoin/PyPerl">pyperl</a> project embeds a Perl interpreter inside a Python module, allowing arbitrary Perl functionality to be used from Python. It&#8217;s really amazing to see it working. Have a look at these simple examples:</p>
<pre>
&gt;&gt;&gt; import perl
&gt;&gt;&gt; perl.eval('sub func { printf("Hello $_[0]!\n"); }')
&gt;&gt;&gt; ret = perl.call("func", "world")
Hello world!

&gt;&gt;&gt; func = perl.get_ref("func")
&gt;&gt;&gt; ret = func("world")
Hello world!

&gt;&gt;&gt; perl.require("Digest::MD5")
1
&gt;&gt;&gt; md5 = perl.callm("new", "Digest::MD5")
&gt;&gt;&gt; md5.add("something").hexdigest()
'437b930db84b8079c2dd804a71936b5f'
</pre>
<p>Isn&#8217;t it fantastic? <img src='http://blog.labix.org/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Another amazing thing is that pyperl also includes support for doing Python inside Perl. Of course, you can do Python inside Perl inside Python: </p>
<pre>
&gt;&gt;&gt; n = 5
&gt;&gt;&gt; perl.eval('Python::eval("n")*2')
10
</pre>
<p>Now, it&#8217;s just a matter of finding some time to create a basic wrapper on top of the Webmin Perl API, turning it into something more pythonic and also protecting Webmin modules developed in Python from changes in the pyperl API.</p>
<p><b>References</b></p>
<ul>
<li><a href="http://www.python.org/cgi-bin/moinmoin/PyPerl">pyperl wiki</a>
<li><a href="ftp://ftp.activestate.com/Zope-Perl/">pyperl download</a>
<li><a href="http://aspn.activestate.com/ASPN/CodeDoc/pyperl/perlmodule.html">pyperl documentation</a>
<li><a href="http://zope.org/Wikis/zope-perl/">Perl for Zope</a>
<li><a href="http://www.webmin.com">Webmin</a>
<li><a href="http://inline.perl.org/">Perl Inline</a>
<li><a href="http://pyinline.sourceforge.net">PyInline</a>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2003/10/25/webmin-and-perl-inside-python/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
