<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>enkidu</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/enkidu/" />
    <link rel="self" type="application/atom+xml" href="http://blogs.perl.org/users/enkidu/atom.xml" />
    <id>tag:blogs.perl.org,2009-11-03:/users/enkidu//1614</id>
    <updated>2013-05-16T14:00:21Z</updated>
    <subtitle>A blog about the Perl programming language</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 4.38</generator>

<entry>
    <title>Porting Tests to Perl 6</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/enkidu/2013/05/porting-tests-to-perl-6.html" />
    <id>tag:blogs.perl.org,2013:/users/enkidu//1614.4683</id>

    <published>2013-05-16T13:32:12Z</published>
    <updated>2013-05-16T14:00:21Z</updated>

    <summary>Just reading Brent Laabs post on Porting a Module to Perl 6 and two things occurred to me. The first was that I should start getting into Perl 6 sooner rather than later and get the few modules I&apos;m involved...</summary>
    <author>
        <name>Enkidu</name>
        
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/enkidu/">
        <![CDATA[<p>Just reading Brent Laabs post on <a href="http://blog.brentlaabs.com/2013/05/porting-module-to-perl-6.html">Porting a Module to Perl 6</a> and two things occurred to me.  The first was that I should start getting into Perl 6 sooner rather than later and get the few modules I'm involved with changed over, ready for the big day.  </p>

<p>The second and more important one is that, if I'm to assure myself that the module behaves in the same way as the Perl 5 module, the original tests need to pass.  I would have invested much on those tests to verify the module works the way I think it should.  To avoid finger trouble in re-writing those tests, I would sleep better if some kind soul could create the bit of magic that will parse [<em>your favourite testing framework</em>] and run it against my future Perl 6 module.  Or, conversely, to write Perl 6 tests that run against Perl 5 modules and verify that the new suite is identical to the old suite, so I can then proceed with porting the module.</p>

<p>Maybe this is already possible in Rakudo.  I don't even know how much I don't know.</p>]]>
        
    </content>
</entry>

<entry>
    <title>Finally turning on to DBIx::Class</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/enkidu/2013/03/finally-turning-on-to-dbixclass.html" />
    <id>tag:blogs.perl.org,2013:/users/enkidu//1614.4454</id>

    <published>2013-03-22T16:22:51Z</published>
    <updated>2013-03-23T10:02:40Z</updated>

    <summary>It&apos;s a snow day and time to play. I have a cow orker who prefers to write all the business logic in SQL because he hasn&apos;t moved on from what he learnt in the 80&apos;s. Nice guy, but I suspect...</summary>
    <author>
        <name>Enkidu</name>
        
    </author>
    
        <category term="Dbic" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="migrating" label="migrating" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="revenge" label="revenge" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/enkidu/">
        <![CDATA[<p>It's a snow day and time to play.  I have a cow orker who prefers to write all the business logic in SQL because he hasn't moved on from what he learnt in the 80's.  Nice guy, but I suspect he has a hearing deficiency because he never listens to what I have to say (or he's got a nasty case of NIH).  Yesterday, he happened to be getting on my last nerve, but that no longer matters for I will have sweet, <em>sweet</em> Revenge.  </p>

<p>Today I started playing with dbic.</p>]]>
        <![CDATA[<p>It always takes me sometime to find the required mental space to try something new.  Having too much on my plate means that I rarely take the time to explore the latest shiny, but I'm sitting here quietly staring at the screen contemplating how <a href="http://www.dbix-class.org/">dbic</a> is going to change my life and you, my brothers and sisters, are only 1 CPAN install and 2 command lines away from where I am because - Jesus H. Tap Dancing Christ - I've seen the light and I'm going to tell you about it.</p>

<p>I work with a huge, sprawling Oracle database that hasn't been designed - it grew.  It consists of 158 tables with 11 primary keys and no foreign keys.  How do I drag this morass into the 21st century?</p>

<p>First thing, install <strong><a href="https://metacpan.org/module/DBIx::Class::Schema::Loader">DBIx::Class::Schema::Loader</a></strong> from CPAN (you know how to do that).  This gives you a life-giving script called <strong>dbicdump</strong>.<br />
Create a temp directory and drop into it.  Run this:<br />
<pre><br />
<code>dbicdump Local::Schema dbi:Oracle:database_name username password</code></pre><br />
where <em>dbi:Oracle:database_name</em> is the connection string that you use in a standard DBI connection.</p>

<p> <br />
This interrogates your database schema and writes out a module for each table.  For me it threw up a few exceptions about 3 ill-behaved tables and finished 5 minutes later.  So, go ahead and do this now and make a brew.  It's ok, I'll wait.</p>

<p><br />
(No <u>really</u> - do it now and read the rest while you're waiting!)</p>

<p>You'll see all the modules with <code>ls Local/Schema/Result/</code>.  Pick out your favourite table and column.  You may notice that the module name is not quite the same as the table name.  It took my underscore separated tables and turned them into camel case (<strong>my_table</strong> becomes <strong>MyTable</strong>).  The only thing I'm going to do is get a list of all the values of the <strong>name</strong> column in that table.  Run this on the command line:<br />
<pre><br />
<code>perl -MLocal::Schema -E '$s = Local::Schema->connect("dbi:Oracle:database_name", "username", "password");<br />
say $_->name() for $s->resultset("MyTable")->all()'<br />
</code></pre><br />
That's it.<br />
<p>...</p></p>

<p>That's all you need to know to realize that the days of writing <br />
<pre><code>$dbh = DBI->connect("dbi:Oracle:database_name", "username", "password"); <br />
$sth = $dbh->prepare("SELECT * FROM my_table" ); <br />
$sth->execute();<br />
while ($hash_ref = $sth->fetchrow_hashref()) {<br />
    print $hash_ref->{name};<br />
}</code></pre> over and over, have gone away forever.<br />
<p><br />
Now, sit back and wonder at how little effort you've just expended to open up <em>any</em> frickin' database like an over-ripe peach and think about what you're going to do with this monster-on-a-lead, juice dribbling down your chin.</p>

<p></p><p></p>

<p>Me?  I'm going to play around with dbic until I've got the killer query that requires contortions in SQL and then just drop it casually on my cow orker, in front of an audience.  That'll fix his little red wagon!</p>]]>
    </content>
</entry>

<entry>
    <title>What goes well with DBIx::Class - the review.</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/enkidu/2013/02/what-goes-well-with-dbixclass---the-review.html" />
    <id>tag:blogs.perl.org,2013:/users/enkidu//1614.4331</id>

    <published>2013-02-15T15:54:00Z</published>
    <updated>2013-02-17T08:41:02Z</updated>

    <summary>In my case, it went very well with Fideos de Sepia and a nice glass of Fina Enguera. This is not really a review of Dave&apos;s Perl School. His style is competent, informative, sometimes entertaining and pitched right at my...</summary>
    <author>
        <name>Enkidu</name>
        
    </author>
    
        <category term="Dbic" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="dbic" label="dbic" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/enkidu/">
        <![CDATA[<p>In my case, it went very well with Fideos de Sepia and a nice glass of Fina Enguera.</p>

<p>This is not really a review of Dave's <a href="http://perlschool.co.uk/courses/database-programming-with-perl-and-dbixclass/">Perl School</a>.  His style is competent, informative, sometimes entertaining and pitched right at my level of Perl.  Unlike the Moose school I went to in December, this lecture could have gone on for another hour discussing the finer points, but people have lives and we finished at 5pm.</p>

<p>This is more a bit of navel-gazing about what DBIx::Class might mean to me.</p>]]>
        <![CDATA[<p>As opposed to Moose, where I'd already seen the power and wanted more, I had no real idea about dbic, other than the general feeling that I should get to know something about it.  I have a local module where I hide away all the database connections I use to extract data from Oracle, Mysql and Postgres.  I use DBI and my skill with SQL is just good enough to get what I want.  It's not as onerous as building an object class before Moose came along, so I didn't come back from the School with the immediate urge to convert all my code.</p>

<p>Don't get me wrong - I see the shiny in abstracting away the database interface by letting you <em>describe</em> the data and I will look for an excuse to write new code in dbic, but I'm not seeing the immediate win in migrating.  Sure, the dbic will go and read the schema and set up the accessors for me (which you have to admit is <em>really</em> cool), but I already had to read the schema myself to figure out how to get the data out.  It's a big win if you need to create a schema from scratch, but is there a way of getting data out of existing tables that I don't intimately know and may not have foreign keys linking tables?  Is this where I should be using <code>dbicdump</code>?</p>

<p>Maybe it's a bit like 2001 where I was introduced to Test.  I wrote a few because other people said it was a good idea.  It wasn't until 6 months later when a test bubbled up a logic error and saved my sanity that I truly understood the power of testing.  Perhaps I will experience the same epiphany after I start to use it.  That time might come sooner than expected ever since Oracle's accounting script found a feature that silently got turned on at our installation, we've never used, but is horrendously expensive.  The Director was livid and wanted to know how fast we could move to Postgres or even (<em>shudder</em>) SQLServer.</p>

<p><big>Cool Things</big><br />
<ul><br />
	<li><strong>Building SQL statements on the fly</strong> - the week before the class, I found myself having to wanting to switch between a select on one table and select joining 2 tables depending on the parameters passed to the function.  I think dbic would have saved me some headache</li><br />
    <li><strong>Doesn't duplicate the schema</strong> - I mentioned this earlier.  It's just the Right Thing to do.</li><br />
    <li><strong>Creates a PNG of the schema</strong> using SQL::Translator - Shiny!</li><br />
    <li><strong>DBIC_TRACE</strong> shows the SQL that would be generated by your call</li><br />
    <li><strong>Generates tables on the fly</strong> - why didn't I know about this a couple of years ago!</li><br />
</ul><br />
<strong>Other things:</strong> Transactions, chaining result sets, find() returns a single row.</p>

<p><strong><br />
Things you need to understand:</strong> The difference between a <strong>Result</strong> and a <strong>ResultSet</strong>.  I think if you can grok what a ResultSet is quickly, this module will be your mollusc.  Time will only tell whether <em>I</em> do or not.</p>

<p><big>Food</big></p>

<p>As planned in the last entry, I did make it to the <a href="http://www.brindisa.com/restaurants/tramontana-brindisa-shoreditch/">tapas bar</a> after the school.  Nice place just off Old Street, only 10 minutes walk from the Google Campus.  Not cheap, but the food was excellent.  As everyone else had scurried home, I was on my own.  The croquetas I could have happily shared, lovely as they were.  Very fine crust and good flavour, but a bit creamy to have all 4 to myself.  The fideos de sepia (cuttlefish with pasta noodles) was just a whim - something I've never had before.  It was excellently prepared while I watched, the seafood just a little crunchy and ... <em>odd</em>.  I still don't know if I like it, but it was worth the £10.</p>

<p>I would go again the next Perl School I come down for (Dave may have his arm twisted into giving sessions on Testing or Web frameworks)  and with the menu in hand you could order immediately on arrival and be back for the afternoon session.  On the other hand, some of the others came back with Vietnamese takeaway lunch box which looked rather tempting.</p>

<p>So, here's an apt simile.  DBIx::Class is like cuttlefish.  Intelligent, tasty, slightly mysterious and I'm still making up my mind about it.  </p>]]>
    </content>
</entry>

<entry>
    <title>But what goes well with DBIx::Class?</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/enkidu/2013/02/but-what-goes-well-with-dbixclass.html" />
    <id>tag:blogs.perl.org,2013:/users/enkidu//1614.4252</id>

    <published>2013-02-04T16:40:17Z</published>
    <updated>2013-02-04T21:16:29Z</updated>

    <summary>Looking forward to Dave&apos;s PerlSchool on Saturday. I was satisfied with the format of his Moose school and found myself going back through the notes for how to set a default hashref for an attribute. Got me thinking of how...</summary>
    <author>
        <name>Enkidu</name>
        
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/enkidu/">
        <![CDATA[<p>Looking forward to Dave's <a href="http://perlschool.co.uk/courses/database-programming-with-perl-and-dbixclass/">PerlSchool</a> on Saturday.  I was satisfied with the format of his Moose school and found myself going back through the notes for how to set a default  hashref for an attribute.  Got me thinking of how I could really use a cheat sheet.  There's a lot of documentation for Moose, certainly enough to get you started, but it saves time if somebody sits down and organizes the information into a flowing story with just enough examples to suggest some use cases.  At the other extreme, when I wanted a quick answer to a simple question, sifting through 15 perldoc pages or 165 slides was less satisfying.  I've got an idea for an A4 cheatsheet with every possible option which I've started in a GoogleDoc.  When it's half-decent, I'll post for comment.</p>

<p>Last time, I intentionally had 2 or 3 hours to kill after the course and wandered the area around Old Street looking for an interesting place to eat.  I ended up at El Paso on Old Street near Curtain Road and had a very nice chimichanga and mojito.  On my way back, I found that I'd missed the mouth-watering Street Food Festival.  Had I but known!!</p>

<p>This time I think I'll try the tapas bar across the road at <a href="http://www.brindisa.com/restaurants/tramontana-brindisa-shoreditch/">La Tramontana</a>.  Anyone care to join me?</p>

<p>&iexcl;Hasta la pr&oacute;xima!</p>]]>
        
    </content>
</entry>

<entry>
    <title>Moose Mashing</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/enkidu/2013/01/moose-mashing.html" />
    <id>tag:blogs.perl.org,2013:/users/enkidu//1614.4228</id>

    <published>2013-01-23T10:27:54Z</published>
    <updated>2013-02-05T17:25:12Z</updated>

    <summary>Does Moose eliminate the sin of False Impatience* by making it so damn easy to just add a dash of OO to your script in order to avoid messing around with data structures? That&apos;s how I&apos;ve started, so there&apos;s bound...</summary>
    <author>
        <name>Enkidu</name>
        
    </author>
    
        <category term="Moose" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="moose" label="Moose" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/enkidu/">
        <![CDATA[<p>Does Moose eliminate the sin of False Impatience* by making it so damn easy to just add a dash of OO to your script in order to avoid messing around with data structures?  That's how I've started, so there's bound to be a few contusions as I find the corners of my own ignorance, digging out the notes from Dave Cross' most excellent <a href="http://perlschool.co.uk/courses/object-oriented-programming-with-perl-and-moose/">Perl School</a> on the subject.  It's not pretty, but it gets the job done.</p>

<p>This is a story about how hacking away at a script taught me some things that I should have known.</p>]]>
        <![CDATA[<p>The second time I used Moose in anger was to manage lists of group members, a 2 attribute, 2 method class, at the top of my original script after I'd loaded in my local utility modules with helper functions (exported by Exporter).</p>

<pre>
<code>
use Keele::Utilities::Database;
use Keele::Utilities::LDAP;

<p>package GoogleGroups;<br />
use Moose;<br />
</code><br />
</pre></p>

<p>My script dies with the error that it can't find the function exported from the Utilities module.  So I move the class declaration to the end of the script and the error is that it can't find the object accessor. Hmmm. ...  Eventually solved by putting the class declaration at the top and <code>use</code>ing the utility modules after the <code>__PACKAGE__->meta->make_immutable;</code> call.  Y'know, it's staring me in the face right now that the <code>package</code> line should have been the first thing in the script, but hey.</p>

<p>The next fun bit was remembering to add in a default empty hashref to hold the members<br />
<pre><br />
<code><br />
has 'members' => (<br />
    is  => 'rw',<br />
    isa => 'HashRef',<br />
    default =>  {},<br />
);<br />
</code><br />
</pre><br />
and then finding that it needs to be wrapped in an anonymous sub<br />
<pre><br />
<code><br />
    default => sub{ {} },<br />
</code><br />
</pre></p>

<p>In the end, it works, it took as long as it would have had I taken the time to get the data structures correct with plain old hashrefs and I'm more comfortable with Moose.  If my crib sheet begins to approach anything close to useful, I'll post it.</p>

<p>Postscript: A lot of things become obvious when you start to tell someone else about them.  Perhaps if I'd blogged first, I could have saved myself an hour faffing around.</p>

<p>* Camel book, 2nd ed. Packages, Modules and Object Classes, p277</p>]]>
    </content>
</entry>

</feed>
