<?xml version="1.0" encoding="iso-8859-1"?><!-- generator="b2evolution/4.0.3" -->
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Web Developer - Author(s): Rob Earl</title>
		<link>http://blogs.lessthandot.com/index.php/WebDev/</link>
		<atom:link rel="self" type="application/rss+xml" href="http://blogs.lessthandot.com/index.php/WebDev/?tempskin=_rss2" />
		<description></description>
		<language>en-GB</language>
		<docs>http://blogs.law.harvard.edu/tech/rss</docs>
		<admin:generatorAgent rdf:resource="http://b2evolution.net/?v=4.0.3"/>
		<ttl>60</ttl>
				<item>
			<title>Adding Stackoverflow user feed to your homepage</title>
			<link>http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/adding-stackoverflow-user-feed-on</link>
			<pubDate>Sun, 16 Dec 2012 16:48:00 +0000</pubDate>			<dc:creator>Rob Earl</dc:creator>
			<category domain="main">Server Programming</category>
<category domain="alt">Perl</category>			<guid isPermaLink="false">1972@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;Following on from a comment on a &lt;a href=&quot;http://blogs.lessthandot.com/index.php/WebDev/perl/taming-rss-feeds-with-xml&quot;&gt;previous post&lt;/a&gt;, this post will demonstrate how to use XML::Feed and Template::Toolkit to format your recent Stackoverflow activity, suitable for including on your own web page. Aptly, this was aided by a &lt;a href=&quot;http://stackoverflow.com/q/13897382/1691146&quot;&gt;stackoverflow question&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;This time we can use XML::Feed both to get and parse the feed:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb50224&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; XML::&lt;span style=&quot;color: #006600;&quot;&gt;Feed&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$feed&lt;/span&gt; = XML::&lt;span style=&quot;color: #006600;&quot;&gt;Feed&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;parse&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;URI-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;http://stackoverflow.com/feeds/user/1691146&#039;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb74936&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We need to adapt the template to account for XML::Feed&#039;s differences: &lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb15426&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$template&lt;/span&gt; = &amp;lt;&amp;lt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;TEMPLATE&amp;quot;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% feed.title %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;Activity:&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% FOREACH item = feed.items %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% item.issued.dmy %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% item.issued.hms %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;\t&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% item.title %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% END %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;TEMPLATE&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb69054&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And the template toolkit invocation:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb40599&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$tt&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;process&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; \&lt;span style=&quot;color: #0000ff;&quot;&gt;$template&lt;/span&gt;, &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;feed&#039;&lt;/span&gt; =&amp;gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$feed&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;or&lt;/span&gt; &lt;a href=&quot;http://perldoc.perl.org/functions/die.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$tt&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb37146&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Full example:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb68360&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/perl&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; strict;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; warnings;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; XML::&lt;span style=&quot;color: #006600;&quot;&gt;Feed&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; Template;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$stackoverflow_id&lt;/span&gt; = &lt;span style=&quot;color: #cc66cc;&quot;&gt;1691146&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$stackoverflow_url&lt;/span&gt; = &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;http://stackoverflow.com/feeds/user/$stackoverflow_id&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$template&lt;/span&gt; = &amp;lt;&amp;lt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;TEMPLATE&amp;quot;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% feed.title %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;Activity:&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% FOREACH item = feed.items %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% item.issued.dmy %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% item.issued.hms %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;\t&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% item.title %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% END %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;TEMPLATE&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$tt&lt;/span&gt; = Template-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;STRICT&#039;&lt;/span&gt; =&amp;gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;or&lt;/span&gt; &lt;a href=&quot;http://perldoc.perl.org/functions/die.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Failed to load template: $Template::ERROR&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$feed&lt;/span&gt; = XML::&lt;span style=&quot;color: #006600;&quot;&gt;Feed&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;parse&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;URI-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$stackoverflow_url&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$tt&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;process&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; \&lt;span style=&quot;color: #0000ff;&quot;&gt;$template&lt;/span&gt;, &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;feed&#039;&lt;/span&gt; =&amp;gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$feed&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;or&lt;/span&gt; &lt;a href=&quot;http://perldoc.perl.org/functions/die.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$tt&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb69244&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is still a really simple script with added support for Atom feeds.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/adding-stackoverflow-user-feed-on&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Following on from a comment on a <a href="http://blogs.lessthandot.com/index.php/WebDev/perl/taming-rss-feeds-with-xml">previous post</a>, this post will demonstrate how to use XML::Feed and Template::Toolkit to format your recent Stackoverflow activity, suitable for including on your own web page. Aptly, this was aided by a <a href="http://stackoverflow.com/q/13897382/1691146">stackoverflow question</a>!</p>

<p>This time we can use XML::Feed both to get and parse the feed:</p>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb76118'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb76118','cb90422'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb76118" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> XML::<span style="color: #006600;">Feed</span>;</li><li style="" class="li2"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$feed</span> = XML::<span style="color: #006600;">Feed</span>-&gt;<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>URI-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'http://stackoverflow.com/feeds/user/1691146'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</li></ol></div><div id="cb90422" style="display: none; color: red;"></div></div></div>

<p>We need to adapt the template to account for XML::Feed's differences: </p>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb42267'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb42267','cb83317'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb42267" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$template</span> = &lt;&lt;<span style="color: #ff0000;">&quot;TEMPLATE&quot;</span>;</li><li style="" class="li2"><span style="color: #66cc66;">&#91;</span>% feed.title %<span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">Activity:</li><li style="" class="li1"><span style="color: #66cc66;">&#91;</span>% FOREACH item = feed.items %<span style="color: #66cc66;">&#93;</span></li><li style="" class="li2"><span style="color: #66cc66;">&#91;</span>% item.issued.dmy %<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>% item.issued.hms %<span style="color: #66cc66;">&#93;</span>\t<span style="color: #66cc66;">&#91;</span>% item.title %<span style="color: #66cc66;">&#93;</span></li><li style="" class="li1"><span style="color: #66cc66;">&#91;</span>% END %<span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">TEMPLATE</li></ol></div><div id="cb83317" style="display: none; color: red;"></div></div></div>

<p>And the template toolkit invocation:</p>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb54274'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb54274','cb76292'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb54274" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000ff;">$tt</span>-&gt;<span style="color: #006600;">process</span><span style="color: #66cc66;">&#40;</span> \<span style="color: #0000ff;">$template</span>, <span style="color: #66cc66;">&#123;</span> <span style="color: #ff0000;">'feed'</span> =&gt; <span style="color: #0000ff;">$feed</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span></li><li style="" class="li2">&nbsp; <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #0000ff;">$tt</span>-&gt;<span style="color: #006600;">error</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li></ol></div><div id="cb76292" style="display: none; color: red;"></div></div></div>

<p>Full example:</p>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb88831'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb88831','cb59086'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb88831" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> strict;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">use</span> warnings;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">use</span> XML::<span style="color: #006600;">Feed</span>;</li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> Template;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$stackoverflow_id</span> = <span style="color: #cc66cc;">1691146</span>;</li><li style="" class="li2"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$stackoverflow_url</span> = <span style="color: #ff0000;">&quot;http://stackoverflow.com/feeds/user/$stackoverflow_id&quot;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$template</span> = &lt;&lt;<span style="color: #ff0000;">&quot;TEMPLATE&quot;</span>;</li><li style="" class="li1"><span style="color: #66cc66;">&#91;</span>% feed.title %<span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">Activity:</li><li style="" class="li2"><span style="color: #66cc66;">&#91;</span>% FOREACH item = feed.items %<span style="color: #66cc66;">&#93;</span></li><li style="" class="li1"><span style="color: #66cc66;">&#91;</span>% item.issued.dmy %<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>% item.issued.hms %<span style="color: #66cc66;">&#93;</span>\t<span style="color: #66cc66;">&#91;</span>% item.title %<span style="color: #66cc66;">&#93;</span></li><li style="" class="li2"><span style="color: #66cc66;">&#91;</span>% END %<span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">TEMPLATE</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$tt</span> = Template-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'STRICT'</span> =&gt; <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span></li><li style="" class="li2">&nbsp; <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">&quot;Failed to load template: $Template::ERROR<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$feed</span> = XML::<span style="color: #006600;">Feed</span>-&gt;<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>URI-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$stackoverflow_url</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000ff;">$tt</span>-&gt;<span style="color: #006600;">process</span><span style="color: #66cc66;">&#40;</span> \<span style="color: #0000ff;">$template</span>, <span style="color: #66cc66;">&#123;</span> <span style="color: #ff0000;">'feed'</span> =&gt; <span style="color: #0000ff;">$feed</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #0000ff;">$tt</span>-&gt;<span style="color: #006600;">error</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li></ol></div><div id="cb59086" style="display: none; color: red;"></div></div></div>

<p>This is still a really simple script with added support for Atom feeds.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/adding-stackoverflow-user-feed-on">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/adding-stackoverflow-user-feed-on#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/WebDev/?tempskin=_rss2&#38;disp=comments&#38;p=1972</wfw:commentRss>
		</item>
				<item>
			<title>Create HTML from output of Perl::Critic</title>
			<link>http://blogs.lessthandot.com/index.php/WebDev/perl/create-html-from-output-of</link>
			<pubDate>Sat, 22 Sep 2012 07:46:00 +0000</pubDate>			<dc:creator>Rob Earl</dc:creator>
			<category domain="main">Perl</category>			<guid isPermaLink="false">1841@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;&lt;a href=&quot;http://search.cpan.org/~thaljef/Perl-Critic-1.118/lib/Perl/Critic.pm&quot;&gt;Perl::Critic&lt;/a&gt; is great. If you haven&#039;t tried it, you should. It can help you improve the quality of your code no end.&lt;/p&gt;

&lt;p&gt;I wrote this script to make it easier to view the feedback in the context of the code itself. When run in a directory containing Perl code it&#039;ll create a critic_html directory containing index.html summarizing the results:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/blogs/WebDev/CriticHtml/critic-html-index.jpg&quot; alt=&quot;critic_html/index.html&quot; title=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Clicking through to any of the files will give you all the violations found by Perl::Critic, inline with the code:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/blogs/WebDev/CriticHtml/critic-html-code.jpg&quot; alt=&quot;&quot; title=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In addition to Perl::Critic, the script uses Template::Toolkit to format the output. The code below and templates can be found in the attached &lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/WebDev/CriticHtml/critic_html.zip&quot;&gt;critic_html.zip&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb8441&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/perl&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; strict;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; warnings;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; Perl::&lt;span style=&quot;color: #006600;&quot;&gt;Critic&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; Perl::&lt;span style=&quot;color: #006600;&quot;&gt;Critic&lt;/span&gt;::&lt;span style=&quot;color: #006600;&quot;&gt;Utils&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; Template;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; Cwd &lt;a href=&quot;http://perldoc.perl.org/functions/qw.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;qw&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;abs_path&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; File::&lt;span style=&quot;color: #006600;&quot;&gt;Basename&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; English &lt;a href=&quot;http://perldoc.perl.org/functions/qw.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;qw&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;-no_match_vars&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; constant SEVERITY &amp;nbsp;=&amp;gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Include all violations.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; constant SKIP_GOOD =&amp;gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Skip files with no violations?&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;a href=&quot;http://perldoc.perl.org/functions/mkdir.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;mkdir&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;critic_html&#039;&lt;/span&gt;;&lt;br /&gt;&lt;a href=&quot;http://perldoc.perl.org/functions/mkdir.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;mkdir&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;critic_html/src&#039;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; autodie;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Analyse all perl files below the current directory.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;@files&lt;/span&gt; = Perl::&lt;span style=&quot;color: #006600;&quot;&gt;Critic&lt;/span&gt;::&lt;span style=&quot;color: #006600;&quot;&gt;Utils&lt;/span&gt;::&lt;span style=&quot;color: #006600;&quot;&gt;all_perl_files&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://perldoc.perl.org/functions/q.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;q&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;.&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;@summary&lt;/span&gt; = &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Store statistics on each file processed.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;foreach&lt;/span&gt; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$file&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;@files&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Create a new Critic for per file statistics.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$critic&lt;/span&gt; = Perl::&lt;span style=&quot;color: #006600;&quot;&gt;Critic&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;-severity&#039;&lt;/span&gt; =&amp;gt; SEVERITY &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$file_safe&lt;/span&gt; = &lt;span style=&quot;color: #0000ff;&quot;&gt;$file&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$file_safe&lt;/span&gt; =~ &lt;a href=&quot;http://perldoc.perl.org/functions/s.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;s&lt;/span&gt;&lt;/a&gt;/&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;\W&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;/_/g;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;@violations&lt;/span&gt; = &lt;span style=&quot;color: #0000ff;&quot;&gt;$critic&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;critique&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$file&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;next&lt;/span&gt; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;!&lt;span style=&quot;color: #0000ff;&quot;&gt;@violations&lt;/span&gt; &amp;amp;&amp;amp; SKIP_GOOD&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/push.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;push&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;@summary&lt;/span&gt;, &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;filename&#039;&lt;/span&gt; =&amp;gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$file&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;link&#039;&lt;/span&gt; &amp;nbsp; &amp;nbsp; =&amp;gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;src/$file_safe.html&amp;quot;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;stats&#039;&lt;/span&gt; &amp;nbsp; &amp;nbsp;=&amp;gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$critic&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;statistics&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/open.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;open&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$FH&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;&amp;lt;&#039;&lt;/span&gt;, &lt;span style=&quot;color: #0000ff;&quot;&gt;$file&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;@lines&lt;/span&gt; = &amp;lt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$FH&lt;/span&gt;&amp;gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/close.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;close&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$FH&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Attach all violations to the line they were found on.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;@violations_by_line&lt;/span&gt; = &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$line_number&lt;/span&gt; = &lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;foreach&lt;/span&gt; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$line&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;@lines&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Get all the violations for the current line.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;@line_violations&lt;/span&gt; = &lt;a href=&quot;http://perldoc.perl.org/functions/grep.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;grep&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$_&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;line_number&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; == &lt;span style=&quot;color: #0000ff;&quot;&gt;$line_number&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;@violations&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/push.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;push&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;@violations_by_line&lt;/span&gt;, &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;number&#039;&lt;/span&gt; &amp;nbsp;=&amp;gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$line_number&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;content&#039;&lt;/span&gt; =&amp;gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$line&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;violations&#039;&lt;/span&gt; =&amp;gt; \&lt;span style=&quot;color: #0000ff;&quot;&gt;@line_violations&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$line_number&lt;/span&gt;++;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; write_html&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;critic_html/src/$file_safe.html&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;codefile&#039;&lt;/span&gt;, &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;title&#039;&lt;/span&gt; =&amp;gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Critic Analysis of $file&amp;quot;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;lines&#039;&lt;/span&gt; =&amp;gt; \&lt;span style=&quot;color: #0000ff;&quot;&gt;@violations_by_line&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;write_html&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;critic_html/index.html&#039;&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;index&#039;&lt;/span&gt;, &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;title&#039;&lt;/span&gt; =&amp;gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;Perl::Critic::HTML Summary&#039;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;files&#039;&lt;/span&gt; =&amp;gt; \&lt;span style=&quot;color: #0000ff;&quot;&gt;@summary&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;sub&lt;/span&gt; write_html &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$filename&lt;/span&gt;, &lt;span style=&quot;color: #0000ff;&quot;&gt;$template&lt;/span&gt;, &lt;span style=&quot;color: #0000ff;&quot;&gt;$data&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; = &lt;span style=&quot;color: #0000ff;&quot;&gt;@_&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Include templates from the install directory.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$tt&lt;/span&gt; = Template-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;INCLUDE_PATH&#039;&lt;/span&gt; =&amp;gt; dirname&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;abs_path&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$PROGRAM_NAME&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;/templates&#039;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/print.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;print&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Writing $filename&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/open.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;open&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$FILE&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;&amp;gt;&#039;&lt;/span&gt;, &lt;span style=&quot;color: #0000ff;&quot;&gt;$filename&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$tt&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;process&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$template&lt;/span&gt;, &lt;span style=&quot;color: #0000ff;&quot;&gt;$data&lt;/span&gt;, &lt;span style=&quot;color: #0000ff;&quot;&gt;$FILE&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/close.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;close&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$FILE&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/return.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;return&lt;/span&gt;&lt;/a&gt;;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb96272&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/WebDev/perl/create-html-from-output-of&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p><a href="http://search.cpan.org/~thaljef/Perl-Critic-1.118/lib/Perl/Critic.pm">Perl::Critic</a> is great. If you haven't tried it, you should. It can help you improve the quality of your code no end.</p>

<p>I wrote this script to make it easier to view the feedback in the context of the code itself. When run in a directory containing Perl code it'll create a critic_html directory containing index.html summarizing the results:</p>

<p><img src="http://blogs.lessthandot.com/media/blogs/WebDev/CriticHtml/critic-html-index.jpg" alt="critic_html/index.html" title="" /></p>

<p>Clicking through to any of the files will give you all the violations found by Perl::Critic, inline with the code:</p>

<p><img src="http://blogs.lessthandot.com/media/blogs/WebDev/CriticHtml/critic-html-code.jpg" alt="" title="" /></p>

<p>In addition to Perl::Critic, the script uses Template::Toolkit to format the output. The code below and templates can be found in the attached <a href="http://blogs.lessthandot.com/media/blogs/WebDev/CriticHtml/critic_html.zip">critic_html.zip</a></p>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb39052'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb39052','cb20844'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb39052" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> strict;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">use</span> warnings;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">use</span> Perl::<span style="color: #006600;">Critic</span>;</li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> Perl::<span style="color: #006600;">Critic</span>::<span style="color: #006600;">Utils</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> Template;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">use</span> Cwd <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #66cc66;">&#40;</span>abs_path<span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> File::<span style="color: #006600;">Basename</span>;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">use</span> English <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #66cc66;">&#40;</span>-no_match_vars<span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">use</span> constant SEVERITY &nbsp;=&gt; <span style="color: #cc66cc;">1</span>; <span style="color: #808080; font-style: italic;"># Include all violations.</span></li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> constant SKIP_GOOD =&gt; <span style="color: #cc66cc;">0</span>; <span style="color: #808080; font-style: italic;"># Skip files with no violations?</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><a href="http://perldoc.perl.org/functions/mkdir.html"><span style="color: #000066;">mkdir</span></a> <span style="color: #ff0000;">'critic_html'</span>;</li><li style="" class="li2"><a href="http://perldoc.perl.org/functions/mkdir.html"><span style="color: #000066;">mkdir</span></a> <span style="color: #ff0000;">'critic_html/src'</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">use</span> autodie;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #808080; font-style: italic;"># Analyse all perl files below the current directory.</span></li><li style="" class="li1"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@files</span> = Perl::<span style="color: #006600;">Critic</span>::<span style="color: #006600;">Utils</span>::<span style="color: #006600;">all_perl_files</span><span style="color: #66cc66;">&#40;</span><a href="http://perldoc.perl.org/functions/q.html"><span style="color: #000066;">q</span></a><span style="color: #66cc66;">&#123;</span>.<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@summary</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;"># Store statistics on each file processed.</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$file</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">@files</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Create a new Critic for per file statistics.</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$critic</span> = Perl::<span style="color: #006600;">Critic</span>-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'-severity'</span> =&gt; SEVERITY <span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$file_safe</span> = <span style="color: #0000ff;">$file</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000ff;">$file_safe</span> =~ <a href="http://perldoc.perl.org/functions/s.html"><span style="color: #000066;">s</span></a>/<span style="color: #66cc66;">&#91;</span>\W<span style="color: #66cc66;">&#93;</span>/_/g;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@violations</span> = <span style="color: #0000ff;">$critic</span>-&gt;<span style="color: #006600;">critique</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$file</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #b1b100;">next</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #0000ff;">@violations</span> &amp;&amp; SKIP_GOOD<span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/push.html"><span style="color: #000066;">push</span></a> <span style="color: #0000ff;">@summary</span>, <span style="color: #66cc66;">&#123;</span> <span style="color: #ff0000;">'filename'</span> =&gt; <span style="color: #0000ff;">$file</span>,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'link'</span> &nbsp; &nbsp; =&gt; <span style="color: #ff0000;">&quot;src/$file_safe.html&quot;</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'stats'</span> &nbsp; &nbsp;=&gt; <span style="color: #0000ff;">$critic</span>-&gt;<span style="color: #006600;">statistics</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/open.html"><span style="color: #000066;">open</span></a> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$FH</span>, <span style="color: #ff0000;">'&lt;'</span>, <span style="color: #0000ff;">$file</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@lines</span> = &lt;<span style="color: #0000ff;">$FH</span>&gt;;</li><li style="" class="li1">&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/close.html"><span style="color: #000066;">close</span></a> <span style="color: #0000ff;">$FH</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Attach all violations to the line they were found on.</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@violations_by_line</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$line_number</span> = <span style="color: #cc66cc;">1</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$line</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">@lines</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Get all the violations for the current line.</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@line_violations</span> = <a href="http://perldoc.perl.org/functions/grep.html"><span style="color: #000066;">grep</span></a> <span style="color: #66cc66;">&#123;</span> <span style="color: #0000ff;">$_</span>-&gt;<span style="color: #006600;">line_number</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #0000ff;">$line_number</span><span style="color: #66cc66;">&#125;</span> <span style="color: #0000ff;">@violations</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/push.html"><span style="color: #000066;">push</span></a> <span style="color: #0000ff;">@violations_by_line</span>, <span style="color: #66cc66;">&#123;</span> <span style="color: #ff0000;">'number'</span> &nbsp;=&gt; <span style="color: #0000ff;">$line_number</span>,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'content'</span> =&gt; <span style="color: #0000ff;">$line</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'violations'</span> =&gt; \<span style="color: #0000ff;">@line_violations</span> <span style="color: #66cc66;">&#125;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$line_number</span>++;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; write_html<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;critic_html/src/$file_safe.html&quot;</span>, <span style="color: #ff0000;">'codefile'</span>, <span style="color: #66cc66;">&#123;</span> <span style="color: #ff0000;">'title'</span> =&gt; <span style="color: #ff0000;">&quot;Critic Analysis of $file&quot;</span>,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'lines'</span> =&gt; \<span style="color: #0000ff;">@violations_by_line</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1"><span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">write_html<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'critic_html/index.html'</span>, <span style="color: #ff0000;">'index'</span>, <span style="color: #66cc66;">&#123;</span> <span style="color: #ff0000;">'title'</span> =&gt; <span style="color: #ff0000;">'Perl::Critic::HTML Summary'</span>,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'files'</span> =&gt; \<span style="color: #0000ff;">@summary</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">sub</span> write_html <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filename</span>, <span style="color: #0000ff;">$template</span>, <span style="color: #0000ff;">$data</span><span style="color: #66cc66;">&#41;</span> = <span style="color: #0000ff;">@_</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Include templates from the install directory.</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$tt</span> = Template-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #ff0000;">'INCLUDE_PATH'</span> =&gt; dirname<span style="color: #66cc66;">&#40;</span>abs_path<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$PROGRAM_NAME</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">'/templates'</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Writing $filename<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/open.html"><span style="color: #000066;">open</span></a> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$FILE</span>, <span style="color: #ff0000;">'&gt;'</span>, <span style="color: #0000ff;">$filename</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000ff;">$tt</span>-&gt;<span style="color: #006600;">process</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$template</span>, <span style="color: #0000ff;">$data</span>, <span style="color: #0000ff;">$FILE</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/close.html"><span style="color: #000066;">close</span></a> <span style="color: #0000ff;">$FILE</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a>;</li><li style="" class="li1"><span style="color: #66cc66;">&#125;</span></li></ol></div><div id="cb20844" style="display: none; color: red;"></div></div></div><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/WebDev/perl/create-html-from-output-of">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/WebDev/perl/create-html-from-output-of#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/WebDev/?tempskin=_rss2&#38;disp=comments&#38;p=1841</wfw:commentRss>
		</item>
				<item>
			<title>Taming RSS Feeds with XML::RSS and Template::Toolkit</title>
			<link>http://blogs.lessthandot.com/index.php/WebDev/perl/taming-rss-feeds-with-xml</link>
			<pubDate>Fri, 14 Sep 2012 11:01:00 +0000</pubDate>			<dc:creator>Rob Earl</dc:creator>
			<category domain="alt">Server Programming</category>
<category domain="main">Perl</category>			<guid isPermaLink="false">1828@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;This is a remarkably simple trick which I&#039;ve found very handy. With a few lines of Perl you can take any RSS feed and format it to your liking.&lt;/p&gt;
&lt;h1&gt;&lt;/h1&gt;
&lt;h1&gt;Get the Feed&lt;/h1&gt;
&lt;p&gt;You can do this using &lt;a href=&quot;http://search.cpan.org/~gaas/libwww-perl-6.04/lib/LWP/Simple.pm&quot;&gt;LWP::Simple&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb67158&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; LWP::&lt;span style=&quot;color: #006600;&quot;&gt;Simple&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$feed_url&lt;/span&gt; = &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;http://feeds.bbci.co.uk/news/rss.xml&#039;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$feed&lt;/span&gt; = get&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$feed_url&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;or&lt;/span&gt; &lt;a href=&quot;http://perldoc.perl.org/functions/die.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Failed to fetch feed.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb85301&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1&gt;&lt;/h1&gt;
&lt;h1&gt;Process the Raw Result&lt;/h1&gt;
&lt;p&gt;Using &lt;a href=&quot;http://search.cpan.org/~kellan/XML-RSS-1.05/lib/RSS.pm&quot;&gt;XML::RSS&lt;/a&gt;, convert the raw feed into a more manageable hash.&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb67153&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; XML::&lt;span style=&quot;color: #006600;&quot;&gt;RSS&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$rss&lt;/span&gt; = XML::&lt;span style=&quot;color: #006600;&quot;&gt;RSS&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$rss&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;parse&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$feed&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb20595&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1&gt;&lt;/h1&gt;
&lt;h1&gt;Format to Your Liking&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;http://template-toolkit.org/&quot;&gt;Template::Toolkit&lt;/a&gt; can take in a &lt;a href=&quot;http://template-toolkit.org/docs/manual/Syntax.html&quot;&gt;template&lt;/a&gt; and a hash reference of values to substitute into the template.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb82669&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Define a template&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$template&lt;/span&gt; = &amp;lt;&amp;lt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;TEMPLATE&amp;quot;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% channel.title %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;Headlines:&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% FOREACH item = items %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% item.pubDate %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;\t&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% item.title %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% END %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;TEMPLATE&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb13650&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This simple template will take the BBC news feed from above and print out a list of headlines with publication dates.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb96086&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$tt&lt;/span&gt; = Template-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;or&lt;/span&gt; &lt;a href=&quot;http://perldoc.perl.org/functions/die.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Failed to load template: $Template::ERROR&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Combine the template with the processed RSS feed.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$tt&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;process&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; \&lt;span style=&quot;color: #0000ff;&quot;&gt;$template&lt;/span&gt;, &lt;span style=&quot;color: #0000ff;&quot;&gt;$rss&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;or&lt;/span&gt; &lt;a href=&quot;http://perldoc.perl.org/functions/die.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$tt&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb80139&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1&gt;&lt;/h1&gt;
&lt;h1&gt;Putting it All Together&lt;/h1&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb86718&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/perl&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; strict;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; warnings;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; XML::&lt;span style=&quot;color: #006600;&quot;&gt;RSS&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; LWP::&lt;span style=&quot;color: #006600;&quot;&gt;Simple&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; Template;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;##################&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Configuration:&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;##################&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$feed_url&lt;/span&gt; = &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;http://feeds.bbci.co.uk/news/rss.xml&#039;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$template&lt;/span&gt; = &amp;lt;&amp;lt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;TEMPLATE&amp;quot;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% channel.title %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;Headlines:&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% FOREACH item = items %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% item.pubDate %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;\t&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% item.title %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;% END %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;TEMPLATE&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;##################&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;##################&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$tt&lt;/span&gt; = Template-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;or&lt;/span&gt; &lt;a href=&quot;http://perldoc.perl.org/functions/die.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Failed to load template: $Template::ERROR&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$feed&lt;/span&gt; = get&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$feed_url&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;or&lt;/span&gt; &lt;a href=&quot;http://perldoc.perl.org/functions/die.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;Failed to fetch feed.&#039;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$rss&lt;/span&gt; = XML::&lt;span style=&quot;color: #006600;&quot;&gt;RSS&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$rss&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;parse&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$feed&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$tt&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;process&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; \&lt;span style=&quot;color: #0000ff;&quot;&gt;$template&lt;/span&gt;, &lt;span style=&quot;color: #0000ff;&quot;&gt;$rss&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;or&lt;/span&gt; &lt;a href=&quot;http://perldoc.perl.org/functions/die.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$tt&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb92546&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;bash&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;bash&quot; id=&quot;cb64089&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;rob@arrakis:~/public_html/rss-reader$ &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;perl&lt;/span&gt; rss-reader.pl &lt;br /&gt;BBC News - Home&lt;br /&gt;&amp;nbsp;&lt;br /&gt;Headlines:&lt;br /&gt;&amp;nbsp;&lt;br /&gt;Fri, &lt;span style=&quot;color: #000000;&quot;&gt;14&lt;/span&gt; Sep &lt;span style=&quot;color: #000000;&quot;&gt;2012&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;12&lt;/span&gt;:&lt;span style=&quot;color: #000000;&quot;&gt;15&lt;/span&gt;:&lt;span style=&quot;color: #000000;&quot;&gt;22&lt;/span&gt; GMT&amp;nbsp; &amp;nbsp;Kate privacy invasion &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;grotesque&#039;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;Fri, &lt;span style=&quot;color: #000000;&quot;&gt;14&lt;/span&gt; Sep &lt;span style=&quot;color: #000000;&quot;&gt;2012&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;11&lt;/span&gt;:&lt;span style=&quot;color: #000000;&quot;&gt;49&lt;/span&gt;:&lt;span style=&quot;color: #000000;&quot;&gt;10&lt;/span&gt; GMT&amp;nbsp; &amp;nbsp;US missions on film protest alert&lt;br /&gt;&amp;nbsp;&lt;br /&gt;Fri, &lt;span style=&quot;color: #000000;&quot;&gt;14&lt;/span&gt; Sep &lt;span style=&quot;color: #000000;&quot;&gt;2012&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;12&lt;/span&gt;:&lt;span style=&quot;color: #000000;&quot;&gt;25&lt;/span&gt;:&lt;span style=&quot;color: #000000;&quot;&gt;25&lt;/span&gt; GMT&amp;nbsp; &amp;nbsp;Alps attack girl returning to UK&lt;br /&gt;&amp;nbsp;&lt;br /&gt;Fri, &lt;span style=&quot;color: #000000;&quot;&gt;14&lt;/span&gt; Sep &lt;span style=&quot;color: #000000;&quot;&gt;2012&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;10&lt;/span&gt;:&lt;span style=&quot;color: #000000;&quot;&gt;17&lt;/span&gt;:&lt;span style=&quot;color: #000000;&quot;&gt;03&lt;/span&gt; GMT&amp;nbsp; &amp;nbsp;Woman is held after car body &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;find&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb30510&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this example the RSS feed and template are defined in code but they can just as easily be defined in files or a database allowing for changes/additions without deploying new code.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/WebDev/perl/taming-rss-feeds-with-xml&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>This is a remarkably simple trick which I've found very handy. With a few lines of Perl you can take any RSS feed and format it to your liking.</p>
<h1></h1>
<h1>Get the Feed</h1>
<p>You can do this using <a href="http://search.cpan.org/~gaas/libwww-perl-6.04/lib/LWP/Simple.pm">LWP::Simple</a>:</p>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb43728'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb43728','cb14465'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb43728" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> LWP::<span style="color: #006600;">Simple</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$feed_url</span> = <span style="color: #ff0000;">'http://feeds.bbci.co.uk/news/rss.xml'</span>;</li><li style="" class="li2"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$feed</span> = get<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$feed_url</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Failed to fetch feed.&quot;</span><span style="color: #66cc66;">&#41;</span>;</li></ol></div><div id="cb14465" style="display: none; color: red;"></div></div></div>

<h1></h1>
<h1>Process the Raw Result</h1>
<p>Using <a href="http://search.cpan.org/~kellan/XML-RSS-1.05/lib/RSS.pm">XML::RSS</a>, convert the raw feed into a more manageable hash.</p>
<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb76416'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb76416','cb30836'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb76416" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> XML::<span style="color: #006600;">RSS</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$rss</span> = XML::<span style="color: #006600;">RSS</span>-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2"><span style="color: #0000ff;">$rss</span>-&gt;<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$feed</span><span style="color: #66cc66;">&#41;</span>;</li></ol></div><div id="cb30836" style="display: none; color: red;"></div></div></div>

<h1></h1>
<h1>Format to Your Liking</h1>
<p><a href="http://template-toolkit.org/">Template::Toolkit</a> can take in a <a href="http://template-toolkit.org/docs/manual/Syntax.html">template</a> and a hash reference of values to substitute into the template.</p>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb6693'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb6693','cb29038'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb6693" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #808080; font-style: italic;"># Define a template</span></li><li style="" class="li2"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$template</span> = &lt;&lt;<span style="color: #ff0000;">&quot;TEMPLATE&quot;</span>;</li><li style="" class="li1"><span style="color: #66cc66;">&#91;</span>% channel.title %<span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">Headlines:</li><li style="" class="li2"><span style="color: #66cc66;">&#91;</span>% FOREACH item = items %<span style="color: #66cc66;">&#93;</span></li><li style="" class="li1"><span style="color: #66cc66;">&#91;</span>% item.pubDate %<span style="color: #66cc66;">&#93;</span>\t<span style="color: #66cc66;">&#91;</span>% item.title %<span style="color: #66cc66;">&#93;</span></li><li style="" class="li2"><span style="color: #66cc66;">&#91;</span>% END %<span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">TEMPLATE</li></ol></div><div id="cb29038" style="display: none; color: red;"></div></div></div>

<p>This simple template will take the BBC news feed from above and print out a list of headlines with publication dates.</p>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb30978'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb30978','cb45167'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb30978" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$tt</span> = Template-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Failed to load template: $Template::ERROR<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #808080; font-style: italic;"># Combine the template with the processed RSS feed.</span></li><li style="" class="li1"><span style="color: #0000ff;">$tt</span>-&gt;<span style="color: #006600;">process</span> <span style="color: #66cc66;">&#40;</span> \<span style="color: #0000ff;">$template</span>, <span style="color: #0000ff;">$rss</span> <span style="color: #66cc66;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #0000ff;">$tt</span>-&gt;<span style="color: #006600;">error</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li></ol></div><div id="cb45167" style="display: none; color: red;"></div></div></div>

<h1></h1>
<h1>Putting it All Together</h1>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb56648'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb56648','cb95508'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb56648" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl</span></li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">use</span> strict;</li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> warnings;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> XML::<span style="color: #006600;">RSS</span>;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">use</span> LWP::<span style="color: #006600;">Simple</span>;</li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> Template;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #808080; font-style: italic;">##################</span></li><li style="" class="li2"><span style="color: #808080; font-style: italic;"># Configuration:</span></li><li style="" class="li1"><span style="color: #808080; font-style: italic;">#</span></li><li style="" class="li2"><span style="color: #808080; font-style: italic;">##################</span></li><li style="" class="li1"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$feed_url</span> = <span style="color: #ff0000;">'http://feeds.bbci.co.uk/news/rss.xml'</span>;</li><li style="" class="li2"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$template</span> = &lt;&lt;<span style="color: #ff0000;">&quot;TEMPLATE&quot;</span>;</li><li style="" class="li1"><span style="color: #66cc66;">&#91;</span>% channel.title %<span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">Headlines:</li><li style="" class="li2"><span style="color: #66cc66;">&#91;</span>% FOREACH item = items %<span style="color: #66cc66;">&#93;</span></li><li style="" class="li1"><span style="color: #66cc66;">&#91;</span>% item.pubDate %<span style="color: #66cc66;">&#93;</span>\t<span style="color: #66cc66;">&#91;</span>% item.title %<span style="color: #66cc66;">&#93;</span></li><li style="" class="li2"><span style="color: #66cc66;">&#91;</span>% END %<span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">TEMPLATE</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #808080; font-style: italic;">##################</span></li><li style="" class="li2"><span style="color: #808080; font-style: italic;">##################</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$tt</span> = Template-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Failed to load template: $Template::ERROR<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$feed</span> = get<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$feed_url</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Failed to fetch feed.'</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$rss</span> = XML::<span style="color: #006600;">RSS</span>-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1"><span style="color: #0000ff;">$rss</span>-&gt;<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$feed</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000ff;">$tt</span>-&gt;<span style="color: #006600;">process</span> <span style="color: #66cc66;">&#40;</span> \<span style="color: #0000ff;">$template</span>, <span style="color: #0000ff;">$rss</span> <span style="color: #66cc66;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #0000ff;">$tt</span>-&gt;<span style="color: #006600;">error</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li></ol></div><div id="cb95508" style="display: none; color: red;"></div></div></div>

<div class="codebox"><div class="codeheader"><span>bash</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb7830'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb7830','cb6286'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="bash" id="cb7830" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">rob@arrakis:~/public_html/rss-reader$ <span style="color: #c20cb9; font-weight: bold;">perl</span> rss-reader.pl </li><li style="" class="li2">BBC News - Home</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">Headlines:</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">Fri, <span style="color: #000000;">14</span> Sep <span style="color: #000000;">2012</span> <span style="color: #000000;">12</span>:<span style="color: #000000;">15</span>:<span style="color: #000000;">22</span> GMT&nbsp; &nbsp;Kate privacy invasion <span style="color: #ff0000;">'grotesque'</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">Fri, <span style="color: #000000;">14</span> Sep <span style="color: #000000;">2012</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">49</span>:<span style="color: #000000;">10</span> GMT&nbsp; &nbsp;US missions on film protest alert</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">Fri, <span style="color: #000000;">14</span> Sep <span style="color: #000000;">2012</span> <span style="color: #000000;">12</span>:<span style="color: #000000;">25</span>:<span style="color: #000000;">25</span> GMT&nbsp; &nbsp;Alps attack girl returning to UK</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">Fri, <span style="color: #000000;">14</span> Sep <span style="color: #000000;">2012</span> <span style="color: #000000;">10</span>:<span style="color: #000000;">17</span>:<span style="color: #000000;">03</span> GMT&nbsp; &nbsp;Woman is held after car body <span style="color: #c20cb9; font-weight: bold;">find</span></li></ol></div><div id="cb6286" style="display: none; color: red;"></div></div></div>

<p>In this example the RSS feed and template are defined in code but they can just as easily be defined in files or a database allowing for changes/additions without deploying new code.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/WebDev/perl/taming-rss-feeds-with-xml">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/WebDev/perl/taming-rss-feeds-with-xml#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/WebDev/?tempskin=_rss2&#38;disp=comments&#38;p=1828</wfw:commentRss>
		</item>
				<item>
			<title>Using a Perl Singleton to Share Values Between Objects</title>
			<link>http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/using-a-perl-singleton</link>
			<pubDate>Wed, 12 Sep 2012 14:54:00 +0000</pubDate>			<dc:creator>Rob Earl</dc:creator>
			<category domain="main">Server Programming</category>
<category domain="alt">Perl</category>			<guid isPermaLink="false">1825@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;It&#039;s been quite a while since I last posted here. Since my last entry I got a new job,  got married, went on honeymoon (got back yesterday!) and spent quite a lot of time maintaining legacy Perl code.&lt;/p&gt;

&lt;p&gt;One thing I see a lot is sharing objects by passing them to the constructor of a new object. This is fine until you&#039;re dealing with complicated sets of objects within objects. &lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb65715&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/perl&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; strict;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Read some variables out of a configuration file.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$config&lt;/span&gt; = get_config&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$config_file&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Create a new object which contains an object which uses some values from the config.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$object&lt;/span&gt; = MyObject-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$config&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$object&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;do_something&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;a href=&quot;http://perldoc.perl.org/functions/package.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;package&lt;/span&gt;&lt;/a&gt; MyObject;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;sub&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$class&lt;/span&gt; = &lt;a href=&quot;http://perldoc.perl.org/functions/shift.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;shift&lt;/span&gt;&lt;/a&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$config&lt;/span&gt; = &lt;a href=&quot;http://perldoc.perl.org/functions/shift.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;shift&lt;/span&gt;&lt;/a&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Store the config incase we create any objects which need it.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$self&lt;/span&gt; = &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;config&#039;&lt;/span&gt; =&amp;gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$config&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/return.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;return&lt;/span&gt;&lt;/a&gt; &lt;a href=&quot;http://perldoc.perl.org/functions/bless.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;bless&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$self&lt;/span&gt;, &lt;span style=&quot;color: #0000ff;&quot;&gt;$class&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;sub&lt;/span&gt; do_something &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$self&lt;/span&gt; = &lt;a href=&quot;http://perldoc.perl.org/functions/shift.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;shift&lt;/span&gt;&lt;/a&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$object2&lt;/span&gt; = MyObject2-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$self&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;config&#039;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/return.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;return&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$object2&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;do_something_else&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb24964&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;MyObject is now holding a reference to the config object which it doesn&#039;t need directly. This will only get worse if MyObject2 needs another object which requires the config object, etc.&lt;/p&gt;

&lt;p&gt;Instead we could use a singleton object:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb83938&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;a href=&quot;http://perldoc.perl.org/functions/package.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;package&lt;/span&gt;&lt;/a&gt; MyConfigObject;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; strict;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; Carp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$config&lt;/span&gt; = &lt;a href=&quot;http://perldoc.perl.org/functions/undef.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;undef&lt;/span&gt;&lt;/a&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;sub&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;!&lt;span style=&quot;color: #0000ff;&quot;&gt;$config&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$class&lt;/span&gt; = &lt;a href=&quot;http://perldoc.perl.org/functions/shift.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;shift&lt;/span&gt;&lt;/a&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$config_file&lt;/span&gt; = &lt;a href=&quot;http://perldoc.perl.org/functions/shift.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;shift&lt;/span&gt;&lt;/a&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; croak &lt;span style=&quot;color: #b1b100;&quot;&gt;unless&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$config_file&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$config&lt;/span&gt; = &lt;a href=&quot;http://perldoc.perl.org/functions/bless.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;bless&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;, &lt;span style=&quot;color: #0000ff;&quot;&gt;$class&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$config&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;parse_file&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$config_file&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/return.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;return&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$config&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb73693&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The example above changes to:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb81413&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/perl&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; strict;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Create a new object to hold the values from our config file.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$config&lt;/span&gt; = MyConfigObject-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$config_file&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Create a new object which contains an object which uses some values from the config.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$object&lt;/span&gt; = MyObject-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$object&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;do_something&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;a href=&quot;http://perldoc.perl.org/functions/package.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;package&lt;/span&gt;&lt;/a&gt; MyObject;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;sub&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$class&lt;/span&gt; = &lt;a href=&quot;http://perldoc.perl.org/functions/shift.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;shift&lt;/span&gt;&lt;/a&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/return.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;return&lt;/span&gt;&lt;/a&gt; &lt;a href=&quot;http://perldoc.perl.org/functions/bless.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;bless&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;, &lt;span style=&quot;color: #0000ff;&quot;&gt;$class&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;sub&lt;/span&gt; do_something &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$self&lt;/span&gt; = &lt;a href=&quot;http://perldoc.perl.org/functions/shift.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;shift&lt;/span&gt;&lt;/a&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$object2&lt;/span&gt; = MyObject2-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/return.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;return&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$object2&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;do_something_else&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;a href=&quot;http://perldoc.perl.org/functions/package.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;package&lt;/span&gt;&lt;/a&gt; MyObject2;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;sub&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Get the config object&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$config&lt;/span&gt; = MyConfigObject-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Do some stuff with it!&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb12643&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Whenever we need to use a value from the config file, we create a new MyConfigObject and get the instance we already created without having to reparse the config file or maintain complicated links through parent objects.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/using-a-perl-singleton&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>It's been quite a while since I last posted here. Since my last entry I got a new job,  got married, went on honeymoon (got back yesterday!) and spent quite a lot of time maintaining legacy Perl code.</p>

<p>One thing I see a lot is sharing objects by passing them to the constructor of a new object. This is fine until you're dealing with complicated sets of objects within objects. </p>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb75524'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb75524','cb89039'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb75524" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> strict;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #808080; font-style: italic;"># Read some variables out of a configuration file.</span></li><li style="" class="li2"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$config</span> = get_config<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$config_file</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #808080; font-style: italic;"># Create a new object which contains an object which uses some values from the config.</span></li><li style="" class="li1"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$object</span> = MyObject-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$config</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2"><span style="color: #0000ff;">$object</span>-&gt;<span style="color: #006600;">do_something</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> MyObject;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$class</span> = <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$config</span> = <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Store the config incase we create any objects which need it.</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> = <span style="color: #66cc66;">&#123;</span> <span style="color: #ff0000;">'config'</span> =&gt; <span style="color: #0000ff;">$config</span> <span style="color: #66cc66;">&#125;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <a href="http://perldoc.perl.org/functions/bless.html"><span style="color: #000066;">bless</span></a> <span style="color: #0000ff;">$self</span>, <span style="color: #0000ff;">$class</span>;</li><li style="" class="li2"><span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">sub</span> do_something <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> = <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$object2</span> = MyObject2-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$self</span>-&gt;<span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">'config'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #0000ff;">$object2</span>-&gt;<span style="color: #006600;">do_something_else</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2"><span style="color: #66cc66;">&#125;</span></li></ol></div><div id="cb89039" style="display: none; color: red;"></div></div></div>

<p>MyObject is now holding a reference to the config object which it doesn't need directly. This will only get worse if MyObject2 needs another object which requires the config object, etc.</p>

<p>Instead we could use a singleton object:</p>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb90837'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb90837','cb31516'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb90837" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> MyConfigObject;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> strict;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">use</span> Carp;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$config</span> = <a href="http://perldoc.perl.org/functions/undef.html"><span style="color: #000066;">undef</span></a>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>!<span style="color: #0000ff;">$config</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$class</span> = <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$config_file</span> = <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; croak <span style="color: #b1b100;">unless</span> <span style="color: #0000ff;">$config_file</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$config</span> = <a href="http://perldoc.perl.org/functions/bless.html"><span style="color: #000066;">bless</span></a> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #0000ff;">$class</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$config</span>-&gt;<span style="color: #006600;">parse_file</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$config_file</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #0000ff;">$config</span>;</li><li style="" class="li1"><span style="color: #66cc66;">&#125;</span></li></ol></div><div id="cb31516" style="display: none; color: red;"></div></div></div>

<p>The example above changes to:</p>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb81379'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb81379','cb20730'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb81379" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> strict;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #808080; font-style: italic;"># Create a new object to hold the values from our config file.</span></li><li style="" class="li2"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$config</span> = MyConfigObject-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$config_file</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #808080; font-style: italic;"># Create a new object which contains an object which uses some values from the config.</span></li><li style="" class="li1"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$object</span> = MyObject-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2"><span style="color: #0000ff;">$object</span>-&gt;<span style="color: #006600;">do_something</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> MyObject;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$class</span> = <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <a href="http://perldoc.perl.org/functions/bless.html"><span style="color: #000066;">bless</span></a> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #0000ff;">$class</span>;</li><li style="" class="li2"><span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">sub</span> do_something <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> = <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$object2</span> = MyObject2-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #0000ff;">$object2</span>-&gt;<span style="color: #006600;">do_something_else</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2"><span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> MyObject2;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Get the config object</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$config</span> = MyConfigObject-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Do some stuff with it!</span></li><li style="" class="li1"><span style="color: #66cc66;">&#125;</span></li></ol></div><div id="cb20730" style="display: none; color: red;"></div></div></div>

<p>Whenever we need to use a value from the config file, we create a new MyConfigObject and get the instance we already created without having to reparse the config file or maintain complicated links through parent objects.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/using-a-perl-singleton">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/using-a-perl-singleton#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/WebDev/?tempskin=_rss2&#38;disp=comments&#38;p=1825</wfw:commentRss>
		</item>
				<item>
			<title>Catching Key Conflicts with Perl's Class::DBI</title>
			<link>http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/catching-key-conflicts-with-perl</link>
			<pubDate>Mon, 31 Jan 2011 09:52:00 +0000</pubDate>			<dc:creator>Rob Earl</dc:creator>
			<category domain="main">Server Programming</category>			<guid isPermaLink="false">1094@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;When inserting to a database I find it more convenient to catch a key conflict than to perform a select in order to check for duplicates. Defining a column as unique will ensure data integrity at the lowest level.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;sql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;sql&quot; id=&quot;cb35435&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;TABLE&lt;/span&gt; customer &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;id int &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;AUTO_INCREMENT&lt;/span&gt;, &lt;br /&gt;name varchar&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;br /&gt;address varchar&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;br /&gt;email varchar&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;br /&gt;constraint &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;PRIMARY&lt;/span&gt; &lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;KEY&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;id&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;br /&gt;&lt;span style=&quot;color: #993333; font-weight: bold;&quot;&gt;UNIQUE&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;email&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb50198&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Catching a user attempting to register with an email address which is already in the database is fairly simple when using DBI:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb7246&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$row&lt;/span&gt; = &lt;span style=&quot;color: #0000ff;&quot;&gt;$dbh&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;do&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://perldoc.perl.org/functions/qq.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;qq&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;INSERT INTO customer &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; name, address, email &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; VALUES &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;?,?,?&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;, &lt;a href=&quot;http://perldoc.perl.org/functions/undef.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;undef&lt;/span&gt;&lt;/a&gt;, &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Rob&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Rob&#039;s House&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;my&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\@&lt;/span&gt;email.com&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Returns undef on error.&lt;/span&gt;&lt;br /&gt;&amp;amp;error&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$dbh&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;errstr&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #b1b100;&quot;&gt;unless&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$row&lt;/span&gt;; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Forward to error handler.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb4626&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href=&quot;http://search.cpan.org/~tmtm/Class-DBI-v3.0.17/lib/Class/DBI.pm&quot;&gt;Class::DBI&lt;/a&gt; is a database abstraction Perl module. It wasn&#039;t immediately apparent to me how to handle a key conflict when using it. After a little investigation I found key violations will trigger an exception which makes it fairly simple to handle:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb10718&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;a href=&quot;http://perldoc.perl.org/functions/eval.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;eval&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$new_cust&lt;/span&gt; = Customer-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;insert&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;name=&amp;gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Rob&amp;quot;&lt;/span&gt;, address=&amp;gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Rob&#039;s House&amp;quot;&lt;/span&gt;, email=&amp;gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;my&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\@&lt;/span&gt;email.com&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Insert using Class::DBI constructor.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;; &amp;amp;error&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;$@&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; $@; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Forward to error handler.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb51801&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The eval block will catch the thrown exception, allowing you to gracefully handle it through your own subroutine.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/catching-key-conflicts-with-perl&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>When inserting to a database I find it more convenient to catch a key conflict than to perform a select in order to check for duplicates. Defining a column as unique will ensure data integrity at the lowest level.</p>

<div class="codebox"><div class="codeheader"><span>sql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb34263'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb34263','cb94428'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="sql" id="cb34263" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> customer <span style="color: #66cc66;">&#40;</span></li><li style="" class="li2">id int <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span>, </li><li style="" class="li1">name varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>, </li><li style="" class="li2">address varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>, </li><li style="" class="li1">email varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>, </li><li style="" class="li2">constraint <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>, </li><li style="" class="li1"><span style="color: #993333; font-weight: bold;">UNIQUE</span><span style="color: #66cc66;">&#40;</span>email<span style="color: #66cc66;">&#41;</span></li><li style="" class="li2"><span style="color: #66cc66;">&#41;</span>;</li></ol></div><div id="cb94428" style="display: none; color: red;"></div></div></div><p> </p>

<p>Catching a user attempting to register with an email address which is already in the database is fairly simple when using DBI:</p>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb63214'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb63214','cb43344'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb63214" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$row</span> = <span style="color: #0000ff;">$dbh</span>-&gt;<span style="color: #006600;">do</span><span style="color: #66cc66;">&#40;</span><a href="http://perldoc.perl.org/functions/qq.html"><span style="color: #000066;">qq</span></a><span style="color: #66cc66;">&#123;</span>INSERT INTO customer <span style="color: #66cc66;">&#40;</span> name, address, email <span style="color: #66cc66;">&#41;</span> VALUES <span style="color: #66cc66;">&#40;</span>?,?,?<span style="color: #66cc66;">&#41;</span>;<span style="color: #66cc66;">&#125;</span>, <a href="http://perldoc.perl.org/functions/undef.html"><span style="color: #000066;">undef</span></a>, <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Rob&quot;</span>, <span style="color: #ff0000;">&quot;Rob's House&quot;</span>, <span style="color: #ff0000;">&quot;my<span style="color: #000099; font-weight: bold;">\@</span>email.com&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;"># Returns undef on error.</span></li><li style="" class="li2">&amp;error<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$dbh</span>-&gt;<span style="color: #006600;">errstr</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">unless</span> <span style="color: #0000ff;">$row</span>; <span style="color: #808080; font-style: italic;"># Forward to error handler.</span></li></ol></div><div id="cb43344" style="display: none; color: red;"></div></div></div>

<p><a href="http://search.cpan.org/~tmtm/Class-DBI-v3.0.17/lib/Class/DBI.pm">Class::DBI</a> is a database abstraction Perl module. It wasn't immediately apparent to me how to handle a key conflict when using it. After a little investigation I found key violations will trigger an exception which makes it fairly simple to handle:</p>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb73564'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb73564','cb72863'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb73564" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><a href="http://perldoc.perl.org/functions/eval.html"><span style="color: #000066;">eval</span></a></li><li style="" class="li2"><span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$new_cust</span> = Customer-&gt;<span style="color: #006600;">insert</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>name=&gt;<span style="color: #ff0000;">&quot;Rob&quot;</span>, address=&gt;<span style="color: #ff0000;">&quot;Rob's House&quot;</span>, email=&gt;<span style="color: #ff0000;">&quot;my<span style="color: #000099; font-weight: bold;">\@</span>email.com&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;"># Insert using Class::DBI constructor.</span></li><li style="" class="li2"><span style="color: #66cc66;">&#125;</span>; &amp;error<span style="color: #66cc66;">&#40;</span>$@<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">if</span> $@; <span style="color: #808080; font-style: italic;"># Forward to error handler.</span></li></ol></div><div id="cb72863" style="display: none; color: red;"></div></div></div>

<p>The eval block will catch the thrown exception, allowing you to gracefully handle it through your own subroutine.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/catching-key-conflicts-with-perl">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/catching-key-conflicts-with-perl#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/WebDev/?tempskin=_rss2&#38;disp=comments&#38;p=1094</wfw:commentRss>
		</item>
				<item>
			<title>Writing Perl Tests with Test::More</title>
			<link>http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/writing-perl-tests-with-test</link>
			<pubDate>Sat, 15 Jan 2011 14:29:00 +0000</pubDate>			<dc:creator>Rob Earl</dc:creator>
			<category domain="main">Server Programming</category>			<guid isPermaLink="false">1073@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;Writing tests for code is just as important as writing the code itself. Time spent writing tests is less time spent tracking down and fixing bugs, making it a great investment. Despite knowing this it can sometimes be hard to force yourself to stop coding and write tests. Fortunately, Perl has some modules to make it pretty simple. Here&#039;s the module we&#039;re going to be testing:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb40591&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/perl&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;a href=&quot;http://perldoc.perl.org/functions/package.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;package&lt;/span&gt;&lt;/a&gt; MyMaths;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; strict;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;sub&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$proto&lt;/span&gt; = &lt;a href=&quot;http://perldoc.perl.org/functions/shift.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;shift&lt;/span&gt;&lt;/a&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$class&lt;/span&gt; = &lt;a href=&quot;http://perldoc.perl.org/functions/ref.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;ref&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$proto&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; || &lt;span style=&quot;color: #0000ff;&quot;&gt;$proto&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$self&lt;/span&gt; = &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/bless.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;bless&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$self&lt;/span&gt;, &lt;span style=&quot;color: #0000ff;&quot;&gt;$class&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/return.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;return&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$self&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;sub&lt;/span&gt; add&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$self&lt;/span&gt; = &lt;a href=&quot;http://perldoc.perl.org/functions/shift.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;shift&lt;/span&gt;&lt;/a&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$total&lt;/span&gt; = &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;foreach&lt;/span&gt; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$factor&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;@_&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$total&lt;/span&gt; += &lt;span style=&quot;color: #0000ff;&quot;&gt;$factor&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/return.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;return&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$total&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb7307&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you can see, we have a simple class with one method, add, which adds together all the arguments passed to it. We now need to define a test file for this module, called mymaths.t:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb59468&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/perl&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; Test::&lt;span style=&quot;color: #006600;&quot;&gt;More&lt;/span&gt; tests=&amp;gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;6&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; MyMaths;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$mymaths&lt;/span&gt; = &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; MyMaths;&lt;br /&gt;is&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$mymaths&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt;,&lt;span style=&quot;color: #cc66cc;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;span style=&quot;color: #cc66cc;&quot;&gt;6&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;1 + 2 + 3 = 6&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;is&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$mymaths&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;6&lt;/span&gt;,&lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;span style=&quot;color: #cc66cc;&quot;&gt;8&lt;/span&gt;,&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;6 + 2 = 8&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;is&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$mymaths&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt;,&lt;span style=&quot;color: #cc66cc;&quot;&gt;3&lt;/span&gt;,&lt;span style=&quot;color: #cc66cc;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;span style=&quot;color: #cc66cc;&quot;&gt;10&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;1 + 2 + 3 + 4 = 10&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;is&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$mymaths&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;span style=&quot;color: #cc66cc;&quot;&gt;3&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;1 + 2 = 3&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;is&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$mymaths&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;2 = 2&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;is&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$mymaths&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt;,&lt;span style=&quot;color: #cc66cc;&quot;&gt;-1&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;2 + -1 = 1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb99050&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This test file uses the is() method of the builtin Test::More module. This method takes 3 arguments: the test, the expected result and a description. If a test fails, is() will give some feedback on the test.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;edit:&lt;/strong&gt; To run the tests use the `prove` utility which is part of the Test::Harness package:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;codespan&quot;&gt;prove -v mymaths.t&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Run all tests in the current directory with:&lt;br /&gt;
&lt;code class=&quot;codespan&quot;&gt;prove -v .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;del cite=&quot;http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/writing-perl-tests-with-test#c7568&quot;&gt;To run the tests we use another builtin module, Test::Harness. Create tests.pl:&lt;/del&gt;&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb47757&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/perl&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; Test::&lt;span style=&quot;color: #006600;&quot;&gt;Harness&lt;/span&gt; &lt;a href=&quot;http://perldoc.perl.org/functions/qw.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;qw&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&amp;amp;runtests&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;@tests&lt;/span&gt; = &lt;span style=&quot;color: #0000ff;&quot;&gt;@ARGV&lt;/span&gt; ? &lt;span style=&quot;color: #0000ff;&quot;&gt;@ARGV&lt;/span&gt; : &amp;lt;*.t&amp;gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;runtests &lt;span style=&quot;color: #0000ff;&quot;&gt;@tests&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb3606&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;del&gt;This script can either be run with a list of test files as arguments, or with no arguments to run all test files. Running the script results in:&lt;/del&gt;&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb7547&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;./mymaths.t .. &lt;br /&gt;1..6&lt;br /&gt;ok 1 - 1 + 2 + 3 = 6&lt;br /&gt;ok 2 - 6 + 2 = 8&lt;br /&gt;ok 3 - 1 + 2 + 3 + 4 = 10&lt;br /&gt;ok 4 - 1 + 2 = 3&lt;br /&gt;ok 5 - 2 = 2&lt;br /&gt;ok 6 - 2 + -1 = 1&lt;br /&gt;ok&lt;br /&gt;All tests successful.&lt;br /&gt;Files=1, Tests=6, &amp;nbsp;0 wallclock secs ( 0.01 usr &amp;nbsp;0.02 sys + &amp;nbsp;0.02 cusr &amp;nbsp;0.01 csys = &amp;nbsp;0.06 CPU)&lt;br /&gt;Result: PASS&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb88870&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Everything went fine, as expected. Now go back to the original class definition and make a typo on line 22, so it reads = instead of += and rerun the tests:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb63940&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;./mymaths.t .. &lt;br /&gt;1..6&lt;br /&gt;not ok 1 - 1 + 2 + 3 = 6&lt;br /&gt;not ok 2 - 6 + 2 = 8&lt;br /&gt;not ok 3 - 1 + 2 + 3 + 4 = 10&lt;br /&gt;not ok 4 - 1 + 2 = 3&lt;br /&gt;ok 5 - 2 = 2&lt;br /&gt;not ok 6 - 2 + -1 = 1&lt;br /&gt;&amp;nbsp;&lt;br /&gt;# &amp;nbsp; Failed test &#039;1 + 2 + 3 = 6&#039;&lt;br /&gt;# &amp;nbsp; at ./mymaths.t line 7.&lt;br /&gt;# &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;got: &#039;3&#039;&lt;br /&gt;# &amp;nbsp; &amp;nbsp; expected: &#039;6&#039;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;# &amp;nbsp; Failed test &#039;6 + 2 = 8&#039;&lt;br /&gt;# &amp;nbsp; at ./mymaths.t line 8.&lt;br /&gt;# &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;got: &#039;2&#039;&lt;br /&gt;# &amp;nbsp; &amp;nbsp; expected: &#039;8&#039;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;# &amp;nbsp; Failed test &#039;1 + 2 + 3 + 4 = 10&#039;&lt;br /&gt;# &amp;nbsp; at ./mymaths.t line 9.&lt;br /&gt;# &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;got: &#039;4&#039;&lt;br /&gt;# &amp;nbsp; &amp;nbsp; expected: &#039;10&#039;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;# &amp;nbsp; Failed test &#039;1 + 2 = 3&#039;&lt;br /&gt;# &amp;nbsp; at ./mymaths.t line 10.&lt;br /&gt;# &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;got: &#039;2&#039;&lt;br /&gt;# &amp;nbsp; &amp;nbsp; expected: &#039;3&#039;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;# &amp;nbsp; Failed test &#039;2 + -1 = 1&#039;&lt;br /&gt;# &amp;nbsp; at ./mymaths.t line 12.&lt;br /&gt;# &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;got: &#039;-1&#039;&lt;br /&gt;# &amp;nbsp; &amp;nbsp; expected: &#039;1&#039;&lt;br /&gt;# Looks like you failed 5 tests of 6.&lt;br /&gt;Dubious, test returned 5 (wstat 1280, 0x500)&lt;br /&gt;Failed 5/6 subtests &lt;br /&gt;&amp;nbsp;&lt;br /&gt;Test Summary Report&lt;br /&gt;-------------------&lt;br /&gt;./mymaths.t (Wstat: 1280 Tests: 6 Failed: 5)&lt;br /&gt;&amp;nbsp; Failed tests: &amp;nbsp;1-4, 6&lt;br /&gt;&amp;nbsp; Non-zero exit status: 5&lt;br /&gt;Files=1, Tests=6, &amp;nbsp;0 wallclock secs ( 0.03 usr &amp;nbsp;0.00 sys + &amp;nbsp;0.03 cusr &amp;nbsp;0.00 csys = &amp;nbsp;0.06 CPU)&lt;br /&gt;Result: FAIL&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb48637&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Most of the tests failed and gave good feedback to help find the problem.&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;Although writing tests can be a chore it is worth doing. If you write tests a little at a time:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When you design a class.&lt;/li&gt;
&lt;li&gt;When you add a method to a class.&lt;/li&gt;
&lt;li&gt;When you find a bug.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You&#039;ll develop a good library of tests without too much effort.&lt;/p&gt;

&lt;p&gt;Further reading: &lt;a href=&quot;http://perldoc.perl.org/Test/More.html&quot;&gt;Test::More perldoc&lt;/a&gt;&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/writing-perl-tests-with-test&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Writing tests for code is just as important as writing the code itself. Time spent writing tests is less time spent tracking down and fixing bugs, making it a great investment. Despite knowing this it can sometimes be hard to force yourself to stop coding and write tests. Fortunately, Perl has some modules to make it pretty simple. Here's the module we're going to be testing:</p>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb98303'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb98303','cb50826'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb98303" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> MyMaths;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">use</span> strict;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000000; font-weight: bold;">new</span></li><li style="" class="li1"><span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$proto</span> = <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$class</span> = <a href="http://perldoc.perl.org/functions/ref.html"><span style="color: #000066;">ref</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$proto</span><span style="color: #66cc66;">&#41;</span> || <span style="color: #0000ff;">$proto</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/bless.html"><span style="color: #000066;">bless</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$self</span>, <span style="color: #0000ff;">$class</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #0000ff;">$self</span>;</li><li style="" class="li2"><span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">sub</span> add</li><li style="" class="li1"><span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> = <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$total</span> = <span style="color: #cc66cc;">0</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$factor</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">@_</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$total</span> += <span style="color: #0000ff;">$factor</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #0000ff;">$total</span>;</li><li style="" class="li1"><span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #cc66cc;">1</span>;</li></ol></div><div id="cb50826" style="display: none; color: red;"></div></div></div>

<p>As you can see, we have a simple class with one method, add, which adds together all the arguments passed to it. We now need to define a test file for this module, called mymaths.t:</p>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb34056'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb34056','cb92778'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb34056" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> Test::<span style="color: #006600;">More</span> tests=&gt;<span style="color: #cc66cc;">6</span>;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">use</span> MyMaths;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000ff;">$mymaths</span> = <span style="color: #000000; font-weight: bold;">new</span> MyMaths;</li><li style="" class="li1">is<span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$mymaths</span>-&gt;<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;">6</span>, <span style="color: #ff0000;">&quot;1 + 2 + 3 = 6&quot;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">is<span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$mymaths</span>-&gt;<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;">8</span>,<span style="color: #ff0000;">&quot;6 + 2 = 8&quot;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">is<span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$mymaths</span>-&gt;<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;">10</span>, <span style="color: #ff0000;">&quot;1 + 2 + 3 + 4 = 10&quot;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">is<span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$mymaths</span>-&gt;<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #ff0000;">&quot;1 + 2 = 3&quot;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">is<span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$mymaths</span>-&gt;<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #ff0000;">&quot;2 = 2&quot;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">is<span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$mymaths</span>-&gt;<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">-1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #ff0000;">&quot;2 + -1 = 1&quot;</span><span style="color: #66cc66;">&#41;</span>;</li></ol></div><div id="cb92778" style="display: none; color: red;"></div></div></div>

<p>This test file uses the is() method of the builtin Test::More module. This method takes 3 arguments: the test, the expected result and a description. If a test fails, is() will give some feedback on the test.</p>

<p><strong>edit:</strong> To run the tests use the `prove` utility which is part of the Test::Harness package:</p>

<p><code class="codespan">prove -v mymaths.t</code></p>

<p>Run all tests in the current directory with:<br />
<code class="codespan">prove -v .</code></p>

<p><del cite="http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/writing-perl-tests-with-test#c7568">To run the tests we use another builtin module, Test::Harness. Create tests.pl:</del></p>

<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb69787'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb69787','cb29238'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb69787" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> Test::<span style="color: #006600;">Harness</span> <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #66cc66;">&#40;</span>&amp;runtests<span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000ff;">@tests</span> = <span style="color: #0000ff;">@ARGV</span> ? <span style="color: #0000ff;">@ARGV</span> : &lt;*.t&gt;;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">runtests <span style="color: #0000ff;">@tests</span>;</li></ol></div><div id="cb29238" style="display: none; color: red;"></div></div></div>

<p><del>This script can either be run with a list of test files as arguments, or with no arguments to run all test files. Running the script results in:</del></p>

<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb92309'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb92309','cb48435'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb92309" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">./mymaths.t .. </li><li style="" class="li2">1..6</li><li style="" class="li1">ok 1 - 1 + 2 + 3 = 6</li><li style="" class="li2">ok 2 - 6 + 2 = 8</li><li style="" class="li1">ok 3 - 1 + 2 + 3 + 4 = 10</li><li style="" class="li2">ok 4 - 1 + 2 = 3</li><li style="" class="li1">ok 5 - 2 = 2</li><li style="" class="li2">ok 6 - 2 + -1 = 1</li><li style="" class="li1">ok</li><li style="" class="li2">All tests successful.</li><li style="" class="li1">Files=1, Tests=6, &nbsp;0 wallclock secs ( 0.01 usr &nbsp;0.02 sys + &nbsp;0.02 cusr &nbsp;0.01 csys = &nbsp;0.06 CPU)</li><li style="" class="li2">Result: PASS</li></ol></div><div id="cb48435" style="display: none; color: red;"></div></div></div>

<p>Everything went fine, as expected. Now go back to the original class definition and make a typo on line 22, so it reads = instead of += and rerun the tests:</p>

<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb70718'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb70718','cb27745'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb70718" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">./mymaths.t .. </li><li style="" class="li2">1..6</li><li style="" class="li1">not ok 1 - 1 + 2 + 3 = 6</li><li style="" class="li2">not ok 2 - 6 + 2 = 8</li><li style="" class="li1">not ok 3 - 1 + 2 + 3 + 4 = 10</li><li style="" class="li2">not ok 4 - 1 + 2 = 3</li><li style="" class="li1">ok 5 - 2 = 2</li><li style="" class="li2">not ok 6 - 2 + -1 = 1</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"># &nbsp; Failed test '1 + 2 + 3 = 6'</li><li style="" class="li1"># &nbsp; at ./mymaths.t line 7.</li><li style="" class="li2"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;got: '3'</li><li style="" class="li1"># &nbsp; &nbsp; expected: '6'</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"># &nbsp; Failed test '6 + 2 = 8'</li><li style="" class="li2"># &nbsp; at ./mymaths.t line 8.</li><li style="" class="li1"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;got: '2'</li><li style="" class="li2"># &nbsp; &nbsp; expected: '8'</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"># &nbsp; Failed test '1 + 2 + 3 + 4 = 10'</li><li style="" class="li1"># &nbsp; at ./mymaths.t line 9.</li><li style="" class="li2"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;got: '4'</li><li style="" class="li1"># &nbsp; &nbsp; expected: '10'</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"># &nbsp; Failed test '1 + 2 = 3'</li><li style="" class="li2"># &nbsp; at ./mymaths.t line 10.</li><li style="" class="li1"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;got: '2'</li><li style="" class="li2"># &nbsp; &nbsp; expected: '3'</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"># &nbsp; Failed test '2 + -1 = 1'</li><li style="" class="li1"># &nbsp; at ./mymaths.t line 12.</li><li style="" class="li2"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;got: '-1'</li><li style="" class="li1"># &nbsp; &nbsp; expected: '1'</li><li style="" class="li2"># Looks like you failed 5 tests of 6.</li><li style="" class="li1">Dubious, test returned 5 (wstat 1280, 0x500)</li><li style="" class="li2">Failed 5/6 subtests </li><li style="" class="li1">&nbsp;</li><li style="" class="li2">Test Summary Report</li><li style="" class="li1">-------------------</li><li style="" class="li2">./mymaths.t (Wstat: 1280 Tests: 6 Failed: 5)</li><li style="" class="li1">&nbsp; Failed tests: &nbsp;1-4, 6</li><li style="" class="li2">&nbsp; Non-zero exit status: 5</li><li style="" class="li1">Files=1, Tests=6, &nbsp;0 wallclock secs ( 0.03 usr &nbsp;0.00 sys + &nbsp;0.03 cusr &nbsp;0.00 csys = &nbsp;0.06 CPU)</li><li style="" class="li2">Result: FAIL</li></ol></div><div id="cb27745" style="display: none; color: red;"></div></div></div>

<p>Most of the tests failed and gave good feedback to help find the problem.</p>

<h2>Summary</h2>

<p>Although writing tests can be a chore it is worth doing. If you write tests a little at a time:</p>
<ul>
<li>When you design a class.</li>
<li>When you add a method to a class.</li>
<li>When you find a bug.</li>
</ul>

<p>You'll develop a good library of tests without too much effort.</p>

<p>Further reading: <a href="http://perldoc.perl.org/Test/More.html">Test::More perldoc</a></p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/writing-perl-tests-with-test">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/writing-perl-tests-with-test#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/WebDev/?tempskin=_rss2&#38;disp=comments&#38;p=1073</wfw:commentRss>
		</item>
				<item>
			<title>Calling Web Services from JSP</title>
			<link>http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/JSP/calling-web-services-from-jsp</link>
			<pubDate>Sat, 18 Dec 2010 14:40:51 +0000</pubDate>			<dc:creator>Rob Earl</dc:creator>
			<category domain="main">JSP / Servlets</category>			<guid isPermaLink="false">1049@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;This blog will demonstrate the steps to generate a Web Service Client and test JSP files in Eclipse. Finally, it will show a basic JSP example which uses the generated client to call a remote Web Service.&lt;/p&gt;

&lt;h2&gt;Setup Tomcat&lt;/h2&gt;

&lt;p&gt;Download and extract &lt;a href=&quot;http://tomcat.apache.org/download-60.cgi&quot;&gt;Tomcat 6.0&lt;/a&gt;. If you&#039;re using Ubuntu and want to use the system installed tomcat, follow &lt;a href=&quot;http://ubuntuforums.org/showthread.php?p=8541057&quot;&gt;these instructions&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Create an Eclipse Project&lt;/h2&gt;

&lt;p&gt;Open Eclipse and create a new Dynamic Web Project:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/users/robearl/DynamicWebProject.png&quot; alt=&quot;&quot; title=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Give your project a name (JSPWSClient will do) and click &quot;New&quot; to add a New Server Runtime Environment. Select Apache Tomcat v6.0 and locate the directory you extracted to earlier. &lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/users/robearl/Tomcat.png&quot; alt=&quot;&quot; title=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2&gt;Create Web Service Client&lt;/h2&gt;

&lt;p&gt;The client will use the Local Time Web Service from &lt;a href=&quot;http://ripedevelopment.com/testService.aspx&quot;&gt;http://ripedevelopment.com/testService.aspx&lt;/a&gt;. Set the Service definition for the client to &lt;a href=&quot;http://www.ripedevelopment.com/webservices/LocalTime.asmx?WSDL&quot;&gt;http://www.ripedevelopment.com/webservices/LocalTime.asmx?WSDL&lt;/a&gt; and drag the slider on the left to the top (Test Client). &lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/users/robearl/WebServiceClient.png&quot; alt=&quot;&quot; title=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If the server isn&#039;t already running the wizard will prompt you to &quot;Start server&quot;, do this and, when prompted to generate a Web Service Client Test, accept the defaults to use Web Service Sample JSPs.&lt;/p&gt;

&lt;p&gt;Eclipse will then generate a Web Service client and sample JSP files to test it. Once they are generated Eclipse will open the sample JSP client. Using this page the service methods can be tested to ensure connectivity with the service. Examining the source of these files in Project-&gt;WebContent-&gt;sampleLocalTimeSoapProxy can show us the methods we need to use. &lt;/p&gt;

&lt;p&gt;Below is an example JSP file which will call the Web Service Client and lookup the current time in New York.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;java&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;java&quot; id=&quot;cb61909&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&amp;lt;%@ page language=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;java&amp;quot;&lt;/span&gt; contentType=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;text/html; charset=UTF-8&amp;quot;&lt;/span&gt; pageEncoding=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;UTF-8&amp;quot;&lt;/span&gt;%&amp;gt;&lt;br /&gt;&amp;lt;!DOCTYPE html &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;PUBLIC&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;-//W3C//DTD HTML 4.01 Transitional//EN&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;http://www.w3.org/TR/html4/loose.dtd&amp;quot;&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;meta http-equiv=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Content-Type&amp;quot;&lt;/span&gt; content=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;text/html; charset=UTF-8&amp;quot;&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;lt;title&amp;gt;The &lt;a href=&quot;http://www.google.com/search?hl=en&amp;amp;q=allinurl%3ATime+java.sun.com&amp;amp;btnI=I%27m%20Feeling%20Lucky&quot;&gt;&lt;span style=&quot;color: #aaaadd; font-weight: bold;&quot;&gt;Time&lt;/span&gt;&lt;/a&gt; in &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;New&lt;/span&gt; York!&amp;lt;/title&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;&amp;lt;jsp:useBean id=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;localTimeSoapProxy&amp;quot;&lt;/span&gt; scope=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;session&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;class&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;com.ripedev.www.LocalTimeSoapProxy&amp;quot;&lt;/span&gt; /&amp;gt;&lt;br /&gt;&amp;lt;%&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;try&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://www.google.com/search?hl=en&amp;amp;q=allinurl%3AString+java.sun.com&amp;amp;btnI=I%27m%20Feeling%20Lucky&quot;&gt;&lt;span style=&quot;color: #aaaadd; font-weight: bold;&quot;&gt;String&lt;/span&gt;&lt;/a&gt; newYorkTime = localTimeSoapProxy.&lt;span style=&quot;color: #006600;&quot;&gt;localTimeByZipCode&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;10001&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Pass a New York zipcode.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; %&amp;gt;The &lt;a href=&quot;http://www.google.com/search?hl=en&amp;amp;q=allinurl%3ATime+java.sun.com&amp;amp;btnI=I%27m%20Feeling%20Lucky&quot;&gt;&lt;span style=&quot;color: #aaaadd; font-weight: bold;&quot;&gt;Time&lt;/span&gt;&lt;/a&gt; in &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;New&lt;/span&gt; York is: &amp;lt;%=newYorkTime %&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;lt;%&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;catch&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?hl=en&amp;amp;q=allinurl%3AException+java.sun.com&amp;amp;btnI=I%27m%20Feeling%20Lucky&quot;&gt;&lt;span style=&quot;color: #aaaadd; font-weight: bold;&quot;&gt;Exception&lt;/span&gt;&lt;/a&gt; e&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;%&amp;gt; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://www.google.com/search?hl=en&amp;amp;q=allinurl%3AException+java.sun.com&amp;amp;btnI=I%27m%20Feeling%20Lucky&quot;&gt;&lt;span style=&quot;color: #aaaadd; font-weight: bold;&quot;&gt;Exception&lt;/span&gt;&lt;/a&gt;: &amp;lt;%=e.&lt;span style=&quot;color: #006600;&quot;&gt;getMessage&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; %&amp;gt;&lt;br /&gt;&amp;lt;%&amp;nbsp; &lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;%&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb39820&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Save this file to WebContent/NewYorkTime.jsp then navigate to &lt;a href=&quot;http://localhost:8080/NewYorkTime.jsp&quot;&gt;http://localhost:8080/NewYorkTime.jsp&lt;/a&gt; and you should see something like:&lt;/p&gt;

&lt;p&gt;The Time in New York is: 12/18/2010 11:26:22 AM&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/JSP/calling-web-services-from-jsp&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>This blog will demonstrate the steps to generate a Web Service Client and test JSP files in Eclipse. Finally, it will show a basic JSP example which uses the generated client to call a remote Web Service.</p>

<h2>Setup Tomcat</h2>

<p>Download and extract <a href="http://tomcat.apache.org/download-60.cgi">Tomcat 6.0</a>. If you're using Ubuntu and want to use the system installed tomcat, follow <a href="http://ubuntuforums.org/showthread.php?p=8541057">these instructions</a>.</p>

<h2>Create an Eclipse Project</h2>

<p>Open Eclipse and create a new Dynamic Web Project:</p>

<p><img src="http://blogs.lessthandot.com/media/users/robearl/DynamicWebProject.png" alt="" title="" /></p>

<p>Give your project a name (JSPWSClient will do) and click "New" to add a New Server Runtime Environment. Select Apache Tomcat v6.0 and locate the directory you extracted to earlier. </p>

<p><img src="http://blogs.lessthandot.com/media/users/robearl/Tomcat.png" alt="" title="" /></p>

<h2>Create Web Service Client</h2>

<p>The client will use the Local Time Web Service from <a href="http://ripedevelopment.com/testService.aspx">http://ripedevelopment.com/testService.aspx</a>. Set the Service definition for the client to <a href="http://www.ripedevelopment.com/webservices/LocalTime.asmx?WSDL">http://www.ripedevelopment.com/webservices/LocalTime.asmx?WSDL</a> and drag the slider on the left to the top (Test Client). </p>

<p><img src="http://blogs.lessthandot.com/media/users/robearl/WebServiceClient.png" alt="" title="" /></p>

<p>If the server isn't already running the wizard will prompt you to "Start server", do this and, when prompted to generate a Web Service Client Test, accept the defaults to use Web Service Sample JSPs.</p>

<p>Eclipse will then generate a Web Service client and sample JSP files to test it. Once they are generated Eclipse will open the sample JSP client. Using this page the service methods can be tested to ensure connectivity with the service. Examining the source of these files in Project->WebContent->sampleLocalTimeSoapProxy can show us the methods we need to use. </p>

<p>Below is an example JSP file which will call the Web Service Client and lookup the current time in New York.</p>

<div class="codebox"><div class="codeheader"><span>java</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb32418'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb32418','cb60543'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="java" id="cb32418" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">&lt;%@ page language=<span style="color: #ff0000;">&quot;java&quot;</span> contentType=<span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span> pageEncoding=<span style="color: #ff0000;">&quot;UTF-8&quot;</span>%&gt;</li><li style="" class="li2">&lt;!DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #ff0000;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span> <span style="color: #ff0000;">&quot;http://www.w3.org/TR/html4/loose.dtd&quot;</span>&gt;</li><li style="" class="li1">&lt;html&gt;</li><li style="" class="li2">&lt;head&gt;</li><li style="" class="li1">&lt;meta http-equiv=<span style="color: #ff0000;">&quot;Content-Type&quot;</span> content=<span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span>&gt;</li><li style="" class="li2">&lt;title&gt;The <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ATime+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Time</span></a> in <span style="color: #000000; font-weight: bold;">New</span> York!&lt;/title&gt;</li><li style="" class="li1">&lt;/head&gt;</li><li style="" class="li2">&lt;body&gt;</li><li style="" class="li1">&lt;jsp:useBean id=<span style="color: #ff0000;">&quot;localTimeSoapProxy&quot;</span> scope=<span style="color: #ff0000;">&quot;session&quot;</span> <span style="color: #000000; font-weight: bold;">class</span>=<span style="color: #ff0000;">&quot;com.ripedev.www.LocalTimeSoapProxy&quot;</span> /&gt;</li><li style="" class="li2">&lt;%</li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">try</span></li><li style="" class="li2"><span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> newYorkTime = localTimeSoapProxy.<span style="color: #006600;">localTimeByZipCode</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;10001&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Pass a New York zipcode.</span></li><li style="" class="li2">&nbsp; &nbsp; %&gt;The <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ATime+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Time</span></a> in <span style="color: #000000; font-weight: bold;">New</span> York is: &lt;%=newYorkTime %&gt;</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">&lt;%</li><li style="" class="li1"><span style="color: #66cc66;">&#125;</span></li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> e<span style="color: #66cc66;">&#41;</span></li><li style="" class="li1"><span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">%&gt; </li><li style="" class="li1">&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a>: &lt;%=e.<span style="color: #006600;">getMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> %&gt;</li><li style="" class="li2">&lt;%&nbsp; </li><li style="" class="li1"><span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">%&gt;</li><li style="" class="li1">&lt;/body&gt;</li><li style="" class="li2">&lt;/html&gt;</li></ol></div><div id="cb60543" style="display: none; color: red;"></div></div></div>

<p>Save this file to WebContent/NewYorkTime.jsp then navigate to <a href="http://localhost:8080/NewYorkTime.jsp">http://localhost:8080/NewYorkTime.jsp</a> and you should see something like:</p>

<p>The Time in New York is: 12/18/2010 11:26:22 AM</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/JSP/calling-web-services-from-jsp">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/JSP/calling-web-services-from-jsp#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/WebDev/?tempskin=_rss2&#38;disp=comments&#38;p=1049</wfw:commentRss>
		</item>
				<item>
			<title>Accurately Simulating Movement of a Tracked Vehicle in 2D Space</title>
			<link>http://blogs.lessthandot.com/index.php/WebDev/UIDevelopment/accurately-simulating-movement-of-a-trac</link>
			<pubDate>Mon, 20 Sep 2010 16:41:38 +0000</pubDate>			<dc:creator>Rob Earl</dc:creator>
			<category domain="main">UI Development</category>			<guid isPermaLink="false">957@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;&lt;img align=&quot;right&quot; src=&quot;http://blogs.lessthandot.com/media/users/robearl/TankMovement-Simplified.png&quot; alt=&quot;Basic Tracked Movement&quot; title=&quot;&quot; /&gt;As part of a simulation I&#039;ve been developing I recently had to decide how to model movement of vehicles within a 2D space. To keep things simple I settled on a tracked vehicle. Each time the simulation updates I calculate how much the vehicle rotates and how far it moves based on the speed of it&#039;s tracks. From there I calculate the new coordinates for redrawing the vehicle.&lt;/p&gt;

&lt;p&gt;Knowing the current location (&lt;strong&gt;x,y&lt;/strong&gt;) and heading (&lt;strong&gt;h&lt;/strong&gt;) I initially adopted a simplified way to calculate the rotation (&lt;strong&gt;g&lt;/strong&gt;) and speed (&lt;strong&gt;s&lt;/strong&gt;) based on the speed of the vehicle&#039;s tracks (&lt;strong&gt;T&lt;span class=&quot;MT_smaller&quot;&gt;1&lt;/span&gt;&lt;/strong&gt; and &lt;strong&gt;T&lt;span class=&quot;MT_smaller&quot;&gt;2&lt;/span&gt;&lt;/strong&gt;) (Figure 1). With these variables I create a right angled triangle and use &lt;a href=&quot;http://www.mathsisfun.com/sine-cosine-tangent.html&quot;&gt;maths&lt;/a&gt; to calculate how far the vehicle moves in the x and y coordinates in order to find it&#039;s finishing location (&lt;strong&gt;x&#039;,y&#039;&lt;/strong&gt;).&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb17785&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;x&#039; = x + s * sin(h + g)&lt;br /&gt;y&#039; = y + s * cos(h + g)&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb64789&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This method is a reasonable approximation for the movement of a tracked vehicle: It&#039;ll move faster when you expect it to and it&#039;ll turn faster when you expect it to. However, as a &lt;a href=&quot;http://yourdaysarenumbered.co.uk/&quot;&gt;stand up mathematician&lt;/a&gt; pointed out to me, it&#039;s not an accurate representation of what should actually be happening. I had also observed vehicles having problems turning to face something close to and behind them resulting in endless circling of the target.&lt;/p&gt;

&lt;h1&gt;Improving Accuracy&lt;/h1&gt;

&lt;p&gt;A more accurate representation is the vehicle travelling a distance, &lt;strong&gt;s&lt;/strong&gt;, along the circumference of a circle who&#039;s radius can be calculated based on &lt;strong&gt;T&lt;span class=&quot;MT_smaller&quot;&gt;1&lt;/span&gt;&lt;/strong&gt;, &lt;strong&gt;T&lt;span class=&quot;MT_smaller&quot;&gt;2&lt;/span&gt;&lt;/strong&gt; and the distance between the tracks (&lt;strong&gt;2d&lt;/strong&gt;) (Figure 2).&lt;/p&gt;

&lt;p&gt;&lt;img align=&quot;right&quot; src=&quot;http://blogs.lessthandot.com/media/users/robearl/TankMovement-Complex-bare3.png&quot; alt=&quot;Figure 2: More Accurate Movement&quot; title=&quot;&quot; /&gt;If &lt;strong&gt;T&lt;span class=&quot;MT_smaller&quot;&gt;2&lt;/span&gt;&lt;/strong&gt; traces a circle of radius &lt;strong&gt;r&lt;/strong&gt; then &lt;strong&gt;T&lt;span class=&quot;MT_smaller&quot;&gt;1&lt;/span&gt;&lt;/strong&gt; traces a circle of radius &lt;strong&gt;r + 2d&lt;/strong&gt;. Since &lt;strong&gt;d&lt;/strong&gt; is static we can say that the ratio of &lt;strong&gt;T&lt;span class=&quot;MT_smaller&quot;&gt;1&lt;/span&gt;&lt;/strong&gt; to &lt;strong&gt;T&lt;span class=&quot;MT_smaller&quot;&gt;2&lt;/span&gt;&lt;/strong&gt; is proportional to the ratio of the radii. This gives us the following equation to solve:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb69896&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;T1 &amp;nbsp; r + 2d&lt;br /&gt;-- = -------&lt;br /&gt;T2 &amp;nbsp; &amp;nbsp; r&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb47551&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Solving this for r gives us:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb31425&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;r = &amp;nbsp; &amp;nbsp; 2d &lt;br /&gt;&amp;nbsp; &amp;nbsp; ----------&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;T1&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;-- &amp;nbsp;- &amp;nbsp;1&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;T2&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb73677&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2&gt;Rotation&lt;/h2&gt;

&lt;p&gt;We now have a triangle with 2 sides of known length. If we divide this triangle in half we can use the right angles to determine the vehicle will move &lt;strong&gt;2g&lt;/strong&gt; radians around the circle (Figure 3).&lt;/p&gt;

&lt;p&gt;We can say that the ratio between &lt;strong&gt;2g&lt;/strong&gt; and 2 PI radians (360 degrees) is the same as the ratio between &lt;b&gt;s&lt;/b&gt; and the circumference of the circle (2 * PI * (&lt;strong&gt;r&lt;/strong&gt; + &lt;strong&gt;d&lt;/strong&gt;)). Setting them to equal we can solve for &lt;strong&gt;g&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img align=&quot;right&quot; src=&quot;http://blogs.lessthandot.com/media/users/robearl/TankMovement-Complex-Solving.png&quot; alt=&quot;Figure 3: Solving Triangles&quot; title=&quot;&quot; /&gt;&lt;/p&gt;&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb40531&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;2g &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;s&lt;br /&gt;------ &amp;nbsp;= &amp;nbsp;-----------------&lt;br /&gt;2 * PI &amp;nbsp; &amp;nbsp; &amp;nbsp;2 * PI * (r + d)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;s&lt;br /&gt;2g = -------&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;r + d&lt;br /&gt;&amp;nbsp;&lt;br /&gt;2g = T1 + T2&lt;br /&gt;&amp;nbsp; &amp;nbsp; --------&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;br /&gt;&amp;nbsp; &amp;nbsp; --------&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;r + d&lt;br /&gt;&amp;nbsp;&lt;br /&gt;2g = &amp;nbsp; &amp;nbsp;T1 + T2&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;--------------&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 2 * ( r + d )&lt;br /&gt;&amp;nbsp;&lt;br /&gt;g &amp;nbsp;= &amp;nbsp; &amp;nbsp; T1 + T2&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; -------------&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 4 * ( r + d )&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb41405&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If we substitute in our value for &lt;b&gt;r&lt;/b&gt; and simplify we end up with:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb51023&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;g = T1 - T2&lt;br /&gt;&amp;nbsp; &amp;nbsp; --------&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4d&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb6375&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Which is a fairly simple calculation that scales by the size of the organism. The other thing missing from the approximate calculation is that the change in heading is actually &lt;b&gt;2g&lt;/b&gt;, as can be seen from the symmetry in our diagrams.&lt;/p&gt;

&lt;h2&gt;Distance&lt;/h2&gt;

&lt;p&gt;Now that we know the angles we can use the &lt;a href=&quot;http://en.wikipedia.org/wiki/Law_of_sines&quot;&gt;Law of Sines&lt;/a&gt; to calculate the straight line distance, &lt;strong&gt;t&lt;/strong&gt;, between our vehicle&#039;s start and end points. &lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb91977&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;r + d &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;t / 2&lt;br /&gt;--------- = -------&lt;br /&gt;sin(PI/2) &amp;nbsp; &amp;nbsp;sin(g)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;r + d &amp;nbsp; &amp;nbsp; t / 2&lt;br /&gt;----- &amp;nbsp;= -------&lt;br /&gt;&amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; sin(g)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;sin(g) * (r + d) = t / 2&lt;br /&gt;&amp;nbsp;&lt;br /&gt;t = 2 * sin(g) * (r + d)&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb35677&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Once again we can substitute in our value for &lt;strong&gt;r&lt;/strong&gt; and simplify to get:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb31688&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;t = d(T1 + T2)&lt;br /&gt;&amp;nbsp; &amp;nbsp; ---------- * 2 * sin(g)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; T1 - T2&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb41599&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now that we know the distance and we know the angle we can use the same method we used earlier to calculate the new coordinates:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb68087&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;x&#039; = x + t * sin(h + g)&lt;br /&gt;y&#039; = y + t * cos(h + g)&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb60608&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2&gt;Applying Our Findings&lt;/h2&gt;

&lt;p&gt;So far we&#039;ve seen a lot of (possibly confusing) numbers and symbols but what do we actually have to do do add this to a simulation? Each time our vehicle updates we need to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Calculate &lt;strong&gt;g&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Calculate &lt;strong&gt;t&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Calculate &lt;strong&gt;x&#039;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Calculate &lt;strong&gt;y&#039;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Adjust the vehicle&#039;s coordinates to &lt;strong&gt;x&#039;,y&#039;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Adjust the vehicle&#039;s heading (&lt;strong&gt;h&lt;/strong&gt;) by &lt;strong&gt;2g&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You may have noticed there&#039;s a possible division by zero error when &lt;strong&gt;T&lt;span class=&quot;MT_smaller&quot;&gt;1&lt;/span&gt;&lt;/strong&gt; equals &lt;strong&gt;T&lt;span class=&quot;MT_smaller&quot;&gt;2&lt;/span&gt;&lt;/strong&gt;. To protect against this we can check if they&#039;re equal (or catch the exception) and use the calculation:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb98768&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;x&#039; = x + T1 * sin(h)&lt;br /&gt;y&#039; = y + T1 * cos(h)&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb89368&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;


&lt;h2&gt;Example&lt;/h2&gt;

&lt;p&gt;Let&#039;s say our vehicle has the properties:&lt;/p&gt;

&lt;p&gt;T&lt;span class=&quot;MT_smaller&quot;&gt;1&lt;/span&gt; = 1.0 (Track 1 Speed)&lt;br /&gt;
T&lt;span class=&quot;MT_smaller&quot;&gt;2&lt;/span&gt; = 0.5 (Track 2 Speed)&lt;br /&gt;
d = 2.0 (Half distance between tracks)&lt;br /&gt;
x = 0 (Coordinate)&lt;br /&gt;
y = 0 (Coordinate)&lt;br /&gt;
h = 2 (Heading)&lt;/p&gt;

&lt;p&gt;1. Calculate &lt;strong&gt;g&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb53468&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;g = T1 - T2&lt;br /&gt;&amp;nbsp; &amp;nbsp; --------&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4d&lt;br /&gt;g = 1.0 - 0.5 &amp;nbsp; &amp;nbsp; 0.5&lt;br /&gt;&amp;nbsp; &amp;nbsp; --------- &amp;nbsp;= &amp;nbsp;--- = 0.0625&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 8 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;8&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb4575&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;2. Calculate &lt;strong&gt;t&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb8953&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;t = d(T1 + T2)&lt;br /&gt;&amp;nbsp; &amp;nbsp; ---------- * 2 * sin(g)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; T1 - T2&lt;br /&gt;= 0 + (2 * (1.0 + 0.5) * 2 * sin(0.0625) ) / ( 1.0 - 0.5 )&lt;br /&gt;= (6 * 0.06246 ) / 0.5&lt;br /&gt;= 0.37476 * 2&lt;br /&gt;= 0.74951&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb97154&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;3. Calculate &lt;strong&gt;x&#039;&lt;/strong&gt;.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb44439&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;x&#039; = x + t * sin(h + g)&lt;br /&gt;= 0 + &amp;nbsp;0.74951 * sin(2.0625)&lt;br /&gt;= 0.66072&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb21317&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;4. Calculate &lt;strong&gt;y&#039;&lt;/strong&gt;.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb10628&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;y&#039; = y + t * cos(h + g)&lt;br /&gt;= 0 + 0.74951 * cos(2.0625)&lt;br /&gt;= -0.35387&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb13542&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;2. Adjust &lt;strong&gt;h&lt;/strong&gt; by &lt;strong&gt;2g&lt;/strong&gt;.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb25990&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;h&#039; = h + 2g&lt;br /&gt;h&#039; = 2 + 2 * 0.0625&lt;br /&gt;h&#039; = 2.125&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb72538&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After this update our new state is:&lt;/p&gt;

&lt;p&gt;x = 0.66072&lt;br /&gt;
y = -0.35387&lt;br /&gt;
h = 2.125&lt;/p&gt;

&lt;p&gt;In the following Java example the Vehicle&#039;s update method would be called from the main program loop.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;java&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;java&quot; id=&quot;cb53362&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;class&lt;/span&gt; Vehicle&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #993333;&quot;&gt;double&lt;/span&gt; x, y, heading;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; size, maxSpeed;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/*&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Other methods for initialising and controlling the vehicle.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;&amp;nbsp; */&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; update&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;double&lt;/span&gt; tracks&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; = controller.&lt;span style=&quot;color: #006600;&quot;&gt;getTrackSpeeds&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Get the track speeds from whatever class is controlling the vehicle.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Could be human input or any kind of AI controller.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Calculate g.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;double&lt;/span&gt; d = &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;this&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;size&lt;/span&gt; / &lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;double&lt;/span&gt; g = &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;tracks&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; - tracks&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; / &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; d * &lt;span style=&quot;color: #cc66cc;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Calculate t.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;double&lt;/span&gt; t = &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;tracks&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;equals&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;tracks&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Use straight line calculation to avoid division by zero.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; t = tracks&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;else&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; t = &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; d * &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; tracks&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; + tracks&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; * &lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt; * &lt;a href=&quot;http://www.google.com/search?hl=en&amp;amp;q=allinurl%3AMath+java.sun.com&amp;amp;btnI=I%27m%20Feeling%20Lucky&quot;&gt;&lt;span style=&quot;color: #aaaadd; font-weight: bold;&quot;&gt;Math&lt;/span&gt;&lt;/a&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;sin&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;g&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; / &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; tracks&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; - tracks&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Update x.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;this&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;x&lt;/span&gt; -= t * &lt;a href=&quot;http://www.google.com/search?hl=en&amp;amp;q=allinurl%3AMath+java.sun.com&amp;amp;btnI=I%27m%20Feeling%20Lucky&quot;&gt;&lt;span style=&quot;color: #aaaadd; font-weight: bold;&quot;&gt;Math&lt;/span&gt;&lt;/a&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;sin&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;this&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;heading&lt;/span&gt; + g&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// See note below.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Update y.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;this&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;y&lt;/span&gt; += t * &lt;a href=&quot;http://www.google.com/search?hl=en&amp;amp;q=allinurl%3AMath+java.sun.com&amp;amp;btnI=I%27m%20Feeling%20Lucky&quot;&gt;&lt;span style=&quot;color: #aaaadd; font-weight: bold;&quot;&gt;Math&lt;/span&gt;&lt;/a&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;cos&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;this&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;heading&lt;/span&gt; + g&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Update the vehicle&#039;s heading.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;this&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;heading&lt;/span&gt; += &lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt; * g;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb58408&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When updating the x coordinate we subtract because, when using a JPanel:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;0,0 is the top left corner.&lt;/li&gt;
&lt;li&gt;A heading of 0 is down.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Using a normal &lt;a href=&quot;http://en.wikipedia.org/wiki/Sine_wave&quot;&gt;sine wave&lt;/a&gt; an increase in heading (&lt;strong&gt;h&lt;/strong&gt;) would result in an increase in &lt;strong&gt;sine h&lt;/strong&gt; and therefore, an increase in &lt;strong&gt;x&lt;/strong&gt; which is the opposite of what is expected/required. &lt;/p&gt;

&lt;p&gt;Inverting the wave ( multiply by -1 ) produces the correct behaviour.&lt;/p&gt;

&lt;h1&gt;How Much Difference Does It Really Make?&lt;/h1&gt;

&lt;p&gt;The accuracy of the basic method decreases as the section of the circle traversed in one increment moves further from a straight line. Therefore, it decreases as:&lt;/p&gt;

&lt;p&gt;&lt;img align=&quot;right&quot; src=&quot;http://blogs.lessthandot.com/media/users/robearl/Charts.png&quot; alt=&quot;Comparison Charts&quot; title=&quot;&quot; /&gt;&lt;/p&gt;&lt;ul&gt;
&lt;li&gt;Speed increases.&lt;/li&gt;
&lt;li&gt;Turning circle decreases.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To demonstrate just how much difference there can be I&#039;ve used a fairly extreme case to produce the charts opposite:&lt;/p&gt;

&lt;p&gt;T&lt;span class=&quot;MT_smaller&quot;&gt;1&lt;/span&gt; = 10&lt;br /&gt;
T&lt;span class=&quot;MT_smaller&quot;&gt;2&lt;/span&gt; = -9&lt;br /&gt;
d = 2&lt;/p&gt;


&lt;p&gt;As we can see a vehicle turning in a tight circle at a fairly high speed has a much bigger turning circle using the approximate movement calculations than when using the improved calculations.&lt;/p&gt;

&lt;p&gt;As the size of the vehicle is now taken into account we can make a vehicle less nimble with a larger turning circle simply by increasing it&#039;s size. We can get a turning circle like the one below by increasing &lt;strong&gt;d&lt;/strong&gt; to 60 without any need to introduce extra variables to clamp the rotation rate.&lt;/p&gt;

&lt;p&gt;&lt;center&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/users/robearl/Bigger.png&quot; alt=&quot;Larger Vehicle&quot; title=&quot;&quot; /&gt;&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;The other difference which is fairly hard to quantify is that the vehicle movement was much smoother once the new calculations were implemented. This could be because the initial technique caused lots of over-rotating which required constant readjustment.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/WebDev/UIDevelopment/accurately-simulating-movement-of-a-trac&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p><img align="right" src="http://blogs.lessthandot.com/media/users/robearl/TankMovement-Simplified.png" alt="Basic Tracked Movement" title="" />As part of a simulation I've been developing I recently had to decide how to model movement of vehicles within a 2D space. To keep things simple I settled on a tracked vehicle. Each time the simulation updates I calculate how much the vehicle rotates and how far it moves based on the speed of it's tracks. From there I calculate the new coordinates for redrawing the vehicle.</p>

<p>Knowing the current location (<strong>x,y</strong>) and heading (<strong>h</strong>) I initially adopted a simplified way to calculate the rotation (<strong>g</strong>) and speed (<strong>s</strong>) based on the speed of the vehicle's tracks (<strong>T<span class="MT_smaller">1</span></strong> and <strong>T<span class="MT_smaller">2</span></strong>) (Figure 1). With these variables I create a right angled triangle and use <a href="http://www.mathsisfun.com/sine-cosine-tangent.html">maths</a> to calculate how far the vehicle moves in the x and y coordinates in order to find it's finishing location (<strong>x',y'</strong>).</p>
<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb53833'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb53833','cb56712'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb53833" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">x' = x + s * sin(h + g)</li><li style="" class="li2">y' = y + s * cos(h + g)</li></ol></div><div id="cb56712" style="display: none; color: red;"></div></div></div>

<p>This method is a reasonable approximation for the movement of a tracked vehicle: It'll move faster when you expect it to and it'll turn faster when you expect it to. However, as a <a href="http://yourdaysarenumbered.co.uk/">stand up mathematician</a> pointed out to me, it's not an accurate representation of what should actually be happening. I had also observed vehicles having problems turning to face something close to and behind them resulting in endless circling of the target.</p>

<h1>Improving Accuracy</h1>

<p>A more accurate representation is the vehicle travelling a distance, <strong>s</strong>, along the circumference of a circle who's radius can be calculated based on <strong>T<span class="MT_smaller">1</span></strong>, <strong>T<span class="MT_smaller">2</span></strong> and the distance between the tracks (<strong>2d</strong>) (Figure 2).</p>

<p><img align="right" src="http://blogs.lessthandot.com/media/users/robearl/TankMovement-Complex-bare3.png" alt="Figure 2: More Accurate Movement" title="" />If <strong>T<span class="MT_smaller">2</span></strong> traces a circle of radius <strong>r</strong> then <strong>T<span class="MT_smaller">1</span></strong> traces a circle of radius <strong>r + 2d</strong>. Since <strong>d</strong> is static we can say that the ratio of <strong>T<span class="MT_smaller">1</span></strong> to <strong>T<span class="MT_smaller">2</span></strong> is proportional to the ratio of the radii. This gives us the following equation to solve:</p>
<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb78575'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb78575','cb63667'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb78575" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">T1 &nbsp; r + 2d</li><li style="" class="li2">-- = -------</li><li style="" class="li1">T2 &nbsp; &nbsp; r</li></ol></div><div id="cb63667" style="display: none; color: red;"></div></div></div>

<p>Solving this for r gives us:</p>

<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb7995'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb7995','cb37242'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb7995" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">r = &nbsp; &nbsp; 2d </li><li style="" class="li2">&nbsp; &nbsp; ----------</li><li style="" class="li1">&nbsp; &nbsp; &nbsp;T1</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;-- &nbsp;- &nbsp;1</li><li style="" class="li1">&nbsp; &nbsp; &nbsp;T2</li></ol></div><div id="cb37242" style="display: none; color: red;"></div></div></div>

<h2>Rotation</h2>

<p>We now have a triangle with 2 sides of known length. If we divide this triangle in half we can use the right angles to determine the vehicle will move <strong>2g</strong> radians around the circle (Figure 3).</p>

<p>We can say that the ratio between <strong>2g</strong> and 2 PI radians (360 degrees) is the same as the ratio between <b>s</b> and the circumference of the circle (2 * PI * (<strong>r</strong> + <strong>d</strong>)). Setting them to equal we can solve for <strong>g</strong>.</p>

<p><img align="right" src="http://blogs.lessthandot.com/media/users/robearl/TankMovement-Complex-Solving.png" alt="Figure 3: Solving Triangles" title="" /></p><div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb42047'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb42047','cb86467'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb42047" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">2g &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s</li><li style="" class="li2">------ &nbsp;= &nbsp;-----------------</li><li style="" class="li1">2 * PI &nbsp; &nbsp; &nbsp;2 * PI * (r + d)</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;s</li><li style="" class="li1">2g = -------</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;r + d</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">2g = T1 + T2</li><li style="" class="li1">&nbsp; &nbsp; --------</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;2</li><li style="" class="li1">&nbsp; &nbsp; --------</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;r + d</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">2g = &nbsp; &nbsp;T1 + T2</li><li style="" class="li1">&nbsp; &nbsp; &nbsp;--------------</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; 2 * ( r + d )</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">g &nbsp;= &nbsp; &nbsp; T1 + T2</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; -------------</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; 4 * ( r + d )</li></ol></div><div id="cb86467" style="display: none; color: red;"></div></div></div>

<p>If we substitute in our value for <b>r</b> and simplify we end up with:</p>

<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb26950'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb26950','cb30021'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb26950" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">g = T1 - T2</li><li style="" class="li2">&nbsp; &nbsp; --------</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;4d</li></ol></div><div id="cb30021" style="display: none; color: red;"></div></div></div>

<p>Which is a fairly simple calculation that scales by the size of the organism. The other thing missing from the approximate calculation is that the change in heading is actually <b>2g</b>, as can be seen from the symmetry in our diagrams.</p>

<h2>Distance</h2>

<p>Now that we know the angles we can use the <a href="http://en.wikipedia.org/wiki/Law_of_sines">Law of Sines</a> to calculate the straight line distance, <strong>t</strong>, between our vehicle's start and end points. </p>

<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb64413'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb64413','cb48622'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb64413" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">r + d &nbsp; &nbsp; &nbsp; &nbsp;t / 2</li><li style="" class="li2">--------- = -------</li><li style="" class="li1">sin(PI/2) &nbsp; &nbsp;sin(g)</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">r + d &nbsp; &nbsp; t / 2</li><li style="" class="li2">----- &nbsp;= -------</li><li style="" class="li1">&nbsp; 1 &nbsp; &nbsp; &nbsp; sin(g)</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">sin(g) * (r + d) = t / 2</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">t = 2 * sin(g) * (r + d)</li></ol></div><div id="cb48622" style="display: none; color: red;"></div></div></div>

<p>Once again we can substitute in our value for <strong>r</strong> and simplify to get:</p>

<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb53503'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb53503','cb66975'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb53503" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">t = d(T1 + T2)</li><li style="" class="li2">&nbsp; &nbsp; ---------- * 2 * sin(g)</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; T1 - T2</li></ol></div><div id="cb66975" style="display: none; color: red;"></div></div></div>

<p>Now that we know the distance and we know the angle we can use the same method we used earlier to calculate the new coordinates:</p>

<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb22263'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb22263','cb47894'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb22263" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">x' = x + t * sin(h + g)</li><li style="" class="li2">y' = y + t * cos(h + g)</li></ol></div><div id="cb47894" style="display: none; color: red;"></div></div></div>

<h2>Applying Our Findings</h2>

<p>So far we've seen a lot of (possibly confusing) numbers and symbols but what do we actually have to do do add this to a simulation? Each time our vehicle updates we need to:</p>

<ol>
<li>Calculate <strong>g</strong>.</li>
<li>Calculate <strong>t</strong>.</li>
<li>Calculate <strong>x'</strong>.</li>
<li>Calculate <strong>y'</strong>.</li>
<li>Adjust the vehicle's coordinates to <strong>x',y'</strong>.</li>
<li>Adjust the vehicle's heading (<strong>h</strong>) by <strong>2g</strong>.</li>
</ol>

<p>You may have noticed there's a possible division by zero error when <strong>T<span class="MT_smaller">1</span></strong> equals <strong>T<span class="MT_smaller">2</span></strong>. To protect against this we can check if they're equal (or catch the exception) and use the calculation:</p>

<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb44603'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb44603','cb64478'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb44603" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">x' = x + T1 * sin(h)</li><li style="" class="li2">y' = y + T1 * cos(h)</li></ol></div><div id="cb64478" style="display: none; color: red;"></div></div></div>


<h2>Example</h2>

<p>Let's say our vehicle has the properties:</p>

<p>T<span class="MT_smaller">1</span> = 1.0 (Track 1 Speed)<br />
T<span class="MT_smaller">2</span> = 0.5 (Track 2 Speed)<br />
d = 2.0 (Half distance between tracks)<br />
x = 0 (Coordinate)<br />
y = 0 (Coordinate)<br />
h = 2 (Heading)</p>

<p>1. Calculate <strong>g</strong></p>
<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb34447'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb34447','cb20034'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb34447" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">g = T1 - T2</li><li style="" class="li2">&nbsp; &nbsp; --------</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;4d</li><li style="" class="li2">g = 1.0 - 0.5 &nbsp; &nbsp; 0.5</li><li style="" class="li1">&nbsp; &nbsp; --------- &nbsp;= &nbsp;--- = 0.0625</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; 8 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8</li></ol></div><div id="cb20034" style="display: none; color: red;"></div></div></div>

<p>2. Calculate <strong>t</strong></p>
<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb64950'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb64950','cb13574'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb64950" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">t = d(T1 + T2)</li><li style="" class="li2">&nbsp; &nbsp; ---------- * 2 * sin(g)</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; T1 - T2</li><li style="" class="li2">= 0 + (2 * (1.0 + 0.5) * 2 * sin(0.0625) ) / ( 1.0 - 0.5 )</li><li style="" class="li1">= (6 * 0.06246 ) / 0.5</li><li style="" class="li2">= 0.37476 * 2</li><li style="" class="li1">= 0.74951</li></ol></div><div id="cb13574" style="display: none; color: red;"></div></div></div>
<p>3. Calculate <strong>x'</strong>.</p>

<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb20363'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb20363','cb38752'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb20363" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">x' = x + t * sin(h + g)</li><li style="" class="li2">= 0 + &nbsp;0.74951 * sin(2.0625)</li><li style="" class="li1">= 0.66072</li></ol></div><div id="cb38752" style="display: none; color: red;"></div></div></div>

<p>4. Calculate <strong>y'</strong>.</p>

<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb28282'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb28282','cb78095'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb28282" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">y' = y + t * cos(h + g)</li><li style="" class="li2">= 0 + 0.74951 * cos(2.0625)</li><li style="" class="li1">= -0.35387</li></ol></div><div id="cb78095" style="display: none; color: red;"></div></div></div>

<p>2. Adjust <strong>h</strong> by <strong>2g</strong>.</p>

<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb56873'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb56873','cb56803'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb56873" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">h' = h + 2g</li><li style="" class="li2">h' = 2 + 2 * 0.0625</li><li style="" class="li1">h' = 2.125</li></ol></div><div id="cb56803" style="display: none; color: red;"></div></div></div>

<p>After this update our new state is:</p>

<p>x = 0.66072<br />
y = -0.35387<br />
h = 2.125</p>

<p>In the following Java example the Vehicle's update method would be called from the main program loop.</p>

<div class="codebox"><div class="codeheader"><span>java</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb22872'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb22872','cb6880'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="java" id="cb22872" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Vehicle</li><li style="" class="li2"><span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">double</span> x, y, heading;</li><li style="" class="li2">&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">int</span> size, maxSpeed;</li><li style="" class="li1">&nbsp; <span style="color: #808080; font-style: italic;">/*</span></li><li style="" class="li2"><span style="color: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp;Other methods for initialising and controlling the vehicle.</span></li><li style="" class="li1"><span style="color: #808080; font-style: italic;">&nbsp; */</span></li><li style="" class="li2">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> update<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #993333;">double</span> tracks<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> = controller.<span style="color: #006600;">getTrackSpeeds</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Get the track speeds from whatever class is controlling the vehicle.</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Could be human input or any kind of AI controller.</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Calculate g.</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #993333;">double</span> d = <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">size</span> / <span style="color: #cc66cc;">2</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #993333;">double</span> g = <span style="color: #66cc66;">&#40;</span>tracks<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> - tracks<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> / <span style="color: #66cc66;">&#40;</span> d * <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Calculate t.</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #993333;">double</span> t = <span style="color: #cc66cc;">0</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>tracks<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span>tracks<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// Use straight line calculation to avoid division by zero.</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; t = tracks<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> </li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #b1b100;">else</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; t = <span style="color: #66cc66;">&#40;</span> d * <span style="color: #66cc66;">&#40;</span> tracks<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> + tracks<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span> * <span style="color: #cc66cc;">2</span> * <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AMath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Math</span></a>.<span style="color: #006600;">sin</span><span style="color: #66cc66;">&#40;</span>g<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> / <span style="color: #66cc66;">&#40;</span> tracks<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> - tracks<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Update x.</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">x</span> -= t * <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AMath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Math</span></a>.<span style="color: #006600;">sin</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">heading</span> + g<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// See note below.</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Update y.</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">y</span> += t * <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AMath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Math</span></a>.<span style="color: #006600;">cos</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">heading</span> + g<span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Update the vehicle's heading.</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">heading</span> += <span style="color: #cc66cc;">2</span> * g;</li><li style="" class="li1">&nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2"><span style="color: #66cc66;">&#125;</span></li></ol></div><div id="cb6880" style="display: none; color: red;"></div></div></div>

<p>When updating the x coordinate we subtract because, when using a JPanel:</p>
<ul>
<li>0,0 is the top left corner.</li>
<li>A heading of 0 is down.</li>
</ul>
<p>Using a normal <a href="http://en.wikipedia.org/wiki/Sine_wave">sine wave</a> an increase in heading (<strong>h</strong>) would result in an increase in <strong>sine h</strong> and therefore, an increase in <strong>x</strong> which is the opposite of what is expected/required. </p>

<p>Inverting the wave ( multiply by -1 ) produces the correct behaviour.</p>

<h1>How Much Difference Does It Really Make?</h1>

<p>The accuracy of the basic method decreases as the section of the circle traversed in one increment moves further from a straight line. Therefore, it decreases as:</p>

<p><img align="right" src="http://blogs.lessthandot.com/media/users/robearl/Charts.png" alt="Comparison Charts" title="" /></p><ul>
<li>Speed increases.</li>
<li>Turning circle decreases.</li>
</ul>

<p>To demonstrate just how much difference there can be I've used a fairly extreme case to produce the charts opposite:</p>

<p>T<span class="MT_smaller">1</span> = 10<br />
T<span class="MT_smaller">2</span> = -9<br />
d = 2</p>


<p>As we can see a vehicle turning in a tight circle at a fairly high speed has a much bigger turning circle using the approximate movement calculations than when using the improved calculations.</p>

<p>As the size of the vehicle is now taken into account we can make a vehicle less nimble with a larger turning circle simply by increasing it's size. We can get a turning circle like the one below by increasing <strong>d</strong> to 60 without any need to introduce extra variables to clamp the rotation rate.</p>

<p><center><img src="http://blogs.lessthandot.com/media/users/robearl/Bigger.png" alt="Larger Vehicle" title="" /></center></p>

<p>The other difference which is fairly hard to quantify is that the vehicle movement was much smoother once the new calculations were implemented. This could be because the initial technique caused lots of over-rotating which required constant readjustment.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/WebDev/UIDevelopment/accurately-simulating-movement-of-a-trac">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/WebDev/UIDevelopment/accurately-simulating-movement-of-a-trac#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/WebDev/?tempskin=_rss2&#38;disp=comments&#38;p=957</wfw:commentRss>
		</item>
				<item>
			<title>Automated Monitoring of Remote VNC Servers</title>
			<link>http://blogs.lessthandot.com/index.php/WebDev/ServerAdmin/automated-monitoring-of-remote-vnc-serve</link>
			<pubDate>Mon, 30 Aug 2010 10:42:38 +0000</pubDate>			<dc:creator>Rob Earl</dc:creator>
			<category domain="alt">Server Programming</category>
<category domain="main">Server Admin</category>			<guid isPermaLink="false">951@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;For the duration of the Edinburgh Festival Fringe I&#039;m responsible for a couple of Samsung MagicNet plasma screens which run 24/7 in the windows outside a venue. They&#039;re essentially 32&quot; screens with a built in PC running Windows XP and a VNC server. They&#039;re pretty flexible as you can set them to open a webpage fullscreen then display just about anything.&lt;/p&gt;

&lt;p&gt;Unfortunately, they&#039;re prone to faults and I have no real way of knowing when one occurs until I attempt to VNC into (or walk passed) one of them which brings me to the purpose of this post: How to ensure your remote VNC sessions are doing what they&#039;re supposed to.&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;perl&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;perl&quot; id=&quot;cb33081&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/perl&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; Net::&lt;span style=&quot;color: #006600;&quot;&gt;VNC&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;use&lt;/span&gt; strict;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$vnc&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$path&lt;/span&gt; = &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;/home/rob/Desktop&amp;quot;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#my $path = &amp;quot;screens/&amp;quot;;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#my $path = &amp;quot;/var/www/vnc-monitoring&amp;quot;;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#my $path = &amp;quot;/home/rob/public_html/vnc-monitoring&amp;quot;;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;a href=&quot;http://perldoc.perl.org/functions/open.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;open&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;LOG, &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&amp;gt;&amp;gt;monitoring.log&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Open the log file.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;a href=&quot;http://perldoc.perl.org/functions/open.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;open&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;SERVERS , &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;vnc-servers&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Open the list of VNC servers, one IP,password per line:&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# 192.168.0.2,password&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# 192.168.0.3&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# 192.168.0.4,anotherpassword&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #b1b100;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;&amp;lt;SERVERS&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$address&lt;/span&gt;,&lt;span style=&quot;color: #0000ff;&quot;&gt;$password&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; = &lt;a href=&quot;http://perldoc.perl.org/functions/split.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;split&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;/,/,&lt;span style=&quot;color: #0000ff;&quot;&gt;$_&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$address&lt;/span&gt; =~ &lt;a href=&quot;http://perldoc.perl.org/functions/s.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;s&lt;/span&gt;&lt;/a&gt;/\r//g;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$address&lt;/span&gt; =~ &lt;a href=&quot;http://perldoc.perl.org/functions/s.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;s&lt;/span&gt;&lt;/a&gt;/\n//g;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$password&lt;/span&gt; =~ &lt;a href=&quot;http://perldoc.perl.org/functions/s.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;s&lt;/span&gt;&lt;/a&gt;/\r//g;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$password&lt;/span&gt; =~ &lt;a href=&quot;http://perldoc.perl.org/functions/s.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;s&lt;/span&gt;&lt;/a&gt;/\n//g;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$vnc&lt;/span&gt; = Net::&lt;span style=&quot;color: #006600;&quot;&gt;VNC&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;hostname =&amp;gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$address&lt;/span&gt;, password =&amp;gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$password&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Create a new connection to vnc.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$vnc&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;depth&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;24&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$message&lt;/span&gt; = &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;my&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$image&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/eval.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;eval&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$vnc&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;login&lt;/span&gt;; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Must be within an eval else the script will bail on error.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$message&lt;/span&gt; = &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Connected: &amp;quot;&lt;/span&gt;.&lt;span style=&quot;color: #0000ff;&quot;&gt;$address&lt;/span&gt;.&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot; (&amp;quot;&lt;/span&gt;.&lt;span style=&quot;color: #0000ff;&quot;&gt;$vnc&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;name&lt;/span&gt;.&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;)&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$image&lt;/span&gt; = &lt;span style=&quot;color: #0000ff;&quot;&gt;$vnc&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;capture&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;$@&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$message&lt;/span&gt; = $@;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$image&lt;/span&gt; = Image::&lt;span style=&quot;color: #006600;&quot;&gt;Imlib2&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;200&lt;/span&gt;, &lt;span style=&quot;color: #cc66cc;&quot;&gt;200&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# Create a blank image to indicate a problem. Could also copy a preset &amp;quot;error&amp;quot; image.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# We could also send out an email here since we definitely have an issue.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$address&lt;/span&gt; =~ &lt;a href=&quot;http://perldoc.perl.org/functions/s.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;s&lt;/span&gt;&lt;/a&gt;/\./_/g;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://perldoc.perl.org/functions/print.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;print&lt;/span&gt;&lt;/a&gt; LOG &lt;span style=&quot;color: #0000ff;&quot;&gt;$message&lt;/span&gt;.&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$image&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;save&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;$path/$address-capture.png&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;a href=&quot;http://perldoc.perl.org/functions/close.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;close&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;LOG&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;a href=&quot;http://perldoc.perl.org/functions/close.html&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;close&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;SERVERS&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb71147&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This Perl script uses &lt;a href=&quot;http://search.cpan.org/~lbrocard/Net-VNC-0.36/lib/Net/VNC.pm&quot;&gt;Net::VNC&lt;/a&gt; to connect to each specified VNC server, capture a screenshot and save it to the specified directory.&lt;/p&gt;

&lt;p&gt;From there all you need to do is add it to a Cron job and decide how you&#039;re going to monitor the resultant images:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Slideshow software on a dedicated screen.&lt;/li&gt;
  &lt;li&gt;Embedded into a web page.&lt;/li&gt;
  &lt;li&gt;Simply saved to your Desktop (Gnome refreshes thumbnails automatically).&lt;/li&gt;
  &lt;li&gt;Maybe even use NotifyOSD (or similar)?&lt;/li&gt;
&lt;/ul&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/WebDev/ServerAdmin/automated-monitoring-of-remote-vnc-serve&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>For the duration of the Edinburgh Festival Fringe I'm responsible for a couple of Samsung MagicNet plasma screens which run 24/7 in the windows outside a venue. They're essentially 32" screens with a built in PC running Windows XP and a VNC server. They're pretty flexible as you can set them to open a webpage fullscreen then display just about anything.</p>

<p>Unfortunately, they're prone to faults and I have no real way of knowing when one occurs until I attempt to VNC into (or walk passed) one of them which brings me to the purpose of this post: How to ensure your remote VNC sessions are doing what they're supposed to.</p>
<div class="codebox"><div class="codeheader"><span>perl</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb23198'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb23198','cb2978'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="perl" id="cb23198" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #000000; font-weight: bold;">use</span> Net::<span style="color: #006600;">VNC</span>;</li><li style="" class="li2"><span style="color: #000000; font-weight: bold;">use</span> strict;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$vnc</span>;</li><li style="" class="li1"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$path</span> = <span style="color: #ff0000;">&quot;/home/rob/Desktop&quot;</span>;</li><li style="" class="li2"><span style="color: #808080; font-style: italic;">#my $path = &quot;screens/&quot;;</span></li><li style="" class="li1"><span style="color: #808080; font-style: italic;">#my $path = &quot;/var/www/vnc-monitoring&quot;;</span></li><li style="" class="li2"><span style="color: #808080; font-style: italic;">#my $path = &quot;/home/rob/public_html/vnc-monitoring&quot;;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><a href="http://perldoc.perl.org/functions/open.html"><span style="color: #000066;">open</span></a><span style="color: #66cc66;">&#40;</span>LOG, <span style="color: #ff0000;">&quot;&gt;&gt;monitoring.log&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;"># Open the log file.</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><a href="http://perldoc.perl.org/functions/open.html"><span style="color: #000066;">open</span></a><span style="color: #66cc66;">&#40;</span>SERVERS , <span style="color: #ff0000;">&quot;vnc-servers&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;"># Open the list of VNC servers, one IP,password per line:</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;"># 192.168.0.2,password</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;"># 192.168.0.3</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;"># 192.168.0.4,anotherpassword</span></li><li style="" class="li2"><span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">&lt;SERVERS&gt;</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li1"><span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$address</span>,<span style="color: #0000ff;">$password</span><span style="color: #66cc66;">&#41;</span> = <a href="http://perldoc.perl.org/functions/split.html"><span style="color: #000066;">split</span></a><span style="color: #66cc66;">&#40;</span>/,/,<span style="color: #0000ff;">$_</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000ff;">$address</span> =~ <a href="http://perldoc.perl.org/functions/s.html"><span style="color: #000066;">s</span></a>/\r//g;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000ff;">$address</span> =~ <a href="http://perldoc.perl.org/functions/s.html"><span style="color: #000066;">s</span></a>/\n//g;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000ff;">$password</span> =~ <a href="http://perldoc.perl.org/functions/s.html"><span style="color: #000066;">s</span></a>/\r//g;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000ff;">$password</span> =~ <a href="http://perldoc.perl.org/functions/s.html"><span style="color: #000066;">s</span></a>/\n//g;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000ff;">$vnc</span> = Net::<span style="color: #006600;">VNC</span>-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>hostname =&gt; <span style="color: #0000ff;">$address</span>, password =&gt; <span style="color: #0000ff;">$password</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;"># Create a new connection to vnc.</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000ff;">$vnc</span>-&gt;<span style="color: #006600;">depth</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">24</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$message</span> = <span style="color: #ff0000;">&quot;&quot;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$image</span>;</li><li style="" class="li1">&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/eval.html"><span style="color: #000066;">eval</span></a></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$vnc</span>-&gt;<span style="color: #006600;">login</span>; <span style="color: #808080; font-style: italic;"># Must be within an eval else the script will bail on error.</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$message</span> = <span style="color: #ff0000;">&quot;Connected: &quot;</span>.<span style="color: #0000ff;">$address</span>.<span style="color: #ff0000;">&quot; (&quot;</span>.<span style="color: #0000ff;">$vnc</span>-&gt;<span style="color: #006600;">name</span>.<span style="color: #ff0000;">&quot;)&quot;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$image</span> = <span style="color: #0000ff;">$vnc</span>-&gt;<span style="color: #006600;">capture</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>$@<span style="color: #66cc66;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$message</span> = $@;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$image</span> = Image::<span style="color: #006600;">Imlib2</span>-&gt;<span style="color: #006600;">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span>, <span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;"># Create a blank image to indicate a problem. Could also copy a preset &quot;error&quot; image.</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># We could also send out an email here since we definitely have an issue.</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000ff;">$address</span> =~ <a href="http://perldoc.perl.org/functions/s.html"><span style="color: #000066;">s</span></a>/\./_/g;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> LOG <span style="color: #0000ff;">$message</span>.<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000ff;">$image</span>-&gt;<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;$path/$address-capture.png&quot;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1"><span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><a href="http://perldoc.perl.org/functions/close.html"><span style="color: #000066;">close</span></a><span style="color: #66cc66;">&#40;</span>LOG<span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2"><a href="http://perldoc.perl.org/functions/close.html"><span style="color: #000066;">close</span></a><span style="color: #66cc66;">&#40;</span>SERVERS<span style="color: #66cc66;">&#41;</span>;</li></ol></div><div id="cb2978" style="display: none; color: red;"></div></div></div>

<p>This Perl script uses <a href="http://search.cpan.org/~lbrocard/Net-VNC-0.36/lib/Net/VNC.pm">Net::VNC</a> to connect to each specified VNC server, capture a screenshot and save it to the specified directory.</p>

<p>From there all you need to do is add it to a Cron job and decide how you're going to monitor the resultant images:</p>

<ul>
  <li>Slideshow software on a dedicated screen.</li>
  <li>Embedded into a web page.</li>
  <li>Simply saved to your Desktop (Gnome refreshes thumbnails automatically).</li>
  <li>Maybe even use NotifyOSD (or similar)?</li>
</ul><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/WebDev/ServerAdmin/automated-monitoring-of-remote-vnc-serve">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/WebDev/ServerAdmin/automated-monitoring-of-remote-vnc-serve#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/WebDev/?tempskin=_rss2&#38;disp=comments&#38;p=951</wfw:commentRss>
		</item>
			</channel>
</rss>
