<?xml version="1.0" encoding="utf-8"?>
<feed version="0.3"
xmlns="http://purl.org/atom/ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
>
<title mode="escaped">Brad's Blog</title>
<link rel="alternate" type="text/html" href="http://bjdean.id.au/blog"/>
<modified>2009-02-17T17:07:07+00:00</modified>
<author>
<name>Bradley Dean</name>
<url>http://bjdean.id.au/blog</url>
</author>

<entry>
<title mode="escaped">Finance::Quote::currency_lookup in action</title>
<author>
<name>Bradley Dean</name>
</author>
<link rel="alternate" type="text/html" href="http://bjdean.id.au/blog/archives/2009/02/#e2009-02-17T17_07_06.txt"/>
<id>http://bjdean.id.au/blog/archives/2009/02/#e2009-02-17T17_07_06.txt</id>
<issued>2009-02-17T17:07:06+00:00</issued>
<modified>2009-02-17T17:07:06+00:00</modified>
<created>2009-02-17T17:07:06+00:00</created>
<dc:subject>Perl, Programming</dc:subject>
<content type="application/xhtml+xml" xml:lang="en" xml:space="preserve" mode="escaped">
<![CDATA[
<p>Having
<a href="http://bjdean.id.au/blog/archives/2009/02/#e2009-02-17T01_52_12.txt">
written Finance::Quote::currency_lookup</a> I was able to get back to the
little script that triggered the idea in the first place</p>

<p>The idea: a script which I can call with partial code or currency names and
an amount which will then try and tell me what the current converted value is.
If there's something other than a single matching currency let me know that
nothing or too many things matched.</p>

<p>Here's the script (also
<a href="http://bjdean.id.au/blog/attachments/2009-02/conv.txt">attached</a>):
</p>
<div class="nbcode">
<pre>
<span class="lnr"> 1</span>  <span class="PreProc">#!/usr/bin/env perl</span>
<span class="lnr"> 2</span>
<span class="lnr"> 3</span>  <span class="PreProc">use strict</span>;
<span class="lnr"> 4</span>  <span class="PreProc">use warnings</span>;
<span class="lnr"> 5</span>
<span class="lnr"> 6</span>  <span class="PreProc">use </span>Finance::Quote;
<span class="lnr"> 7</span>  <span class="Statement">my</span> <span class="Identifier">$q</span> = Finance::Quote-&gt;<span class="Statement">new</span>();
<span class="lnr"> 8</span>
<span class="lnr"> 9</span>  <span class="Statement">my</span> <span class="Identifier">%trans</span> = ();
<span class="lnr">10</span>  <span class="Identifier">@trans{</span><span class="Constant">qw(</span><span class="Constant">from to</span><span class="Constant">)</span><span class="Identifier">}</span> = <span class="Identifier">@ARGV</span>;
<span class="lnr">11</span>
<span class="lnr">12</span>  <span class="Comment"># Copied this patter from Finance::Quote::currency to be consistent</span>
<span class="lnr">13</span>  <span class="Identifier">$trans</span><span class="Identifier">{</span><span class="Constant">from</span><span class="Identifier">}</span> =~ <span class="Statement">s/</span><span class="Constant">^</span><span class="Special">\s</span><span class="Special">*(</span><span class="Special">\d</span><span class="Special">*</span><span class="Special">\.</span><span class="Special">?</span><span class="Special">\d</span><span class="Special">*)</span><span class="Special">\s</span><span class="Special">*</span><span class="Statement">//</span>;
<span class="lnr">14</span>  <span class="Statement">my</span> <span class="Identifier">$amount</span> = <span class="Identifier">$1</span> || <span class="Constant">1</span>;
<span class="lnr">15</span>
<span class="lnr">16</span>  <span class="Comment"># Search out currencies</span>
<span class="lnr">17</span>  <span class="Statement">for</span> <span class="Statement">my</span> <span class="Identifier">$key</span> (<span class="Constant">qw(</span><span class="Constant"> from to </span><span class="Constant">)</span>) {
<span class="lnr">18</span>    <span class="Comment"># Is it a code or a name?</span>
<span class="lnr">19</span>    <span class="Statement">my</span> <span class="Identifier">$currencies</span>;
<span class="lnr">20</span>    <span class="Statement">if</span> ( <span class="Identifier">$trans{$key}</span> =~<span class="Statement"> /</span><span class="Constant">^</span><span class="Special">[A-Z]{2,}</span><span class="Constant">$</span><span class="Statement">/</span> ) {
<span class="lnr">21</span>      <span class="Comment"># Guess code</span>
<span class="lnr">22</span>      <span class="Identifier">$currencies</span> = <span class="Identifier">$q</span><span class="Identifier">-&gt;currency_lookup</span>( <span class="Constant">code </span>=&gt; <span class="Constant">qr/</span><span class="Identifier">$trans{$key}</span><span class="Constant">/</span> );
<span class="lnr">23</span>    }
<span class="lnr">24</span>    <span class="Statement">else</span> {
<span class="lnr">25</span>      <span class="Comment"># Guess name</span>
<span class="lnr">26</span>      <span class="Identifier">$currencies</span> = <span class="Identifier">$q</span><span class="Identifier">-&gt;currency_lookup</span>( <span class="Constant">name </span>=&gt; <span class="Constant">qr/</span><span class="Identifier">$trans{$key}</span><span class="Constant">/i</span> );
<span class="lnr">27</span>    }
<span class="lnr">28</span>    <span class="Statement">if</span> ( <span class="Statement">scalar</span> <span class="Statement">keys</span> <span class="Identifier">%{$currencies}</span> == <span class="Constant">1</span> ) {
<span class="lnr">29</span>      <span class="Statement">my</span> <span class="Identifier">$real_key</span> = <span class="Constant">&quot;</span><span class="Constant">real_</span><span class="Identifier">${key}</span><span class="Constant">&quot;</span>;
<span class="lnr">30</span>      (<span class="Identifier">$trans{$real_key}</span>) = <span class="Statement">keys</span> <span class="Identifier">%{$currencies}</span>;
<span class="lnr">31</span>      <span class="Statement">printf</span> <span class="Constant">&quot;</span><span class="Identifier">%s</span><span class="Constant"> : </span><span class="Identifier">%s</span><span class="Constant"> (</span><span class="Identifier">%s</span><span class="Constant">)</span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Identifier">$key</span>
<span class="lnr">32</span>                             , <span class="Identifier">$trans{$real_key}</span>
<span class="lnr">33</span>                             , <span class="Identifier">$currencies-&gt;{$trans{$real_key}}</span><span class="Identifier">-&gt;{</span><span class="Constant">name</span><span class="Identifier">}</span>;
<span class="lnr">34</span>    }
<span class="lnr">35</span>    <span class="Statement">elsif</span> ( <span class="Statement">scalar</span> <span class="Statement">keys</span> <span class="Identifier">%{$currencies}</span> &gt; <span class="Constant">1</span> ) {
<span class="lnr">36</span>      <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">Multiple currency matches for </span><span class="Identifier">${key}</span><span class="Constant">:</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="lnr">37</span>      <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant"> * </span><span class="Identifier">$_</span><span class="Constant"> (</span><span class="Constant">&quot;</span> . <span class="Identifier">$currencies-&gt;{$_}</span><span class="Identifier">-&gt;{</span><span class="Constant">name</span><span class="Identifier">}</span> . <span class="Constant">&quot;</span><span class="Constant">)</span><span class="Special">\n</span><span class="Constant">&quot;</span> <span class="Statement">for</span> <span class="Statement">keys</span> <span class="Identifier">%{$currencies}</span>;
<span class="lnr">38</span>    }
<span class="lnr">39</span>    <span class="Statement">else</span> {
<span class="lnr">40</span>      <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Constant">No currency matches for </span><span class="Identifier">${key}</span><span class="Constant">: </span><span class="Constant">&quot;</span> . <span class="Identifier">$trans{$key}</span> . <span class="Constant">&quot;</span><span class="Constant">!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="lnr">41</span>    }
<span class="lnr">42</span>  }
<span class="lnr">43</span>
<span class="lnr">44</span>  <span class="Comment"># If real from and to exist run the conversion</span>
<span class="lnr">45</span>  <span class="Statement">if</span> ( <span class="Statement">exists</span> <span class="Identifier">$trans</span><span class="Identifier">{</span><span class="Constant">real_from</span><span class="Identifier">}</span> &amp;&amp; <span class="Statement">exists</span> <span class="Identifier">$trans</span><span class="Identifier">{</span><span class="Constant">real_to</span><span class="Identifier">}</span> ) {
<span class="lnr">46</span>    <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Identifier">${amount}</span><span class="Constant"> </span><span class="Identifier">$trans</span><span class="Identifier">{</span><span class="Constant">real_from</span><span class="Identifier">}</span><span class="Constant"> =&gt; </span><span class="Constant">&quot;</span>
<span class="lnr">47</span>        . <span class="Identifier">$q</span><span class="Identifier">-&gt;currency</span>(<span class="Constant">&quot;</span><span class="Identifier">${amount}$trans</span><span class="Identifier">{</span><span class="Constant">real_from</span><span class="Identifier">}</span><span class="Constant">&quot;</span>, <span class="Identifier">$trans</span><span class="Identifier">{</span><span class="Constant">real_to</span><span class="Identifier">}</span>)
<span class="lnr">48</span>        . <span class="Constant">&quot;</span><span class="Constant"> </span><span class="Identifier">$trans</span><span class="Identifier">{</span><span class="Constant">real_to</span><span class="Identifier">}</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="lnr">49</span>  }
</pre>
</div>

<p>And here's some examples of this script in action:</p>
<pre>
# Some Australian dollars to pounds
# (name searches only)
$ ./conv.pl 42aus brit
from : AUD (Australian Dollar)
to : GBP (British Pound)
42 AUD => 18.8454 GBP

# And the some pounds to US dollars
# (a name and a code search, because US is all-caps)
$ ./conv.pl brit US
from : GBP (British Pound)
to : USD (U.S. Dollar)
1 GBP => 1.4225 USD

# Oops - a typo on pounds
$ ./conv.pl GPB euro
No currency matches for from: GPB!
to : EUR (Euro)

# And now, to make the British cry...
$ ./conv.pl GBP euro
from : GBP (British Pound)
to : EUR (Euro)
1 GBP => 1.1317 EUR

# And finally, too many matches:
$ ./conv.pl egypt ven
from : EGP (Egyptian Pound)
Multiple currency matches for to:
 * SIT (Slovenian Tolar)
 * VEB (Venezuelan Bolivar)
</pre>
]]>
</content>

</entry>
<entry>
<title mode="escaped">Adding Finance::Quote::currency_lookup to Finance::Quote</title>
<author>
<name>Bradley Dean</name>
</author>
<link rel="alternate" type="text/html" href="http://bjdean.id.au/blog/archives/2009/02/#e2009-02-17T01_52_12.txt"/>
<id>http://bjdean.id.au/blog/archives/2009/02/#e2009-02-17T01_52_12.txt</id>
<issued>2009-02-17T01:52:12+00:00</issued>
<modified>2009-02-17T01:52:12+00:00</modified>
<created>2009-02-17T01:52:12+00:00</created>
<dc:subject>Perl, Programming</dc:subject>
<content type="application/xhtml+xml" xml:lang="en" xml:space="preserve" mode="escaped">
<![CDATA[
<p>While using <a href="http://search.cpan.org/~ecocode/Finance-Quote-1.15/lib/Finance/Quote.pm#CURRENCY">
Finance::Quote::currency()</a> to do some conversions I found myself
having to go searching around the web for currency codes. At the time I
thought it would be much easier if you could just asked the module what
currency codes existed - and so I started work on a
<code>currency_lookup()</code> function to do just that.</p>

<p><a href="http://finance-quote.sourceforge.net/">Finance::Quote</a> uses the
<a href="http://uk.finance.yahoo.com/currency-converter"> Yahoo Currency
Converter</a> website so the new function needed to report a compatible
list of currencies.</p>

<p>Having developed the search method against a statically stored list of 
currencies (as I hadn't implemented the live extraction yet) I then
<a href="http://sourceforge.net/mailarchive/forum.php?thread_name=20090127223124.GW31745%40bjdean.id.au&forum_name=finance-quote-devel">
discussed options</a> with the other developers and decided that a hard
coded list was a better approach given that the list wasn't going to change
very often.</p>

<p>Here's a basic idea of usage:</p>
<div class="nbcode">
<pre>
<span class="lnr">1</span>    <span class="Identifier">$currencies_by_name</span> = <span class="Identifier">$q</span><span class="Identifier">-&gt;currency_lookup</span>( <span class="Constant">name </span>=&gt; <span class="Constant">'</span><span class="Constant">Australian</span><span class="Constant">'</span> );
<span class="lnr">2</span>    <span class="Identifier">$currencies_by_code</span> = <span class="Identifier">$q</span><span class="Identifier">-&gt;currency_lookup</span>( <span class="Constant">code </span>=&gt; <span class="Constant">qr/</span><span class="Constant">^b</span><span class="Constant">/i</span>      );
<span class="lnr">3</span>    <span class="Identifier">$currencies_by_both</span> = <span class="Identifier">$q</span><span class="Identifier">-&gt;currency_lookup</span>( <span class="Constant">name </span>=&gt; <span class="Constant">qr/</span><span class="Constant">pound</span><span class="Constant">/i</span>
<span class="lnr">4</span>                                             , <span class="Constant">code </span>=&gt; <span class="Constant">'</span><span class="Constant">GB</span><span class="Constant">'</span>         );
</pre>
</div>

<p>The return value is a hash of hash-refs - for example for the first query above:</p>
<div class="nbcode">
<pre>
<span class="lnr">1</span>    <span class="Identifier">$VAR1</span> = { <span class="Constant">AUD </span>=&gt; { <span class="Constant">name </span>=&gt; <span class="Constant">&quot;</span><span class="Constant">Australian Dollar</span><span class="Constant">&quot;</span> } }
</pre>
</div>

<p>I had added a module to contain the currency list (<code>Finance::Quote::Currencies</code>).
Once the search code was complete I then added a test to compare the static list with a live
list and implemented <code>Finance::Quote::Currencies::fetch_live_currencies()</code>. This
function uses <a href="http://search.cpan.org/~gaas/HTML-Parser-3.60/Parser.pm">HTML::Parser</a>
to extract the currency list. If the currency list changes this function can be used to easily
updated the stored list.</p>

<p>These changes are currently available on the
<a href="http://github.com/pfenwick/finance-quote/commits/currency_lookup">
currency_lookup</a> branch on <a href="http://github.com/">github</a>. Coming
to a CPAN release in your town real soon now.</p>
]]>
</content>

</entry>
<entry>
<title mode="escaped">Embedding HTML in MoinMoin page contents</title>
<author>
<name>Bradley Dean</name>
</author>
<link rel="alternate" type="text/html" href="http://bjdean.id.au/blog/archives/2009/01/#e2009-01-27T19_54_40.txt"/>
<id>http://bjdean.id.au/blog/archives/2009/01/#e2009-01-27T19_54_40.txt</id>
<issued>2009-01-27T19:54:40+00:00</issued>
<modified>2009-01-27T19:54:40+00:00</modified>
<created>2009-01-27T19:54:40+00:00</created>

<content type="application/xhtml+xml" xml:lang="en" xml:space="preserve" mode="escaped">
<![CDATA[
<p><strong>Question:</strong> How to use my wiki to store and test bits and pieces of plain html?</p>

<p><strong>Answer:</strong> It should be simple, but there's no such formatter in <a href="http://moinmo.in/">MoinMoin</a> (and no way I know to just say 'embed this text as-is') but surely this must be one of the simplest <a href="/wiki/HelpOnParsers">MoinMoin Parsers</a> that could be written - so let's have a look...</p>

<p>The <a href="/wiki/HelpOnParsers#head-3f3fb0f66200688a41639605c3238f6acf3ee8b1">CSV Parser</a> looked quite simple - from there it became clear that all you really need to do is initialise a couple of attributes in the constructor and then write the simplest-case format method:</p>
<div class="nbcode">
<pre>
<span class="lnr"> 1</span>  <span class="Statement">class</span> <span class="Identifier">Parser</span>:
<span class="lnr"> 2</span>      &quot;&quot;&quot;<span class="Constant"> Format HTML as HTML</span>
<span class="lnr"> 3</span>  <span class="Constant">    </span>&quot;&quot;&quot;
<span class="lnr"> 4</span>
<span class="lnr"> 5</span>      <span class="Statement">def</span> <span class="Identifier">__init__</span>(self, raw, request, **kw):
<span class="lnr"> 6</span>          &quot;&quot;&quot;<span class="Constant"> Store the source text.</span>
<span class="lnr"> 7</span>  <span class="Constant">        </span>&quot;&quot;&quot;
<span class="lnr"> 8</span>          self.raw = raw
<span class="lnr"> 9</span>          self.request = request
<span class="lnr">10</span>          self._ = request.getText
<span class="lnr">11</span>
<span class="lnr">12</span>      <span class="Statement">def</span> <span class="Identifier">format</span>(self, formatter):
<span class="lnr">13</span>          &quot;&quot;&quot;<span class="Constant"> Send it back as it came</span>
<span class="lnr">14</span>  <span class="Constant">        </span>&quot;&quot;&quot;
<span class="lnr">15</span>          self.request.write(self.raw)
</pre>
</div>

<p>Load this into your wiki plugin directory as <code>data/plugin/parser/html.py</code> directory, start a document with <code>#FORMAT html</code> and it just works. This probably wouldn't be a great feature for a public wiki, but it's nice to be able to roll your own so easily. <code>:)</code></p>
]]>
</content>

</entry>
<entry>
<title mode="escaped">Using Time::Local::timelocal rather than POSIX::strftime('%s', ...) to find seconds since the epoch</title>
<author>
<name>Bradley Dean</name>
</author>
<link rel="alternate" type="text/html" href="http://bjdean.id.au/blog/archives/2009/01/#e2009-01-22T11_27_42.txt"/>
<id>http://bjdean.id.au/blog/archives/2009/01/#e2009-01-22T11_27_42.txt</id>
<issued>2009-01-22T11:27:42+00:00</issued>
<modified>2009-01-22T11:27:42+00:00</modified>
<created>2009-01-22T11:27:42+00:00</created>

<content type="application/xhtml+xml" xml:lang="en" xml:space="preserve" mode="escaped">
<![CDATA[
<p>I ran into a cross-platform gotcha today with the perl
<a href="http://search.cpan.org/~nwclark/perl-5.8.9/ext/POSIX/POSIX.pod#strftime">
POSIX::strftime</a> function. I was after the
<a href="http://en.wikipedia.org/wiki/Unix_time">seconds since the epoch</a> for
a given date and had called:</p>
<div class="nbcode">
<pre>
<span class="lnr">1</span>    <span class="Comment"># ... determine second, minute, hour, day, month, year</span>
<span class="lnr">2</span>    <span class="Statement">my</span> <span class="Identifier">$epoch_seconds</span> = strftime(<span class="Constant">'</span><span class="Constant">%s</span><span class="Constant">'</span>, <span class="Identifier">$second</span>, <span class="Identifier">$minute</span>, <span class="Identifier">$hour</span>
<span class="lnr">3</span>                                , <span class="Identifier">$day</span>, <span class="Identifier">$month</span> - <span class="Constant">1</span>, <span class="Identifier">$year</span> - <span class="Constant">1900</span> );
</pre>
</div>

<p>Which of course works fine under *nix - but it turns out (a bit obviously
with hindsight) that the windows strftime does not support '%s'. Unfortunately
this does not result in any sort of error - rather you just end up with the
literal string '%s'</p>

<p><a href="http://search.cpan.org/~drolsky/Time-Local-1.1901/lib/Time/Local.pm">Time::Local</a>
to the rescue - this module implements the reverse of the builtin
<code>localtime</code> and <code>gmtime</code> functions.</p>

<p>Here's a version of the above code which works cross-platform:</p>
<div class="nbcode">
<pre>
<span class="lnr">1</span>    <span class="PreProc">use </span>Time::Local;
<span class="lnr">2</span>    <span class="Comment"># ... determine second, minute, hour, day, month, year</span>
<span class="lnr">3</span>    <span class="Statement">my</span> <span class="Identifier">$epoch_seconds</span> = timelocal( <span class="Identifier">$second</span>, <span class="Identifier">$minute</span>, <span class="Identifier">$hour</span>
<span class="lnr">4</span>                                 , <span class="Identifier">$day</span>, <span class="Identifier">$month</span> - <span class="Constant">1</span>, <span class="Identifier">$year</span>);
</pre>
</div>

<p>As an aside - the module extends the interpretation of the year to be
more user friendly (so you can use fully qualified years, the familiar
'<code>$year&nbsp;-&nbsp;1900</code>' or two-digit years with a rolling window).
It's probably better to stick with the fully qualified year - particularly when
using old dates as it's possible that '<code>$year&nbsp;-&nbsp;1900</code>' will
end up in a sliding window and drop you in the wrong century.</p>

<p>(For other approaches read 
`<a href="http://search.cpan.org/~nwclark/perl-5.8.9/pod/perlfaq4.pod#How_can_I_take_a_string_and_turn_it_into_epoch_seconds?"><code>perldoc -q epoch</code></a>`)</p>

<hr />
<p><strong>Comment by:</strong> Bradley Dean at
Thu, 22 Jan 2009  14:18:27 (GMT)</p>

<p>So why isn't this problem more apparent? When I first started looking
around for the causes I couldn't really see much. One reason is probably that
some developers 'just know' that their strftime has '%s' (not to mention that
their time is based on the epoch) while others have never run into the epoch so
would never call on '%s'.</p>

<p>The perl strftime documentation refers the reader to the local strftime
man page for conversion specifiers, while others (I've looked at python, ruby
and php) give a list of available conversion specifiers. I've checked
<code>time.strftime</code> in python and it support '%s' (but it's
undocumented) which I suspect will be true for most other languages as they'll
be wrapping the system strftime rather than reimplimenting it.</p>
]]>
</content>

</entry>
<entry>
<title mode="escaped">Adding an option to exclude a user from top</title>
<author>
<name>Bradley Dean</name>
</author>
<link rel="alternate" type="text/html" href="http://bjdean.id.au/blog/archives/2008/11/#e2008-11-22T08_16_48.txt"/>
<id>http://bjdean.id.au/blog/archives/2008/11/#e2008-11-22T08_16_48.txt</id>
<issued>2008-11-22T08:16:48+00:00</issued>
<modified>2008-11-22T08:16:48+00:00</modified>
<created>2008-11-22T08:16:48+00:00</created>

<content type="application/xhtml+xml" xml:lang="en" xml:space="preserve" mode="escaped">
<![CDATA[
<p>Over the years I've found that I've wanted to exclude a user from top
(most often 'root' on a fairly quiet system so I can easily see what other
user processes are up to even though they're being quiet).</p>

<p>I've written a patch for the <code>top</code> in
<a href="http://procps.sourceforge.net/">procps</a> which adds <code>j/J</code>
command line and run-time options which essentially mirror the <code>u/U</code>
filters.</p>

<p>I've <a href="http://sourceforge.net/mailarchive/forum.php?thread_name=20081122080304.GD3994%40bjdean.id.au&forum_name=procps-feedback">sent this in</a>
to the procps maintainers just in case they want to include it in the trunk.</p>

<p>The patch is here:
<a href="http://bjdean.id.au/blog/attachments/2008-11/procps-3.2.7-exclude-user-patch">
procps-3.2.7-exclude-user-patch</a>.</p>

<p>The full source bundle is here:
<a href="http://bjdean.id.au/blog/attachments/2008-11/procps-3.2.7-exclude-user.tgz">
procps-3.2.7-exclude-user.tgz</a>.</p>
]]>
</content>

</entry>
<entry>
<title mode="escaped">Converting ISO-8859-1 filenames to UTF-8</title>
<author>
<name>Bradley Dean</name>
</author>
<link rel="alternate" type="text/html" href="http://bjdean.id.au/blog/archives/2008/11/#e2008-11-01T03_20_05.txt"/>
<id>http://bjdean.id.au/blog/archives/2008/11/#e2008-11-01T03_20_05.txt</id>
<issued>2008-11-01T03:20:05+00:00</issued>
<modified>2008-11-01T03:20:05+00:00</modified>
<created>2008-11-01T03:20:05+00:00</created>

<content type="application/xhtml+xml" xml:lang="en" xml:space="preserve" mode="escaped">
<![CDATA[
<p>While falling asleep at the keyboard I thought I'd leave a process running
overnight to process a bunch of my old files, only to find a series of errors
popping up telling me that there were unexpected characters in the filenames.</p>

<p>A quick look revealed that I had ended up with some ISO-8859-1 (ie Latin-1)
character encodings within filenames, which was all very well before I switched
to UTF-8!.</p>

<p>Contemplating possible temporary resetting of my locale (but what about my
newer files that include UTF-8 encodings?) or some sort of simple filter that
dropped characters I chanced upon <a href="http://www.unicodetools.com/">Unicode
Tools</a> and a neat list of tools available in various languages.</p>

<p>In this case perl's <a href="http://search.cpan.org/search?query=Encode">Encode</a>
came to the rescue with this rather short and nicely effective script:</p>
<div class="nbcode">
<pre>
<span class="lnr"> 1</span>  <span class="PreProc">#!/usr/bin/env perl</span>
<span class="lnr"> 2</span>
<span class="lnr"> 3</span>  <span class="Comment">#</span>
<span class="lnr"> 4</span>  <span class="Comment"># For each filepath given on stdin, convert the filename from</span>
<span class="lnr"> 5</span>  <span class="Comment"># iso-8859-1 to utf-8 (or try to!)</span>
<span class="lnr"> 6</span>  <span class="Comment">#</span>
<span class="lnr"> 7</span>
<span class="lnr"> 8</span>  <span class="PreProc">use strict</span>;
<span class="lnr"> 9</span>  <span class="PreProc">use warnings</span>;
<span class="lnr">10</span>  <span class="PreProc">use </span>Encode;
<span class="lnr">11</span>
<span class="lnr">12</span>  <span class="Statement">while</span> ( <span class="Statement">my</span> <span class="Identifier">$orig_filepath</span> = <span class="Identifier">&lt;STDIN&gt;</span> ) {
<span class="lnr">13</span>    <span class="Statement">if</span> ( <span class="Identifier">$orig_filepath</span> =~<span class="Statement"> /</span><span class="Constant">[</span><span class="Special">\xA0</span><span class="Constant">-</span><span class="Special">\xFF</span><span class="Constant">]</span><span class="Statement">/</span> ) {
<span class="lnr">14</span>      <span class="Statement">chomp</span> <span class="Identifier">$orig_filepath</span>;
<span class="lnr">15</span>      <span class="Statement">my</span> <span class="Identifier">$new_filepath</span> = <span class="Identifier">$orig_filepath</span>;
<span class="lnr">16</span>      Encode::from_to(<span class="Identifier">$new_filepath</span>, <span class="Constant">'</span><span class="Constant">iso-8859-1</span><span class="Constant">'</span>, <span class="Constant">'</span><span class="Constant">utf-8</span><span class="Constant">'</span>)
<span class="lnr">17</span>        <span class="Statement">or</span> <span class="Statement">die</span> <span class="Identifier">$!</span>;
<span class="lnr">18</span>      <span class="Statement">print</span> <span class="Constant">&quot;</span><span class="Identifier">$orig_filepath</span><span class="Constant"> ---&gt; </span><span class="Identifier">$new_filepath</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
<span class="lnr">19</span>      <span class="Statement">rename</span> <span class="Identifier">$orig_filepath</span>, <span class="Identifier">$new_filepath</span>;
<span class="lnr">20</span>    }
<span class="lnr">21</span>  }
</pre>
</div>

<p>The check for characters between <code>0xA0</code> and <code>0xFF</code> is
far from perfect for general use (indeed differenciating between the various
ISO-8859-*'s would be a bit of a nightmare) but as I knew my encodings were
limited to ISO-8859-1 I got away with a bit of a shortcut there.</p>

<hr />
<p><strong>Comment by:</strong> Bradley Dean at
Sat, 1  Nov 2008  09:44:11 (BST)</p>

<p>Of course being somewhat late at night I missed the obvious blunder
in the logic - there are characters in the range <code>0xA0-0xFF</code>
in UTF-8 encoded strings so rerunning can cause a bit of a mess.</p>

<p>I'm still thinking about this - the UTF-8 multibyte marker byte is
in the range <code>0xC0</code> to <code>0xFD</code>. These are all legal
ISO-8859-1 character codes so I think I need to draw some sets out on a
piece of paper and find the mutually exclusive bits.</p>

<hr />
<p><strong>Comment by:</strong> Bradley Dean at
Sun, 2  Nov 2008  00:33:40 (BST)</p>

<p>Given that UTF-8 character bytes for non-ASCII characters fall within
the range <code>0x80</code> to <code>0xFD</code>, and given that this overlaps
with the character bytes used by ISO-8859-1, there's not a way to differenciate
a high-byte string just by looking at individual bytes.</p>

<p>In my scenario (text will either be plain ASCII, ISO-8859-1 or UTF-8) the
best test seems to be to try and interpret the string as UTF-8. If errors occur
it's going to be ISO-8859-1, if not it's going to be UTF-8.</p>
]]>
</content>

</entry>
<entry>
<title mode="escaped">Die::Retry - a perl module to easily retry on exceptions</title>
<author>
<name>Bradley Dean</name>
</author>
<link rel="alternate" type="text/html" href="http://bjdean.id.au/blog/archives/2008/10/#e2008-10-14T00_25_25.txt"/>
<id>http://bjdean.id.au/blog/archives/2008/10/#e2008-10-14T00_25_25.txt</id>
<issued>2008-10-14T00:25:25+00:00</issued>
<modified>2008-10-14T00:25:25+00:00</modified>
<created>2008-10-14T00:25:25+00:00</created>

<content type="application/xhtml+xml" xml:lang="en" xml:space="preserve" mode="escaped">
<![CDATA[
<p>I've just written
<a href="http://bjdean.id.au/wiki/DieRetry"><code>Die::Retry</code></a> - a
perl module which makes is easy to write a concise exception retry loop.</p>

<p>The module is only useful if <em>any</em> exception can be silently caught
and the code in question re-run, but in that case it makes it very easy to
write the loop in question:</p>
<div class="nbcode">
<pre>
<span class="lnr">1</span>    <span class="PreProc">use </span>Die::Retry <span class="Constant">qw(</span><span class="Constant"> retry </span><span class="Constant">)</span>;
<span class="lnr">2</span>    <span class="Statement">my</span> <span class="Identifier">$retval</span> = retry(<span class="Identifier"> </span><span class="Statement">sub</span><span class="Identifier"> </span>{ some_func_with_exceptions(<span class="Identifier">$param1</span>, <span class="Identifier">$param2</span>) }
<span class="lnr">3</span>                            , <span class="Statement">times</span> =&gt; <span class="Constant">3</span>
<span class="lnr">4</span>                            , <span class="Constant">delay </span>=&gt; <span class="Constant">0</span>
<span class="lnr">5</span>                            );
</pre>
</div>

<p>In that example the code ref:</p>
<div class="nbcode">
<pre>
<span class="lnr">1</span>  <span class="Statement">sub</span><span class="Identifier"> </span>{ some_func_with_exceptions(<span class="Identifier">$param1</span>, <span class="Identifier">$param2</span>) }&lt;/pre&gt;
</pre>
</div>

<p>is called up to three times if exceptions are thrown. There is no
delay between retries. Finally - if a successful call is made to the code
ref <code>$retval</code> will get the (scalar context) return value of the
code ref call.</p>

<p>I'm submitting this to <a href="http://www.cpan.org">CPAN</a> and have
already set it up on <a href="http://github.com/">GitHub</a> and
<a href="http://www.ohloh.net/">Ohloh</a>.</p>

<hr />
<p><strong>Comment by:</strong> Bradley Dean at
Mon, 20 Oct 2008  22:56:49 (BST)</p>

<p>I've just found
<a href="http://search.cpan.org/author/MARKF/Attempt-1.01/lib/Attempt.pm">Attempt</a>
which does exactly what I coded for <code>Die::Retry</code> - in fact both the 
code and the syntax are practically identical. So dies <code>Die::Retry</code>.
</p>
]]>
</content>

</entry>
<entry>
<title mode="escaped">Blur &quot;The Best Of&quot; Album Cover - Turkish Rail style</title>
<author>
<name>Bradley Dean</name>
</author>
<link rel="alternate" type="text/html" href="http://bjdean.id.au/blog/archives/2008/10/#e2008-10-10T17_35_53.txt"/>
<id>http://bjdean.id.au/blog/archives/2008/10/#e2008-10-10T17_35_53.txt</id>
<issued>2008-10-10T17:35:53+00:00</issued>
<modified>2008-10-10T17:35:53+00:00</modified>
<created>2008-10-10T17:35:53+00:00</created>

<content type="application/xhtml+xml" xml:lang="en" xml:space="preserve" mode="escaped">
<![CDATA[
<p>While travelling through Istanbul on the train I happened to notice that
their 'make this seat avaialble to folk in need' sticker held a certain
resemblance to a Blur album cover:</p>

<p><a href="http://images.bjdean.id.au/Blur-The_Best_Of_Blur-Frontal.jpg">
   <img src="http://images.bjdean.id.au/Blur-The_Best_Of_Blur-Frontal.jpg"
        alt="Blur The Best Of Album Cover"
        width="200" height="200"
        style="border: none;" /></a>

   <a href="http://images.bjdean.id.au/TurkeyRailMergedWithBlurBestOf.jpg">
   <img src="http://images.bjdean.id.au/TurkeyRailMergedWithBlurBestOf.jpg"
        alt="Turkish Rail sticker merged with Blur Album"
        width="200" height="200"
        style="border: none;" /></a>
</p>

<p>Albeit without the "blur" label! <code>:)</code></p>
]]>
</content>

</entry>
<entry>
<title mode="escaped">Back to my beloved Fvwm desktop</title>
<author>
<name>Bradley Dean</name>
</author>
<link rel="alternate" type="text/html" href="http://bjdean.id.au/blog/archives/2008/09/#e2008-09-24T09_36_55.txt"/>
<id>http://bjdean.id.au/blog/archives/2008/09/#e2008-09-24T09_36_55.txt</id>
<issued>2008-09-24T09:36:55+00:00</issued>
<modified>2008-09-24T09:36:55+00:00</modified>
<created>2008-09-24T09:36:55+00:00</created>

<content type="application/xhtml+xml" xml:lang="en" xml:space="preserve" mode="escaped">
<![CDATA[
<p>After what seems like a bit of an eternity of battling with obstructively
gui-driven and complex desktop configuration (such as with gnome and kde), or
just plain awful configuration <strong>and</strong> usage (such as ms windows)
I've finally blown the dust off my old <a href="http://www.fvwm.org/">Fvwm</a>
<code>.fvwmrc</code> file and have re-entered my desktop utopia once again:</p>

<p style="text-align: center">
<img src="http://bjdean.id.au/blog/attachments/2008-09/BradsFvwmDesktop.jpg"
     alt="Brad's Fvwm Desktop"
     />
</p>

<p>Of course I'm back to configuring most things by hand again (wireless,
mounting usb drives, X) - but that has always held a certain perverse 
enjoyment for me anyway!</p>
]]>
</content>

</entry>
<entry>
<title mode="escaped">Server Status Images</title>
<author>
<name>Bradley Dean</name>
</author>
<link rel="alternate" type="text/html" href="http://bjdean.id.au/blog/archives/2008/09/#e2008-09-19T15_26_37.txt"/>
<id>http://bjdean.id.au/blog/archives/2008/09/#e2008-09-19T15_26_37.txt</id>
<issued>2008-09-19T15:26:37+00:00</issued>
<modified>2008-09-19T15:26:37+00:00</modified>
<created>2008-09-19T15:26:37+00:00</created>

<content type="application/xhtml+xml" xml:lang="en" xml:space="preserve" mode="escaped">
<![CDATA[
<p>Here's a sort of long-running private joke between some of my friends and
I...</p>

<p>Many years ago we were setting up a server monitor and it was decided that
we'd come up with an executive summary style - that being a simple
<em>thumbs-up</em>:</p>

<p><img src="http://bjdean.id.au/blog/attachments/2008-09/ThumbsUp.png"
        alt="Thumbs Up" /></p>

<p>Or a <em>thumbs-down</em>:</p>

<p><img src="http://bjdean.id.au/blog/attachments/2008-09/ThumbsDown.png"
        alt="Thumbs Down" /></p>

<p>Somewhere along the way the system developed a bit of an attitude and
somehow the <em>thumbs-up</em> became a little bit more like....</p>

<p><img src="http://bjdean.id.au/blog/attachments/2008-09/FingerUp.png"
        alt="Finger Up" /></p>

<p>Don't really know why I've posted that but a recent conversation lead to
re-building those simple images and I thought I'd put them up in lights.
<code>:)</code></p>
]]>
</content>

</entry>

</feed>

