<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>bluefeet &#187; CPAN</title>
	<atom:link href="http://blog.bluefeet.net/tag/cpan/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.bluefeet.net</link>
	<description>Yet another Perl Hacker weblog</description>
	<lastBuildDate>Thu, 10 Jun 2010 15:22:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Generating Constants in Perl</title>
		<link>http://blog.bluefeet.net/2010/05/generating-constants-in-perl/</link>
		<comments>http://blog.bluefeet.net/2010/05/generating-constants-in-perl/#comments</comments>
		<pubDate>Fri, 21 May 2010 23:29:35 +0000</pubDate>
		<dc:creator>bluefeet</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[CPAN]]></category>
		<category><![CDATA[Perl 5]]></category>

		<guid isPermaLink="false">http://blog.bluefeet.net/?p=240</guid>
		<description><![CDATA[The other day @ $work I was throwing together a module that creates constants based on a fairly static table in the database.  Of course I didn&#8217;t want to hard code the contants, I wanted them to be magically created by what was in the database.  For the sake of this example I [...]]]></description>
			<content:encoded><![CDATA[<p>The other day @ $work I was throwing together a module that creates constants based on a fairly static table in the database.  Of course I didn&#8217;t want to hard code the contants, I wanted them to be magically created by what was in the database.  For the sake of this example I had a table called &#8220;toy_categories&#8221; where each record has a unique ID (toy_category_id) and a name that can only be letters and underscores.  The data would look something like:</p>
<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ID &nbsp;NAME<br />
-- &nbsp;--------------<br />
12 &nbsp;dolls<br />
&nbsp;7 &nbsp;action_figures<br />
92 &nbsp;education</div></div>
<p>And the resulting constants would look something like this:</p>
<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">TOY_CATEGORY_DOLLS = 12<br />
TOY_CATEGORY_ACTION_FIGURES = 7<br />
TOY_CATEGORY_EDUCATION = 92</div></div>
<p>If you read the core perl documents you might be lead to think that the <a href="http://perldoc.perl.org/constant.html">constant</a> pragma is the way to go.  This is not the case &#8211; just because a particular library is distributed with Perl does not mean its a good tool, instead it usually means that the library cannot be removed or substantially modified for backwards-compatibility reasons.  In my experience most core libraries are to be avoided &#8211; there are much better solutions on CPAN.</p>
<p>So, the CPAN solution to constants is <a href="http://search.cpan.org/perldoc?Readonly">Readonly</a>.  This module hooks in to Perl&#8217;s ability to mark a variable as read-only, much like how variables can be flagged as tainted or scalars as UTF.  Make sure you grab <a href="http://search.cpan.org/perldoc?Readonly::XS">Readonly::XS</a> as well to get the full benefit of read-only variables without the performance hit.</p>
<p>So, if you were to create a module for these constants you might do something like this:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> MyApp<span style="color: #339933;">::</span><span style="color: #006600;">Constants</span><span style="color: #339933;">::</span><span style="color: #006600;">ToyCategories</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> Readonly<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Exporter <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span> <a href="http://perldoc.perl.org/functions/import.html"><span style="color: #000066;">import</span></a> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">our</span> <span style="color: #0000ff;">@EXPORT</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$TOY_CATEGORY_DOLLS</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$TOY_CATEGORY_ACTION_FIGURES</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$TOY_CATEGORY_EDUCATION</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
Readonly <span style="color: #b1b100;">our</span> <span style="color: #0000ff;">$TOY_CATEGORY_DOLLS</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">12</span><span style="color: #339933;">;</span><br />
Readonly <span style="color: #b1b100;">our</span> <span style="color: #0000ff;">$TOY_CATEGORY_ACTION_FIGURES</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">;</span><br />
Readonly <span style="color: #b1b100;">our</span> <span style="color: #0000ff;">$TOY_CATEGORY_EDUCATION</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">92</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></div></div>
<p>And then in some module you can access these constants:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">use</span> MyApp<span style="color: #339933;">::</span><span style="color: #006600;">Constants</span><span style="color: #339933;">::</span><span style="color: #006600;">ToyCategories</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$toy</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">category_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">$TOY_CATEGORY_EDUCATION</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$response</span> <span style="color: #339933;">=</span> ask_question<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Are you an educator?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Now, like I said, I don&#8217;t want to hardcode the constants.  I want them to be dynamically created by records in my toy_categories table which resides in my database.  Its actually pretty simple to do this with some tricky evals:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> MyApp<span style="color: #339933;">::</span><span style="color: #006600;">Constants</span><span style="color: #339933;">::</span><span style="color: #006600;">ToyCategories</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> Readonly<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Exporter<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Exporter <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span> <a href="http://perldoc.perl.org/functions/import.html"><span style="color: #000066;">import</span></a> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">our</span> <span style="color: #0000ff;">@EXPORT</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> code_that_returns_a_dbi_handle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/q.html"><span style="color: #000066;">q</span></a><span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SELECT toy_category_id<span style="color: #339933;">,</span> name<br />
&nbsp; &nbsp; &nbsp; &nbsp; FROM toy_categories<br />
&nbsp; &nbsp; <span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">bind_columns</span><span style="color: #009900;">&#40;</span> \<span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$id</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$name</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _export_variable<span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;toy_category_$name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$id</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> _export_constant <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$variable</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">$variable</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'$'</span> <span style="color: #339933;">.</span> <a href="http://perldoc.perl.org/functions/uc.html"><span style="color: #000066;">uc</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$variable</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$error</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$failed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/local.html"><span style="color: #000066;">local</span></a> <span style="color: #0000ff;">$@</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$failed</span> <span style="color: #339933;">=</span> <span style="color: #b1b100;">not</span> <a href="http://perldoc.perl.org/functions/eval.html"><span style="color: #000066;">eval</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Readonly our $variable =&gt; <span style="color: #000099; font-weight: bold;">\$</span>value&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$error</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$@</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$failed</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">&quot;Unable to create constant $variable: $error&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></div></div>
<p>What&#8217;s going on here?  Let&#8217;s start at the top:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">use</span> Exporter <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span> <a href="http://perldoc.perl.org/functions/import.html"><span style="color: #000066;">import</span></a> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>This is the least intrusive way of using exporter and doesn&#8217;t pollute your namespace nearly as much as &#8216;use base qw( Exporter );&#8217; does and is friendlies to other modules.</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> code_that_returns_a_dbi_handle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Replace this with whatever you use to get a DBI database handle.  Take a look at <a href="http://search.cpan.org/perldoc?DBIx::Connector">DBIx::Connector</a> for a great alternative to doing this directly with DBI.</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/q.html"><span style="color: #000066;">q</span></a><span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SELECT toy_category_id<span style="color: #339933;">,</span> name<br />
&nbsp; &nbsp; &nbsp; &nbsp; FROM toy_categories<br />
&nbsp; &nbsp; <span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">bind_columns</span><span style="color: #009900;">&#40;</span> \<span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$id</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$name</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">...</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>I&#8217;m a big fan of writing my DBI selects in this fashion with bind_columns() because it tends to be the fastest way to get the data (versus fetchrow_hashref, etc) and tends to lead to the simplest code within the while loop since it just needs to use $id and $name versus $row->{$id} and $row->{name} (for example).</p>
<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; _export_variable( &quot;toy_category_$name&quot; =&gt; $id );</div></div>
<p>While all of the code within _export_variable() could just be inlined right in the spot, that&#8217;s bad design.  If you can get a piece of your code generalized and moved to a subroutine, do it.</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #0000ff;">$variable</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'$'</span> <span style="color: #339933;">.</span> <a href="http://perldoc.perl.org/functions/uc.html"><span style="color: #000066;">uc</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$variable</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Constants should always, due to convention, be uppercase.  It is very important that you code to popular conventions because other people will most likely be working on your code later and if you come from a common expectation of how various things are done they will have an easier time ramping up to your code.  I try to code to the Modern Perl / CPAN conventions.</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$error</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$failed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/local.html"><span style="color: #000066;">local</span></a> <span style="color: #0000ff;">$@</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$failed</span> <span style="color: #339933;">=</span> <span style="color: #b1b100;">not</span> <a href="http://perldoc.perl.org/functions/eval.html"><span style="color: #000066;">eval</span></a> <span style="color: #339933;">...;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$error</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$@</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$failed</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span></div></div>
<p>This bit of code was taken from <a href="http://blog.woobling.org/2009/09/trytiny.html">nothingmuch&#8217;s blog entry</a> where he announces <a href="http://search.cpan.org/perldoc?Try::Tiny">Try::Tiny</a> which provides a safe way to handle eval errors.  Read up there if you want to understand why the code was written this way.</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$failed</span> <span style="color: #339933;">=</span> <span style="color: #b1b100;">not</span> <a href="http://perldoc.perl.org/functions/eval.html"><span style="color: #000066;">eval</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Readonly our $variable =&gt; <span style="color: #000099; font-weight: bold;">\$</span>value&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>While this code is several blocks deep in scope, this constant will exist in the package&#8217;s scope since it is being declared with &#8216;our&#8217;.</p>
<p>That&#8217;s it.  After this was implemented I thought it was so useful, simple, but requiring the knowledge of a few tricks, that I&#8217;d share it with ya&#8217;all.  Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bluefeet.net/2010/05/generating-constants-in-perl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Apache Cassandra and the Thrift API on CPAN</title>
		<link>http://blog.bluefeet.net/2010/04/apache-cassandra-and-the-thrift-api-on-cpan/</link>
		<comments>http://blog.bluefeet.net/2010/04/apache-cassandra-and-the-thrift-api-on-cpan/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 00:25:13 +0000</pubDate>
		<dc:creator>bluefeet</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cassandra]]></category>
		<category><![CDATA[CPAN]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[Perl 5]]></category>
		<category><![CDATA[Thrift]]></category>

		<guid isPermaLink="false">http://blog.bluefeet.net/?p=224</guid>
		<description><![CDATA[I&#8217;m currently playing around with a project that uses Apache Cassandra.  The Cassandra website states:
&#8220;The Apache Cassandra Project develops a highly scalable second-generation distributed database, bringing together  Dynamo&#8217;s fully distributed design and Bigtable&#8217;s ColumnFamily-based data model.&#8221;
So, from what I&#8217;ve experienced so far this is either a distributed Berkely DB, or a persistent Memcached, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently playing around with a project that uses <a href="http://cassandra.apache.org/">Apache Cassandra</a>.  The Cassandra website states:</p>
<p><em>&#8220;The Apache Cassandra Project develops a highly scalable second-generation distributed database, bringing together  Dynamo&#8217;s fully distributed design and Bigtable&#8217;s ColumnFamily-based data model.&#8221;</em></p>
<p>So, from what I&#8217;ve experienced so far this is either a distributed Berkely DB, or a persistent Memcached, depending on which concept you are more familiar with (I know, that&#8217;s incredibly over simplified).</p>
<p>Cassandra was developed by Facebook and was made open source several years ago.  It uses the <a href="http://incubator.apache.org/thrift/">Thrift API</a> to communicate, which was also developed by Facebook.  Supposedly there are some steps being taken to move Cassandra on to a different API, but Thrift is the defacto for now.</p>
<p>There are two modules on CPAN that deal with Cassandra.  <a href="http://search.cpan.org/perldoc?Net::Cassandra">Net::Cassandra</a> is the original, and in my opinion, the superior/simpler/easier module.  <a href="http://search.cpan.org/perldoc?Net::Cassandra::Easy">Net::Cassandra::Easy</a> is a later implementation that really isn&#8217;t any simpler from what I can tell and neither seems any more or less tied to the intricacies of the Thrift API despite Net::Cassandra::Easy&#8217;s accusations of Net::Cassandra.  Both implement the Thrift API under the scenes in their own way, and both are lacking in features.</p>
<p>Now, the Thrift API itself is distributed with a perl library called simply &#8220;Thrift&#8221;.  This library is very new and it shows.  It needs a lot of work, a lot of automated tests, and IMO should be on CPAN.  I consider it a fail when a project decides to distribute Perl libraries independently from CPAN.  This leads to very little exposure &#8211; I was lucky I found the perl library in the Thrift tarball, I doubt others have been as lucky.</p>
<p>So, I&#8217;d like to make a Thrift driver library and put it on CPAN.  Then, a truly Thrift independent Cassandra client library can be written that uses the Thrift library behind the scenes, but is not directly tied in with it.  Later, when Cassandra decides to change their API library, the Cassandra library can be changed to use a new driver.</p>
<p>An additional benefit to this is if any Cassandra features are not yet available via the Cassandra module, then the perl developer can easily drop down to the Thrift driver and directly write Thrift queries.</p>
<p>Has anyone thought of this, or perhaps begun working on something like this?  If not, I&#8217;d like to do it and I&#8217;d love to hear any suggestions and thoughts people have.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bluefeet.net/2010/04/apache-cassandra-and-the-thrift-api-on-cpan/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>use HTML::FormHandler;</title>
		<link>http://blog.bluefeet.net/2009/10/use-htmlformhandler/</link>
		<comments>http://blog.bluefeet.net/2009/10/use-htmlformhandler/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 16:00:45 +0000</pubDate>
		<dc:creator>bluefeet</dc:creator>
				<category><![CDATA[Reviews]]></category>
		<category><![CDATA[CPAN]]></category>
		<category><![CDATA[Moose]]></category>
		<category><![CDATA[Perl 5]]></category>

		<guid isPermaLink="false">http://blog.bluefeet.net/?p=206</guid>
		<description><![CDATA[I&#8217;ve been meaning to give HTML::FormHandler a try for quite some time now.  Over the weekend I converted a form from some home-grown form validation and html generation to use HTML::FormHandler instead.  HTML::FormHandler was inspired by, and quasi-forked from, Form::Processor.  There are many options for processing forms, such as FormValidator::Simple (Catalyst&#8217;s default) [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been meaning to give <a href="http://search.cpan.org/perldoc?HTML::FormHandler">HTML::FormHandler</a> a try for quite some time now.  Over the weekend I converted a form from some home-grown form validation and html generation to use HTML::FormHandler instead.  HTML::FormHandler was inspired by, and quasi-forked from, <a href="http://search.cpan.org/perldoc?Form::Processor">Form::Processor</a>.  There are many options for processing forms, such as <a href="http://search.cpan.org/perldoc?FormValidator::Simple">FormValidator::Simple</a> (Catalyst&#8217;s default) and <a href="http://search.cpan.org/perldoc?Data::FormValidator">Data::FormValidator</a>, to name a few.</p>
<p>Up until now I&#8217;ve always thought that the existing form handling/validating modules on CPAN were incredibly lacking.  They all have these crazy and inconsistent APIs that in the end limit what you are able to do.  I don&#8217;t expect a form validation API to handle every possible situation, but I do expect it to provide a consistent API that is well documented and allows me to easily extend if I need additional functionality.  Plus none of them use Moose (AFAIK), which isn&#8217;t just me being a Moose evangelist, its also practical because Moose provides mechanisms to extend functionality via roles and to provide reusable validation with <a href="http://search.cpan.org/perldoc?MooseX::Types">MooseX::Types</a>.</p>
<p>HTML::FormHandler solves these issues.  HTML::FormHandler can be a complete end-to-end solution for declaring form fields and types, applying server-side validation, generating the HTML for a form, and applying form submits to the database.  Almost everything and the kitchen sink is there, except client-side form validation, which can be added separately of course.  Normally a &#8220;do everything&#8221; package scares me, especially for something like form validation.  But, Gerda Shank (the author of HTML::FormHandler) did a great job splitting out the various pieces of HTML::FormHandler so that any piece of the form handling process can be subverted to your own needs, and there is <a href="http://search.cpan.org/perldoc?HTML::FormHandler::Manual">copious amounts of documentation</a> explaining how to do so.</p>
<p>The <a href="http://search.cpan.org/perldoc?HTML::FormHandler::Manual::Intro">HTML::FormHandler::Manual::Intro</a> does a great job introducing HTML::FormHandler.</p>
<p>There are a few downsides to this module&#8217;s current state.  It is relatively new and is still stabilizing.  For example, just recently the results of form validation were separated from the form object itself.  This is a huge fundamental change, but a necessary one.  Also, the concept of widgets was recently introduced.  Widgets are a great way to extend and customize the display of form fields, and the form as a whole.  But, this was just introduced and is still experimental, and has some rough edges.  Despite this, the widget design is very promising and I ended up using it myself (thanks to <a href="http://search.cpan.org/perldoc?HTML::FormHandler::Manual::Rendering">HTML::FormHandler::Manual::Rendering</a>) as my preferred rendering method.</p>
<p>Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bluefeet.net/2009/10/use-htmlformhandler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
