<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>preaction</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/preaction/" />
    <link rel="self" type="application/atom+xml" href="http://blogs.perl.org/users/preaction/atom.xml" />
    <id>tag:blogs.perl.org,2009-11-03:/users/preaction//398</id>
    <updated>2013-06-14T01:01:37Z</updated>
    <subtitle>My adventures in Perl</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 4.38</generator>

<entry>
    <title>Chicago.PM New Website! New Meetup URL! New Presentations Project!</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/preaction/2013/06/chicagopm-new-website-new-meetup-url-new-presentations-project.html" />
    <id>tag:blogs.perl.org,2013:/users/preaction//398.4781</id>

    <published>2013-06-14T00:57:24Z</published>
    <updated>2013-06-14T01:01:37Z</updated>

    <summary>Lots of news for the Chicago.PM group! We&apos;ve got a new Chicago.PM website, powered by Github, up at http://chicago.pm.org. The website is completely editable via Github using the Octopress system. We hope to start sharing resources about Perl on our...</summary>
    <author>
        <name>preaction</name>
        
    </author>
    
        <category term="chicago.pm" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="meetup" label="meetup" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="presentations" label="presentations" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/preaction/">
        <![CDATA[<p>Lots of news for the Chicago.PM group! We've got a <a href="http://chicago.pm.org">new Chicago.PM website</a>, powered by <a href="http://github.com">Github</a>, up at <a href="http://chicago.pm.org">http://chicago.pm.org</a>. The website is completely editable via Github using the <a href="http://octopress.org">Octopress</a> system. We hope to start sharing resources about Perl on our website, increasing the exposure of the good tutorials and learning sites.</p>
]]>
        <![CDATA[<p>In addition to the new website, we've changed our Meetup URL, so <a href="http://meetup.com/ChicagoPM">the Chicago.PM Meetup group</a> is now at <a href="http://meetup.com/ChicagoPM">http://meetup.com/ChicagoPM</a>.</p>

<p>Finally, with our new website, we're starting a project to help teach some topics with more depth, to help fill in the gaps between the beginner's materials and writing useful, maintainable code using the best CPAN has to offer. We've started <a href="http://github.com/ChicagoPM">the Chicago.PM Github Organization</a>, where we will be developing presentations that can be used freely by anyone wishing to learn more about Perl, or wanting to teach others about Perl. The presentations and the code inside is licensed under the same terms as Perl itself, so if you can use Perl, you can use and edit the presentations to suit your needs.</p>

<p>The first presentation we're releasing is about <a href="https://github.com/ChicagoPM/Command-Line-Apps">building command-line applications in Perl</a>. I've given this presentation twice now, to excellent reviews. There is still some content that can be added to make it a more robust presentation (especially in the <a href="App::Cmd">http://metacpan.org/release/App-Cmd</a> section), but it's a good talk on going from "Just read my first Perl book and my first Linux book" to "Wielding Perl to build command-line applications."</p>

<p>We hope to build more presentations of this type in the future, as well as other learning materials as time goes on. Let us know if you want to help!</p>
]]>
    </content>
</entry>

<entry>
    <title>Chicago.PM - Beyond grep - Expanding the Programmer Toolset</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/preaction/2013/05/chicagopm---beyond-grep---expanding-the-programmer-toolset.html" />
    <id>tag:blogs.perl.org,2013:/users/preaction//398.4632</id>

    <published>2013-05-01T05:45:14Z</published>
    <updated>2013-05-01T05:50:10Z</updated>

    <summary>Last week, Andy Lester (author of Land the Tech Job You Love) came to talk about tools to help programmers work more efficiently and the 2.0 release of his Ack search tool....</summary>
    <author>
        <name>preaction</name>
        
    </author>
    
        <category term="chicago.pm" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/preaction/">
        <![CDATA[<p>Last week, <a href="http://petdance.com">Andy Lester</a> (author of <a href="http://petdance.com/book/">Land the Tech Job You Love</a>) came to talk about tools to help programmers work more efficiently and the 2.0 release of his <a href="http://beyondgrep.com">Ack search tool</a>.</p>
]]>
        <![CDATA[<p>If you haven't tried Ack, but you use long, complicated Grep lines every day, you're missing out on a great, time-saving tool. <a href="http://beyondgrep.com/install">Installing ack</a> is easy on many platforms (including Windows!).</p>

<p>For those who have added Ack to their regular arsenal, the 2.0 release brings some minor breaking changes (removing the -a option for "search all files" and changing the default file type filter to "all text files") designed to make Ack more Do-What-I-Mean and more compatible with Grep (when it makes sense, ack is made for humans). The result being that using Ack should not take you out of The Zone by requiring you to check the manual every time (something I must confess to doing often with Grep and Find).</p>

<p>I must also confess to using only the most basic Ack command-line options, but Andy explored a lot of functionality I was not aware of, both in Ack and Grep:</p>

<pre><code># Search for full words
ack -w
grep -w

# List the files that matched instead of the matching line
ack -l
grep -l

# Invert the match ("does not match X")
ack -v
grep -v

# Override the default output with capture groups
ack 'use (\S+);' --output='$1' -h | sort -u

# Search the file path for matches (Beyond Find)
ack -f
ack -g

# Accept a list of files to search on STDIN (Built-in Xargs)
ack -x
</code></pre>

<p>The last two features are great for chaining together Ack commands:</p>

<pre><code># Find all perl files not in the 'release' repository
ack -g -v 'release' --perl |
# ... search them for the word '2.46.3207.2'
    ack -x '2.46.3207.2' -w -l | 
# ... and if they match, edit them with sed
    xargs sed -i 's/2.46.3207.2/2.49.3333.12/'
# This little snippet saved me some hours of manual work upgrading version numbers
</code></pre>

<p>There was also a lot of discussion on other tools to help programmers be more efficient. The ones I was most interested in were ctags, which I don't use nearly enough (I use ack instead), and cscope, which I had never heard about. </p>

<p>The problem with adding new things to your toolkit is integrating them into your flow (the flow that can be consciously experienced is not the true flow). Whenever you add a new thing, especially if it replaces an existing thing, you're losing efficiency in the short-term in order to add potential efficiency in the long-term. Despite this, a lot of things are worth it, Ack more-so than most. </p>

<p>Sometimes you don't know you're doing something inefficiently until someone comes along and tells you so, or shows you a solution to a problem you never knew you were having. Ack is a solution to a grep problem: Grep is harder to use in the most-common cases. That said, Grep still has its uses, just uncommon ones.</p>

<p>A big thanks to Andy for giving a presentation on such short notice! Our next meeting is on May 23, so check out <a href="http://chicago.pm.org">the Chicago.pm website</a> and join us!</p>
]]>
    </content>
</entry>

<entry>
    <title>I Bless You in the Name of the Stringified Object</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/preaction/2013/04/i-bless-you-in-the-name-of-the-stringified-object.html" />
    <id>tag:blogs.perl.org,2013:/users/preaction//398.4578</id>

    <published>2013-04-16T00:38:35Z</published>
    <updated>2013-04-16T14:00:03Z</updated>

    <summary><![CDATA[A co-worker came to me today with a curious error message: use DateTime; my $date = DateTime-&gt;new( year =&gt; 2013, month =&gt; 4, day =&gt; 15 ); $date-&gt;set_time_zone("Australia/Sydney"); print $date-&gt;today;' This code gives the error Can't locate object method "_normalize_nanoseconds"...]]></summary>
    <author>
        <name>preaction</name>
        
    </author>
    
        <category term="weird" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="error" label="error" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/preaction/">
        <![CDATA[<p>A co-worker came to me today with a curious error message:</p>

<pre><code>use DateTime;
my $date = DateTime-&gt;new( year =&gt; 2013, month =&gt; 4, day =&gt; 15 );
$date-&gt;set_time_zone("Australia/Sydney");
print $date-&gt;today;'
</code></pre>

<p>This code gives the error <code>Can't locate object method "_normalize_nanoseconds" via package "2013-04-15T00:00:00" at /usr2/local/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/x86_64-linux-thread-multi/DateTime.pm line 252.</code></p>

<p>The package "2013-04-15T00:00:00" is the curious part: It looks like a stringified DateTime, but who could possibly be stringifying a DateTime object and then using that as a package name?</p>
]]>
        <![CDATA[<p>It turns out the problem is at <code>$date-&gt;today</code>. <code>today()</code> is a constructor, constructors are class methods, constructors inevitably call <code>bless</code> on the class they are invoked with. But, we called this constructor with an object instance (a blessed reference), not a class.</p>

<p>In an object without overloads, this fails (as expected) with an error message: <code>Attempt to bless into a reference</code>. But DateTime overloads stringification. So instead of trying to use an object as a package name, the object gets stringified and that string gets used as a package name.</p>

<p>This problem could be mitigated by checking for refs in the constructor, dying as a result. I'm not sure if it would be a good idea to disallow stringification during a call to <code>bless</code>, for historical reasons (it's not a bug if it's working as expected, it is a bug if a change breaks code).</p>

<p>Here's some sample code to play around with the problem. Enable/disable the overload (comment out the <code>use overload (...)</code>) and see what changes:</p>

<pre><code>use strict;
use warnings;

package Foo;

use overload (
    q{""} =&gt; sub { return "Bar" },
);

sub new {
    my ( $class ) = @_;
    return bless {}, $class;
}

sub greet {
    print "Hello, World!\n";
}

package main;

my $obj = Foo-&gt;new;
$obj-&gt;greet;

my $clone = $obj-&gt;new;
$clone-&gt;greet;
</code></pre>
]]>
    </content>
</entry>

<entry>
    <title>Thoughts while changing the API of a massive framework...</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/preaction/2013/04/thoughts-while-changing-the-api-of-a-massive-framework.html" />
    <id>tag:blogs.perl.org,2013:/users/preaction//398.4553</id>

    <published>2013-04-12T00:39:46Z</published>
    <updated>2013-04-12T00:50:43Z</updated>

    <summary>At the Bank we have a home-grown ETL framework that we&apos;ve been using for quite some time. We recently completed a total rewrite, but unfortunately we left out a few changes. Had I gotten those changes in 5 months ago,...</summary>
    <author>
        <name>preaction</name>
        
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/preaction/">
        <![CDATA[<p>At the Bank we have a home-grown ETL framework that we've been using for quite some time. We recently completed a total rewrite, but unfortunately we left out a few changes. Had I gotten those changes in 5 months ago, I would have only had to break the API of about 10 modules. Today, in order to make those changes, I have to break the API of 122 modules.</p>

<p>What follows is an account of this ordeal, provided for entertainment value only. There will be a future post that explains some of the things I did to make this task surmountable.</p>
]]>
        <![CDATA[<ul>
<li><strong>Day 1:</strong></li>
<li>3:45pm - 122 modules left</li>
<li>4:31pm - 112 modules left - And then I remember there's another feature to add that will require another migration of all these modules I will have to do.</li>
<li>4:52pm - 106 modules left - <a href="https://metacpan.org/module/Test::Continuous">Test::Continuous</a> removes 3 steps for each module. Total time saved: HOLY FUCK THAT'S AWESOME</li>
<li>5:35pm - 97 modules left - Every commit message during this ordeal is another love note to those who put off this migration five months ago, when there were only 10 modules to migrate.</li>
<li>6:09pm - 94 modules left - New API to change: Create a role to do it for me! +100 experience points!</li>
<li>6:15pm - 93 modules left - Why unpack the hash of args passed-in to the method if the method you're calling takes exactly the same arguments? <code>my $arg_name = $args{arg_name}; return $self-&gt;method( arg_name =&gt; $arg_name )</code> should never happen!</li>
<li>6:37pm - 87 modules left - A thought: If the other team using this project ultimately rejects this API change, I get to write my own brand-new ETL framework from scratch! Temptation, thy name is Zoidberg.</li>
<li>6:51pm - 84 modules left - Found a bug in the new API! Finally something interesting to do!</li>
<li>7:00pm - 80 modules left - Every time you copy/paste code in tests, God inflicts another programmer with carpal tunnel. Please think of the programmers.</li>
<li><strong>Day 2:</strong></li>
<li>3:02pm - 80 modules left - Let's see if I remember all the macros I left in vim over the weekend... Test::Continuous is still running, which is nice</li>
<li>3:18pm - 71 modules left - The end is in sight!</li>
<li>4:30pm - 52 modules left - Perhaps I was premature...</li>
<li>6:30pm - 41 modules left - Caught up putting out fires in other places. Derail.</li>
<li><strong>Day 3:</strong></li>
<li>1:35pm - 41 modules left - Another bug in the new API. Doing it this way is certainly shaking out the bugs.</li>
<li>2:21pm - 20 modules left - Smooth sailing at last...</li>
<li>3:47pm - 0 modules left - AND THE CROWD GOES WILD!</li>
</ul>

<p>Total time elapsed: 3.25+3.5+2.25 = 9 hours. Not bad for 130 commits to migrate 122 modules.</p>
]]>
    </content>
</entry>

<entry>
    <title>Chicago.PM - Dependency Injection (also: Beam::Wire)</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/preaction/2013/03/chicagopm---dependency-injection-also-beamwire.html" />
    <id>tag:blogs.perl.org,2013:/users/preaction//398.4483</id>

    <published>2013-03-29T17:47:19Z</published>
    <updated>2013-03-29T17:56:16Z</updated>

    <summary>At this month&apos;s Chicago.PM meeting, I gave a presentation on Dependency Injection and my new module, Beam::Wire. [EDIT: The presentation doesn&apos;t appear to work on mobile devices. I&apos;m trying deck.js, and I&apos;m not sure I like it.]...</summary>
    <author>
        <name>preaction</name>
        
    </author>
    
        <category term="chicago.pm" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="beam" label="beam" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="cpan" label="cpan" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="dependencyinjection" label="dependency injection" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/preaction/">
        <![CDATA[<p>At <a href="http://www.meetup.com/Windy-City-Perl-mongers-Meetup/events/104681992/">this month's Chicago.PM meeting</a>, I gave <a href="http://preaction.github.com/Perl/Dependency-Injection.html">a presentation on Dependency Injection and my new module, Beam::Wire</a>.</p>

<p>[EDIT: The presentation doesn't appear to work on mobile devices. I'm trying deck.js, and I'm not sure I like it.]</p>
]]>
        <![CDATA[<p>When I started my current job, I was introduced to the custom Dependency Injection framework they built. At the time, I was dismissive: "It's programming via config file, which is always limiting (and so, a Bad Idea)." "It's something that Java needs, but not Perl."</p>

<p>Slowly, and with great reluctance, I learned where dependency injection fits into a large framework: It removes the work of creating objects. Any object. Anywhere. </p>

<p>For an idea of how powerful this is, do a quick search through your libraries and scripts (except for tests) for /->new/. Hopefully, the results will only be defaults (otherwise you've got tight coupling, another problem DI can fix). But every time you call an object constructor is another place where dependency injection can reduce the amount of code you write and maintain.</p>

<p>So, since the bureaucracy means I can't simply open-source the DI framework we have at $dayjob, I decided to make my own. </p>

<p><a href="http://metacpan.org/modules/Beam::Wire">Beam::Wire</a> is <a href="http://search.cpan.org/~preaction/Beam-Wire/">available on CPAN</a>. There are a lot of features planned for it, so stay tuned! If you're interested, <a href="http://play-perl.org/quest/5130110d20d03f841200005b">up-vote Beam::Wire on play-perl</a>.</p>
]]>
    </content>
</entry>

<entry>
    <title>Chicago.PM - Mojolicious</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/preaction/2013/03/chicagopm---mojolicious.html" />
    <id>tag:blogs.perl.org,2013:/users/preaction//398.4433</id>

    <published>2013-03-15T01:38:42Z</published>
    <updated>2013-03-15T01:49:01Z</updated>

    <summary>February&apos;s meeting was about the Mojolicious Web Framework. Joel Berger has written a minimal Perl CMS called Galileo, and agreed to give a talk about the benefits of Mojolicious. Best of all, the talk itself was written in Mojolicious! As...</summary>
    <author>
        <name>preaction</name>
        
    </author>
    
        <category term="chicago.pm" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="chicago" label="chicago" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/preaction/">
        <![CDATA[<p>February's meeting was about the <a href="http://mojolicio.us">Mojolicious Web Framework</a>. <a href="http://blogs.perl.org/users/joel_berger/">Joel Berger</a> has written a <a href="https://metacpan.org/module/Galileo">minimal Perl CMS called Galileo</a>, and agreed to give a talk about the benefits of Mojolicious.</p>

<p>Best of all, <a href="https://github.com/jberger/MojoliciousIntroduction">the talk itself was written in Mojolicious</a>! As Joel was talking, he was able to edit the code and display the results, showing off various features of Mojolicious like:</p>

<ul>
<li>Websockets</li>
<li>Easy testing (even of websockets)</li>
<li>Helper scripts</li>
<li>Mojo templates</li>
</ul>

<p>There are quite a few interesting parts of Mojolicious that make it worth giving a look to, and I hope to be able to do so with some web projects that have been sitting in my queue for a while (I wrote a nice ticket tracker with AngularJS, but the backend is Python, I'd like to fix that glaring mistake).</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Testing is a Feature of Your Service</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/preaction/2012/11/testing-is-a-feature-of-your-service.html" />
    <id>tag:blogs.perl.org,2012:/users/preaction//398.4019</id>

    <published>2012-11-05T05:00:00Z</published>
    <updated>2012-11-05T04:37:12Z</updated>

    <summary>My job at Bank of America consists largely of data collection and storage. To collect data in Perl, I have to write XS modules to interface with the vendor-supplied native libraries. Because I want to know my code works, my...</summary>
    <author>
        <name>preaction</name>
        
    </author>
    
        <category term="testing" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="perl" label="Perl" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="testing" label="testing" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/preaction/">
        <![CDATA[<p>My job at Bank of America consists largely of data collection and storage. To collect data in Perl, I have to write XS modules to interface with the vendor-supplied native libraries. Because I want to know my code works, my XS modules come with robust test suites, testing that everything works correctly.</p>

<p>Since the XS module was intended to be used by other, larger systems, I decided to help those larger systems test their dependency on my module: I included a <a href="http://search.cpan.org/perldoc?Test::MockObject">Test::MockObject</a> that mocked my module's interface. By using my test module, the tests can try some data and see if their code works.</p>

<p>But the hardest part to test is always the failures. How do they test if the news service goes down in the middle of a data pull? How about if it goes down between data pulls but still inside the same process? How do they test if the user has input an invalid ID for data?</p>

<p>To help them write good error-checking and recovery, I added specific Test::MockObjects that return failure conditions. Want to know what happens if an ID is invalid? Use the Test::Feed::InvalidId mock API. Want to know what happens if the feed goes down in the middle of a process? Use the Test::Feed::RandomDisconnect mock API.</p>

<p>By providing these mock objects, users can write more robust code more simply. Then, in the eventuality that the service fails, they know exactly what their code won't do: Wake them up at 3 in the morning because of an unexpected, unhandled error. These mock objects don't even need an external connection to operate, so they can be as fast as possible.</p>

<p>Unfortunately, this is a limited solution that covers only what I know about. If the service changes, or has an error I didn't cover, the code still fails. For this reason, for the web services I build, I build in explicit ways to get errors. When the service itself gives the exact error output it gives in exceptional circumstances, it can be tested and handled. Instead of a mock service, it's the real service (perhaps on a dev system), with real input, returning a real (but desired) error message.</p>

<p>By providing as much help as possible to the users of my code, I can make sure they can create robust applications that make their own users happy. By helping users respond to and deal with the error conditions of my code, I can cut down on the support requests and bug reports. Being proactive about testing helps everyone write better code.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Chicago.PM Report - Scripting Git With Perl</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/preaction/2012/10/chicagopm-report---scripting-git-with-perl.html" />
    <id>tag:blogs.perl.org,2012:/users/preaction//398.4002</id>

    <published>2012-10-29T04:45:10Z</published>
    <updated>2012-10-29T04:59:39Z</updated>

    <summary>This month&apos;s presentation was on the Git::Repository Perl module, given by me. In both my jobs, I use the Git::Repository module to automate releases. At Double Cluepon, I use it to create the release packages based on tagged commits, so...</summary>
    <author>
        <name>preaction</name>
        
    </author>
    
        <category term="chicago.pm" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="git" label="git" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="perl" label="perl" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="release" label="release" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/preaction/">
        <![CDATA[<p>This month's presentation was on the <a href="http://search.cpan.org/dist/Git-Repository/">Git::Repository</a> Perl module, given by me. In both my jobs, I use the Git::Repository module to automate releases.</p>

<p>At <a href="http://www.doublecluepon.com">Double Cluepon</a>, I use it to create the release packages based on tagged commits, so that releasing our software is exactly: git tag vX.X.X &amp;&amp; git push --tags. A Perl script builds every package and then pushes them to our update server, where the game will check for a new release.</p>

<p>At Bank of America, we use it to combine our 20-30 Perl distributions into a single release. Using git submodules, we have a "release repository" that holds references to all the modules for each team's releases (some are team-specific, others are shared between teams). A Perl script manages the submodules, determines when the submodule refs need to be updated, tags and branches for each release, and finally builds and installs our modules using <a href="http://search.cpan.org/dist/Module-Build/">Module::Build</a> and <a href="http://search.cpan.org/dist/local-lib/">local::lib</a>.</p>

<p>All this Git stuff gave me some ideas for possible useful code I can release, perhaps leading to me finally recovering my CPAN ID.</p>

<p><a href="http://preaction.github.com/Perl/Scripting-Git.html">The slides for my Scripting Git With Perl talk</a></p>

<p><a href="https://gist.github.com/3943302">The code for the script that automatically builds releases tagged like "vX.X.X"</a></p>
]]>
        

    </content>
</entry>

<entry>
    <title>Using MooseX::Types to inflate config values</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/preaction/2012/10/using-moosextypes-to-inflate-config-values.html" />
    <id>tag:blogs.perl.org,2012:/users/preaction//398.3980</id>

    <published>2012-10-22T05:44:35Z</published>
    <updated>2012-10-22T05:50:05Z</updated>

    <summary>For a large application, configuration files become a necessity. They help flexible code be used in multiple instances across multiple modules. But they are, for the most part, only data structures, which can be a problem if the configured object...</summary>
    <author>
        <name>preaction</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/preaction/">
        <![CDATA[<p>For a large application, configuration files become a necessity. They help flexible code be used in multiple instances across multiple modules. But they are, for the most part, only data structures, which can be a problem if the configured object is expecting another configured object.</p>

<pre><code>package FakeRepository;

use Moose;
use TimeSeries;
has timeseries =&gt; (
    is =&gt; 'rw',
    isa =&gt; 'TimeSeries',
    required =&gt; 1,
);

package TimeSeries;

use Moose;
has dates =&gt; (
    is =&gt; 'rw',
    isa =&gt; 'HashRef[Number]', # Date =&gt; Value pairs
    default =&gt; sub { {} },
);
</code></pre>

<p>Here we have a FakeRepository that requires a TimeSeries object. Certainly, this is where a Dependency Injection framework could step in and inject the required TimeSeries. The drawback is the indirection: The two configured objects are completely separate, joined only by the reference, like so:</p>

<pre><code># dependency.yml
- service:
    name: 'test_repo'
    class: 'FakeRepository'
    constructor_args:
        timeseries: { ref: test_data }

- service:
    name: 'test_data'
    class: 'TimeSeries'
    constructor_args:
        dates:
            2012-01-01: 1.56
            2012-01-02: 1.69
            2012-01-03: 1.45
</code></pre>

<p>So here, we define two services (objects), test_repo and test_data, and test_repo uses test_data to fill its timeseries attribute. test_data fills in its dates attribute directly from the configuration file.</p>

<p>This works great if test_data is needed by more than just test_repo. It also works fine as-is, the dependency injection framework does the work. But what if we wanted to specify the timeseries value directly, instead of indirectly?</p>

<p>Moose's typing system allows us to do just that. By creating a custom type with a coercion from the data structure in our configuration file, we can create the dependency that our test_repo needs.</p>

<pre><code>package My::Types;

use MooseX::Types qw( HashRef Number );
use TimeSeries;

# declare our TimeSeries class as a type
class_type TimeSeriesType;
# coerce a TimeSeries from a hash of date =&gt; value pairs
coerce TimeSeriesType, from HashRef[Number], via sub {
    # coercions put the value to be coerce in $_
    return TimeSeries-&gt;new( data =&gt; $_ );
};
</code></pre>

<p>Once we have our new custom types, we must use them in our package.</p>

<pre><code>package FakeRepository;

use Moose;
use My::Types qw( TimeSeriesType );

has timeseries =&gt; qw(
    is =&gt; 'rw',
    isa =&gt; TimeSeriesType,  # Not quoted!
    coerce =&gt; 1,            # Activate coercion powers!
    required =&gt; 1,
);
</code></pre>

<p>Now, we can configure our TimeSeries directly from our configuration file, without indirection.</p>

<pre><code># dependency.yml
- service:
    name: 'test_repo'
    class: 'FakeRepository'
    constructor_args:
        timeseries:
            2012-01-01: 1.56
            2012-01-02: 1.69
            2012-01-03: 1.45
</code></pre>

<p>Moose will create the object for us using our defined coercion.</p>

<p>There are other ways to solve this: Enhance the Dependency Injection framework to allow anonymous objects (instead of providing a ref: to an object, provide a full object definition with class: and constructor_args:), but having these coercions in place also helps when writing test code:</p>

<pre><code>use Test::More;
use FakeRepository;
my $repo = FakeRepository-&gt;new(
    timeseries =&gt; {
        '2012-01-01' =&gt; 1.56,
        '2012-01-02' =&gt; 1.69,
        '2012-01-03' =&gt; 1.45,
    },
);
</code></pre>

<p>No need to increase the apparent coverage of the test by including the TimeSeries class, we never have to refer to it at all. No need to lock the interface to a specific TimeSeries class (if that's a desired goal of the project), the coercion takes care of creating the actual object used.</p>

<p>Coercions are a powerful feature. I've used them to build complicated trees from arrays of arrays (more on that later), and I've used them to simply force-uppercase a string so that Log4perl would do its job. Coercions are one more very useful tool in a robust toolbox.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Chicago.PM Report - Project Night: Alien::Base</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/preaction/2012/10/chicagopm-report---project-night-alienbase.html" />
    <id>tag:blogs.perl.org,2012:/users/preaction//398.3957</id>

    <published>2012-10-15T05:04:04Z</published>
    <updated>2012-10-15T05:37:42Z</updated>

    <summary>This month&apos;s project night focused on Joel Berger&apos;s Alien::Base module. The final bugs are either squashed or very close, and we got an introduction to how the whole thing works. I learned some interesting things about DynaLoader that helped cement...</summary>
    <author>
        <name>preaction</name>
        
    </author>
    
        <category term="chicago.pm" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/preaction/">
        <![CDATA[<p>This month's project night focused on Joel Berger's Alien::Base module. The final bugs are either squashed or very close, and we got an introduction to how the whole thing works.</p>

<p>I learned some interesting things about DynaLoader that helped cement the idea that it's an interface on to the system's dynamic linker. I've never dealt with DynaLoader from the Perl side of things, except when it broke, and I would solve those problems in system-specific ways. I learned that DynaLoader could solve those problems in a cross-platform way.</p>

<p>I started writing my own Alien::Base module for libusb, but unfortunately ran out of time. We had some interesting side-discussion on human interface devices and augmented reality.</p>

<p>In two weeks, I'll be <a href="http://www.meetup.com/Windy-City-Perl-mongers-Meetup/events/85508372/">giving a talk on scripting Git with Perl</a>. Come on down and check it out!</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Run-time Class Composition With Moose</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/preaction/2012/10/run-time-class-composition-with-moose.html" />
    <id>tag:blogs.perl.org,2012:/users/preaction//398.3930</id>

    <published>2012-10-08T03:44:59Z</published>
    <updated>2012-10-08T03:50:29Z</updated>

    <summary>Moose is great! At its very basic, it simplifies the boilerplate required to create Perl objects immensely, providing attributes with type constraints, method modifiers for semantic enhancement, and role-based class composition for better code re-use. Moose is built on top...</summary>
    <author>
        <name>preaction</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/preaction/">
        <![CDATA[<p>Moose is great! At its very basic, it simplifies the boilerplate required to create Perl objects immensely, providing attributes with type constraints, method modifiers for semantic enhancement, and role-based class composition for better code re-use.</p>

<p>Moose is built on top of Class::MOP. MOP stands for Meta-Object Protocol. A meta-object is an object that describes an object. So, each attribute and method in your class has a corresponding entry in the meta-object describing it. The meta-object is where you can find out what type constraints are on an attribute, or what methods a class has available.</p>

<p>Since the meta-object is a Plain Old Perl Object, we can call methods on it at runtime. Using those meta-object methods to add an attribute would modify our object, adding that attribute to the object. Using Class::MOP, we can compose classes at runtime!</p>

<p>I have recently used this to great effect in a custom dependency injection and configuration framework we have at Bank of America. By adding a "with" key to the configuration YAML file, the DI will create a new, anonymous class that composes the roles specified.</p>

<pre><code>{
    name: "Repository",
    class: "Bank::HistoricalData::DailyRepository",
    with: [ "Bank::Role::FlattenIntraday", "Bank::Role::CalculateHighLow" ],
    constructor_args: { }
}
</code></pre>

<p>So, when I ask the DI for the "Repository" object, it will get the meta-object for Bank::HistoricalData::DailyRepository, create an anonymous class that extends Bank::HistoricalData::DailyRepository, and then compose the two roles into the new class.</p>

<p>The code to do all this is extremely short:</p>

<pre><code>my $class = $conf-&gt;{class};
my $meta = Moose::Meta::Class-&gt;create_anon_class( 
    superclasses =&gt; [ $class ],
    roles =&gt; $conf-&gt;{with},
);
$meta-&gt;make_immutable; # for performance
my $object = $meta-&gt;name-&gt;new( %{ $conf-&gt;{constructor_args} } );
return $object;
</code></pre>

<p>If a lot of objects end up composing the same roles, I can create a concrete class to get a bit of a performance boost. Since I create a new, anonymous meta-class, I don't have to worry about the class I'm extending being modified, or being made mutable again ($class->meta->make_immutable speeds up a lot of things, but doesn't allow us to add attributes or methods).</p>

<p>With this, I can be a lot more flexible about my dependencies, adding whatever role I want to change their behavior whenever I need.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Chicago.PM Report - App::Services by Sean Blanton</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/preaction/2012/09/chicagopm-report---appservices-by-sean-blanton.html" />
    <id>tag:blogs.perl.org,2012:/users/preaction//398.3892</id>

    <published>2012-09-28T04:49:44Z</published>
    <updated>2012-09-28T05:21:18Z</updated>

    <summary>This month&apos;s technical presentation at Chicago Perl Mongers was about Sean Blanton&apos;s project called App::Services. It&apos;s an interesting project that uses Bread::Board to access resources like databases, logging, ssh, and others. Along the way, we discussed logging practices (most of...</summary>
    <author>
        <name>preaction</name>
        
    </author>
    
        <category term="chicago.pm" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="chicago" label="chicago" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/preaction/">
        <![CDATA[<p>This month's technical presentation at <a href="http://chicago.pm.org">Chicago Perl Mongers</a> was about <a href="http://seanblanton.com">Sean Blanton</a>'s project called <a href="https://github.com/sblanton/App-Services">App::Services</a>. It's an interesting project that uses <a href="http://search.cpan.org/perldoc?Bread::Board">Bread::Board</a> to access resources like databases, logging, ssh, and others.</p>

<p>Along the way, we discussed logging practices (most of us are using <a href="http://search.cpan.org/perldoc?Log%3A%3ALog4perl">Log::Log4perl</a>), and the best way to get a Perl module ready for CPAN (I suggested using <a href="http://search.cpan.org/perldoc?Module%3A%3ABuild">Module::Build</a> directly, but <a href="http://search.cpan.org/perldoc?Dist::Zilla">Dist::Zilla</a> outvoted me).</p>

<p>Sean mentioned in passing the <a href="http://saltstack.org">Salt stack</a> for executing commands across multiple machines, which looks like a very interesting alternative to more detailed tools like Puppet or Chef. Salt seems to be just a simple way to execute commands on multiple machines. Those commands could be administrative (restart httpd), or they could be the application.</p>

<p>One other very interesting thing from App::Services was providing a role for the larger system to use. So one creates App::Service::Log::Log4perl and App::Service::Log::Role, and in other services you consume the App::Service::Log::Role and then can use the Log4perl service. What's interesting about this is that the role is provided alongside the logging service, it isn't something that you have to create yourself. Doing it this way improves interoperability and simplifies adoption by giving you the tedious bits already. It's the difference between "Here's a service, figure out how to use it best" and "Here's a service, and here's a good way to use it" (I've been doing something similar by providing Test::MockObject implementations of my modules for testing purposes in the same distribution as the module itself. An "official" mock object).</p>

<p>In two weeks we have another project night, which I'm hoping is as productive as the last. If you're in the Chicago area, check us out on the <a href="http://www.meetup.com/Windy-City-Perl-mongers-Meetup/">Chicago Perl Mongers meetup page</a>.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Plan (software) to live forever</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/preaction/2012/09/plan-software-to-live-forever.html" />
    <id>tag:blogs.perl.org,2012:/users/preaction//398.3871</id>

    <published>2012-09-23T19:50:27Z</published>
    <updated>2012-09-23T19:54:55Z</updated>

    <summary>How often have I told myself, &quot;I&apos;ll kludge this now and rewrite it later&quot;? And how many times did I actually go back and rewrite that kludgy bit? &quot;Too often&quot; and &quot;not enough&quot;. Many job postings include the phrase &quot;update...</summary>
    <author>
        <name>preaction</name>
        
    </author>
    
        <category term="design" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="design" label="design" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/preaction/">
        <![CDATA[<p>How often have I told myself, "I'll kludge this now and rewrite it later"? And how many times did I actually go back and rewrite that kludgy bit? "Too often" and "not enough". Many job postings include the phrase "update legacy applications," as a euphemism for "rewrite poorly-designed spaghetti." The Y2K problem was a huge exercise in code out-living the developer's plan, with a healthy dose of cargo-culting thrown in. Lately, I've been learning to plan for a likely possibility: My code will survive to haunt my bug lists and my resume for a long time.</p>

<p>We developers are a lazy lot, it's one of our greatest strengths. But like all virtues, it is a double-edged sword that must be wielded responsibly. Laziness is defined as maximum gain for minimal effort. For laziness's sake, I write automated tests to avoid manual testing, I build a development server that mimics production closely so I don't get woken up at 2:00 am when my code blows up, I write documentation that explains the design of the software so I don't have to trace through layers of code to figure it out, and anything else I can do to make sure I don't have to work as hard solving the same problem in the future.</p>

<p>Automated tests are the programmer's best friend. How do I know my code works? By running it. How do I know it works every time I change something? By running it again. Automated tests are simply running the code, controlling the input and checking the output. Since I write tests once according to expected input and output, I can refactor the entire program and be absolutely sure it works for that set of input. I don't need to test manually, clicking everywhere or preparing input on-the-fly, which saves me a lot of time in the long run (because this code will live forever).</p>

<p>A development server is another necessity for laziness. At a former employer I instituted a policy: All custom client projects included renting a development/staging server, no exceptions. This staging server could be completely destroyed and rebuilt with zero consequences. Our deployment processes were automated (more laziness) to make it easy to recover from mistakes. Once our automated tests passed, the clients had a place they could verify the expected behavior and see their ideas in action before releasing the code on to an important, production system. We instituted this policy from learning our lesson: Our laptops were never the same as the production server, so we needed some place that was. Deploying code directly to production is only rarely without unforeseen consequences.</p>

<p>I've been called a hero a couple times now. I don't believe or understand why, but I've noticed it's always after I mention how much I enjoy writing documentation. Writing documentation is like explaining my code to someone else, usually future me: It forces me to think critically about the code, how it works, and what side-effects and edge-cases it has. Every time I go back to write documentation I've found bugs in my code: incorrect assumptions, inelegant algorithms, or undesired side-effects. Usually, the first other person who reads them finds even more: things I didn't think of, or things I thought would never be a problem (famous last words). Writing the documentation gets me out of the code mindset and into the design mindset, and well-designed code is code that will live forever.</p>

<p>There is nothing more boring than a solved problem. By making sure I have automated tests, a sandbox environment, good documentation, I can make maintenance easier, which is the true definition of laziness: Maximum gain for minimum effort.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Chicago.PM Report - App-Prima-REPL</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/preaction/2012/09/chicagopm-report---app-prima-repl.html" />
    <id>tag:blogs.perl.org,2012:/users/preaction//398.3834</id>

    <published>2012-09-15T06:42:46Z</published>
    <updated>2012-09-15T06:52:13Z</updated>

    <summary>We had our first project night at Chicago.PM this week, where we discussed ideas and wrote code for a Perl REPL GUI program (App-Prima-REPL on Github) by David Mertens built on Prima. There were some small ideas to make the...</summary>
    <author>
        <name>preaction</name>
        
    </author>
    
        <category term="chicago.pm" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="appprimareplchicagopm" label="App-Prima-REPL Chicago.PM" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/preaction/">
        <![CDATA[<p>We had our first project night at <a href="http://chicago.pm.org">Chicago.PM</a> this week, where we discussed ideas and wrote code for a Perl REPL GUI program (<a href="http://github.com/run4flat/App-Prima-REPL">App-Prima-REPL on Github</a>) by David Mertens built on <a href="http://www.prima.eu.org/">Prima</a>.</p>

<p>There were some small ideas to make the program more user-friendly, and some larger ideas like an IRC client and guided tutorials based on the same format that <a href="http://perltuts.com">http://perltuts.com</a> uses.</p>

<p>I'm hoping that if I keep saying this, I'll be embarrassed into doing it: I would really love to see the <a href="http://rubykoans.com">Ruby Koans</a> translated into Perl (in spirit, if not in actual content). I've started writing down ideas for chapters, but there is a lot of content to cover.</p>

<p>I added a -I flag and a -M flag to the prima-repl command-line launcher that work as close to perl's flags as I could get. This is one of the things I love about prove and plackup: Where it makes sense, they work like perl does. So now the prima-repl can have subs and modules imported on the command-line.</p>

<p>Altogether it was a wonderfully productive evening and I'm looking forward to the next one. </p>
]]>
        

    </content>
</entry>

<entry>
    <title>Adventures in Debugging C/XS</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/preaction/2011/11/adventures-in-debugging-cxs.html" />
    <id>tag:blogs.perl.org,2011:/users/preaction//398.2452</id>

    <published>2011-11-17T03:30:00Z</published>
    <updated>2011-11-17T06:06:53Z</updated>

    <summary>... or Why A Good Perl Developer Is Not Automatically A Good C Developer, the Story of C Programming via Google. My tests failed, but only sometimes. I was building an XS module to interface with a C wrapper around...</summary>
    <author>
        <name>preaction</name>
        
    </author>
    
    <category term="debugging" label="debugging" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="xs" label="xs" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/preaction/">
        <![CDATA[<p>... or Why A Good Perl Developer Is Not Automatically A Good C Developer, the
Story of C Programming via Google.</p>

<p>My tests failed, but only sometimes. I was building an XS module to interface
with a C wrapper around a C++ library (wrapper unnecessary? probably). <code>make
test</code> was failing with exit code 11. Some quick searching revealed that I had
an intermittent segfault. Calling a function <code>as_xml</code> would fail with a SEGV
in <code>strlen()</code>. This only happened in perl after <code>as_xml</code> when perl was making
a SV out of the return value. This also only mainly happened during <code>make test</code>.
Doing <code>prove</code> myself would succeed 19 times out of 20, where make test would
fail 19 times out of 20. Worse, my C test program would never fail at all.</p>

<p>I changed everything I could think of: Using a debugging perl and keeping
debug symbols in my C library and XS module made the failures happen less
frequently (making debugging ever more frustrating). perlbrew was a big help
here, letting me switch between different versions of perl, debugging perl,
threaded perl, and combinations thereof.</p>

<p>After playing with GDB (once again succeeding 19 times out of 20), I gave up
and searched the web. I found <a href="http://bit.ly/vwnMbb">the same mailing list thread</a>
multiple times, and read it multiple times, not coming up with
anything that was relevant to my situation.</p>

<p>Until I read the thread again after another frustrating half-hour with GDB: I
had forgotten to put a prototype in the .h file, causing the char* pointer
being returned to be treated as an int. On my 64-bit system, this causes
segfaults. The compiler was warning me of this, "<tt>warning: initialization makes
pointer from integer without a cast</tt>", but I didn't understand the warning (and
the web was not helpful on that one).</p>

<p>Adding the function prototype to the C wrapper header and recompiling fixed
the problem.</p>

<p>And that is why I need to learn a lot more about C. Function prototypes?
Header files? Why are those necessary (I'm asking rhetorically, of course)?
Heap and stack? Might as well be herp and derp.</p>

<p>Lesson reinforced: Depth of knowledge does not equal breadth of knowledge.</p>

<p>Also, having IRC at work might have saved me a few hours of hassle.</p>
]]>
        

    </content>
</entry>

</feed>
