<?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>Understanding Nothing &#187; code</title>
	<atom:link href="http://nothing.tmtm.com/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://nothing.tmtm.com</link>
	<description>Tony Bowden's ramblings</description>
	<lastBuildDate>Mon, 16 Jan 2012 08:23:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Migrating Movable Type to WordPress</title>
		<link>http://nothing.tmtm.com/2006/02/migrating-movable-type-to-wordpress/</link>
		<comments>http://nothing.tmtm.com/2006/02/migrating-movable-type-to-wordpress/#comments</comments>
		<pubDate>Mon, 06 Feb 2006 09:03:28 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Movable Type]]></category>
		<category><![CDATA[whipuptitude]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://nothing.tmtm.com/archives/2565</guid>
		<description><![CDATA[The server on which my weblog used to run is getting rather old and crumbly, and brings with it a constant low-level dread that some day, real soon now, it&#8217;s going to give up the ghost. So for the last while, we&#8217;ve gradually been moving everything off it. When it came time to move this [...]]]></description>
			<content:encoded><![CDATA[<p>The server on which my weblog used to run is getting rather old and crumbly, and brings with it a constant low-level dread that some day, real soon now, it&#8217;s going to give up the ghost. So for the last while, we&#8217;ve gradually been moving everything off it. When it came time to move this site, I decided that it was also a good opportunity to migrate away from Movable Type, mostly for the same philosophical reasons that Mark Pilgrim has already <a href="http://diveintomark.org/archives/2004/05/14/freedom-0">set out.</a></p>
<p>I considered moving to <a href="http://typo.leetsoft.com/">Typo</a>, following <a href="http://www.bofh.org.uk/">Piers</a>&#8216; lead, mostly just so I could play with Ruby, but in the end I decided on WordPress. I&#8217;ve made the mistake too many times now of choosing software based on the language in which it&#8217;s written. Yes, it would be nicer to be able to hack on my weblog in Ruby or Perl than in PHP, but I know enough PHP to get by, and I doubt I&#8217;ll be doing that much hacking anyway.</p>
<p>Setting the weblog up was fairly trivial, as most good PHP installations tend to be. Migrating all my old content wasn&#8217;t quite so simple. WordPress 2 seems to have made the import process much simpler than before; most of the information on the process I&#8217;ve found relates to older versions and isn&#8217;t really applicable any more. Unfortunately, although the simple case of importing my MT archive was fairly painless, I really didn&#8217;t want to break all my old links.</p>
<p>There are a few sites that discuss how to maintain your Movable Type post IDs, but they all seem to relate to the old WordPress process. So I had to get my hands dirty in PHP much quicker than expected.</p>
<p>Firstly, I had to edit MT/App/CMS.pm in my MT setup, adding a line to include the entry id in the export output:</p>
<pre>
AUTHOR: &lt;$MTEntryAuthor$&gt;
TITLE: &lt;$MTEntryTitle$&gt;
<strong>ID: &lt;$MTEntryID$&gt;</strong>
STATUS: &lt;$MTEntryStatus$&gt;
</pre>
<p>Then I was able to export all my posts.</p>
<p>I had to post-process the output file, however, as I&#8217;ve been creating my posts using the MT Kwiki plugin. This meant that none of my links imported correctly. I spent much too long wrestling with vim&#8217;s non-greedy regular expressions before giving up and processing the data in Perl instead:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">perl <span style="color: #339933;">-</span>pe <span style="color: #ff0000;">'s/[(http:.*?) (.*?)]/$2/g'</span> mt<span style="color: #339933;">-</span><span style="color: #000066;">dump</span><span style="color: #339933;">.</span>txt <span style="color: #339933;">|</span>
perl <span style="color: #339933;">-</span>pe <span style="color: #ff0000;">'s/[(.*?) (http:.*?)]/$1/g'</span> <span style="color: #339933;">&gt;</span> deWikied<span style="color: #339933;">.</span>txt</pre></div></div>

<p>Then I had to persuade WordPress to maintain the MT ids. In the old WordPress import script it just inserted the posts by hand, and it was a simple matter of &#8216;fixing&#8217; the SQL it used to do this. But now the importer calls the same code that is used when you create a post through the normal interface.</p>
<p>So I needed to add a check for the ID into the import/mt.php script:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'AUTHOR'</span> <span style="color: #339933;">:</span>
    <span style="color: #000088;">$post_author</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'ID'</span> <span style="color: #339933;">:</span>
    <span style="color: #000088;">$post_ID</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span></pre></div></div>

<p>And then fix the call that inserts the data:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$postdata</span> <span style="color: #339933;">=</span> <span style="color: #990000;">compact</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post_ID'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'post_author'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'post_date'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'post_date_gmt'</span><span style="color: #339933;">,</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then I needed to adjust the wp_insert_post() call to cope with an incoming post_ID:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post_ID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$post_ID</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post_password</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$post_password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span></pre></div></div>

<p>and adjust its SQL accordingly</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">&quot;INSERT IGNORE INTO <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span> (id, post_author, post_date, ... ) VALUES
  (<span style="color: #006699; font-weight: bold;">$post_ID</span>, '<span style="color: #006699; font-weight: bold;">$post_author</span>', '<span style="color: #006699; font-weight: bold;">$post_date</span>', ...)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>(The arguments are passed as an extract() of a get_object_vars(), so there&#8217;s no need to change any of the other handling).</p>
<p>I <em>believe</em> that this is a safe enough approach that won&#8217;t interfere with creating new posts or editing old ones, but you can always revert this file back after importing if there are any problems.</p>
<p>With this in place, I was able to import all my old posts. (There were a lot of them, so I actually had to split the file and import 4 segments in turn). The other thing that the docs don&#8217;t make clear is that you need to have an upload directory which is writable by your webserver, but that was easy enough to work out from the error message.</p>
<p>They all came in with the same IDs as they used to have, so then it was just a matter of setting up some Apache redirects on the old server:</p>
<pre>
Redirect permanent /nothing/index.rdf

http://nothing.tmtm.com/feed/

RedirectMatch permanent /nothing/archives/([0-9]{6}).html

http://nothing.tmtm.com/archives/$1

RedirectMatch permanent /nothing/archives/([0-9]{4})_([0-9]{2}).html

http://nothing.tmtm.com/archives/date/$1/$2

RedirectMatch permanent /nothing/archives/([0-9]{4})_([0-9]{2})_([0-9]{2}).html

http://nothing.tmtm.com/archives/date/$1/$2/$3

RedirectMatch permanent /nothing/archives/([0-9]{4})_([0-9]{2})_([0-9]{2}).html

http://nothing.tmtm.com/archives/date/$1/$2/$3</pre>
<p>(I&#8217;ve already changed my permalink structure in WordPress to have this style of URL)</p>
<p>There will be many more things to change later to replicate the changes I&#8217;d made to my MT set-up, but this at least gets me up and running on WordPress.</p>
]]></content:encoded>
			<wfw:commentRss>http://nothing.tmtm.com/2006/02/migrating-movable-type-to-wordpress/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Joys of CSV</title>
		<link>http://nothing.tmtm.com/2004/11/the-joys-of-csv/</link>
		<comments>http://nothing.tmtm.com/2004/11/the-joys-of-csv/#comments</comments>
		<pubDate>Fri, 12 Nov 2004 09:42:12 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[whipuptitude]]></category>

		<guid isPermaLink="false">http://nothing.tmtm.com/?p=1326</guid>
		<description><![CDATA[I&#8217;ve been working with CSV files a lot recently, mostly as a way of building web based management information tools out of SAGE data. But I&#8217;ve always really hated working with the interface to Text::CSV_XS. So I put together Text::CSV::Simple. You just point it at the file you want, and read out all the rows: [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working with CSV files a lot recently, mostly as a way of building web based management information tools out of <a href="http://www.sage.co.uk/">SAGE</a> data.</p>
<p>But I&#8217;ve always really hated working with the interface to <a href="http://search.cpan.org/~jwied/Text-CSV_XS">Text::CSV_XS</a>. So I put together <a href="http://search.cpan.org/~tmtm/Text-CSV-Simple">Text::CSV::Simple</a>. You just point it at the file you want, and read out all the rows:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$parser</span> <span style="color: #339933;">=</span> Text<span style="color: #339933;">::</span><span style="color: #006600;">CSV</span><span style="color: #339933;">::</span><span style="color: #006600;">Simple</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$parser</span><span style="color: #339933;">-</span><span style="color: #0000ff;">&amp;gt</span><span style="color: #339933;">;</span>read_file<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$datafile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can tell it you only want certain fields:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">$parser</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">want_fields</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And that you want the results straight into a hashref rather than just a listref:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">$parser</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">field_map</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">qw/id name null town/</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There are also trigger points where you can pre- and post-process the data.</p>
<p>It&#8217;s certainly made dealing with CSV much easier for me. And it seems to be useful for other people too, as within a few weeks of its release I&#8217;ve had several feature requests and bug reports. Usually it takes a couple of months for a new module of mine to build up enough steam to get that.</p>
<p>However, I&#8217;ve now had several people all report a problem that I didn&#8217;t even consider before: it doesn&#8217;t handle newlines in strings. This disturbed me as I hadn&#8217;t realised until this that CSV files could actually contain embedded newlines! Of course, I can&#8217;t find any sensible documentation anywhere of what the CSV file format actually does and doesn&#8217;t allow, as it seems that Microsoft just made it a defacto standard by making it the main export format from Excel, without ever really specifying how it can be used. The few sites that I found that claim to provide more details on the format are contradictory (e.g. over the issue of header rows).</p>
<p>But it certainly does seem that linebreaks are acceptable, as long as they&#8217;re properly quoted. This shoots my whole approach to parsing the files apart, and means I&#8217;m going to have to go back and pretty much rewrite the module from scratch, and I may even have to lose one of my trigger points, as I still want to use Text::CSV_XS to do the actual parsing for me, but I&#8217;ll need to hook in at a different level now.</p>
<p>Of course I face my normal Open Source dilemma with this. The code clearly has a bug, but it&#8217;s not one that has any effect on me. None of the CSV files I have to deal with have linebreaks inside records. If the code wasn&#8217;t released, I&#8217;d apply my XP YAGNI principles, and defer the fix until I needed it. In some ways I&#8217;d like to be able to tell people who reported the bug that I&#8217;ll happily accept a patch if they can fix it, but otherwise they&#8217;ll have to wait until I need it. But having public code out there with known bugs irks me, so I guess I&#8217;ll just have to find the time from somewhere to fix it myself!</p>
]]></content:encoded>
			<wfw:commentRss>http://nothing.tmtm.com/2004/11/the-joys-of-csv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MT Amazon Reading List</title>
		<link>http://nothing.tmtm.com/2004/11/mt-amazon-reading-list/</link>
		<comments>http://nothing.tmtm.com/2004/11/mt-amazon-reading-list/#comments</comments>
		<pubDate>Thu, 11 Nov 2004 07:41:33 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Movable Type]]></category>
		<category><![CDATA[reading]]></category>
		<category><![CDATA[whipuptitude]]></category>

		<guid isPermaLink="false">http://nothing.tmtm.com/?p=1323</guid>
		<description><![CDATA[I&#8217;ve been asked which plugin I&#8217;m using to generate my &#8220;reading list&#8221; over on the sidebar. Like any true geek, of course, I actually wrote my own. Of course I&#8217;m generally into reuse where possible, but I wanted to learn how to write MT plugins, and it seemed like a good place to start. It [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been asked which plugin I&#8217;m using to generate my &#8220;reading list&#8221; over on the sidebar. Like any true geek, of course, I actually wrote my own. Of course I&#8217;m generally into reuse where possible, but I wanted to learn how to write MT plugins, and it seemed like a good place to start. It also helped that I didn&#8217;t like any of the 3rd party plugins out there for this. There are probably much better ones available now. Mine also isn&#8217;t very good, but it&#8217;s part of a bigger plan&#8230;</p>
<p>Over the last year or so I&#8217;ve gradually been drinking the semantic web kool-aid. I&#8217;m sure I&#8217;ll rant more about this later, but I don&#8217;t believe it&#8217;s going to happen the way most people have been pushing for it, but I&#8217;m now convinced that it&#8217;s going to happen.</p>
<p>Of course I&#8217;m part of the problem for making it happen, as I&#8217;m a data geek. I collect structured information. My friends laugh at the fact that I could run queries to tell you how much I&#8217;ve spent on milk in the last year, but I find the information useful. (Well maybe not <em>that</em> information, exactly, but the general principle of being able to analyze my spending&#8230;)</p>
<p>Unfortunately most of the people wanting to make the semantic web happen are also data geeks who believe in structured information, even though the vast majority of the world aren&#8217;t. This is a very big problem for traditional semweb thinking, but I no longer think it matters very much.</p>
<p>But, in the meantime, I want to do stuff with my structured information. Such as the list of books I&#8217;ve read.</p>
<p>The first problem was how to store them. I&#8217;m reasonably well known to be a <a href="//search.cpan.org/~tmtm/Class-DBI/">database guy</a>.  I also have a simple framework for building simple web apps to manage databases, so I considered building one for managing my books. But that seemed like too much hassle for now &#8211; I really wanted to just edit a file when I started reading a new book.</p>
<p>Faced with this problem, most techies these days seem to instinctively reach for XML. Personally I can&#8217;t stand it. I really hate how verbose it is. Unfortunately a large part of the Semantic Web work is also based around XML. Theoretically you can express your RDF in other ways, but really almost everyone is using XML. This used to bother me as I thought I&#8217;d need to do this, but now I believe that the more obscure and arcane we can make this stuff the better, as then everyone will want tools to do it, and only masochists will end up doing it by hand.</p>
<p>So for my books I, instead, reached instinctively for <a href="//www.yaml.org/">YAML</a>. I thought for a while about what information I&#8217;d want to store, before realising that I was much too lazy to want to type any information that could be found elsewhere. So my YAML file really just includes the ISBN of the book, and the rough date that I read it. Of course I don&#8217;t usually read a book in one day &#8211; I quite often read 4 or 5 books simultaneously over a period, just to get an interplay of ideas happening. And there are lots of books I start, read about half of, and don&#8217;t get round to finishing for months, or sometimes even years (if at all). I spent a while trying to find a sensible way to model that, before deciding it was all much to complex, and I&#8217;d be happy enough with just entering a rough date.</p>
<p>So I ended up with a very basic YAML file:</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">---
books:
&nbsp;
   - isbn    : &quot;0596007515&quot;
     title   : &quot;Ggl Hacks&quot;
     date    : &quot;2004-11-01&quot;
     current : 1
&nbsp;
   - isbn    : &quot;0439977789&quot;
     title   : &quot;Ruby / Smoke&quot;
     date    : &quot;2004-11-01&quot;
&nbsp;
   - isbn    : &quot;075093204X&quot;
     title   : &quot;Decline and Fall Everybody&quot;
     date    : &quot;2004-10-09&quot;</pre></div></div>

<p>The &#8216;title&#8217; field is there just as a placeholder to aid human readability. It never actually gets used anywhere, so I can fill it with shorthand etc. The &#8216;current&#8217; field is for books I&#8217;m still reading. This is my token concession to the &#8220;I started this a month ago but haven&#8217;t finished yet&#8221; problem.</p>
<p>The next phase is to turn that into a more detailed YAML file that includes proper titles, Amazon links, cover URLs etc.</p>
<p>I have a small perl script to do that:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> YAML<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Net<span style="color: #339933;">::</span><span style="color: #006600;">Amazon</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Cache<span style="color: #339933;">::</span><span style="color: #006600;">File</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$yaml</span> <span style="color: #339933;">=</span> YAML<span style="color: #339933;">::</span><span style="color: #006600;">LoadFile</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">shift</span> <span style="color: #339933;">||</span> <span style="color: #ff0000;">&quot;reading-yaml.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@out</span> <span style="color: #339933;">=</span> <span style="color: #000066;">map</span> expanded_data<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$yaml</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>books<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> Dump <span style="color: #009900;">&#123;</span> books <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\@out</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> expanded_data <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$book</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$property</span> <span style="color: #339933;">=</span> get_book<span style="color: #009900;">&#40;</span><span style="color: #000066;">sprintf</span> <span style="color: #ff0000;">&quot;%010s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$book</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>isbn<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">return</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">%$book</span><span style="color: #339933;">,</span>
    isbn  <span style="color: #339933;">=&gt;</span> <span style="color: #000066;">sprintf</span><span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;%010s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$book</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>isbn<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    title <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$property</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">title</span><span style="color: #339933;">,</span>
    img   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$property</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">ImageUrlSmall</span><span style="color: #339933;">,</span>
    url   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$property</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">url</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">BEGIN</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%amzn_opt</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>
      token        <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">&quot;MY_AMAZON_KEY&quot;</span><span style="color: #339933;">,</span>
      affiliate_id <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">&quot;tmtm-20&quot;</span><span style="color: #339933;">,</span>
      cache        <span style="color: #339933;">=&gt;</span> Cache<span style="color: #339933;">::</span><span style="color: #006600;">File</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span>
        cache_root      <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'/tmp/amzn_cache'</span><span style="color: #339933;">,</span>
        cache_umask     <span style="color: #339933;">=&gt;</span> 000<span style="color: #339933;">,</span>
        default_expires <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'30 day'</span><span style="color: #339933;">,</span>
      <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$us</span> <span style="color: #339933;">=</span> Net<span style="color: #339933;">::</span><span style="color: #006600;">Amazon</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%amzn_opt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$uk</span> <span style="color: #339933;">=</span> Net<span style="color: #339933;">::</span><span style="color: #006600;">Amazon</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%amzn_opt</span><span style="color: #339933;">,</span> locale <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">&quot;uk&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">sub</span> get_book <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$isbn</span> <span style="color: #339933;">=</span> <span style="color: #000066;">sprintf</span> <span style="color: #ff0000;">&quot;%010s&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$resp</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$uk</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">search</span><span style="color: #009900;">&#40;</span> asin <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$isbn</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$resp</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$us</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">search</span><span style="color: #009900;">&#40;</span> asin <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$isbn</span> <span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">unless</span> <span style="color: #0000ff;">$resp</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">is_success</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Can't find $isbn&quot;</span> <span style="color: #b1b100;">unless</span> <span style="color: #0000ff;">$resp</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">is_success</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$property</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$resp</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">properties</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">return</span> <span style="color: #0000ff;">$property</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It simply reads in my raw book file, uses Amazon Web Services to look up more data about the books, (storing the data in cache for 30 days to speed the whole thing up on later runs), and throws out a new YAML file with more fields. Amazon US has slightly more likelihood of having cover scans, so I check it first falling back on the UK if there&#8217;s no results there. I pick up a lot of my books in the US anyway, so it isn&#8217;t that much of an issue, although I occasionally a different cover from the one that I have.</p>
<p>Then I have a simple MT plugin, called mt-reading.pl which I drop straight into my MT/cgi-bin/plugins/ directory:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">package</span> MT<span style="color: #339933;">::</span><span style="color: #006600;">Plugin</span><span style="color: #339933;">::</span><span style="color: #006600;">ReadingList</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> lib <span style="color: #ff0000;">'/usr/local/MT/cgi-bin/lib'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> MT<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">::</span><span style="color: #006600;">Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Data<span style="color: #339933;">::</span><span style="color: #006600;">BookList</span><span style="color: #339933;">;</span>
&nbsp;
MT<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">::</span><span style="color: #006600;">Context</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">add_container_tag</span><span style="color: #009900;">&#40;</span>
  ReadingList <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#40;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ctx</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$args</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$builder</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$ctx</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">stash</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'builder'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$tokens</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$ctx</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">stash</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'tokens'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$yaml_src</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$args</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>src<span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">or</span> <span style="color: #000066;">return</span> <span style="color: #0000ff;">$ctx</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">error</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;No YAML source file specified.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$list</span> <span style="color: #339933;">=</span> Data<span style="color: #339933;">::</span><span style="color: #006600;">BookList</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$yaml_src</span><span style="color: #009900;">&#41;</span>
      <span style="color: #b1b100;">or</span> <span style="color: #000066;">return</span> <span style="color: #0000ff;">$ctx</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">error</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Invalid YAML source file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$content</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$book</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$list</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">reading_list</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #0000ff;">$ctx</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">stash</span><span style="color: #009900;">&#40;</span> book <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$book</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #0000ff;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">$builder</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">build</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$ctx</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$tokens</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066;">return</span> <span style="color: #0000ff;">$content</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
MT<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">::</span><span style="color: #006600;">Context</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">add_tag</span><span style="color: #009900;">&#40;</span>
  ReadingListBook <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$book</span> <span style="color: #339933;">=</span> shift<span style="color: #339933;">-&gt;</span><span style="color: #006600;">stash</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'book'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$args</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$book</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>cover<span style="color: #009900;">&#125;</span> <span style="color: #339933;">||=</span> <span style="color: #000066;">sprintf</span> <span style="color: #000066;">qq</span><span style="color: #009900;">&#123;</span><span style="color: #339933;">&lt;</span>a xhref<span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;%s&quot;</span> mce_href<span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;%s&quot;</span> <span style="color: #339933;">&gt;&lt;</span>img
      border<span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;0&quot;</span> alt<span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;%s&quot;</span> xsrc<span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;%s&quot;</span> mce_src<span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;%s&quot;</span> <span style="color: #339933;">/&gt;&lt;/</span>a<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">$book</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>url<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$book</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>title<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$book</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>img<span style="color: #009900;">&#125;</span> <span style="color: #339933;">||</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">return</span> <span style="color: #000066;">exists</span> <span style="color: #0000ff;">$args</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>display<span style="color: #009900;">&#125;</span>
      <span style="color: #339933;">?</span> <span style="color: #0000ff;">$book</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$args</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>display<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span>
      <span style="color: #339933;">:</span> <span style="color: #0000ff;">$book</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>cover<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></div></div>

<p>This simply adds two new tags &#8216;ReadingList&#8217; and &#8216;ReadingListBook&#8217; that I can add to my MT templates, and have them expanded at build time.</p>
<p>So, in my template I include something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;p&gt;Recent Reading&lt;/p&gt;
&lt;div class=&quot;book&quot;&gt;
  &lt;MTReadingList src=&quot;/path/to/reading.yaml&quot; lastn=&quot;9&quot;&gt;
    &lt;$MTReadingListBook display=&quot;cover&quot; $&gt;
  &lt;/MTReadingList&gt;
&lt;/div&gt;</pre></div></div>

<p>The only remaining piece is the Data::BookList module, which is a simple &#8216;load the data from YAML, and return whichever ones I want&#8217;:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">package</span> Data<span style="color: #339933;">::</span><span style="color: #006600;">BookList</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> YAML<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$class</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$src</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$books</span> <span style="color: #339933;">=</span> YAML<span style="color: #339933;">::</span><span style="color: #006600;">LoadFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$src</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">return</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">bless</span> <span style="color: #009900;">&#123;</span> _booklist <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$books</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>books<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$class</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> reading_list <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@books</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span> shift<span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>_booklist<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">exists</span> <span style="color: #0000ff;">$args</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>current<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">@books</span> <span style="color: #339933;">=</span> <span style="color: #000066;">grep</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>current<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">@books</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">exists</span> <span style="color: #0000ff;">$args</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>lastn<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">@books</span> <span style="color: #339933;">=</span>
      <span style="color: #009900;">&#40;</span><span style="color: #000066;">sort</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>date<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">cmp</span> <span style="color: #0000ff;">$a</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>date<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span> <span style="color: #0000ff;">@books</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$args</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>lastn<span style="color: #009900;">&#125;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066;">return</span> <span style="color: #0000ff;">@books</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></div></div>

<p>This allows me to ask for only &#8216;current&#8217; books and/or the &#8216;lastn&#8217; books: currently 9 for my blog. I plan to add more features here later, but for now this does what I need.</p>
<p>In some ways this is all over-complicated if all I wanted was a &#8216;recent reading&#8217; section on my blog. But I find the separation of concerns useful. Managing my raw data is distinct from fetching information about it, which is distinct from slicing that data up, which is distinct from presenting it on my blog. So, when I find an ontology for expressing all this in RDF I should really only to write a new presentation script.</p>
<p>Of course, in practice, the ontology will specify some fields that I don&#8217;t currently store, so I&#8217;ll probably need to also expand the amazon lookup code, and it&#8217;ll probably want me to do my dates differently, etc., but that&#8217;s the theory anyway!</p>
]]></content:encoded>
			<wfw:commentRss>http://nothing.tmtm.com/2004/11/mt-amazon-reading-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>::Simple</title>
		<link>http://nothing.tmtm.com/2004/05/simple/</link>
		<comments>http://nothing.tmtm.com/2004/05/simple/#comments</comments>
		<pubDate>Sat, 01 May 2004 11:52:05 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://nothing.tmtm.com/?p=1270</guid>
		<description><![CDATA[As of this morning there are just under 250 modules on CPAN matching &#8216;::Simple&#8217;. I take a certain amount of blame for this. I&#8217;ve released a couple myself, and my kitchen has a credit as the birthplace of Test::Simple. But I like to think that in those cases the module really does deserve the &#8216;simple&#8217; [...]]]></description>
			<content:encoded><![CDATA[<p>As of this morning there are just under 250 modules on <a href="http://search.cpan.org/">CPAN</a> matching &#8216;::Simple&#8217;. I take a certain amount of blame for this. I&#8217;ve released a <a href="http://search.cpan.org/search?query=%3A%3ASimple+TMTM&amp;mode=all">couple</a> myself, and my kitchen has a credit as the <a href="http://search.cpan.org/~mschwern/Test-Simple/lib/Test/Simple.pm#HISTORY">birthplace</a> of Test::Simple.</p>
<p>But I like to think that in those cases the module really does deserve the &#8216;simple&#8217; moniker. Perl&#8217;s spreadsheet modules are notoriously complex, and there&#8217;s no need to jump through all the hoops of two-dimensional cell access and data vs. formatting if all you want to do is read or write each row as an array. Similarly, Test::Simple has one trivial test function that is all a beginning test-writer needs to get into the way of writing tests, and there&#8217;s a clear migration path up to Test::More.</p>
<p>But somewhere along the line ::Simple seems to have mutated into ::I::Don&#8217;t::Like::The::Normal::Syntax. Take <a href="http://search.cpan.org/~linnin/DBD-mysql-SimpleMySQL/SimpleMySQL.pm">DBD::mysql::SimpleMySQL</a>. Ignoring the fact that it&#8217;s in completely the wrong namespace as it&#8217;s not a driver, I&#8217;m at a complete loss to see how it&#8217;s &#8220;simpler&#8221; than, well, anything really.</p>
<p>Now, I&#8217;m obviously <a href="http://search.cpan.org/~tmtm/Class-DBI/">biased</a>, but even the example in the docs makes my brain hurt:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$select</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'Passwd.*'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'UsrGrp.UsrGrpName'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$from</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'Passwd'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$joins</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">push</span> <span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$joins</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> join_struct<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;PasswdHostGrp&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Passwd.PasswdID&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;PasswdHostGrp.PasswdID&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">push</span> <span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$joins</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> join_struct<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;UsrGrp&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Passwd.PrimaryGroupID&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;UsrGrp.UsrGrpID&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$wheres</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;PasswdHostGrp.HostGrpID IN ('group1', 'group2')&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$arrayref</span> <span style="color: #339933;">=</span> dbselect_array<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dbh</span><span style="color: #339933;">,</span> build_select<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$select</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$from</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$joins</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$wheres</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>I can understand trying to make it slightly easier to build SQL dynamically, or trying of provide a way to abstract and package up SQL patterns, but I really don&#8217;t understand programmers&#8217; fascination with trying to completely reinvent SQL. The basics are really quite straightforward, especially at the level that these modules are able to &#8216;hide&#8217; from you. And it&#8217;s going to be much simpler and more powerful when you eventually need to do something considerably more complex. There&#8217;s really no substitute for learning how to use SQL if you&#8217;re going to work with databases.</p>
<p>In fact I&#8217;d say that trying to reimplement SQL is a move in exactly the wrong direction. Especially in a dynamic and flexible language like Perl, it&#8217;s good practice to invent little mini-languages for the domain in which you&#8217;re working. But, of course when working with databases there&#8217;s no need to invent the language &#8211; it already exists!</p>
<p>Somehow I doubt this will stop the proliferation of modules trying to do away with SQL. Soon we&#8217;ll probably have as many database abstraction libraries as we do templating systems&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://nothing.tmtm.com/2004/05/simple/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Object Oriented JavaScript</title>
		<link>http://nothing.tmtm.com/2003/01/object-oriented-javascript/</link>
		<comments>http://nothing.tmtm.com/2003/01/object-oriented-javascript/#comments</comments>
		<pubDate>Wed, 08 Jan 2003 10:50:48 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://nothing.tmtm.com/?p=624</guid>
		<description><![CDATA[I can see lots of similarities between JavaScript and Perl. Both are languages that are often written by people with no real programming experience, just to get a job done &#8211; usually involving web sites. A lot of the code in each isn&#8217;t written from scratch, but starts by taking some other code that almost [...]]]></description>
			<content:encoded><![CDATA[<p>I can see lots of similarities between JavaScript and Perl. Both are languages that are often written by people with no real programming experience, just to get a job done &#8211; usually involving web sites. A lot of the code in each isn&#8217;t written from scratch, but starts by taking some other code that almost does what you want, and hacking it around until it does do what you want. And, as a result, a lot of the code that exists in both languages is really ugly, clumsy, and contains lots of special case code and lots of subtle bugs. Which the next person to adapt the script hacks around until it does what they want. And so on.</p>
<p>But beneath it all, both are actually very powerful languages, which can be well written, clean, expressive, and well factored.</p>
<p>And whilst I&#8217;m perfectly at home writing Perl like that, my JavaScript skills are still rather lacking.</p>
<p>So, I was playing around with Object Oriented JavaScript over the holidays. I found a good example at <a href="http://www.chunkysoup.net/advanced/oojavascript1/">ChunkySoup</a>, but I still wasn&#8217;t entirely happy with the results.</p>
<p>The code for the <a href="http://www.chunkysoup.net/advanced/oojavascript1/finishedgallery.html">test page</a> is still a little uglier, and more repetitive than I&#8217;d like. In particular, each link on the page still needs to explicitly set up its<br />
own handling code for the image rollovers etc.:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCB1428.jpg&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;elements[0].handleMouseOver()&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;elements[0].handleMouseOut()&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;return</span>
<span style="color: #009900;">elements[0].handleClick()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN4337.jpg&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;elements[1].handleMouseOver()&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;elements[1].handleMouseOut()&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;return</span>
<span style="color: #009900;">elements[1].handleClick()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link3&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN4358.jpg&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;elements[2].handleMouseOver()&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;elements[2].handleMouseOut()&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;return</span>
<span style="color: #009900;">elements[2].handleClick()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link4&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN4373.jpg&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;elements[3].handleMouseOver()&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;elements[3].handleMouseOut()&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;return</span>
<span style="color: #009900;">elements[3].handleClick()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link5&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN1509.jpg&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;elements[4].handleMouseOver()&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;elements[4].handleMouseOut()&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;return</span>
<span style="color: #009900;">elements[4].handleClick()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>And setting up the JavaScript that gets called onLoad is a little repititive too:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> elements <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> thumbnailID <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;thumbnail&quot;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// this is universal for the page</span>
<span style="color: #003366; font-weight: bold;">var</span> emptyimg <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;blank.gif&quot;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// this is universal for the page</span>
<span style="color: #003366; font-weight: bold;">var</span> photoID <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;bigimage&quot;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// this is universal for the page</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> initpage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  elements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> csnPhotoNavObject<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span>
csnPhotoObject<span style="color: #009900;">&#40;</span>thumbnailID<span style="color: #339933;">,</span>emptyimg<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN1428tn.jpg&quot;</span><span style="color: #339933;">,</span>photoID<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN1428.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  elements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> csnPhotoNavObject<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span>
csnPhotoObject<span style="color: #009900;">&#40;</span>thumbnailID<span style="color: #339933;">,</span>emptyimg<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN4337tn.jpg&quot;</span><span style="color: #339933;">,</span>photoID<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN4337.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  elements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> csnPhotoNavObject<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span>
csnPhotoObject<span style="color: #009900;">&#40;</span>thumbnailID<span style="color: #339933;">,</span>emptyimg<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN4358tn.jpg&quot;</span><span style="color: #339933;">,</span>photoID<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN4358.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  elements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> csnPhotoNavObject<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span>
csnPhotoObject<span style="color: #009900;">&#40;</span>thumbnailID<span style="color: #339933;">,</span>emptyimg<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN4373tn.jpg&quot;</span><span style="color: #339933;">,</span>photoID<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN4373.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  elements<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> csnPhotoNavObject<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span>
csnPhotoObject<span style="color: #009900;">&#40;</span>thumbnailID<span style="color: #339933;">,</span>emptyimg<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN1509tn.jpg&quot;</span><span style="color: #339933;">,</span>photoID<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;DSCN1509.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So, I figured it should be possible to abstract some of that away further too. The JavaScript should be able to dynamically alter the DOM and set up all the event handles. Then the links could just be set up as:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link1&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN1428.jpg&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link2&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN4337.jpg&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link3&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN4358.jpg&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link4&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN4373.jpg&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;link5&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;DSCN1509.jpg&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>And at the start of the page, I&#8217;d just want to associate images with each link:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> initpage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  addImage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;link1&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN1428.jpg&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN1428tn.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  addImage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;link2&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN4337.jpg&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN4337tn.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  addImage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;link3&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN4358.jpg&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN4358tn.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  addImage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;link4&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN4373.jpg&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN4373tn.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  addImage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;link5&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN1509.jpg&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DSCN1509tn.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The addImage JavaScript would then find the image element with the id of the first parameter, associate an onclick() with the second element, and a rollover() with the third.</p>
<p>After a lot of playing around I ended up with a nice abstract JavaScript addImage function that does just this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> addImage<span style="color: #009900;">&#40;</span>id<span style="color: #339933;">,</span> img<span style="color: #339933;">,</span> thumb<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> pno <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> csnPhotoNavObject<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> csnPhotoObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;thumbnail&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;blank.gif&quot;</span><span style="color: #339933;">,</span>thumb<span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;bigimage&quot;</span><span style="color: #339933;">,</span>img<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> img <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  img.<span style="color: #660066;">onmouseover</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> pno.<span style="color: #660066;">handleMouseOver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  img.<span style="color: #660066;">onmouseout</span>  <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> pno.<span style="color: #660066;">handleMouseOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  img.<span style="color: #660066;">onclick</span>     <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> pno.<span style="color: #660066;">handleClick</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>What I don&#8217;t like about this though, is the need to set up the anonymous closures. (Of course, before this I didn&#8217;t even know I could actually <em>do</em> that in JavaScript!). I can&#8217;t see why I can&#8217;t just say:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">  img.<span style="color: #660066;">onmouseover</span> <span style="color: #339933;">=</span> pno.<span style="color: #660066;">handleMouseOver</span><span style="color: #339933;">;</span>
  img.<span style="color: #660066;">onmouseout</span>  <span style="color: #339933;">=</span> pno.<span style="color: #660066;">handleMouseOut</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The onmouseover and onmouseout need to be assigned a function. But if I give them the foreign object function directly, then something later gets confused. Whereas if I give it an anonymous function that just calls that other function, everything works just fine.</p>
<p>I don&#8217;t know if I&#8217;m doing something wrong. Or if there&#8217;s some strange JavaScript language issues I don&#8217;t know about yet. Or what.</p>
<p>Anyone any suggestions?</p>
]]></content:encoded>
			<wfw:commentRss>http://nothing.tmtm.com/2003/01/object-oriented-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Camel Poop</title>
		<link>http://nothing.tmtm.com/2003/01/camel-poop/</link>
		<comments>http://nothing.tmtm.com/2003/01/camel-poop/#comments</comments>
		<pubDate>Sun, 05 Jan 2003 17:32:18 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://nothing.tmtm.com/?p=618</guid>
		<description><![CDATA[In response to the Camel POOP article on Evolt, Simon Willison complains: I have no intention of starting a language war, but my God this is ugly. Still, I guess it must work for some people. As a user and fan of Perl, I have to agree. This is exceptionally ugly. In fact it&#8217;s the [...]]]></description>
			<content:encoded><![CDATA[<p>In response to the <a href="http://www.evolt.org/article/Camel_POOP_Object_Oriented_Programming_in_Perl/17/50922/index.html">Camel POOP</a> article on Evolt, Simon Willison <a href="http://simon.incutio.com/archive/2003/01/04/#crufty">complains</a>: <q>I have no<br />
intention of starting a language war, but my God this is ugly. Still, I guess it must work for some people.</q></p>
<p>As a user and fan of Perl, I have to agree. This is exceptionally ugly. In fact it&#8217;s the sort of thing that turns people against Perl. Thankfully OO in Perl doesn&#8217;t have to be like that.</p>
<p>Let&#8217;s take his Person class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#class Person</span>
<span style="color: #000066;">package</span> Person<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Address<span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">#Person class will contain an Address</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#constructor</span>
<span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$class</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    _firstName <span style="color: #339933;">=&gt;</span> <span style="color: #000066;">undef</span><span style="color: #339933;">,</span>
    _lastName  <span style="color: #339933;">=&gt;</span> <span style="color: #000066;">undef</span><span style="color: #339933;">,</span>
    _ssn       <span style="color: #339933;">=&gt;</span> <span style="color: #000066;">undef</span><span style="color: #339933;">,</span>
    _address   <span style="color: #339933;">=&gt;</span> <span style="color: #000066;">undef</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">bless</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$class</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">return</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#accessor method for Person first name</span>
<span style="color: #000000; font-weight: bold;">sub</span> firstName <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$firstName</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>_firstName<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$firstName</span> <span style="color: #b1b100;">if</span> <span style="color: #000066;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$firstName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">return</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>_firstName<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#accessor method for Person last name</span>
<span style="color: #000000; font-weight: bold;">sub</span> lastName <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$lastName</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>_lastName<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$lastName</span> <span style="color: #b1b100;">if</span> <span style="color: #000066;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$lastName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">return</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>_lastName<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#accessor method for Person address</span>
<span style="color: #000000; font-weight: bold;">sub</span> address <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$address</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>_address<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$address</span> <span style="color: #b1b100;">if</span> <span style="color: #000066;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$address</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">return</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>_address<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#accessor method for Person social security number</span>
<span style="color: #000000; font-weight: bold;">sub</span> ssn <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$ssn</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>_ssn<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$ssn</span> <span style="color: #b1b100;">if</span> <span style="color: #000066;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$ssn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">return</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>_ssn<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000066;">print</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">#print Person info</span>
  <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Name:%s %snn&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">firstName</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">lastName</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>There are numerous problems with this. Let&#8217;s start from the top.</p>
<ol>
<li>The &#8216;use Address&#8217; is completely needless. Misleading comment notwithstanding, nothing in the package actually uses the Address module, so there&#8217;s no need to load it.</li>
<li>The constructor is complete overkill. The object is going to be a hash, but in Perl hash keys autovivify the first time you assign to them, so there&#8217;s absolutely no need to set up lots of keys that contain undef. As the constructor does nothing, it could simply be <code lang="perl">sub new { bless {}, shift }</code>.</li>
<li>The data methods all do exactly the same thing. This breaks one of the cardinal rules of programming &#8211; Don&#8217;t Repeat Yourself. firstName, lastName, address, and ssn are all trivial accessor/mutator methods. They could all be abstracted away in a variety of methods, but as this is Perl, someone else has already done that for us. Class::Accessor lets us set up all these methods simply by doing:<br />
<code lang="perl">Person-&gt;mk_accessors(qw/firstName lastName address ssn/);</code></li>
<li>The constructor doesn&#8217;t all you to set object data. It&#8217;s pretty much a matter of style, but in general it&#8217;s nice to be able to instantiate your object with data, rather than having to call all the mutators in turn. As this is a simple hash-based object (as with 90% of all perl objects) Class::Accessor gives us a default new() as well that allows us to pass a hashref of the data members.</li>
</ol>
<p>So, this entire class could be replaced with:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">package</span> Person<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> base <span style="color: #ff0000;">'Class::Accessor'</span><span style="color: #339933;">;</span>
&nbsp;
Person<span style="color: #339933;">-&gt;</span><span style="color: #006600;">mk_accessors</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">qw/firstName lastName address ssn/</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000066;">print</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">printf</span> <span style="color: #ff0000;">&quot;Name:%s %snn&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">firstName</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">lastName</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Similarly, the Employee subclass, which simply adds &#8216;id&#8217; and &#8216;title&#8217; data members, and overrides print could become:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">package</span> Employee<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> base <span style="color: #ff0000;">'Person'</span><span style="color: #339933;">;</span>
&nbsp;
Employee<span style="color: #339933;">-&gt;</span><span style="color: #006600;">mk_accessors</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">qw/id title/</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000066;">print</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">SUPER</span><span style="color: #339933;">::</span><span style="color: #000066;">print</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Name:%s %snn&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">id</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">title</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>(The example on the site makes no sense as it keeps referring to an Address class that doesn&#8217;t seem to exist. I&#8217;ve assumed that the overridden print should be outputting the extra data members of this class instead&#8230;)</p>
<p>Then, instead of the longwinded test program (with a eval to catch exceptions that&#8217;s completely needless as neither class throws them):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">use</span> Employee<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#create Employee class instance</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$khurt</span> <span style="color: #339933;">=</span>  <span style="color: #000066;">eval</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">new</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>  <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$@</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#set object attributes</span>
<span style="color: #0000ff;">$khurt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">firstName</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Khurt'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$khurt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">lastName</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Williams'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$khurt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">id</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1001</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$khurt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">title</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Executive Director'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#diplay Employee info</span>
<span style="color: #0000ff;">$khurt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">print</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We can simply have:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">use</span> Employee<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$khurt</span> <span style="color: #339933;">=</span> Employee<span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  firstName <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Khurt'</span><span style="color: #339933;">,</span>
  lastName  <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Williams'</span><span style="color: #339933;">,</span>
  id        <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1001</span><span style="color: #339933;">,</span>
  title     <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Executive Director'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$khurt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">print</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>It may not be as nice as OO in some other languages, but it&#8217;s a lot nicer than the example in the article&#8230; and as it&#8217;s Perl, there&#8217;s plenty of other approaches if you want to dice it another way.</p>
]]></content:encoded>
			<wfw:commentRss>http://nothing.tmtm.com/2003/01/camel-poop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Importing Radio posts to MT</title>
		<link>http://nothing.tmtm.com/2003/01/importing-radio-posts-to-mt/</link>
		<comments>http://nothing.tmtm.com/2003/01/importing-radio-posts-to-mt/#comments</comments>
		<pubDate>Sun, 05 Jan 2003 15:58:33 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Movable Type]]></category>
		<category><![CDATA[Radio Userland]]></category>
		<category><![CDATA[whipuptitude]]></category>

		<guid isPermaLink="false">http://nothing.tmtm.com/?p=617</guid>
		<description><![CDATA[A couple of people have asked for more pointers on exactly how I got my Radio posts into MT. So here&#8217;s some more detailed information. Firstly, I ran into lots of problems with Radio seemingly caching macros. When I edit radio macro files and resave them, Radio doesn&#8217;t seem to notice the changes for about [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of people have asked for more pointers on exactly how I got my Radio posts into MT. So here&#8217;s some more detailed information.</p>
<p>Firstly, I ran into lots of problems with Radio seemingly caching macros. When I edit radio macro files and resave them, Radio doesn&#8217;t seem to notice the changes for about 5 minutes. Which is far ideal for the programming approach I use (particularly in a language I&#8217;m not that familiar with, such as UserTalk), which basically involves keeping the code running at all times (code a line, save it, test it still works). I would never survived as a programmer in the old days of coding &#8216;offline&#8217;.</p>
<p>I originally thought this was to do with my set up (I have my PC&#8217;s C: drive mounted via samba onto the linux box on which I do most of my programming, as I find it much easier to code in that environment), but using Notepad didn&#8217;t seem to make any difference, and there are a few scattered references to this problem littering the Userland noticeboards.</p>
<p>Anyway, I ended up having to code in the scratchpad instead. The script is below. It was cut-n-pasted from Radio&#8217;s outline editor so the formatting is a little strange, but it should do the trick. This can either be used as a macro, or, as I did, or by adding it to the scratchpad and calling  <tt>&lt;% workspace.showAll ()%&gt;</tt> in a page (I just created a new page in my Radio Userland/www/ directory that had that in it.</p>
<p>This outputs all my posts in XML, with the titles and bodies wrapped in CDATA tags. I then wrote a simple Perl script to turn this into the MT input format. You then import this into MT following the instructions in the manual.</p>
<p>I&#8217;m sure this could all be tidied up some more, but hopefully it&#8217;s of some use to someone as is!</p>
<p><span id="more-617"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code"><pre class="usertalk" style="font-family:monospace;">on showAll () {
 on showAll () {
  local (s = &quot;&quot;, i);;
&nbsp;
  on cData (text) {
    return &quot;&lt;![CDATA[&quot; + text + &quot;]]&gt;&quot;;};
&nbsp;
  on tag(tag, text) {
    return &quot;&lt;&quot; + tag + &quot;&gt;&quot; + text + &quot;&lt;/&quot; + tag + &quot;&gt;&quot;;};
&nbsp;
  on postTitle (post, title = &quot;&quot;) {
    if defined (post.title) {
      title = post.title};
    return tag(&quot;ptitle&quot;, title);};
&nbsp;
  on postLink (post, link = &quot;&quot;) {
    if defined (post.link) {
      link = cdata(post.link)};
    return tag(&quot;plink&quot;, link);};
&nbsp;
  on postCategories (post, cats = &quot;&quot;) {
    local (adrcats = post.categories);;
    for adr in @adrcats {
      local (name = nameOf(adr^));
      local (adrcat= @adrblog^.categories.[name]);;
      if (defined (adrcat) &amp;&amp; defined (adrcat^.htmlUrl)) {
        cats = cats + tag(&quot;category&quot;, name);}};
    return tag(&quot;categories&quot;, cats);};
&nbsp;
  on postText (post) {
    return tag(&quot;text&quot;, cData(string(post.text)))};
&nbsp;
  local (adrblog = radio.weblog.init ());;
  local (adrposts = @adrblog^.posts);;
  for i = sizeof (adrposts^) downto 1 {
    local (adrpost = @adrposts^ [i]);;
    local (url); radio.weblog.getUrlForPost (adrpost, @url);;
    local (thisdate = adrpost^.when);;
    s = s + &quot;&lt;post id=\&quot;&quot; + i + &quot;\&quot;&gt;&quot;;;
    s = s + tag(&quot;date&quot;, thisdate) + postTitle(adrpost^) +
      postLink(adrpost^) + postCategories(adrpost^) + postText(adrpost^);;
    s = s + &quot;&lt;/post&gt;&quot;;};
  s = tag(&quot;allposts&quot;, s);;
  return (s);
}</pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">=head1 NAME
&nbsp;
radio2mt - turn Radio UserLand posts into Moveable Type posts
&nbsp;
=head1 SYNOPSIS
&nbsp;
	radio2mt filename &gt; outputfile
&nbsp;
=head1 DESCRIPTION
&nbsp;
This script takes a Radio UserLand database that has been output to XML
(using the Radio macro available at http://www.tmtm.com/radioShowAll.txt)
and outputs a file suitable for importing into Moveable Type.
&nbsp;
=head1 AUTHOR
&nbsp;
Tony Bowden &lt;tony@tmtm.com&gt;
&nbsp;
=head1 LICENSE
&nbsp;
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
&nbsp;
=cut</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> File<span style="color: #339933;">::</span><span style="color: #006600;">Slurp</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> XML<span style="color: #339933;">::</span><span style="color: #006600;">LibXML</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Time<span style="color: #339933;">::</span><span style="color: #006600;">Piece</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Usage: $0 &lt;filename&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$parser</span> <span style="color: #339933;">=</span> XML<span style="color: #339933;">::</span><span style="color: #006600;">LibXML</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$text</span> <span style="color: #339933;">=</span> read_file<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #0000ff;">$text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$1</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$text</span> <span style="color: #339933;">=~</span> <span style="color: #000066;">m</span><span style="color: #666666; font-style: italic;">#(&lt;allposts&gt;.*&lt;/allposts&gt;)#gs;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$doc</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$parser</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">parse_string</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$post</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">reverse</span> <span style="color: #0000ff;">$doc</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">findnodes</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'//post'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$id</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">findvalue</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'@id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$title</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">findvalue</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;./ptitle&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$date</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">findvalue</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;./date&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$body</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">findvalue</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;./text&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$tp</span>    <span style="color: #339933;">=</span> Time<span style="color: #339933;">::</span><span style="color: #006600;">Piece</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">strptime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$date</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%d/%m/%Y; %H:%M:%S&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$link</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">findvalue</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;./plink&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@cats</span>  <span style="color: #339933;">=</span> <span style="color: #000066;">map</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">findvalue</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">findnodes</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;.//category&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$pr_ct</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span> <span style="color: #0000ff;">@cats</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">print</span>  <span style="color: #ff0000;">&quot;TITLE: $title<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$title</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">printf</span> <span style="color: #ff0000;">&quot;DATE: %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$tp</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%m/%d/%Y %H:%M:%S&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">print</span>  <span style="color: #ff0000;">&quot;PRIMARY CATEGORY: $pr_ct<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$pr_ct</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">print</span>  <span style="color: #ff0000;">&quot;CATEGORY: $_<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #b1b100;">foreach</span> <span style="color: #0000ff;">@cats</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">print</span>  <span style="color: #ff0000;">&quot;STATUS: publish<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">print</span>  <span style="color: #ff0000;">&quot;-----<span style="color: #000099; font-weight: bold;">\n</span>BODY:<span style="color: #000099; font-weight: bold;">\n</span>$body<span style="color: #000099; font-weight: bold;">\n</span>-----<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">print</span>  <span style="color: #ff0000;">&quot;--------<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://nothing.tmtm.com/2003/01/importing-radio-posts-to-mt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Great Language Shootout</title>
		<link>http://nothing.tmtm.com/2002/10/the-great-language-shootout/</link>
		<comments>http://nothing.tmtm.com/2002/10/the-great-language-shootout/#comments</comments>
		<pubDate>Wed, 02 Oct 2002 10:41:49 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://nothing.tmtm.com/?p=509</guid>
		<description><![CDATA[I was talking to Marty again recently about his anti-Java stance, and we were trying to think of ways in which different languages could be rated. This of course reminded me to go back and check out Doug Bagley&#8217;s Great Language Shootout, which compares multiple languages&#8217; speed and memory usage for doing the same task. [...]]]></description>
			<content:encoded><![CDATA[<p>I was talking to Marty again recently about his <a href="http://www.martian.org/marty/archives/000066.html#000066">anti-Java</a> stance, and we were trying to think of ways in which different languages could be rated.</p>
<p>This of course reminded me to go back and check out Doug Bagley&#8217;s <a href="http://www.bagley.org/~doug/shootout/">Great Language Shootout</a>, which compares multiple languages&#8217; speed and memory usage for doing the same task.</p>
<p>I&#8217;d previously helped optimise some of the Perl solutions, and last night I noticed that there were a few new tests from when I last looked at it. In partiuclar the results for the Matrix Multiplication test seemed slightly strange: Perl was much further down the list that I&#8217;d have expected.</p>
<p>The meat of the code seemed to be the function mmult:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">sub</span> mmult <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$rows</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$cols</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$m1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$m2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@m3</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #339933;">--</span><span style="color: #0000ff;">$rows</span><span style="color: #339933;">;</span> <span style="color: #339933;">--</span><span style="color: #0000ff;">$cols</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$rows</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@row</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$m1i</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$m1</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$j</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$cols</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$val</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$k</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$cols</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #0000ff;">$val</span> <span style="color: #339933;">+=</span> <span style="color: #0000ff;">$m1i</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$k</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> <span style="color: #0000ff;">$m2</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$k</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$j</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@row</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@m3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">\@row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066;">return</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">\@m3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I played around for a while, and got about a 50% speedup with quite a nasty nested map approach:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">sub</span> mmult2 <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$rows</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$cols</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$m1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$m2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
  <span style="color: #339933;">--</span><span style="color: #0000ff;">$rows</span><span style="color: #339933;">;</span> <span style="color: #339933;">--</span><span style="color: #0000ff;">$cols</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@m3</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$rows</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">push</span> <span style="color: #0000ff;">@m3</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span> <span style="color: #000066;">map</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$j</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">;</span>
      sum <span style="color: #000066;">map</span> <span style="color: #0000ff;">$m1</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> <span style="color: #0000ff;">$m2</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$j</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">..</span><span style="color: #0000ff;">$cols</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$cols</span> <span style="color: #009900;">&#93;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066;">return</span> <span style="color: #0000ff;">\@m3</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I tried various approaches to turn the outer for() into a map as well, but my brain started hurting too much as it all got very messy.</p>
<p>And then I noticed that we were pushing to @m3 each time around a loop that counted from 0, and realised it would probably be much more efficient to just assign directly each time. So I replaced the <tt>push @m3, ...</tt> with <tt>$m3[$i] = ... </tt>, and performance shot up.</p>
<p>So I rolled back all the other changes I&#8217;d made, and just applied this straight through:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">sub</span> mmult3 <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$rows</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$cols</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$m1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$m2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$m3</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #339933;">--</span><span style="color: #0000ff;">$rows</span><span style="color: #339933;">;</span> <span style="color: #339933;">--</span><span style="color: #0000ff;">$cols</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$rows</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$j</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$cols</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #0000ff;">$m3</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$j</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+=</span> <span style="color: #0000ff;">$m1</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> <span style="color: #0000ff;">$m2</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$j</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">for</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">..</span><span style="color: #0000ff;">$cols</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066;">return</span> <span style="color: #0000ff;">$m3</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I think this version is much neater, more idiomatic Perl, and also more understandable and maintainable than not just my optimised one, but the original as well.  And it&#8217;s 3 times faster.</p>
<p>Optimising for speed doesn&#8217;t always mean trading off maintainability. Usually finding a better approach gets better results that micro-optimisations, and can end up producing an all-round better solution, not just a faster one.</p>
<p>Unfortunately Doug has stopped updating the Shootout pages, so perl will just have to languish 4 places lower on this test than it should be &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://nothing.tmtm.com/2002/10/the-great-language-shootout/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Lightweight XSLT with TT</title>
		<link>http://nothing.tmtm.com/2002/04/lightweight-xslt-with-tt/</link>
		<comments>http://nothing.tmtm.com/2002/04/lightweight-xslt-with-tt/#comments</comments>
		<pubDate>Tue, 16 Apr 2002 14:54:10 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://nothing.tmtm.com/?p=217</guid>
		<description><![CDATA[I discovered a wonderful Template Toolkit plugin yesterday: XML::Style. The basic idea, according to the docs, is that you can apply various attributes to your HTML. The example given is of transforming an HTML table: [% USE xmlstyle table = { attributes = { border = 0 cellpadding = 4 cellspacing = 1 } } [...]]]></description>
			<content:encoded><![CDATA[<p>I discovered a wonderful <a href="http://www.t2.org/">Template Toolkit</a> plugin yesterday: XML::Style.</p>
<p>The basic idea, according to the docs, is that you can apply various attributes to your HTML. The example given is of transforming an HTML table:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">           [% USE xmlstyle
                  table = {
                      attributes = {
                          border      = 0
                          cellpadding = 4
                          cellspacing = 1
                      }
                  }
           %]
&nbsp;
           [% FILTER xmlstyle %]
&nbsp;
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
             <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Foo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Bar<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Baz<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
           [% END %]</pre></div></div>

<p>This didn&#8217;t sit quite right with me though, as that seemed to be something you should be doing in CSS. But as I read through the docs I discovered you can also change tags. Again though the docs gave a bad example:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">           [% FILTER xmlstyle
                     th = {
                         element = 'td'
                         attributes = { bgcolor='red' }
                     }
           %]
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
             <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;th<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Heading<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/th<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
             <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Value<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           [% END %]</pre></div></div>

<p>Having been playing with XSLT recently, though, a lightbulb went off. The real power of this plugin is more to be able to do things like:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">   <span style="color: #009900;">&#91;</span><span style="color: #339933;">%</span> USE xmlstyle
        video <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
          pre_start <span style="color: #339933;">=</span> <span style="color: #ff0000;">'&lt;html&gt;&lt;head&gt;&lt;title=&quot;Video Info&quot;&gt;&lt;/head&gt;&lt;body&gt;'</span>
          element <span style="color: #339933;">=</span> <span style="color: #ff0000;">'table'</span>
          attributes <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> class<span style="color: #339933;">=</span><span style="color: #ff0000;">'videoTable'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
          post_end  <span style="color: #339933;">=</span> <span style="color: #ff0000;">'&lt;/body&gt;&lt;/html&gt;'</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        title <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
          pre_start <span style="color: #339933;">=</span> <span style="color: #ff0000;">'&lt;tr&gt;&lt;td&gt;Title:&lt;/td&gt;'</span>
          element    <span style="color: #339933;">=</span> <span style="color: #ff0000;">'td'</span>
          attributes <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> class<span style="color: #339933;">=</span><span style="color: #ff0000;">'videoTitle'</span> <span style="color: #009900;">&#125;</span>
          post_end  <span style="color: #339933;">=</span> <span style="color: #ff0000;">'&lt;/tr&gt;'</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        price <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
          pre_start <span style="color: #339933;">=</span> <span style="color: #ff0000;">'&lt;tr&gt;&lt;td&gt;Price:&lt;/td&gt;'</span>
          element    <span style="color: #339933;">=</span> <span style="color: #ff0000;">'td'</span>
          attributes <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> class<span style="color: #339933;">=</span><span style="color: #ff0000;">'videoPrice'</span> <span style="color: #009900;">&#125;</span>
          post_end  <span style="color: #339933;">=</span> <span style="color: #ff0000;">'&lt;/tr&gt;'</span>
        <span style="color: #009900;">&#125;</span>
   <span style="color: #339933;">%</span><span style="color: #009900;">&#93;</span></pre></div></div>

<p>And then, given some XML such as:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;video<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>La Double Vie De Veronique<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>10.99<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/video<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>We end up with:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span>&lt;<span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;Video Info&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;table</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;videoTable&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Title:<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;td</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;videoTitle&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>La Double Vie De Veronique<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Price:<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;td</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;videoPrice&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>10.99<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This could be used as a first step towards &#8220;true&#8221; XSLT if you&#8217;re already using TT. If the only reason you&#8217;re moving towards XSLT is because a PHB says to, it might even be enough to convince them that you&#8217;ve done so :)</p>
]]></content:encoded>
			<wfw:commentRss>http://nothing.tmtm.com/2002/04/lightweight-xslt-with-tt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

