<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>confuseAcat</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/confuseacat/" />
    <link rel="self" type="application/atom+xml" href="http://blogs.perl.org/users/confuseacat/atom.xml" />
    <id>tag:blogs.perl.org,2009-11-03:/users/confuseacat//351</id>
    <updated>2012-11-29T20:23:06Z</updated>
    <subtitle>Random observations that may in some way be related to Perl.</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 4.38</generator>

<entry>
    <title>How to use SOAP::Transport::HTTP::Plack</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/confuseacat/2012/11/how-to-use-soaptransporthttpplack.html" />
    <id>tag:blogs.perl.org,2012:/users/confuseacat//351.4080</id>

    <published>2012-11-29T20:10:57Z</published>
    <updated>2012-11-29T20:23:06Z</updated>

    <summary>I needed to port a little cgi-script that implements a simple SOAP-server to Plack. After a little searching, I came across SOAP::Transport::HTTP::Plack. Unfortunately, the documentation of this module is ... not really helpful. Here&apos;s my (slightly modified) cgi-script: #!/usr/bin/perl use...</summary>
    <author>
        <name>confuseAcat</name>
        
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/confuseacat/">
        <![CDATA[<p>I needed to port a little cgi-script that implements a simple SOAP-server to <a href="https://metacpan.org/module/Plack">Plack</a>. After a little searching, I came across <a href="https://metacpan.org/module/SOAP::Transport::HTTP::Plack">SOAP::Transport::HTTP::Plack</a>. </p>

<p>Unfortunately, the documentation of this module is ... not really helpful.</p>

<p>Here's my (slightly modified) cgi-script:</p>

<pre><code>#!/usr/bin/perl

use SOAP::Transport::HTTP;

SOAP::Transport::HTTP::CGI
    -&gt;dispatch_to(
        '/some/directory', 
        'Some::SOAP::Module1', 
        'Some::SOAP::Module2',
    )-&gt;handle;
</code></pre>

<p>Simple enough.</p>

<p>After some help of the awesome hobbs on IRC, here's part of my Plack psgi file:</p>

<pre><code>use Plack::Builder;
use Plack::Request;
use SOAP::Transport::HTTP::Plack;

my $soap = SOAP::Transport::HTTP::Plack-&gt;new;

my $soap_app = sub {
    my $env = shift;

    return $soap
            -&gt;dispatch_to(
                '/some/directory', 
                'Some::SOAP::Module1', 
                'Some::SOAP::Module2',
            )-&gt;handler( Plack::Request-&gt;new( $env ) );
};

return builder {
    mount 
        '/soap_test' =&gt; $soap_app;
}
</code></pre>

<p>And low and behold, it works!</p>
]]>
        

    </content>
</entry>

<entry>
    <title>LCLOC of the month</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/confuseacat/2012/11/lcloc-of-the-month.html" />
    <id>tag:blogs.perl.org,2012:/users/confuseacat//351.4050</id>

    <published>2012-11-16T09:53:09Z</published>
    <updated>2012-11-16T09:58:42Z</updated>

    <summary><![CDATA[Here's the Least Comprehensible Line Of Code I came across this month. Took me a while to make sure it really did what I thought it did. my @array; # ... @array = map {$_;} (@array, keys %{$this-&gt;{'someobject'}-&gt;get_some_hash_ref()}); Bonus points...]]></summary>
    <author>
        <name>confuseAcat</name>
        
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/confuseacat/">
        <![CDATA[<p>Here's the Least Comprehensible Line Of Code I came across this month. Took me a while to make sure it really did what I thought it did.</p>

<pre><code>my @array;
# ...
@array = map {$_;} (@array, keys %{$this-&gt;{'someobject'}-&gt;get_some_hash_ref()});
</code></pre>

<p>Bonus points for using <code>$this</code> instead of <code>$self</code>.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>What Moose is doing to my nose</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/confuseacat/2012/05/what-moose-is-doing-to-my-nose.html" />
    <id>tag:blogs.perl.org,2012:/users/confuseacat//351.3279</id>

    <published>2012-05-22T19:21:19Z</published>
    <updated>2012-05-22T19:32:30Z</updated>

    <summary>It&apos;s only been a couple of months since I&apos;ve been using Moose, but it is already changing the way I read code. Of course, it changed the way I write code in a very short time, but I was surprised...</summary>
    <author>
        <name>confuseAcat</name>
        
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/confuseacat/">
        <![CDATA[<p>It's only been a couple of months since I've been using <a href="https://metacpan.org/module/Moose">Moose</a>, but it is already changing the way I read code. Of course, it changed the way I write code in a very short time, but I was surprised when I realized that it also changes the way I <em>smell</em> code.</p><p>Things that seemed normal last year, now are a code smell to me. Whenever I see an object being constructed inside a method whose name doesn't start with '<tt>_build_</tt>', I wonder if something bad is going on. Even method calls with parameters are now becoming a code smell. Whenever I smell that smell, I ask myself "should this be an attribute?". </p><p>And that's a pretty good thing. I never found testing my code easier. "Dependency injection" is harder to spell out than it is to just do it. And I find myself being able to understand my own code a couple of weeks later.</p><p>So, to sum it all up: Thank you, Moose Cabal!</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Perl testing with Jenkins/Hudson: avoiding some pitfalls</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/confuseacat/2011/09/perl-testing-with-jenkinshudson-avoiding-some-pitfalls.html" />
    <id>tag:blogs.perl.org,2011:/users/confuseacat//351.2166</id>

    <published>2011-09-04T10:36:05Z</published>
    <updated>2011-09-04T17:08:37Z</updated>

    <summary>Having continuous integration is incredibly helpful and setting up a Jenkins server is surprisingly easy. However, configuring Jenkins to run your Perl unit tests is a wee bit harder, although it may seem easy at first. Here are a couple...</summary>
    <author>
        <name>confuseAcat</name>
        
    </author>
    
    <category term="jenkinstapjunitperl" label="jenkins tap junit perl" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/confuseacat/">
        <![CDATA[<p>Having continuous integration is incredibly helpful and setting up a Jenkins server is surprisingly easy. However, configuring Jenkins to run your Perl unit tests is a wee bit harder, although it may seem easy at first. Here are a couple of issues I ran into and some things I learned:</p>

<p>We all know that Perl unit tests, aided by <code>Test::More</code> and <code>prove</code> produce results in <a href="http://testanything.org">TAP format</a>. But since Jenkins is Java and someone once dictated that Java shall only read XML and only write XML, Jenkins expects test results to conform to the <a href="http://www.junit.org/">JUnit XML format</a>. Well, OK, if they want XML, let's give them XML. So you turn to your favorite search engine and ask it <a href="http://www.google.com/search?q=transform+TAP+to+junit">how to transform TAP to JUnit</a>. You will, of course, find <a href="http://taint.org/2008/03/26/124602a.html">something</a>. And since that something was actually written by Justin Mason of SpamAssassin fame, you stop searching at that point and download that conversion script.</p>

<p>Well, the first mistake was to use Google. It's always better to search <a href="http://search.cpan.org/search?query=junit&amp;mode=module">CPAN</a> first in a case like that. A quick look at those CPAN search results goes a long way. It turns out that you don't have to transform TAP to JUnit, you can simply tell <code>prove</code> to use a different formatter for its output:</p>

<pre><code>prove --formatter=TAP::Formatter::JUnit -l t &gt; test_results.xml
</code></pre>

<p>It's really that simple. Now go ahead and look at the <a href="http://search.cpan.org/perldoc?prove">documentation for <code>prove</code></a>. It's full of goodness! For example, you might want to add <code>--timer</code> to the line above, and you will not only see which tests failed or didn't, but also how long each test took.</p>

<pre><code>prove --timer --formatter=TAP::Formatter::JUnit -l t &gt; test_results.xml
</code></pre>

<p>There is really no need to save temporary TAP output and then transform it into JUnit later.</p>

<p>Moving along, you will find that some people out there are actually trying to tell you that you should run a <code>find</code> to gather your test files and then loop over the results to pass them to <code>prove</code>. Of course that's possible, but it's also quite clumsy, completely unnecessary and it will run your tests in no particular order. So don't do this:</p>

<pre><code>for t in $(find t -name '*.t') ; do prove ....; done
</code></pre>

<p><code>prove</code> will find you test files if you tell it to, simply use <code>-r</code> for that.</p>

<pre><code>prove -r --timer --formatter=TAP::Formatter::JUnit -l t &gt; test_results.xml
</code></pre>

<p>Still relying on Google, you might come up with some clever Unix sexyness: </p>

<pre><code>prove -r --timer --formatter=TAP::Formatter::JUnit -l t | tee test_results.xml
</code></pre>

<p>I guess people use the <code>tee</code> because that lets them see the test output when looking at the console output of their build jobs, but <code>tee</code> has a nasty side effect: It will hide <code>prove</code>'s exit value from Jenkins. Your builds will not fail when you <code>tee</code> their output. At worst, they will be marked as unstable. So if you want your build to fail when some of your tests fail, stay away from <code>tee</code>! Use it for the initial configuration of your jobs to make sure you don't miss any error messages in the console output, but then get rid of it. </p>

<p>Perl is elegant. Or can be. For me, what I learned about <code>prove</code> when working with Jenkins, confirmed this once again.</p>

<p>This is what I started with:</p>

<pre><code>for t in $(find t -name '*.t') ; do prove -l $t 2&gt;&amp;1 | tee -a $OUTPUT/test-$BUILD_NUMBER.tap; done
tap-to-junit-xml --input=$OUTPUT/test-$BUILD_NUMBER.tap --output=test_results.xml
</code></pre>

<p>And this is what I ended up with:</p>

<pre><code>prove -r --timer --formatter=TAP::Formatter::JUnit -l t &gt; test_results.xml
</code></pre>

<p>Not only is this easier to read, it also does what I want.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Can we afford bad code on blogs.perl.org?</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/confuseacat/2011/04/can-we-afford-bad-code-on-blogsperlorg.html" />
    <id>tag:blogs.perl.org,2011:/users/confuseacat//351.1648</id>

    <published>2011-04-13T13:53:58Z</published>
    <updated>2011-04-13T13:57:52Z</updated>

    <summary>If you have a regular look on blogs.perl.org, you will have noticed a certain newcomer around here who keeps posting uncommented Perl scripts. I don&apos;t mind a noob asking stupid questions. Heck, I don&apos;t even think that noobs can ask...</summary>
    <author>
        <name>confuseAcat</name>
        
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/confuseacat/">
        <![CDATA[<p>If you have a regular look on blogs.perl.org, you will have noticed a certain newcomer around here who keeps posting uncommented Perl scripts. I don't mind a noob asking stupid questions. Heck, I don't even think that noobs can ask stupid questions. But can the Perl community afford to have really bad code on a domain named 'perl.org'?</p>

<p>Just asking.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Signal-to-noise ration as a metric of low-level code readability?</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/confuseacat/2010/06/signal-to-noise-ration-as-a-metric-of-low-level-code-readability.html" />
    <id>tag:blogs.perl.org,2010:/users/confuseacat//351.648</id>

    <published>2010-06-18T19:43:30Z</published>
    <updated>2010-06-18T19:51:57Z</updated>

    <summary>I am currently reading through &quot;Effective Perl Programming&quot;. Another fine book that I wish my coworkers would read. (And another fine book they are not going to read). The authors not only want you to write effectively, they want you...</summary>
    <author>
        <name>confuseAcat</name>
        
    </author>
    
    <category term="perl" label="perl" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/confuseacat/">
        <![CDATA[<p>I am currently reading through "<a href="http://www.effectiveperlprogramming.com/">Effective Perl Programming</a>". Another fine book that I wish my coworkers would read. (And another fine book they are not going to read). The authors not only want you to write effectively, they want you to write concise code that is easy to read for anyone with a bit of Perl experience. For example, there is a piece of advice in there that says "Avoid excessive punctuation": "[...] Excessive puctuation makes programs less readale [...]" Exactly!</p>

<p>But why is code less readable when it's full of parenthesis, e.g.? I think the answer is simply that the puctuation lowers the signal-to-noise ratio. When reading code, you are looking for the signals, the statements that actually do something, the meat, the beef. A corollary of this is that a bit of whitespace here and there will increase your code's signal-to-noise ratio because it will make the beef stand out. </p>

<p>Here's a stupid little example:</p>

<pre><code>$x=foo();
if(defined($x)) {
    bar($y,$x,$z);
}
</code></pre>

<p>That's pretty cramped code and whitespace will make it more readable:</p>

<pre><code>$x = $foo();
if ( defined( $x ) ) {
    bar( $y, $x, $z );
}
</code></pre>

<p>It's a lot easier to see what is is that needs to be defined here and what the arguments passed to bar() actually are. </p>

<p>Now, assuming this is possible here, let's get rid of the excessive punctuation:</p>

<pre><code>$x = foo;
if ( defined $x ) {
    bar $y, $x, $z;
}
</code></pre>

<p>Even better! Readable! You don't have to concentrate too hard to see what is going on here.</p>

<p>All this got me thinking, that at this low level of symbols, code readability should be measurable. If punctuation increases the noise and whitespace increases the signal, it should be easy to come up with a formula here. Let's try that, with those examples:</p>

<pre><code>$x=foo();
</code></pre>

<p>Tokens you need to understand: 3 ($x, =, and foo). Amount of whitespace: 0. Punctuation: 2 (the parens).</p>

<p>So that might be a SNR of ( 3 + 0 ) / 2 = 1.5.</p>

<pre><code>$x = $foo();
</code></pre>

<p>Tokens: 3, whitespace: 2, puctuation: 2. This gives us ( 3 + 2 ) / 2 = 2.5.</p>

<p>And finally:</p>

<pre><code>$x = foo;
</code></pre>

<p>which results in a divide by zero exception! In this rare case, this is exactly what we want.</p>

<p>I wonder whether something like this could be turned into a <a href="http://search.cpan.org/perldoc?Perl::Critic">Perl::Critic</a> policy? Or does is already exist?</p>

<p>PS: What I really don't get about "Effective Perl Programming" is the unexplained insistence to put the lexical file handle variable with three-argument open in parentheses. As in open <code>my ($foo), '&lt;', 'foo.bar'</code>.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>L. Peter Deutsch hates Perl</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/confuseacat/2010/05/l-peter-deutsch-hates-perl.html" />
    <id>tag:blogs.perl.org,2010:/users/confuseacat//351.576</id>

    <published>2010-05-22T09:56:44Z</published>
    <updated>2010-05-22T12:03:08Z</updated>

    <summary>... and Larry Wall. Deutsch: [...] that is Lisp is lexically pretty monotonous. Seibel: I think Larry Wall described it as a bowl of oatmeal with fingernail clippings in it. Deutsch: Well, my description of Perl is something that looks...</summary>
    <author>
        <name>confuseAcat</name>
        
    </author>
    
        <category term="quotes" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="perl" label="perl" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/confuseacat/">
        <![CDATA[<p>... and Larry Wall.</p>

<blockquote>
  <p><strong>Deutsch</strong>: [...] that is Lisp is lexically pretty monotonous.</p>

<p><strong>Seibel</strong>: I think Larry Wall described it as a bowl of oatmeal with fingernail clippings in it.</p>

<p><strong>Deutsch</strong>: Well, my description of Perl is something that looks like it came out of the wrong end of a dog. I think Larry Wall has a lot of nerve talking about language design - Perl is an abomination as a language.</p>
</blockquote>

<p>I wonder what the right end of a dog is?</p>

<p><a href="http://en.wikipedia.org/wiki/L._Peter_Deutsch">L. Peter Deutsch</a> (of Ghostscript fame) in <a href="http://www.codersatwork.com">Coders at Work</a></p>
]]>
        

    </content>
</entry>

<entry>
    <title>Why?</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/confuseacat/2010/05/why.html" />
    <id>tag:blogs.perl.org,2010:/users/confuseacat//351.575</id>

    <published>2010-05-22T07:20:31Z</published>
    <updated>2010-05-22T12:03:23Z</updated>

    <summary><![CDATA[I see code&#8230; my @showart; my @articles; map {push(@showart, $_) if($_-&gt;{'article_description_cat'} eq $category);} @{$alteration}; @articles = sort {$a-&gt;{'article_description_rank'} &lt;=&gt; $b-&gt;{'article_description_rank'}} @showart; So what do we have there? A superfluous temporary variable: @showart. A use of map that explicitly throws away...]]></summary>
    <author>
        <name>confuseAcat</name>
        
    </author>
    
        <category term="coworkers" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="rants" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="perlcoworkersrant" label="perl coworkers rant" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/confuseacat/">
        <![CDATA[<p>I see code&#8230;</p>

<pre><code>my @showart;
my @articles;
map {push(@showart, $_) if($_-&gt;{'article_description_cat'} eq $category);} @{$alteration};
@articles = sort {$a-&gt;{'article_description_rank'} &lt;=&gt; $b-&gt;{'article_description_rank'}} @showart;
</code></pre>

<p>So what do we have there?</p>

<ul>
<li>A superfluous temporary variable: <code>@showart</code>.</li>
<li>A use of <code>map</code> that explicitly throws away map&#8217;s return value and goes for the side-effects only.</li>
<li>Lines that are way too long.</li>
<li>Don&#8217;t get me started about the semicolon in the map expression.</li>
<li>Don&#8217;t make me think about the whitespace that is missing in all sorts of places.</li>
<li>An instance of intentional obfuscation?</li>
</ul>
]]>
        

    </content>
</entry>

</feed>
