<?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>peter.makholm.net &#187; Open source (english)</title>
	<atom:link href="http://peter.makholm.net/category/open-source/en/feed/" rel="self" type="application/rss+xml" />
	<link>http://peter.makholm.net</link>
	<description></description>
	<lastBuildDate>Fri, 06 Jan 2012 09:22:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Splicing two sockets in Perl (request for help)</title>
		<link>http://peter.makholm.net/2011/10/17/splicing-two-sockets-in-perl-request-for-help/</link>
		<comments>http://peter.makholm.net/2011/10/17/splicing-two-sockets-in-perl-request-for-help/#comments</comments>
		<pubDate>Mon, 17 Oct 2011 17:54:53 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=510</guid>
		<description><![CDATA[A couple of times I have written code which basically does this: accept() an incoming connection do some magic to get another socket start passing bit between the two sockets perform a correct close down sequence An example use case of this is a proxy implementing the HTTP CONNECT method, but in for some known [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of times I have written code which basically does this:</p>
<ol>
<li>accept() an incoming connection
<li>do some magic to get another socket
<li>start passing bit between the two sockets
<li>perform a correct close down sequence
</ol>
<p>An example use case of this is a proxy implementing the HTTP CONNECT method, but in for some known hostnames it will log a message and mangle the hostname before proceeding. This has been used as a legacy fall back solution while changing a network setup. But my uses are not restricted to HTTP proxies I have done the same for a few legacy protocols where the magic has been of different complexities.</p>
<p>The two final steps are quite general and it would be nice to have a module doing just that. Take two sockets and make it easy (or even automatic) to pass bytes between them.</p>
<p>The naïve non-blocking solution would use a scalar string buffer for each direction and perform a select loop while maintaining the write vector depending on which buffers contain data. I have written this code multiple times. In development this is usually quite successful, in production less so. While Perl might be quite suited for the magic in step 2, the naïve way of passing bytes have quite an overhead for the buffer management.</p>
<p>A less naïve way would use a array of strings for buffers, but I&#8217;m not quite sure if this would be a win in all cases. You might be able to get away with some string operations on the read side of the buffer, but it might be more expensive on the write side. I have not benchmarked this.</p>
<p>Most of the time I don&#8217;t care about Perl level IO handles. I know that there is a real C level file descriptor beneath. So an even better POSIX compliant solution might be to use XS to have plain C strings and use readv()/writev() and a iovec structure as buffer.</p>
<p>Can we do even better? At least on Linux we can. With the Linux splice() specific system call it is possible to us a pipe as buffer and never to have to copy data from and to user space. </p>
<p>I have not been able to find any off the shelf solution on CPAN. So I think I need to write it myself, but what would the nice and general API be? I guess the basic interface would be something like:<br />
<code>
<pre>
    my $chain = IO::Splice->new($fh1, $fh2);

    $chain->pump(); # read and write from both handles if possible and needed
    $chain->read($fh1);  # read to buffer from one specific handle
    $chain->write($fh2); # write from buffer to one specific handle
    $chain->can_write(); # returns the handles it needs to write to
</pre>
<p></code></p>
<p>but it might be simpler to have two callbacks for setting a file handle in write or no-write state:<br />
<code>
<pre>
    my $readset  = IO::Select->new( $fh1, $fh2);
    my $writeset = IO::Select->new();
    my $chain = IO::Splice->new( $fh1, $fh2,
        writable => sub {  $writeset->add( shift ) },
        unwritable => sub { $writeset->remove( shift ) }
    );

    while ( ... select ... ) {
        $chain->pump();
    }
</pre>
<p></code></p>
<p>As said, I think I have plenty of implementations of the naïve way but before releasing some code it would be nice to get some input on the API. But the best feedback would be a module that already have a usable API but might not implement the Linux specific way. That would allow me to steal the interface&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2011/10/17/splicing-two-sockets-in-perl-request-for-help/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Wrapping mod_perl in Plack</title>
		<link>http://peter.makholm.net/2010/11/30/wrapping-mod_perl-in-plack/</link>
		<comments>http://peter.makholm.net/2010/11/30/wrapping-mod_perl-in-plack/#comments</comments>
		<pubDate>Tue, 30 Nov 2010 20:40:54 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[mod_perl]]></category>
		<category><![CDATA[Plack]]></category>
		<category><![CDATA[Plack::App::WrapApacheRec]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=489</guid>
		<description><![CDATA[Plack is one of these wonderful adventures in the modern Perl world that makes it fun to write Web applications in Perl again. But I have a few applications written with Apache/mod_perl and they are not fun to work with. So what would you do? One option would be to take the long road and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://search.cpan.org/perldoc?Plack">Plack</a> is one of these wonderful adventures in the modern Perl world that makes it fun to write Web applications in Perl again. But I have a few applications written with Apache/mod_perl and they are not fun to work with. So what would you do?</p>
<p>One option would be to take the long road and port these apps to use Plack instead of messing around with <a href="http://search.cpan.org/perldoc?Apache2::RequestRec">Apache2::RequestRec</a>. For this to work you might need to review the full code base before seeing any signs of progress.</p>
<p>Another option is to mock your Apache2::RequestRec object using Plack. This is the road explored by <a href="http://search.cpan.org/perldoc?Plack::App::WrapApacheReq">Plack::App::WrapApacheReq</a>. With very little work this enables you to run your mod_perl application with any Plack handler you want. You can run your application as a stand alone server or serve it with nginx trough FastCGI.</p>
<p>But the fun doesn&#8217;t stop here. Debugging and profiling mod_perl have always been a PITA, but with Plack::App::WrapApacheReq it is easy. Just take the <a href="https://github.com/pmakholm/plack-app-wrapapachereq/blob/master/examples/generic.psgi">generic.psgi</a> example and it enables you to run the Perl debugger easy or just to use NYTProf on <b>your</b> request handling code.</p>
<p>The initial ideas for writing Plack::App::WrapApacheRec came from my mocking code to test a legacy mod_perl application. Even this gets more fun by using Plack. I havn&#8217;t tested it yet, but with <a href="http://search.cpan.org/perldoc?Plack::Test">Plack::Test</a> and <a href="http://search.cpan.org/perldoc?Plack::Test::ExternalServer">Plack::Test::ExternalServer</a> it should be trivial to run the same set of tests directly in a single process test suite or against your deployed server.</p>
<p>Plack::App::WrapApacheRec is still in it&#8217;s infancy. It only mocks as much of Apache2::RequestRec as I need to run a single legacy application and to run Plack::Handler::Apache2 (as an absurd example, but yes we are self hosting). But I think that with very little work we should be able to run most mod_perl applications. Take it out for a ride, if it complains about a missing method please report it with <a href="https://rt.cpan.org/Public/Bug/Report.html?Queue=Plack-App-WrapApacheReq">CPAN&#8217;s RequestTracker</a>. Patches would be appreciated (or pull requests on github), but just a list of unimplemented method you need would be excellent.</p>
<p>Plack is fun, now working with mod_perl applications might become fun too.</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2010/11/30/wrapping-mod_perl-in-plack/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Reading Twitter with generic RSS reader</title>
		<link>http://peter.makholm.net/2010/09/18/reading-twitter-with-generic-rss-reader/</link>
		<comments>http://peter.makholm.net/2010/09/18/reading-twitter-with-generic-rss-reader/#comments</comments>
		<pubDate>Sat, 18 Sep 2010 15:30:02 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Open source (english)]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=475</guid>
		<description><![CDATA[For a long time I have mainly been reading Twitter with a generic RSS reader (newsbeuter). Recently this stopped working when twitter.com disabled &#8216;HTTP Basic Auth&#8217; in preference of using OAuth. Now I can say a lot of good and bad things about OAuth, but to keep a short story short: It doesn&#8217;t work with [...]]]></description>
			<content:encoded><![CDATA[<p>For a long time I have mainly been reading Twitter with a generic RSS reader (<a href="http://www.newsbeuter.org/">newsbeuter</a>). Recently this stopped working when twitter.com disabled &#8216;HTTP Basic Auth&#8217; in preference of using OAuth. Now I can say a lot of good and bad things about OAuth, but to keep a short story short: It doesn&#8217;t work with generic RSS readers.</p>
<p>Instead of getting the RSS feed from a URL, my RSS reader can run a script that prints an RSS feed. So if I could just write a script that does the OAuth dance to convince twitter.com to give me an RSS feed?</p>
<p>Perl to the rescue! Or rather CPAN to the rescue: <a href="http://search.cpan.org/perldoc?Net::Twitter::Lite">Net::Twitter::Lite</a> have a nice simple <a href="http://search.cpan.org/perldoc?Net::Twitter::Lite#OAUTH_EXAMPLES">example for setting up OAuth with a desktop application</a>. It also provides access to most of Twitters REST API, but no method for retrieving generic URLs.</p>
<p>Using some Jedi mind tricks and reading the source I found a private and undocumented method <tt>_oauth_authenticated_request</tt> which does exactly what I need. It is quite simple:</p>
<ol>
<li>Get my script from <a href="http://gist.github.com/585710">gist.github.com/585710</a> and install the dependencies (Net::Twitter:Lite).</li>
<li>Register you own app at <a href="http://dev.twitter.com">dev.twitter.com</a> to get a Consumer Key and Consumer Secret.</li>
<li>Run the script once to setup access and get an Access Token and Access Token Secret.</li>
<li>Run the script with an RSS feed url as parameter to get the RSS feeds.</li>
</ol>
<p>Most interesting feed to follow is probably <tt>http://api.twitter.com/1/statuses/home_timeline.atom</tt>, which show the same as the home page on twitter.com would show you, and <tt>http://api.twitter.com/1/statuses/mentions.atom</tt>, which shows any tweet that mentions you.</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2010/09/18/reading-twitter-with-generic-rss-reader/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>New Module: Benchmark::Serialize</title>
		<link>http://peter.makholm.net/2009/10/30/new-module-benchmarkserialize/</link>
		<comments>http://peter.makholm.net/2009/10/30/new-module-benchmarkserialize/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 11:50:06 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=424</guid>
		<description><![CDATA[Tim Bunce mentioned my blogpost about Benchmarking serialization modules in a post on the perl5-porters mailing list. He wished that someone would make that benchmark into a distribution on CPAN. How can I refuse. So here it is, Benchmark::Serialize is just uploaded to CPAN. (Might be some time before it appears). Besides making the script [...]]]></description>
			<content:encoded><![CDATA[<p>Tim Bunce mentioned my blogpost about <a href="http://peter.makholm.net/2009/06/15/benchmarking-serialization-modules/">Benchmarking serialization modules</a> in a <a href="http://www.nntp.perl.org/group/perl.perl5.porters/2009/10/msg152675.html">post on the perl5-porters mailing list</a>. He wished that someone would make that benchmark into a distribution on CPAN.</p>
<p>How can I refuse. So here it is, <a href="http://search.cpan.org/dist/Benchmark-Serialize/">Benchmark::Serialize</a> is just uploaded to CPAN. (Might be some time before it appears).</p>
<p>Besides making the script into a module I also added a list of the size of the serialized data to the output. A replacement of the original script is available in the examples directory.</p>
<p>(I planned on naming the module Benchmark::Serialization, but my fingers slipped. Should I rename it?)</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/10/30/new-module-benchmarkserialize/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Benchmarking some ORLite variants</title>
		<link>http://peter.makholm.net/2009/09/15/benchmarking-some-orlite-variants/</link>
		<comments>http://peter.makholm.net/2009/09/15/benchmarking-some-orlite-variants/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 11:47:47 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[ORLite]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=412</guid>
		<description><![CDATA[It would be nice if I could get my ORLite performance boost without really changing the (undocumented) API. So I got one more idea: Do the slicing in the ORLite generated code. It&#8217;s available in a new branch on GitHub. To benchmark all three solutions I used a variant of CPANDB::Dependecy::csv(): sub csv { my [...]]]></description>
			<content:encoded><![CDATA[<p>It would be nice if I could get my <a href="http://peter.makholm.net/2009/09/14/optimizing-orlite-pm/">ORLite performance boost</a> without really changing the (undocumented) API. So I got one more idea: Do the slicing in the ORLite generated code. It&#8217;s available in a <a href="http://github.com/pmakholm/orlite-unsliced-perl/tree/selfslice">new branch on GitHub</a>.</p>
<p>To benchmark all three solutions I used a variant of CPANDB::Dependecy::csv():</p>
<p><code>
<pre>
sub csv {
    my $class = shift;
    for my $edge ( $class->select ) {
        my $foo = $edge->distribution . "\t" . $edge->dependency . "\n";
    }
}

My::Plain->begin;
My::Unsliced->begin;
My::SelfSlice->begin;

cmpthese( -30, {
    plain => sub { csv("My::Plain::Dependency") },
    unsliced => sub { csv("My::Unsliced::Dependency") },
    selfslice => sub { csv("My::SelfSlice::Dependency") },
});
</pre>
<p></code></p>
<p>Unfortunately it seems like both having DBI doing the slicing and doing it myself costs roughly the same:</p>
<pre>
            Rate selfslice     plain  unsliced
selfslice 1.61/s        --       -1%      -41%
plain     1.64/s        2%        --      -40%
unsliced  2.71/s       68%       66%        --
</pre>
<p>So I probably end up making some sort of ORLite subclass as Adam Kennedy suggested in <a href="http://peter.makholm.net/2009/09/14/optimizing-orlite-pm/#comments">a comment</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/09/15/benchmarking-some-orlite-variants/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Optimizing ORLite.pm</title>
		<link>http://peter.makholm.net/2009/09/14/optimizing-orlite-pm/</link>
		<comments>http://peter.makholm.net/2009/09/14/optimizing-orlite-pm/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 13:52:42 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[ORLite]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=410</guid>
		<description><![CDATA[While profiling some code making heavy use of Adam Kennedy&#8217;s ORLite module for accessing a SQLite database I found that most of my time was spend in DBI.pm. Sorted by inclusive time (ie. including time spent in subroutines) two non-XS functions stood out: selectall_arrayref and fetchall_arrayref. Looking a the code both of these functions had [...]]]></description>
			<content:encoded><![CDATA[<p>While profiling some code making heavy use of  Adam Kennedy&#8217;s <a href="http://search.cpan.org/dist/ORLite/">ORLite</a> module for accessing a SQLite database I found that most of my time was spend in DBI.pm.</p>
<p>Sorted by inclusive time (ie. including time spent in subroutines) two non-XS functions stood out: selectall_arrayref and fetchall_arrayref. Looking a the code both of these functions had a comment stating that a C implementation existed in Drivers.xst and the comment at selectall_arrayref further said that the Perl version is used as a fallback if a slice is given</p>
<p>So could I get away with the slicing?</p>
<p>Turned out to <a href="http://github.com/pmakholm/orlite-unsliced-perl/commit/f48db011a9fc028c6e8d321f2a27521a3a8980aa">quite easy</a>. Just use array refs as objects instead of the usual hash refs. </p>
<p>The run time of the select() method provided by ORLite went from 344µs/call to 162µs/call on average. And as my test data makes at roughly 100000 select() calls this is a quite noticeable speedup. All included my running time (under Devel::NYTProf) improved from 400 seconds to 340 seconds.</p>
<p>Unfortunately I have to be able to update the state of my ORLite generated objects. This was easy while the objects was blessed has refs. Array refs are not as easy to update. The easy solution was to make the simple non-fk accessors to be lvalue subroutines. </p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/09/14/optimizing-orlite-pm/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>HTTP::Engine is great</title>
		<link>http://peter.makholm.net/2009/09/10/httpengine-is-great/</link>
		<comments>http://peter.makholm.net/2009/09/10/httpengine-is-great/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 06:10:01 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[CGI.pm]]></category>
		<category><![CDATA[HTTP::Engine]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=408</guid>
		<description><![CDATA[For simple web-based services I usually just use Perl and CGI.pm. After having read a bit about HTTP::Engine I tried it for a simple project yesterday. Beside my own logic it only took a few line of code to have a stand alone HTTP server for my service. My colleague needed it to be served [...]]]></description>
			<content:encoded><![CDATA[<p>For simple web-based services I usually just use Perl and CGI.pm. After having read a bit about <a href="http://search.cpan.org/perldoc?HTTP::Engine">HTTP::Engine</a> I tried it for a simple project yesterday. Beside my own logic it only took a few line of code to have a stand alone HTTP server for my service.</p>
<p>My colleague needed it to be served from the same Apache server as the rest of his webapplication. Some tiny changes and my stand alone server was transformed into a plain CGI script. When we going to deploy the script I&#8217;m guessing we make some tiny changes and have it running as a mod_perl module.  </p>
<p>Try it for you next project! Even if you usually just use CGI.pm.</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/09/10/httpengine-is-great/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Private methods in Perl5</title>
		<link>http://peter.makholm.net/2009/08/14/private-methods-in-perl5/</link>
		<comments>http://peter.makholm.net/2009/08/14/private-methods-in-perl5/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 06:39:23 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=395</guid>
		<description><![CDATA[It is common knowledge that you can&#8217;t have private functions and methods in Perl5. But it turns out that you can do it, one way is to use namespace::clean. Using this module you can either declare all the names of private functions at the top or use a serie of non-obvious &#8220;use namespace::clean&#8220;, &#8220;no namespace::clean&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>It is common knowledge that you can&#8217;t have private functions and methods in Perl5. But it turns out that you can do it, one way is to use <a href="http://search.cpan.org/perldoc?namespace::clean">namespace::clean</a>. Using this module you can either declare all the names of private functions at the top or use a serie of non-obvious &#8220;<code>use namespace::clean</code>&#8220;, &#8220;<code>no namespace::clean</code>&#8221; calls.</p>
<p>Wouldn&#8217;t it be much nicer just to be able to write:</p>
<pre><code>
sub foo :Private {
    ...
}
</code></pre>
<p>You can, with my brand new <a href="http://search.cpan.org/perldoc?Sub::Private">Sub::Private</a> module. It is actually quite simple:</p>
<pre><code>
use Attribute::Handlers;

use namespace::clean     qw();
use B::Hooks::EndOfScope qw(on_scope_end);
use Sub::Identify        qw(get_code_info);

sub UNIVERSAL::Private :ATTR(CODE,BEGIN) {
    my ($package, $symbol, $referent, $attr, $data) = @_;

    on_scope_end {
        namespace::clean->clean_subroutines( get_code_info( $referent ) );
    }
}
</code></pre>
<p>Putting the attribute handler in the UNIVERSAL namespace isn&#8217;t nice. I have to find a solution for that for the next version.</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/08/14/private-methods-in-perl5/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Awesome is awesome!</title>
		<link>http://peter.makholm.net/2009/08/12/awesome-is-awesome/</link>
		<comments>http://peter.makholm.net/2009/08/12/awesome-is-awesome/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 08:00:52 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Open source (english)]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=393</guid>
		<description><![CDATA[At work I have a dual-head setup with two screens. Previously it seemed to me that you had to choose: Either you could set the screens up as separate entities in xorg.conf, which means separate workspaces but not being able to move windows from one screen to the other or you set it up as [...]]]></description>
			<content:encoded><![CDATA[<p>At work I have a dual-head setup with two screens. Previously it seemed to me that you had to choose:</p>
<ul>
<li>Either you could set the screens up as separate entities in xorg.conf, which means separate workspaces but not being able to move windows from one screen to the other
<li>or you set it up as one big virtual screen with you workspace spanning both screens.
</ul>
<p>I like workspaces and use them a lot on my primary screen, but on my secondary screen I mostly want my browser and my instant messaging (IRC). Some solves this by making the windows on the secondary screen sticky, so the appears on every workspace. That doesn&#8217;t work for me &#8211; I still want my worksapces on the secondary screen, I just want the workspaces to behave separately from the workspaces on the primary screen.</p>
<p>For a long time I thought this impossible, so I used the first option with separate entries in my xorg.conf. I havn&#8217;t really missed moving windows from screen to screen, but Firefox is a bother. I want to be able to have Firefox windows on both screen without having to keep two profiles in sync.</p>
<p>Awesome3 to the rescue. I finally upgraded my window manager to <a href="http://awesome.naquadah.org/">awesome3</a> and after using xrandr to configure my screes, awesome works out of the box as I need. X only provides one display, which means that I can move windows freely around and Firefox can open windows on both screens, but Awesome automatically gives me separated workspaces on the two screens.</p>
<p>Of course I had do make some changes in the standard configuration for awesome. Other menus, click to focus (remove the awful.hooks.mouse_enter.register call), enforce floating layout with a proper window placement (some changes in the awful.hooks.manage.register call). View my configuration on <a href="http://gist.github.com/166368/">GitHub Gists</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/08/12/awesome-is-awesome/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Regular Expressions: Beyond the fundamentals</title>
		<link>http://peter.makholm.net/2009/08/03/regular-expressions-beyond-the-fundamentals/</link>
		<comments>http://peter.makholm.net/2009/08/03/regular-expressions-beyond-the-fundamentals/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 14:32:21 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[regexp]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=391</guid>
		<description><![CDATA[At this years YAPC::EU I&#8217;m giving a talk about some of the more advanced features of regular expressions. Slides are available at http://hacking.dk/talks/yapceu2009/ &#8211; There is a pretty regexp validating dates (including February 29th) in it!]]></description>
			<content:encoded><![CDATA[<p>At this years YAPC::EU I&#8217;m giving a talk about some of the more advanced features of regular expressions.</p>
<p>Slides are available at <a href="http://hacking.dk/talks/yapceu2009/">http://hacking.dk/talks/yapceu2009/</a> &#8211; There is a pretty regexp validating dates (including February 29th) in it!</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/08/03/regular-expressions-beyond-the-fundamentals/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Shell hacks: bash functions in files</title>
		<link>http://peter.makholm.net/2009/07/07/shell-hacks-bash-functions-in-files/</link>
		<comments>http://peter.makholm.net/2009/07/07/shell-hacks-bash-functions-in-files/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 18:34:52 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Open source (english)]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[local::lib]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=381</guid>
		<description><![CDATA[Over the years I have collected a few shell snippets that have to be sourced or called as shell functions to work correctly. Having all these function definitions in a monolithic file sourced from my .bashrc isn&#8217;t cool, so for some time I&#8217;ve had the following in my .bashrc: for i in $HOME/.lib.sh/* ; do [...]]]></description>
			<content:encoded><![CDATA[<p>Over the years I have collected a few shell snippets that have to be sourced or called as shell functions to work correctly. Having all these function definitions in a monolithic file sourced from my .bashrc isn&#8217;t cool, so for some time I&#8217;ve had the following in my .bashrc:</p>
<p><code>
<pre>
for i in $HOME/.lib.sh/* ; do
    source $i
done
</pre>
<p></code></p>
<p>This has at least one obvious downside. Each time I change a function definition I have to reload the function i each and every open shell if I want a consistent work environment. Today I came up with a nifty solution. Each file in ~/.lib.sh which previously contained a function definition isedited to only contain the function body. The I add the following new function called <tt><a href="http://gist.github.com/141988">load_functions</a></tt>:</p>
<p><code>
<pre>
FUNCTIONS=$HOME/.lib.sh

eval $(
    find $FUNCTIONS -type f | \
        while read file ; do echo function $(basename $file)\(\) { source $file\; }\; ; done
)
</pre>
<p></code></p>
<p>For each file under ~/.lib.sh a new function is automatically defined which just sources the real file. This means I can edit the files without having to reload the function in every open shell. ~/.lib.sh/load_files itself is just sourced from my .bashrc.</p>
<p>One of my functions wraps the wonderful Perl5 module <a href="http://search.cpan.org/perldoc?local::lib">local::lib</a>. This module makes it easy to manage multiple sandboxes with locally installed Perl modules. It works by setting a couple of environment variables including $PERL5LIB. Instead of using local::lib directly I used a function called <a href="http://gist.github.com/142267">perllibs</a>:</p>
<p><code>
<pre>
# Defaults:
PERL_LOCAL_LIB="home"
DIR=$HOME/.perl

if [ -n "$1" ]; then
    PERL_LOCAL_LIB=$1
fi

export PERL_LOCAL_LIB

case $PERL_LOCAL_LIB in
    alpha)
        DIR=$HOME/projects/alpha/ ;;
    mercury)
        DIR=$HOME/subversion/mercury/perl ;;
    tmp)
        mkdir -p /tmp/makholm/perl
        DIR=/tmp/makholm/perl ;;
esac

eval $( perl -Mlocal::lib=$DIR )
</pre>
<p></code></p>
<p>Adding new projects to this list would be a hassle if I had to reload the file in multiple shells each time.</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/07/07/shell-hacks-bash-functions-in-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why write an editor in Perl?</title>
		<link>http://peter.makholm.net/2009/07/04/why-write-an-editor-in-perl/</link>
		<comments>http://peter.makholm.net/2009/07/04/why-write-an-editor-in-perl/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 19:55:07 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Padre]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=378</guid>
		<description><![CDATA[I&#8217;m a happy vim-user and I don&#8217;t see that change any time soon. But recently I have been hacking around with Padre, an editor writen in Perl and for Perl programmers. And even though I like my non-GUI editor I think Padre has one advantage: It is quite hackable inPerl &#8211; my favourite language for [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a happy vim-user and I don&#8217;t see that change any time soon. But recently I have been hacking around with Padre, an editor writen in Perl and for Perl programmers. And even though I like my non-GUI editor I think Padre has one advantage: It is quite hackable inPerl &#8211; my favourite language for hacking.</p>
<p>One thing I wanted to play with was debugging from my editor. It is probably posible to write in vim-perl scripts, but having easy access to all the internals without language barriers made the task of writing a debugger plugin much easier and more importantly, much more fun.</p>
<p>So if you have ever thought about nice features you wanted while editing Perl, then please join the Padre project and keep hacking perl while improving you tools.</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/07/04/why-write-an-editor-in-perl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The evil of a global $_</title>
		<link>http://peter.makholm.net/2009/06/24/the-evil-of-a-global-_/</link>
		<comments>http://peter.makholm.net/2009/06/24/the-evil-of-a-global-_/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 13:04:57 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=367</guid>
		<description><![CDATA[I often write code like this: $_->store() for @objects; and quite often it actually works. But suddenly a piece of code doing this in a loop broke with this error message: Can&#8217;t call method &#8220;store&#8221; without a package or object reference. And quite right, sometimes @objects would contain plain integers. Unfortunately it wouldn&#8217;t be quite [...]]]></description>
			<content:encoded><![CDATA[<p>I often write code like this:</p>
<p><code>
<pre>
    $_->store() for @objects;
</pre>
<p></code></p>
<p>and quite often it actually works. But suddenly a piece of code doing this in a loop broke with this error message: <i>Can&#8217;t call method &#8220;store&#8221; without a package or object reference</i>. And quite right, sometimes @objects would contain plain integers.</p>
<p>Unfortunately it wouldn&#8217;t be quite easy to track down the relevant change, so enter the perl debugger. Declaring a watch on &#8216;@objects&#8217; isn&#8217;t useful as it triggers each time @objects enters or leaves scope. But saving a reference to @objects in $my::objects and the watching $my::objects-&gt;[0] worked.</p>
<p>I had reimplemented the store() method using <a href="http://search.cpan.org/perldoc?Data::Walk">Data::Walk</a> for walking some structure instead of doing it by hand. And Data::Walk sets $_ to the current node. A due to the aliasing implied in the for-modifier each element in my @objects array was garbled.</p>
<p>Two solutions: The store() method can localize $_ by adding <tt>local $_;</tt> before using Data::Walk &#8211; this works in legacy perl interpreters. The routine looping through @objects can make $_ a lexical variable by adding <tt>my $_;</tt> before the loop &#8211; This is a new feature in Perl 5.10.</p>
<p>An even more robust solution would be to have Data::Walk localize $_ itself AND use a lexical $_ in code where $_ is aliased to important data. See <a href="https://rt.cpan.org/Ticket/Display.html?id=47309">RT #47309</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/06/24/the-evil-of-a-global-_/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>New version of the Padre debugger plugin</title>
		<link>http://peter.makholm.net/2009/06/16/new-version-of-the-padre-debugger-plugin/</link>
		<comments>http://peter.makholm.net/2009/06/16/new-version-of-the-padre-debugger-plugin/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 06:56:45 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=365</guid>
		<description><![CDATA[I&#8217;ve just uploaded a new version of Padre-Plugin-Debugger to CPAN. It has been stuck in github for a while as I meant to write some documentation and start calling it version 1.0. But as I haven&#8217;t written any documentation yet, it is still just a puny 0.3 version. The major update is how the interpreter [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just uploaded a new version of Padre-Plugin-Debugger to CPAN. It has been stuck in github for a while as I meant to write some documentation and start calling it version 1.0. But as I haven&#8217;t written any documentation yet, it is still just a puny 0.3 version.</p>
<p>The major update is how the interpreter is called. Now it will actually find the modules you are using, if you add the correct directories to &#8216;Edit -> Preferences -> Run Paramerters&#8217;</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/06/16/new-version-of-the-padre-debugger-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Benchmarking serialization modules</title>
		<link>http://peter.makholm.net/2009/06/15/benchmarking-serialization-modules/</link>
		<comments>http://peter.makholm.net/2009/06/15/benchmarking-serialization-modules/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 08:17:53 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=361</guid>
		<description><![CDATA[First you have to optimize for correctness, then you can optimize for speed. At the moment I&#8217;m working on a project where I do a lot of serialization, and to ensure that I could debug the correctness I have chosen an easy readable serialization format: YAML. But running Devel::NYTProf on my code showed that an [...]]]></description>
			<content:encoded><![CDATA[<p>First you have to optimize for correctness, then you can optimize for speed. At the moment I&#8217;m working on a project where I do a lot of serialization, and to ensure that I could debug the correctness I have chosen an easy readable serialization format: YAML.</p>
<p>But running <a href="http://search.cpan.org/dist/Devel-NYTProf/">Devel::NYTProf</a> on my code showed that an awful lot of time was spend in the YAML code. Changing a few lines to use <a href="http://search.cpan.org/dist/Storable">Storable</a> instead showed an improvement in speed going from 1000s to 200s for my test data. This is a significant win even while developing, but of course I shouldn&#8217;t have used the <a href="http://search.cpan.org/dist/YAML::Old">old YAML-implementation</a> to begin with.</p>
<p>Mentioning this to the local Perl Mongers group I got referred to a <a href="http://idisk.mac.com/christian.hansen/Public/perl/serialize.pl">benchmark of different serialization modules</a> by Christian Hansen (I think). I&#8217;ve <a href="http://gist.github.com/130005">updated the script</a> and the interesting part is that <a href="http://search.cpan.org/dist/JSON-XS">JSON::XS</a> outruns Storable on both tests.</p>
<p>Thank you Devel::NYTProf for showing me that 80% of my run time was spent in old pure perl serialization code.</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/06/15/benchmarking-serialization-modules/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>More fun with DESTROY</title>
		<link>http://peter.makholm.net/2009/06/09/more-fun-with-destroy/</link>
		<comments>http://peter.makholm.net/2009/06/09/more-fun-with-destroy/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 17:38:35 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=352</guid>
		<description><![CDATA[When I wrote about exception handling in Perl I mentioned briefly that DESTROY() methods should localize $? too. Try this script: #!/usr/bin/perl package Foo; sub new { my $class = shift; open my $fh, "-&#124;", "cat /dev/zero"; return bless { fh =&#62; $fh }, $class; } sub DESTROY { my $self = shift; close $self-&#62;{fh} [...]]]></description>
			<content:encoded><![CDATA[<p>When I wrote about <a href="http://peter.makholm.net/2009/05/22/perl-exception-handling-is-hard/">exception handling in Perl</a> I mentioned briefly that DESTROY() methods should localize $? too. </p>
<p>Try this script:</p>
<p><code>
<pre>
#!/usr/bin/perl

package Foo;

sub new {
    my $class = shift;
    open my $fh, "-|", "cat /dev/zero";
    return bless { fh =&gt; $fh }, $class;
}

sub DESTROY {
    my $self = shift;
    close $self-&gt;{fh}
}

package main;

my $foo = Foo-&gt;new();
exit 42;
</pre>
<p></code></p>
<p>You would expect it to return with status code 42, but when I run it the status code is 13. Localizing $? in the DESTROY() method gives the expected 42 status code.</p>
<p>The most common usage of $? is explained in the perlvar manual page:</p>
<blockquote><p>The status returned by the last pipe close, backtick (&#8220;&#8220;&#8221;) command, successful call to wait() or waitpid(), or from the system() operator.</p></blockquote>
<p>And the above destructor makes a &#8216;pipe close&#8217; changing the value of $?. But the perlvar manual also mentions an secondary usage of $?:</p>
<blockquote><p>Inside an &#8220;END&#8221; subroutine $? contains the value that is going to be given to &#8220;exit()&#8221;.  You can modify $? in an &#8220;END&#8221; subroutine to change the exit status of your program.</p></blockquote>
<p>But this doesn&#8217;t just holds for END block, it is true for any code run after the exit() invokation &#8211; including destructors.</p>
<p>And then to a mystery. Given the above Foo package it isn&#8217;t very surprising that this script have the status code 13:</p>
<p><code>
<pre>
use Foo;
my $foo = Foo-&gt;new();
</pre>
<p></code></p>
<p>But why does the following change make the script have the status code 0?</p>
<p><code>
<pre>
use Foo;
my $foo = Foo-&gt;new();
$? = 42;
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/06/09/more-fun-with-destroy/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>When perfection isn&#8217;t good enough</title>
		<link>http://peter.makholm.net/2009/06/02/when-perfection-isnt-good-enough/</link>
		<comments>http://peter.makholm.net/2009/06/02/when-perfection-isnt-good-enough/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 07:45:21 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=346</guid>
		<description><![CDATA[One of the many possible problems with using CPAN modules is that they often just implement the parts of the problems the author needed a solution for. But once in a while you run into the opposite problem: The module implements parts of a standard that you just don&#8217;t want. Some time ago I had [...]]]></description>
			<content:encoded><![CDATA[<p>One of the many possible problems with using CPAN modules is that they often just implement the parts of the problems the author needed a solution for. But once in a while you run into the opposite problem: The module implements parts of a standard that you just don&#8217;t want.</p>
<p>Some time ago I had to parse parts of the <a href="http://tools.ietf.org/html/rfc4918">WebDAV protocol</a>. WebDAV properties are transmitted using XML with namespaces, which is one thing I think <a href="http://search.cpan.org/perldoc?XML::Simple">XML::Simple</a> is particular bad for. So I turned to <a href="http://search.cpan.org/perldoc?XML::LibXML">XML::LibXML</a> (which is becoming my XML module of choice either way).</p>
<p>So, the WebDAV RFC have examples like this:</p>
<pre>
     &lt;?xml version="1.0" encoding="utf-8" ?&gt;
     &lt;D:propfind xmlns:D="DAV:"&gt;

       &lt;D:prop xmlns:R="http://ns.example.com/boxschema/"&gt;
         &lt;R:bigbox/&gt;
         &lt;R:author/&gt;
         &lt;R:DingALing/&gt;
         &lt;R:Random/&gt;
       &lt;/D:prop&gt;

     &lt;/D:propfind&gt;
</pre>
<p>Unfortunately XML::LibXML insists on namespace URI&#8217;s to conform to the URI specification, which <tt>DAV:</tt> doesn&#8217;t. Due to XML::LibXML&#8217;s perfection I&#8217;m not able to just use it. Solution:</p>
<pre>
sub escapeNamespace {
    $_[0] =~ s/(xmlns(?::\w+)?)="(?!urn|http)([^"]+)"/$1="urn:xxx:$2"/g;
    $_[0] =~ s/(xmlns(?::\w+)?)=""/$1="urn:xxx:nonamespace"/g;
}
</pre>
<p>I&#8217;m not quite sure that the second substitution is needed by the standard, but the <a href="http://www.webdav.org/neon/litmus/">Litmus webdav test suite</a> needs it&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/06/02/when-perfection-isnt-good-enough/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Benchmarking is hard</title>
		<link>http://peter.makholm.net/2009/05/26/benchmarking-is-hard/</link>
		<comments>http://peter.makholm.net/2009/05/26/benchmarking-is-hard/#comments</comments>
		<pubDate>Tue, 26 May 2009 07:29:12 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=341</guid>
		<description><![CDATA[Benchmarking is hard to do right. Recently Jason Switzer found an old benchmark of the smart match operator written by Michael Schwern. Jason Switcher comments: If I were to publish a paper with such a gaping hole like that, it would never be taken serious. In each test, he’s generating a random number and a [...]]]></description>
			<content:encoded><![CDATA[<p>Benchmarking is hard to do right. Recently Jason Switzer found an <a href="http://use.perl.org/articles/07/12/22/1855215.shtml">old benchmark of the smart match operator</a> written by Michael Schwern. <a href="http://s1n.dyndns.org/index.php/2009/05/25/yet-another-splice-function/">Jason Switcher comments:</a></p>
<blockquote><p>
If I were to publish a paper with such a gaping hole like that, it would never be taken serious. In each test, he’s generating a random number and a random character and storing the result! That’s not part of the test. The $needles should each be generated outside the timing loop. This is adding tons of additional instructions that are not considered part of the test. These results are basically useless and should be redone.
</p></blockquote>
<p>After doing his test, Jason concludes: &#8220;Those results are astonishing!&#8221;. Yes, it clearly shows that the grep solutions are 25% faster than using first. This is astonishing and surprising &mdash; and probably warrants an explanation.</p>
<p>Jason&#8217;s methodology consists of generating a single random array and needle for each piece of code he wants to benchmark. Running his benchmark on my machine shows that first is 15 times faster than smart match and about 28 times faster than grep.  Of course, my random arrays might have been more biased against &#8216;first&#8217; and randomly placing the needle at the start.</p>
<p>So Jason&#8217;s test are not to be taken seriously either. Even when benchmarking with random data, we have to benchmark all our code pieces against the same data and not just on one data set.</p>
<p><b>Update:</b> I&#8217;ve been running some more benchmarks in the background. It seems that the general pattern is that &#8216;first&#8217; from List::Util and any from List::MoreUtils is about 25% slower than grep, but with some spikes where &#8216;first&#8217;/'any&#8217; is blazingly fast. Is being a native opcode that much faster than XS-code?</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/05/26/benchmarking-is-hard/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Perl exception handling is hard</title>
		<link>http://peter.makholm.net/2009/05/22/perl-exception-handling-is-hard/</link>
		<comments>http://peter.makholm.net/2009/05/22/perl-exception-handling-is-hard/#comments</comments>
		<pubDate>Fri, 22 May 2009 06:54:48 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=334</guid>
		<description><![CDATA[It is a well known perl idiom to write exception handling with eval and die, instead of try and catch. eval { do_something() or die "Err!"; }; if ($@) { print "Catched exception: $@"; } Even Damian Conway does it in chapter 16 of Perl Best Practices and &#8216;perldoc -f die&#8216; does it too. Unfortunately [...]]]></description>
			<content:encoded><![CDATA[<p>It is a well known perl idiom to write exception handling with <code>eval</code> and <code>die</code>, instead of <code>try</code> and <code>catch</code>.</p>
<pre><code>
eval {
    do_something()
        or die "Err!";
 };

if ($@) {
    print "Catched exception: $@";
}
</code></pre>
<p>Even Damian Conway does it in chapter 16 of <a href="http://oreilly.com/catalog/9780596001735/index.html">Perl Best Practices</a> and &#8216;<tt>perldoc -f die</tt>&#8216; does it too. Unfortunately it doesn&#8217;t work all the time. Consider the following situation:</p>
<pre><code>
#!/usr/bin/perl

package Foo;

sub new {
    my $class = shift;
    return bless {}, $class;
}

sub DESTROY {
    my $self = shift;
    eval { 1; };
}

package main;
eval {
    my $foo = Foo-&gt;new();
    open my $fh, "&gt;", "/"
        or die "Could not open /: $!";
    print "Doing some work\n";
};
if ($@) {
    print STDERR "Something bad happened: $@\n";
} else {
    print "Everything went well\n";
}
</code></pre>
<p>As we shouldn&#8217;t be able to open &#8216;/&#8217; for writing, we expect to see an error message. But it turns out that the script claims to succeed even tough it didn&#8217;t do any &#8220;work&#8221;. So what happened? Well, $foo went out of scope, the destructor was called and the embedded eval changed $@.</p>
<p>What can we do to solve the problem. First of all, make sure that the destructor doesn&#8217;t chage $@. You can do this by localizing it. Even tough you&#8217;re not calling eval directly, some other code might do it. So always start destructors like this:</p>
<pre><code>
sub DESTROY {
    my $self = shift;
    local $@;
    ...;
}
</code></pre>
<p>By the way, you want to localize $? too, but that is another story.</p>
<p>Another solution is to change you try/catch emulation. If an eval&#8217;ed block dies, the resulting value is undef. So make sure that the ordinary case always ends with a true value, and replace catch with a simple or:</p>
<pre><code>
eval {
    do_work();
    1;
} or do {
    print "catch: $@";
};
</code></pre>
<p>Of course $@might be misleading about the real error, so this doesn&#8217;t work as well as fixing the destructors. But when using modules written by others, you might implement defense in depth by using both solutions. I think there is a Perl::Critic policy advocating the above try/catch construction.</p>
<p>But how does the execption modules on CPAN fare against the above destructor? I tried two of them: Error.pm works correctly, because it&#8217;s <code>throw</code> implementation stores the exception before dying. The much newer TryCatch.pm doesn&#8217;t work (<a href="https://rt.cpan.org/Ticket/Display.html?id=46294">See rt #46294</a>), but is much cooler otherwise.</p>
<p><b>Update:</b> I&#8217;ve managed to write a <a href="http://github.com/pmakholm/trycatch/commit/e24964bc2f6a938004b8c5b97ffd9278c7fc5830">fix for TryCatch.pm</a>. It includes a throw function, just like Error.pm.</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/05/22/perl-exception-handling-is-hard/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Persistence and objects from XS modules</title>
		<link>http://peter.makholm.net/2009/05/15/persistence-and-objects-from-xs-modules/</link>
		<comments>http://peter.makholm.net/2009/05/15/persistence-and-objects-from-xs-modules/#comments</comments>
		<pubDate>Fri, 15 May 2009 08:23:19 +0000</pubDate>
		<dc:creator>Peter Makholm</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://peter.makholm.net/?p=330</guid>
		<description><![CDATA[After three workdays spend debugging, it is time for some blame allocation. But who to blame? In a larger system we&#8217;re using Geo::IP to locate users. Recently we have discovered a few hanging or segv faulting processes. After finding a reproducible case we found out that both cases shared the same backtrace: [...] #1 0x55791570 [...]]]></description>
			<content:encoded><![CDATA[<p>After three workdays spend debugging, it is time for some blame allocation. But who to blame?</p>
<p>In a larger system we&#8217;re using <a href="http://search.cpan.org/perldoc?Geo::IP">Geo::IP</a> to locate users. Recently we have discovered a few hanging or segv faulting processes. After finding a reproducible case we found out that both cases shared the same backtrace:</p>
<pre>
[...]
#1  0x55791570 in GeoIPRecord_delete () from /usr/lib/libGeoIP.so.1
#2  0x5577ecd8 in XS_Geo__IP__Record_DESTROY () from /usr/lib/perl5/auto/Geo/IP/IP.so
#3  0x080bdaa1 in Perl_pp_entersub ()
#4  0x080626db in Perl_magicname ()
#5  0x080632ee in Perl_call_sv ()
#6  0x080c9bf4 in Perl_sv_clear ()
#7  0x080ca3e5 in Perl_sv_free ()
#8  0x080c9f07 in Perl_sv_clear ()
#9  0x080ca3e5 in Perl_sv_free ()
#10 0x080bb5da in Perl_av_undef ()
#11 0x080ca0e7 in Perl_sv_clear ()
#12 0x080ca3e5 in Perl_sv_free ()
#13 0x080c9f07 in Perl_sv_clear ()
#14 0x080ca3e5 in Perl_sv_free ()
#15 0x080b6f69 in Perl_hv_free_ent ()
#16 0x080b70e5 in Perl_hv_iterinit ()
#17 0x080b8e69 in Perl_hv_undef ()
#18 0x080ca0cc in Perl_sv_clear ()
#19 0x080ca3e5 in Perl_sv_free ()
#20 0x080e92ef in Perl_leave_scope ()
#21 0x080e93bc in Perl_pop_scope ()
#22 0x080bec4d in Perl_pp_leavesub ()
#23 0x080bc379 in Perl_runops_standard ()
#24 0x08063bfd in perl_run ()
#25 0x0805ffd1 in main ()
</pre>
<p>GeoIPRecord_delete consists of 3 free(3) calls for members of a struct and 1 to free(3) the struct itself. Running with Gnu libc&#8217;s  MALLOC_CHECK and using valgrind confirmed that it triet to free invalid pointers. But the &#8216;leak&#8217; was nowhere to be found. Using the pure perl version of Geo::IP removed any sign of problems.</p>
<p>Almost giving up I suddenly discovered that it happend while a value read from Storable was going out of scope. The it was easy to reproduce:</p>
<pre><code>
use Geo::IP;
use Storable;
my $geoip = Geo::IP->open_type(
    GEOIP_CITY_EDITION_REV1,
    GEOIP_MEMORY_CACHE|GEOIP_CHECK_CACHE
);
my $gir = $geoip->record_by_name( "peter.makholm.net" );
my $copy = Storable::dclone( $gir );
$gir = undef;
$copy = undef;
[... crash ...]
</code></pre>
<p>What happens is that Geo::IP::Record is just a scalar reference containing the a C pointer as the integer value. Dumping it with Devel::Peek loks like this:</p>
<pre>
SV = RV(0x2cdd4c8) at 0x2cdd4b8
  REFCNT = 2
  FLAGS = (ROK)
  RV = 0x27a4a50
  SV = PVMG(0x297e610) at 0x27a4a50
    REFCNT = 1
    FLAGS = (OBJECT,IOK,pIOK)
    IV = 47079264
    NV = 0
    PV = 0
    STASH = 0x2ca9520   "Geo::IP::Record"
</pre>
<p>Almost the same as what we get for <code>my $foo = bless do { \(my $o = 123456);  }, "Foo::Bar"</code></p>
<pre>
SV = RV(0x2ce82f0) at 0x2ce82e0
  REFCNT = 2
  FLAGS = (ROK)
  RV = 0x2cf7d30
  SV = PVMG(0x297e730) at 0x2cf7d30
    REFCNT = 3
    FLAGS = (OBJECT,IOK,pIOK)
    IV = 123456
    NV = 0
    PV = 0
    STASH = 0x2cbde28   "Foo::Bar"
</pre>
<p>so Storable can&#8217;t really see that the object is a c-pointer and Geo::IP::Record wasn&#8217;t meant to be dumped wioth Storable. The only hint is the description in the documentation of Storable:</p>
<blockquote><p>
The Storable package brings persistence to your Perl data structures containing SCALAR, ARRAY, HASH or REF objects, i.e. anything that can be conveniently stored to disk and retrieved at a later time.
</p></blockquote>
<p>But using Geo::IP::Record it looks exactly like you run of the mill HASH-based record objects. Lessons learned? Really no-one, except to watch out for XS objects.</p>
]]></content:encoded>
			<wfw:commentRss>http://peter.makholm.net/2009/05/15/persistence-and-objects-from-xs-modules/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

