<?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"
	>

<channel>
	<title>Joey Mazzarelli</title>
	<atom:link href="http://joey.mazzarelli.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://joey.mazzarelli.com</link>
	<description>Escalator Temporarily Stairs</description>
	<pubDate>Wed, 26 Nov 2008 05:54:46 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
	<language>en</language>
			<item>
		<title>Easy Date parsing with JavaScript</title>
		<link>http://joey.mazzarelli.com/2008/11/25/easy-date-parsing-with-javascript/</link>
		<comments>http://joey.mazzarelli.com/2008/11/25/easy-date-parsing-with-javascript/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 05:51:35 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
		
		<category><![CDATA[Coding]]></category>

		<category><![CDATA[date]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[strtotime]]></category>

		<guid isPermaLink="false">http://joey.mazzarelli.com/?p=80</guid>
		<description><![CDATA[Date.fromString()
For the impatient, scroll to the bottom of the page for an interactive example.
Date.fromString() is a method that allows you to easily parse user input into a date. There are currently many implementations of calendar pop-ups that aim to solve a similar problem. However, these calendars are cumbersome to use if the target date is [...]]]></description>
			<content:encoded><![CDATA[<h2>Date.fromString()</h2>
<p>For the impatient, scroll to the bottom of the page for an interactive example.</p>
<p><tt>Date.fromString()</tt> is a method that allows you to easily parse user input into a date. There are currently many implementations of calendar pop-ups that aim to solve a similar problem. However, these calendars are cumbersome to use if the target date is more than a couple months away. The goal of this project is to parse free-form user input into a valid Date object.</p>
<p><tt>Date.fromString()</tt> accepts two parameters:</p>
<ul>
<li><tt><strong>input</strong></tt><br />
    <tt>string</tt> &mdash; User input string to parse
  </li>
<li><tt><strong>options</strong></tt> <em>optional</em><br />
    <tt>object</tt> &mdash; An object that can override the default behavior<br />
    <tt>{ order: &#8216;MDY&#8217;, strict: false }</tt>
  </li>
</ul>
<p><strong>Examples</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Sept 5th, 2006  4:00 pm'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// Tue Sep 05 2006 16:00:00 GMT-0600 (MDT)</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Sept 5th, 2006  4:23pm'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// Tue Sep 05 2006 16:23:00 GMT-0600 (MDT)</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Sept 5th, 2006  4pm'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// Tue Sep 05 2006 16:00:00 GMT-0600 (MDT)</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Sept 5th, 2006  12:34:56 am'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// Tue Sep 05 2006 00:34:56 GMT-0600 (MDT)</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Sept 5th, 2006  7:30 am'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// Tue Sep 05 2006 07:30:00 GMT-0600 (MDT)</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2006/09/05  4:23pm'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// Tue Sep 05 2006 16:23:00 GMT-0600 (MDT)</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2006/09/05'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// Tue Sep 05 2006 00:00:00 GMT-0600 (MDT)</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'9/5/06'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">//Tue Sep 05 2006 00:00:00 GMT-0600 (MDT)</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2/13/78'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// Mon Feb 13 1978 00:00:00 GMT-0700 (MST)</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Feb 13 09'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// Fri Feb 13 2009 00:00:00 GMT-0700 (MST)</span></pre></div></div>

<p>When part of the date is left off, it fills in the missing parts with today&#8217;s details. When these examples were written, it was November 25, 2008.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Feb 13th'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// Wed Feb 13 2008 00:00:00 GMT-0700 (MST)</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2/13'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// Wed Feb 13 2008 00:00:00 GMT-0700 (MST)</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'4pm'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #006600; font-style: italic;">// Tue Nov 25 2008 16:00:00 GMT-0700 (MST)</span></pre></div></div>

<p>If you would rather have the date not fill in those parts, pass the option <tt>strict</tt> set to <tt>true</tt>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'4pm'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #006600; font-style: italic;">// Tue Nov 25 2008 16:00:00 GMT-0700 (MST)</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'4pm'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>strict<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #006600; font-style: italic;">// Invalid Date</span></pre></div></div>

<p>Sometimes the date cannot be parsed without ambiguity. In these cases, the default ordering of month/day/year (<tt>MDY</tt>) is used. To change this behavior, pass the option <tt>order</tt> set to your preferred ordering; <tt>YMD</tt>, <tt>DMY</tt>, etc.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'09/05/06'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// Tue Sep 05 2006 00:00:00 GMT-0600 (MDT)</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'09/05/06'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>order<span style="color: #339933;">:</span> <span style="color: #3366CC;">'YMD'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// Wed May 06 2009 00:00:00 GMT-0600 (MDT)</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'09/05/06'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>order<span style="color: #339933;">:</span> <span style="color: #3366CC;">'DMY'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// Tue May 09 2006 00:00:00 GMT-0600 (MDT)</span></pre></div></div>

<p>Finally, try it for yourself. I have not been able to test this much in browsers other than Firefox on Linux, so if you find any bugs, please let me know.</p>
<p><fieldset><legend>Options</legend></p>
<input type="text" maxlength="3" style="width:50px;" id="date_order" value="MDY" />
<input type="checkbox" id="date_strict" /><label for="date_strict">Strict</label><br />
</fieldset><br />
<fieldset><legend>Input</legend></p>
<input type="text" id="date_input" value="Dec. 31st, 1999" />
<button id="create_date">Create Date</button><br />
</fieldset><br />
<strong>Output:</strong> <span id="date_output" style="background-color:#ffc;padding:2px"></span></p>
<p><script type="text/javascript" src="http://www.google.com/jsapi"></script><br />
<script type="text/javascript" src="/js/date.js"></script><br />
<script type="text/javascript">
google.load("jquery", "1.2.6");
google.setOnLoadCallback(function() {
var f = function () {
  var opts = {order:$('#date_order').val(), strict:$('#date_strict').attr('checked')};
  $('#date_output').html(String(Date.fromString($('#date_input').val(), opts)));
};
f();
$('#create_date').click(f);
});
</script></p>
<p><a class="dlimg" href="http://joey.mazzarelli.com/wp-content/plugins/download-monitor/download.php?id=2" title="Download Javascript Date.fromString() Version 1.1"><img src="http://joey.mazzarelli.com/wp-content/plugins/download-monitor/img/download.gif" alt="Download Javascript Date.fromString() Version 1.1" /></a></p>
<p class="dlstat">Downloaded a total of 84 times</p>
<p><a href="http://joey.mazzarelli.com/wp-content/plugins/download-monitor/download.php?id=2" title="Version 1.1 downloaded 84 times" >Javascript Date.fromString() (5.27 KB)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joey.mazzarelli.com/2008/11/25/easy-date-parsing-with-javascript/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Runtime PHP Annotations. What a tease.</title>
		<link>http://joey.mazzarelli.com/2008/05/30/runtime-php-annotations-what-a-tease/</link>
		<comments>http://joey.mazzarelli.com/2008/05/30/runtime-php-annotations-what-a-tease/#comments</comments>
		<pubDate>Fri, 30 May 2008 06:03:53 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
		
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://joey.mazzarelli.com/?p=19</guid>
		<description><![CDATA[Today at work I found myself on the tedious side of software development. I mainly develop in PHP, and PHP lacks many things that would otherwise make it an enjoyable language to use. It seems like the mantra of the PHP developers and community as a whole is &#8220;half-assed&#8221; (in all due respect, of course [...]]]></description>
			<content:encoded><![CDATA[<p>Today at work I found myself on the tedious side of software development. I mainly develop in PHP, and PHP lacks many things that would otherwise make it an enjoyable language to use. It seems like the mantra of the PHP developers and community as a whole is &#8220;half-assed&#8221; <small>(in all due respect, of course <img src='http://joey.mazzarelli.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> )</small>.</p>
<p>Let me explain. If you want to use a library, tool, or feature in a way that the original developer didn&#8217;t care about or think about, then odds are that the documentation will claim it will work, but it won&#8217;t. It will work for the one case the developer tested, but that is it. Of course there are also many projects and features out there that will prove me wrong, and kudos to those ones.</p>
<p>I am getting sidetracked. Today I wanted annotations in PHP. I wanted to provide meta data for the methods and classes I was writing.</p>
<p>It would be easy enough to write a code generator that parses the source, handles the annotations, and spits out the desired code. In fact, many things already do this, like documenters and style checkers. However, that is not what I want. I want it done at runtime and I want it to change the behavior of the code.</p>
<p>Sometimes I have to back up my complaints with some action, so today I implemented this feature. In an entirely proof-of-concept way, but a real solution for runtime PHP annotations nonetheless.</p>
<p>Since examples speak louder than words, here we go.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #990000;">Pi</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-style: italic;">/**
   * Use the monte carlo method of estimating PI
   * @return float
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> estimate <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$rounds</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1000000</span>;
    <span style="color: #000088;">$hits</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0</span>;
    <span style="color: #000088;">$max</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mt_getrandmax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color:#800080;">0</span>; <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$rounds</span>; <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mt_rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">1.0</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$max</span>;
      <span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mt_rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">1.0</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$max</span>;
      <span style="color: #000088;">$hits</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">*</span><span style="color: #000088;">$x</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$y</span><span style="color: #339933;">*</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color:#800080;">1.0</span><span style="color: #009900;">&#41;</span>? <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> <span style="color:#800080;">0</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color:#800080;">4.0</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$hits</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$rounds</span>;
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Consistent return value</span>
<span style="color: #990000;">mt_srand</span><span style="color: #009900;">&#40;</span><span style="color: #208080;">0xDEAD</span><span style="color: #009900;">&#41;</span>;
<span style="color: #990000;">echo</span> <span style="color: #990000;">Pi</span><span style="color: #339933;">::</span><span style="color: #004000;">estimate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #990000;">mt_srand</span><span style="color: #009900;">&#40;</span><span style="color: #208080;">0xDEAD</span><span style="color: #009900;">&#41;</span>;
<span style="color: #990000;">echo</span> <span style="color: #990000;">Pi</span><span style="color: #339933;">::</span><span style="color: #004000;">estimate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</pre></div></div>

<p>Does anyone else enjoy naming a php variable &#8216;hit&#8217; or &#8216;hits&#8217;? Anyway, for this example I wanted a function that would take some time to compute. Admittedly contrived, but I didn&#8217;t ask you to read this post. Using the monte carlo method, this static class method estimates the value of PI and is called twice.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">time</span> php Pi.php 
<span style="color: #000000;">3.141196</span>
<span style="color: #000000;">3.141196</span>
&nbsp;
real    0m2.186s</pre></div></div>

<p>Obviously, if many calls are made to this method, the time adds up quickly. No one in their right mind would actually release code like this. I said right mind. Something I find myself doing often is caching the result before returning it. I&#8217;m not going to show that example since it is trivial to implement. Clearly all subsequent calls to the time-consuming method would return immediately.</p>
<p>I don&#8217;t like it. I do it, but I don&#8217;t like it. The method starts out very clean. All it does is estimate the value of PI (or search a graph, or prune a tree, whatever). When you add caching into it, suddenly the method is doing two things. And two completely unrelated things at that. This should bother you at least a little bit. Off the top of my head, I can avoid this approach by:</p>
<ul>
<li>Relying on consumer to cache the results. I don&#8217;t think so.</li>
<li>Extending the class with a new &#8220;Caching class&#8221;. Yuck. Not to mention the example above uses a static method.</li>
<li>Moving the two routines into to separate methods: <tt>public getEstimate()</tt> and <tt>private calculateEstimate()</tt>. <tt>getEstimate()</tt> would do the caching and call <tt>calculateEstimate()</tt> internally. Ok, but pretty tedious and causes the code to grow quickly.</li>
<li>Use annotations to modify the runtime behavior of the method.</li>
</ul>
<p>What I really want to do is annotate the method, indicating that the operation is idempotent and that the results can be cached. I don&#8217;t want to design a whole framework right now, I&#8217;ll leave that for another day. To see if it can or can&#8217;t be done, I just want to implement this single feature for now.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #990000;">Pi</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-style: italic;">/**
   * Use the monte carlo method of estimating PI.
   * @cache
   * @return float
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> estimate <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$rounds</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1000000</span>;
    <span style="color: #000088;">$hits</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0</span>;
    <span style="color: #000088;">$max</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mt_getrandmax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color:#800080;">0</span>; <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$rounds</span>; <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mt_rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">1.0</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$max</span>;
      <span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mt_rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">1.0</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$max</span>;
      <span style="color: #000088;">$hits</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">*</span><span style="color: #000088;">$x</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$y</span><span style="color: #339933;">*</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color:#800080;">1.0</span><span style="color: #009900;">&#41;</span>? <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> <span style="color:#800080;">0</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color:#800080;">4.0</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$hits</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$rounds</span>;
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="">'Annotations.php'</span>;
Annotations<span style="color: #339933;">::</span><span style="color: #004000;">annotate</span><span style="color: #009900;">&#40;</span><span style="">'Pi'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// Consistent return value</span>
<span style="color: #990000;">mt_srand</span><span style="color: #009900;">&#40;</span><span style="color: #208080;">0xDEAD</span><span style="color: #009900;">&#41;</span>;
<span style="color: #990000;">echo</span> <span style="color: #990000;">Pi</span><span style="color: #339933;">::</span><span style="color: #004000;">estimate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #990000;">mt_srand</span><span style="color: #009900;">&#40;</span><span style="color: #208080;">0xDEAD</span><span style="color: #009900;">&#41;</span>;
<span style="color: #990000;">echo</span> <span style="color: #990000;">Pi</span><span style="color: #339933;">::</span><span style="color: #004000;">estimate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</pre></div></div>

<p>Ideally, I&#8217;d like to add just that one line to the documentation comment above the method. Of course I&#8217;d need some way to trigger the parsing, so I put that at the end for the sake of the example. The code of the method remains solely dedicated to estimating the value of PI. I&#8217;ve only hinted that at runtime, it&#8217;d be great if the results were only calculated once.</p>
<p>It took less than twenty lines of code to implement this annotation. The methods of the class are retrieved, the annotation is searched for, and if found, the code of the method is altered to cache the results. <small>(Actually I created a new class method that does the same thing, and rewrote the original one to do the caching only, calling the new method for the calculated value.)</small></p>
<p>Then I ran it, and sure enough it runs in half the time.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">time</span> php Pi.3.php 
<span style="color: #000000;">3.141196</span>
<span style="color: #000000;">3.141196</span>
&nbsp;
real    0m1.175s</pre></div></div>

<p>By the way, there are many attempts at a solution out there, none of which do what I want from what I can tell in 30 seconds of reading their summaries. The PHP reflection API seems to be &#8220;look but don&#8217;t touch&#8221;. I want to grope. I want a <tt>[@deprecated]</tt> method to raise a warning when the method is called. Likewise for a <tt>[@testing]</tt> method. I want my <tt>[@cache]</tt>. I want annotations for parameter validation&#8230; maybe that is a little too far, but you get the idea. The solutions I have seen let you poke and prod, but they don&#8217;t let you add/change functionality to the running code. Implementing these types of annotations would be pretty straightforward to do with the method I used above.</p>
<p>Since I made it work, and claim it was easy, why am I complaining? The problem is that the features I had to use to get it working are marked as unstable, unmaintained, subject to change, and risky to rely on for critical production code. The runkit extension doesn&#8217;t even compile for newer versions of PHP and some features even segfault.</p>
<p>So close. Half-assed.</p>
]]></content:encoded>
			<wfw:commentRss>http://joey.mazzarelli.com/2008/05/30/runtime-php-annotations-what-a-tease/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Making a useful JavaScript library SUPER-USEFUL!</title>
		<link>http://joey.mazzarelli.com/2008/03/26/making-a-useful-javascript-library-super-useful/</link>
		<comments>http://joey.mazzarelli.com/2008/03/26/making-a-useful-javascript-library-super-useful/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 06:31:41 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
		
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://joey.mazzarelli.com/2008/03/26/making-a-useful-javascript-library-super-useful/</guid>
		<description><![CDATA[Everyone likes (or should like) succinct-yet-readable code. Here is a technique you can use for taking your javascript modules and making them even more fun to use.

The following is a useful javascript module for getting and setting cookies. (It uses jQuery.) The usage is as straightforward as it gets. It provides three useful functions...]]></description>
			<content:encoded><![CDATA[<p>Everyone likes (or should like) succinct-yet-readable code. Here is a technique you can use for taking your javascript modules and making them even more fun to use.</p>
<p>The following is a useful javascript module for getting and setting cookies. (It uses <a href="http://www.jquery.com">jQuery</a>.) The usage is as straightforward as it gets. It provides three useful functions:</p>
<ol>
<li><tt>Cookies.get(name);</tt></li>
<li><tt>Cookies.set(name, value, [options]);</tt></li>
<li><tt>Cookies.remove(name);</tt></li>
</ol>
<p>In addition, it provides the function <tt>Cookies.all()</tt> for debugging purposes.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Cookies <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  options <span style="color: #339933;">:</span>
    <span style="color: #009900;">&#123;</span> path   <span style="color: #339933;">:</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">,</span>
      expiry <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// Days</span>
      secure <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">location</span>.<span style="color: #660066;">protocol</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'https:'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  set <span style="color: #339933;">:</span>
    <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> value<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> opts <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>;
      jQuery.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>opts<span style="color: #339933;">,</span> Cookies.<span style="color: #660066;">options</span><span style="color: #339933;">,</span> options || <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
      options <span style="color: #339933;">=</span> opts;
&nbsp;
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">expiry</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// convert number of days from now to string</span>
        options.<span style="color: #660066;">expiry</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span>
            <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">expiry</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">86400000</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toGMTString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// Build the cookie string</span>
      document.<span style="color: #660066;">cookie</span> <span style="color: #339933;">=</span> <span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'='</span> <span style="color: #339933;">+</span> value <span style="color: #339933;">+</span>
          <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">expiry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span> <span style="color: #3366CC;">'; expires='</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">expiry</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>
          <span style="color: #339933;">+</span> <span style="color: #3366CC;">'; path='</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">path</span> <span style="color: #339933;">+</span> 
          <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">secure</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span> <span style="color: #3366CC;">'; secure=true'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  get <span style="color: #339933;">:</span>
    <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> $.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">grep</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">';'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>cookie<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">return</span> $.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span>cookie<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>0<span style="color: #339933;">,</span> <span style="color: #000066;">name</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066;">name</span>;
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span> || <span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'='</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  remove <span style="color: #339933;">:</span>
    <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> Cookies.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  all <span style="color: #339933;">:</span>
    <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">';'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">trim</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>; <span style="color: #006600; font-style: italic;">// end Cookies</span></pre></div></div>

<p>So what&#8217;s the big deal? The first weakness of this code is that the default options for setting a cookie are modifiable by the world. I don&#8217;t want your code changing settings for my code, nor mine for yours. This wouldn&#8217;t be too hard to fix by placing them as a local variable inside the <tt>set</tt> function. But what we&#8217;d really like is some sort of read-only access to those default options.</p>
<p>The second issue is more interesting. If you notice, there are four functions. And each of the functions takes a unique set of parameters. If this were Java, we could (but ought not to) implement this all with polymorphism. Sounds quite dumb, but bear with me. If the object <tt>Cookies</tt> were <em>also</em> a function, we could create a dispatcher and remove the need for explicit calls to the individual methods. Instead of <tt>Cookies.get(&#8217;something&#8217;)</tt>, we could just call <tt>Cookie(&#8217;something&#8217;)</tt>. Likewise for all the methods. Of course, you&#8217;d still be able to call the function by its full name. Let&#8217;s see it.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Create a dispatcher</span>
<span style="color: #003366; font-weight: bold;">var</span> Cookies <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> value<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// Case 1: no parameters supplied, dump the cookies.</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> Cookies.<span style="color: #660066;">all</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
  <span style="color: #006600; font-style: italic;">// Case 2: null value, delete cookie</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>value <span style="color: #339933;">===</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> Cookies.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span>;
&nbsp;
  <span style="color: #006600; font-style: italic;">// Case 3: name supplied only, so get value</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>value <span style="color: #339933;">==</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> Cookies.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span>;
&nbsp;
  <span style="color: #006600; font-style: italic;">// Case 4: set a cookie with a value</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> Cookies.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> value<span style="color: #339933;">,</span> options || <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>; <span style="color: #006600; font-style: italic;">// end dispatcher</span>
&nbsp;
$.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Cookies<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Create the private variables</span>
  <span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    path   <span style="color: #339933;">:</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">,</span>
    expiry <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// Days</span>
    secure <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">location</span>.<span style="color: #660066;">protocol</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'https:'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>;
&nbsp;
  <span style="color: #006600; font-style: italic;">// Create the public methods</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
    defaults <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> defaults;
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
    set <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> value<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> opts <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>;
      jQuery.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>opts<span style="color: #339933;">,</span> defaults<span style="color: #339933;">,</span> options || <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
      options <span style="color: #339933;">=</span> opts;
&nbsp;
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">expiry</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// convert number of days from now to string</span>
        options.<span style="color: #660066;">expiry</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span>
            <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">expiry</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">86400000</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toGMTString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// Build the cookie string</span>
      document.<span style="color: #660066;">cookie</span> <span style="color: #339933;">=</span> <span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'='</span> <span style="color: #339933;">+</span> value <span style="color: #339933;">+</span>
          <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">expiry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span> <span style="color: #3366CC;">'; expires='</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">expiry</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>
          <span style="color: #339933;">+</span> <span style="color: #3366CC;">'; path='</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">path</span> <span style="color: #339933;">+</span> 
          <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">secure</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span> <span style="color: #3366CC;">'; secure=true'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
    get <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> $.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">grep</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">';'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
        <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>cookie<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">return</span> $.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span>cookie<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>0<span style="color: #339933;">,</span> <span style="color: #000066;">name</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066;">name</span>;
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span> || <span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'='</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
    remove <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> Cookies.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> expiry<span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
    all <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">';'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">trim</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>;
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>The first thing you&#8217;ll notice is the dispatcher function. The does the simple logic for mapping the parameter combinations to the intended function. Then we extend the <tt>Cookies</tt> objects (remember, functions are just objects) with a new object. If you look, you&#8217;ll see that it is extended with a function that is immediately applied and returns an object. The advantage to doing this is that we can create closures, and encompass some &#8220;private&#8221; variables for those methods. Look at the defaults for an example of this. I added another function for inspecting the defaults, but you cannot modify them.</p>
<p>Now this library can be used as:</p>
<ul>
<li><tt>Cookies.get(name)</tt> <em>or just</em> <tt>Cookies(name)</tt></li>
<li><tt>Cookies.set(name, value, [options])</tt> <em>or just</em> <tt>Cookies(name, value, [options])</tt></li>
<li><tt>Cookies.remove(name)</tt> <em>or just</em> <tt>Cookies(name, null)</tt></li>
<li><tt>Cookies.all()</tt> <em>or just</em> <tt>Cookies()</tt></li>
<li><tt>Cookies.defaults()</tt></li>
</ul>
<p>Hopefully this makes sense. If it gives you any good ideas, let me know.</p>
<p>Oh, and if you really were looking for a cookie library for javascript, you can download and use this (documented and commented) one if you have <a href="http://jquery.com">jQuery</a> installed.</p>
<p><a class="dlimg" href="http://joey.mazzarelli.com/wp-content/plugins/download-monitor/download.php?id=1" title="Download Javascript Cookies Version 1.0"><img src="http://joey.mazzarelli.com/wp-content/plugins/download-monitor/img/download.gif" alt="Download Javascript Cookies Version 1.0" /></a></p>
<p class="dlstat">Downloaded a total of 12 times</p>
<p><a href="http://joey.mazzarelli.com/wp-content/plugins/download-monitor/download.php?id=1" title="Version 1.0 downloaded 12 times" >Javascript Cookies (2.57 KB)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joey.mazzarelli.com/2008/03/26/making-a-useful-javascript-library-super-useful/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Betty Jean, Here I Come!</title>
		<link>http://joey.mazzarelli.com/2008/01/09/betty-jean-here-i-come/</link>
		<comments>http://joey.mazzarelli.com/2008/01/09/betty-jean-here-i-come/#comments</comments>
		<pubDate>Wed, 09 Jan 2008 17:45:14 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
		
		<category><![CDATA[Whatever]]></category>

		<guid isPermaLink="false">http://joey.mazzarelli.com/2008/01/09/betty-jean-here-i-come/</guid>
		<description><![CDATA[About a month ago I was sitting in the waiting room of a doctor&#8217;s office when I an elderly man walked up to the counter to sign in. The receptionist took his information, but after a minute of looking on her schedule she couldn&#8217;t find his name. She soon found it, but he was scheduled [...]]]></description>
			<content:encoded><![CDATA[<p>About a month ago I was sitting in the waiting room of a doctor&#8217;s office when I an elderly man walked up to the counter to sign in. The receptionist took his information, but after a minute of looking on her schedule she couldn&#8217;t find his name. She soon found it, but he was scheduled for the next day. The man was probably a little embarrassed, but I couldn&#8217;t really tell. (He threw the blame on his wife). I sat there and thought to myself, &#8220;That is definitely going to be me one day.&#8221;</p>
<p>Today I had my follow up visit to the same doctor. I went to sign in and the nurse couldn&#8217;t find my name. Who knew &#8220;one day&#8221; would come so soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://joey.mazzarelli.com/2008/01/09/betty-jean-here-i-come/feed/</wfw:commentRss>
		</item>
		<item>
		<title>If JavaScript is so cool, then why does my nephew say it&#8217;s not a real programming langauge?</title>
		<link>http://joey.mazzarelli.com/2007/08/22/if-javascript-is-so-cool-then-why-does-my-nephew-say-its-not-a-real-programming-langauge/</link>
		<comments>http://joey.mazzarelli.com/2007/08/22/if-javascript-is-so-cool-then-why-does-my-nephew-say-its-not-a-real-programming-langauge/#comments</comments>
		<pubDate>Thu, 23 Aug 2007 03:28:56 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
		
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://joey.mazzarelli.com/2007/08/22/if-javascript-is-so-cool-then-why-does-my-nephew-say-its-not-a-real-programming-langauge/</guid>
		<description><![CDATA[Every now and then I am asked which is my favorite programming language. To me that is like choosing which is my favorite kid. Am I going out to play Frisbee, or trying to get them to flip off the car next to us? It all depends, man. But I must say, I like JavaScript.
If [...]]]></description>
			<content:encoded><![CDATA[<p>Every now and then I am asked which is my favorite programming language. To me that is like choosing which is my favorite kid. Am I going out to play Frisbee, or trying to get them to flip off the car next to us? It all depends, man. But I must say, I like JavaScript.</p>
<p>If you are like most people, you just gave me that - &#8220;Did you just <em>fart</em>?&#8221; - look. Yes, JavaScript. Most of the time, my questioner doesn&#8217;t know how to respond, and I imagine they are having flashbacks of rainbow-colored trailing cursors. I figure they think I am joking, so I offer a couple reasons why I like it. I try to explain that JavaScript just does what I want it to do. I think of the best way to explain 1st class functions and closures, but I give up before I start, &#8220;I&#8217;m just kidding. I like Ruby.&#8221;</p>
<p>&#8220;Yeah,&#8221; they nod, &#8220;my cousin has his certification for Ruby with Rails, and he says that in ten years anybody will be able to write web software by just talking to their Mac.&#8221;</p>
<p>&#8220;Have I introduced you to my two year old yet? He&#8217;s good with his hands&#8230;&#8221;</p>
<p>From my experience, anyone that understands any concepts of functional programming generally doesn&#8217;t give a rat&#8217;s ass which language I prefer. (Ruby is my other favorite language.)</p>
<p>I really wish I could more elegantly explain why I like JavaScript, but the examples I come up with are usually contrived and show no benefit to doing it &#8220;that way&#8221;. At work last week, I found myself using closures to solve a problem in a way I felt was a clear example of why I like JavaScript. So, I&#8217;ll share.</p>
<p>Without wandering too far from the topic, I&#8217;ll explain the background of the problem. Often when you are creating an interactive web page, your JavaScript needs to write out HTML. Whether you are updating some status, showing some dialog box or message, or inserting rows into a table, it needs to be done. There are more or less two ways of doing this. Concatenating HTML and variables is ugly, but fast. Creating DOM elements and manipulating the DOM is dreadfully tedious and slow, but clean. Both methods tend to drive me crazy. I personally solve this problem by using <a href="http://prototypejs.org/api/template">Prototype&#8217;s Template object</a>. You don&#8217;t need to be familiar with it, just know that you can create an object, then call an <code>evaluate</code> method on that object to render a template.</p>
<p>I put my HTML snippets into a global collection, and use them from there. This lets me keep the code at least <em>looking</em> clean. This is where the JavaScript fun comes in. There are a million ways to accomplish this, so I am going to throw out some requirements, just to narrow down the number of ways to do this. I don&#8217;t think these are unreasonable.</p>
<ol>
<li>It must be simple. By simple, I don&#8217;t mean easy to understand, just easy to use.  I like succinct.</li>
<li>It must be efficient. I only want one Template object that I can reuse, and it must be instantiated when it is first used (lazily).</li>
<li>I must be able to insert debugging statements at will. I don&#8217;t want to have to refactor code just to put logging messages.</li>
<li>It must be elegant. This is, of course, purely opinion, but wasn&#8217;t that the point of this whole thing?</li>
<li>And one more, I want to be able to reference these snippets, too. You know, a way to pass them around as parameters or something.</li>
</ol>
<p>In other words, I want to be able to do this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">header.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> Snippets.<span style="color: #660066;">welcome</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'Joey'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>That would replace the token &#8220;<code>name</code>&#8221; with &#8220;<code>Joey</code>&#8221; in a template that probably looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">new</span> Template<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Welcome, #{name}!'</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>I want <code>welcome</code> to be a function, I don&#8217;t want it to be an object that I have to call <code>evaluate</code> on. To me, that violates requirements 1, 3, and 4. If I were writing this in PHP, I would do something along the lines of the following in order to meet all the requirements. (I&#8217;ll just use two snippets for the sake of the example, &#8220;welcome&#8221; and &#8220;status&#8221;. Usually I&#8217;d have many.)</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Snippets <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #990000;">static</span> <span style="color: #000088;">$templates</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> load <span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$templates</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$templates</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Template<span style="color: #009900;">&#40;</span><span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$templates</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span>;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> evaluate <span style="color: #009900;">&#40;</span><span style="color: #000088;">$template</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// insert debugging statements here</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">evaluate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> welcome <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="">'welcome'</span><span style="color: #339933;">,</span> <span style="">'Welcome, #{name}!'</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">evaluate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$template</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> status <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="">'status'</span><span style="color: #339933;">,</span> <span style="">'System is #{status}.'</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">evaluate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$template</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That is pretty clean, and can be pretty much translated directly into any other object oriented language. It meets requirements 1, 2, 3, and 4. The <code>load</code> method caches the template objects, and only instantiates them when they are first used. The <code>evaluate</code> method is abstracted out to allow me to put all debugging/logging statements in one location. For some languages, requirement 5 can still be met. For example, with PHP you can &#8220;reference&#8221; which snippet to use by doing something like:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #0000ff; font-style: italic;">/**
 * Greet a user by name
 * @param string $name Name of user
 * @param array $template Option snippet to use, defaults to &quot;welcome&quot;
 * @return void Displays output directly
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> greetUser <span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$snippet</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'Snippets'</span><span style="color: #339933;">,</span> <span style="">'welcome'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #990000;">call_user_func</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$template</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
greetUser<span style="color: #009900;">&#40;</span><span style="">'Joey'</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Requirement 5 is met, but requirement 4 is now broken. It&#8217;s not pretty by any definition. Less dynamic languages, like Java, will undoubtedly be even uglier, resorting to reflection. The cleaner way in Java would be to make those functions be object references instead of functions, require them to implement some interface, and have the user call the <code>evaluate</code> method. To me, that is the opposite of requirement 1: it is easy to understand, but annoying to use. <a href="http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html">This explains it much better, and even if you disagree, you should read it.</a></p>
<p>The JavaScript code will basically be the same, except that I will be able to reference the functions directly.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Snippet <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  create <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> template <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span>;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>template<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        template <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Template<span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;
      <span style="color: #009900;">&#125;</span>
      <span style="color: #006600; font-style: italic;">// insert debugging/logging statements here</span>
      <span style="color: #000066; font-weight: bold;">return</span> template.<span style="color: #660066;">evaluate</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> Snippets <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  welcome <span style="color: #339933;">:</span> Snippet.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Welcome, #{name}!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #000066;">status</span> <span style="color: #339933;">:</span> Snippet.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'System is #{status}.'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>;</pre></div></div>

<p>That code satisfies all the requirements, and it does so beautifully. Not only is <code>Snippets.welcome</code> a method that I can call and pass the data to, but I can pass it around as a parameter, or assign it to a variable. Imagine adding another 20 snippets to both the PHP and JavaScript code above, and I think the effect is even more dramatic.</p>
<p>As I said in the beginning, JavaScript just does what I want it to do. No, I didn&#8217;t fart.</p>
]]></content:encoded>
			<wfw:commentRss>http://joey.mazzarelli.com/2007/08/22/if-javascript-is-so-cool-then-why-does-my-nephew-say-its-not-a-real-programming-langauge/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How can a sloth beget monkeys?</title>
		<link>http://joey.mazzarelli.com/2007/08/06/how-can-a-sloth-beget-monkeys/</link>
		<comments>http://joey.mazzarelli.com/2007/08/06/how-can-a-sloth-beget-monkeys/#comments</comments>
		<pubDate>Mon, 06 Aug 2007 17:11:23 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
		
		<category><![CDATA[Whatever]]></category>

		<guid isPermaLink="false">http://joey.mazzarelli.com/2007/08/06/how-can-a-sloth-beget-monkeys/</guid>
		<description><![CDATA[About a year ago, I redesigned Hayden&#8217;s website. You know which one. I was busy though, and didn&#8217;t get around to actually implementing the new look and feel. I told her I would get it done over Christmas break. Well, Christmas break came and went. Then January came and went. Then February came and went, [...]]]></description>
			<content:encoded><![CDATA[<p>About a year ago, I redesigned Hayden&#8217;s website. You know which one. I was busy though, and didn&#8217;t get around to actually implementing the new look and feel. I told her I would get it done over Christmas break. Well, Christmas break came and went. Then January came and went. Then February came and went, along with March, April, May, and June. I kid you not, but even July came and went! August came, too. Months are relentless. But August wasn&#8217;t going to get off that easy.</p>
<p>So anyway, her site finally has a new look and feel to it. I think it looks much more reputable. That is something you can fake on the web with fancy graphics, didn&#8217;t you know? In celebration of the new website, why don&#8217;t you go buy something from her? If you don&#8217;t know where her website is, then she doesn&#8217;t want your business anyway.</p>
]]></content:encoded>
			<wfw:commentRss>http://joey.mazzarelli.com/2007/08/06/how-can-a-sloth-beget-monkeys/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The monkeys: if only I were this cool.</title>
		<link>http://joey.mazzarelli.com/2007/07/31/the-monkeys-if-only-i-were-this-cool/</link>
		<comments>http://joey.mazzarelli.com/2007/07/31/the-monkeys-if-only-i-were-this-cool/#comments</comments>
		<pubDate>Tue, 31 Jul 2007 07:29:13 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
		
		<category><![CDATA[Whatever]]></category>

		<guid isPermaLink="false">http://joey.mazzarelli.com/2007/07/31/the-monkeys-if-only-i-were-this-cool/</guid>
		<description><![CDATA[I thought I ought to put a couple pictures of two of my favorite people on my website.
I&#8217;ll kick it off with a couple portraits. Although both of them constantly beg for attention, only one of them enjoys having pictures taken of him.
 
JD has had a mohawk for over a month now. We gave [...]]]></description>
			<content:encoded><![CDATA[<p>I thought I ought to put a couple pictures of two of my favorite people on my website.</p>
<p>I&#8217;ll kick it off with a couple portraits. Although both of them constantly beg for attention, only one of them enjoys having pictures taken of him.</p>
<p><img src="/images/jd.jpg" vspace="2" /> <img src="/images/zeph.jpg" vspace="2" /></p>
<p>JD has had a mohawk for over a month now. We gave him liberty spikes for the 4th of July as headed to the park for the fireworks. He wasn&#8217;t so tough when they started exploding. You can see Zephyr in his usual pose, making sure you still know how cute he is.</p>
<p>Now a couple action shots.</p>
<p><img src="/images/goal.jpg" vspace="2" /> <img src="/images/zeph_bowling.jpg" vspace="2" /></p>
<p>Ok, so the action is somewhat lacking in that last one. But you should have seen him turn around and cheer when the ball finally touches a pin.</p>
]]></content:encoded>
			<wfw:commentRss>http://joey.mazzarelli.com/2007/07/31/the-monkeys-if-only-i-were-this-cool/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Nutch and Hadoop (as user with NFS)</title>
		<link>http://joey.mazzarelli.com/2007/07/25/nutch-and-hadoop-as-user-with-nfs/</link>
		<comments>http://joey.mazzarelli.com/2007/07/25/nutch-and-hadoop-as-user-with-nfs/#comments</comments>
		<pubDate>Wed, 25 Jul 2007 07:09:36 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
		
		<category><![CDATA[Thesis]]></category>

		<guid isPermaLink="false">http://joey.mazzarelli.com/2007/07/25/nutch-and-hadoop-as-user-with-nfs/</guid>
		<description><![CDATA[This is from http://wiki.apache.org/nutch/NutchHadoopTutorial with some modifications to fit my needs. The article above assumes you have root access, which should be the case if you are going to consume the resources needed to crawl the internet. However, I want to run this as a normal user. Another gotcha is that I am working on [...]]]></description>
			<content:encoded><![CDATA[<p>This is from <a href="http://wiki.apache.org/nutch/NutchHadoopTutorial" title="http://wiki.apache.org/nutch/NutchHadoopTutorial">http://wiki.apache.org/nutch/NutchHadoopTutorial</a> with some modifications to fit my needs. The article above assumes you have root access, which should be the case if you are going to consume the resources needed to crawl the internet. However, I want to run this as a normal user. Another gotcha is that I am working on a cluster that shares users&#8217; home directories over NFS. For more details or explanation, refer to the original article.</p>
<p>First, I need to be able to login to all the various nodes on the cluster through SSH without being prompted for a password.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ssh-keygen</span> <span style="color: #660033;">-t</span> rsa
<span style="color: #666666; font-style: italic;">#Leave the password empty.</span>
<span style="color: #c20cb9; font-weight: bold;">cat</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>id_rsa.pub <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>authorized_keys</pre></div></div>

<p>Next, get Nutch built.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>~<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>nutch,~<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>nutch<span style="color: #000000; font-weight: bold;">/</span>build<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>nutch
<span style="color: #c20cb9; font-weight: bold;">svn</span> <span style="color: #c20cb9; font-weight: bold;">co</span> http:<span style="color: #000000; font-weight: bold;">//</span>svn.apache.org<span style="color: #000000; font-weight: bold;">/</span>repos<span style="color: #000000; font-weight: bold;">/</span>asf<span style="color: #000000; font-weight: bold;">/</span>lucene<span style="color: #000000; font-weight: bold;">/</span>nutch<span style="color: #000000; font-weight: bold;">/</span>trunk .
<span style="color: #7a0874; font-weight: bold;">echo</span> dist.dir=<span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>nutch<span style="color: #000000; font-weight: bold;">/</span>build <span style="color: #000000; font-weight: bold;">&gt;</span> build.properties
ant package</pre></div></div>

<p>I want mine working with one copy for the crawling, and another copy for searching or just poking around with, without changing the settings for the crawling one.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>nutch<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-r</span> build crawler
<span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-r</span> build sandbox</pre></div></div>

<p><tt>~/opt/nutch</tt> now contains three directories:</p>
<ol>
<li><tt>build</tt> – Ant builds Nutch into this directory.</li>
<li><tt>crawler</tt> – The instance used for crawling.</li>
<li><tt>sandbox</tt> – The instance used for searching the indices created by the crawls, or whatever I want to play with.</li>
</ol>
<p>If I need to rebuild Nutch, I can copy the project from the <tt>build</tt> directory into the other two. Since my home directory is mounted via NFS, there is no need to log into the other nodes of the cluster and repeat this process. It is done.</p>
<p>Now hadoop needs configured. I am only going to go over the configuration of the crawler instance. The sandbox one will not use hadoop, and therefore is straightforward.  I need a place for the logs, the pid files for managing processes, and the filesystem hadoop uses.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>nutch<span style="color: #000000; font-weight: bold;">/</span>crawler
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>logs,pids<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> conf
<span style="color: #c20cb9; font-weight: bold;">vim</span> hadoop-env.sh</pre></div></div>

<p>I added/modified <tt>hadoop-env.sh</tt> in the following manner:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">HOSTNAME</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">hostname</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">HADOOP_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>jmazzare<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>nutch<span style="color: #000000; font-weight: bold;">/</span>crawler
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">JAVA_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #000000; font-weight: bold;">/</span>java
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">HADOOP_LOG_DIR</span>=<span style="color: #800000;">${HADOOP_HOME}</span><span style="color: #000000; font-weight: bold;">/</span>logs<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${HOSTNAME}</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">HADOOP_PID_DIR</span>=<span style="color: #800000;">${HADOOP_HOME}</span><span style="color: #000000; font-weight: bold;">/</span>pids<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${HOSTNAME}</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">HADOOP_IDENT_STRING</span>=<span style="color: #800000;">${USER}</span>_<span style="color: #800000;">${HOSTNAME}</span></pre></div></div>

<p>You will, of course, adjust the path to your home directory, and your java home as needed. Now the xml configuration files need set up. I suggest you read this articles on which files should contain which information. <a href="http://wiki.apache.org/lucene-hadoop/HowToConfigure">http://wiki.apache.org/lucene-hadoop/HowToConfigure</a>. That said, I threw everything into <tt>hadoop-site.xml</tt>. To keep these examples short, I am only showing one master node (<tt>node00</tt>) and two slave nodes (<tt>node01, node02</tt>).</p>
<p><tt>conf/masters</tt></p>

<div class="wp_syntax"><div class="code"><pre class="text text" style="font-family:monospace;">node00</pre></div></div>

<p><tt>conf/slaves</tt></p>

<div class="wp_syntax"><div class="code"><pre class="text text" style="font-family:monospace;">node01
node02</pre></div></div>

<p><tt>conf/hadoop-site.xml</tt></p>

<div class="wp_syntax"><div class="code"><pre class="xml xml" style="font-family:monospace;">&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>fs.default.name<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>node00:9000<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      The name of the default file system. Either the literal string
      &quot;local&quot; or a host:port for NDFS.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mapred.job.tracker<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>node00:9001<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      The host and port that the MapReduce job
      tracker runs at. If &quot;local&quot;, then jobs are
      run in-process as a single map and reduce task.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mapred.map.tasks<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      Define mapred.map.tasks to be the number
      of slave hosts.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mapred.reduce.tasks<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      Define mapred.reduce.tasks to be the
      number of slave hosts.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>dfs.name.dir<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/tmp/hadoop-filesystem-jmazzare/name<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>dfs.data.dir<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/tmp/hadoop-filesystem-jmazzare/data<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mapred.system.dir<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/tmp/hadoop-filesystem-jmazzare/mapreduce/system<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mapred.local.dir<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/tmp/hadoop-filesystem-jmazzare/mapreduce/local<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>dfs.replication<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      Define dfs.replication to be the
      number of slave hosts.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The filesystem hadoop uses expects to have its own directories on each host to write to, and since I am using NFS, this isn&#8217;t good. So I put the filesystem in <tt>/tmp</tt> since that is mounted separately on each node. I linked to it for convenience.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>nutch<span style="color: #000000; font-weight: bold;">/</span>crawler
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>hadoop-filesystem-<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">whoami</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #c20cb9; font-weight: bold;">chmod</span> <span style="color: #000000;">700</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>hadoop-filesystem-<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">whoami</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>hadoop-filesystem-<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">whoami</span><span style="color: #000000; font-weight: bold;">`</span> .<span style="color: #000000; font-weight: bold;">/</span>filesystem
bin<span style="color: #000000; font-weight: bold;">/</span>hadoop namenode <span style="color: #660033;">-format</span></pre></div></div>

<p>It seems to create the directories for the log files just fine, but not so much for the pids. So&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>nutch<span style="color: #000000; font-weight: bold;">/</span>crawler<span style="color: #000000; font-weight: bold;">/</span>conf
<span style="color: #000000; font-weight: bold;">for</span> node <span style="color: #000000; font-weight: bold;">in</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">cat</span> slaves masters<span style="color: #7a0874; font-weight: bold;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #c20cb9; font-weight: bold;">mkdir</span> ..<span style="color: #000000; font-weight: bold;">/</span>pids<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$node</span>;
<span style="color: #000000; font-weight: bold;">done</span>;
<span style="color: #7a0874; font-weight: bold;">cd</span> ..
bin<span style="color: #000000; font-weight: bold;">/</span>start-all.sh</pre></div></div>

<p>The easiest way I have found to verify that everything went correctly is to run the <tt>bin/stop-all.sh</tt> command. If it complains that there was nothing to stop, then something isn&#8217;t configured correctly. If claims to have stopped everything, then all is well. If things don&#8217;t seem right, make sure you don&#8217;t have any processes that have escaped your attention. Kill those.</p>
<p>When you run Nutch now, it will use the filesystem from Hadoop. So any files that Nutch needs to be aware of need put into Hadoop&#8217;s filesystem. I will show the classic example of crawling the apache website.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>nutch<span style="color: #000000; font-weight: bold;">/</span>crawler<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> urls
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'http://lucene.apache.org'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> urls<span style="color: #000000; font-weight: bold;">/</span>urllist.txt</pre></div></div>

<p>This file needs put into the Hadoop filesystem.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Must be running... bin/start-all.sh</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>nutch<span style="color: #000000; font-weight: bold;">/</span>crawler
bin<span style="color: #000000; font-weight: bold;">/</span>hadoop dfs <span style="color: #660033;">-put</span> urls urls
<span style="color: #666666; font-style: italic;"># You can verify with:</span>
bin<span style="color: #000000; font-weight: bold;">/</span>hadoop dfs <span style="color: #660033;">-ls</span>
bin<span style="color: #000000; font-weight: bold;">/</span>hadoop dfs <span style="color: #660033;">-cat</span> urls<span style="color: #000000; font-weight: bold;">/</span>urllist.txt</pre></div></div>

<p>More references for configuring Nutch can be found <a href="http://wiki.apache.org/nutch/NutchTutorial">here</a> and <a href="http://lucene.apache.org/nutch/tutorial.html">here</a> show.</p>
<p>In particular, make sure a <tt>http.agent.name</tt> is set in <tt>conf/</tt> and add <tt>lucene.apache.org</tt> to the &#8220;whitelist&#8221; in <tt>conf/crawl-urlfilter.txt</tt></p>
<p>Now, finally do the crawl.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Again, make sure it is all running</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>nutch<span style="color: #000000; font-weight: bold;">/</span>crawler
bin<span style="color: #000000; font-weight: bold;">/</span>nutch crawl urls <span style="color: #660033;">-dir</span> crawled <span style="color: #660033;">-depth</span> <span style="color: #000000;">3</span></pre></div></div>

<p>You can monitor the output directly, or open a browser and go to port 50030 of the master node. <tt>http://node00:50030</tt>. You will be able to see the output very easily from there. Check <tt>http://node00:50030/machines.jsp</tt> and check for failures. If everything is going fine, just wait for it to finish&#8230;</p>
<p>After it is done, you can export the data to the sandbox.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>nutch<span style="color: #000000; font-weight: bold;">/</span>crawler
bin<span style="color: #000000; font-weight: bold;">/</span>hadoop dfs <span style="color: #660033;">-copyToLocal</span> crawled ..<span style="color: #000000; font-weight: bold;">/</span>sandbox<span style="color: #000000; font-weight: bold;">/</span>crawl
bin<span style="color: #000000; font-weight: bold;">/</span>stop-all.sh</pre></div></div>

<p>Now you can do whatever you want with that; <a href="http://lucene.apache.org/nutch/tutorial.html#Searching">point tomcat at it</a>, or query it directly.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>nutch<span style="color: #000000; font-weight: bold;">/</span>sandbox
bin<span style="color: #000000; font-weight: bold;">/</span>nutch org.apache.nutch.searcher.NutchBean apache</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://joey.mazzarelli.com/2007/07/25/nutch-and-hadoop-as-user-with-nfs/feed/</wfw:commentRss>
		</item>
		<item>
		<title>7 steps to break the ice and instantly make friends&#8230;</title>
		<link>http://joey.mazzarelli.com/2007/07/19/7-steps-to-break-the-ice-and-instantly-make-friends/</link>
		<comments>http://joey.mazzarelli.com/2007/07/19/7-steps-to-break-the-ice-and-instantly-make-friends/#comments</comments>
		<pubDate>Thu, 19 Jul 2007 04:08:11 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
		
		<category><![CDATA[Whatever]]></category>

		<guid isPermaLink="false">http://joey.mazzarelli.com/2007/07/19/7-steps-to-break-the-ice-and-instantly-make-friends/</guid>
		<description><![CDATA[I forgot what they are&#8230; does anyone have them? Forget it, I&#8217;ll just open with a Mitch Hedberg quote:
I don&#8217;t have a girlfriend,  but I do know a woman who&#8217;d be mad at me for saying that.
And she&#8217;s my wife&#8230;
]]></description>
			<content:encoded><![CDATA[<p>I forgot what they are&#8230; does anyone have them? Forget it, I&#8217;ll just open with a Mitch Hedberg quote:</p>
<blockquote><p>I don&#8217;t have a girlfriend,  but I do know a woman who&#8217;d be mad at me for saying that.</p></blockquote>
<p>And she&#8217;s my <a href="http://hayden.mazzarelli.com">wife</a>&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://joey.mazzarelli.com/2007/07/19/7-steps-to-break-the-ice-and-instantly-make-friends/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
