<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>transfixed but not dead! &#187; Devel::Declare</title>
	<atom:link href="http://transfixedbutnotdead.com/tag/develdeclare/feed/" rel="self" type="application/rss+xml" />
	<link>http://transfixedbutnotdead.com</link>
	<description>my ramblings on life, work &#38; anything left in-between</description>
	<lastBuildDate>Tue, 08 May 2012 09:57:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='transfixedbutnotdead.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/0a317653027efb1ab2bf8adde3dcb067?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>transfixed but not dead! &#187; Devel::Declare</title>
		<link>http://transfixedbutnotdead.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://transfixedbutnotdead.com/osd.xml" title="transfixed but not dead!" />
	<atom:link rel='hub' href='http://transfixedbutnotdead.com/?pushpress=hub'/>
		<item>
		<title>Perl block with lexically-scoped method delegation</title>
		<link>http://transfixedbutnotdead.com/2010/05/15/perl-block-with-lexically-scoped-method-delegation/</link>
		<comments>http://transfixedbutnotdead.com/2010/05/15/perl-block-with-lexically-scoped-method-delegation/#comments</comments>
		<pubDate>Sat, 15 May 2010 14:00:45 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Devel::Declare]]></category>
		<category><![CDATA[dsl]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=980</guid>
		<description><![CDATA[About a year ago I came across this interesting Ruby blog post Keyword With. This is something I always wanted in Perl and immediately marked a note in my list of blog/CPAN ideas with &#8220;Devel::Declare and with statement&#8221; (with side note that &#8220;using&#8221; might be more appropriate so not to clash with Moose roles sugar). [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&#038;blog=351142&#038;post=980&#038;subd=draegtun&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>About a year ago I came across this interesting Ruby blog post <a href="http://www.ironicwolf.com/2009/05/keyword-with/">Keyword With</a>.  </p>
<p>This is something I always wanted in Perl and immediately marked a note in my list of blog/CPAN ideas with &#8220;Devel::Declare and with statement&#8221; (with side note that &#8220;using&#8221; might be more appropriate so not to clash with Moose roles sugar).</p>
<p>Unsurprisingly this just languished in my ever increasing list <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />   </p>
<p>However last month I had a few hours spare one weekend so pulled out my laptop and rummaged through my list and decided to see how difficult it would be to implement &#8220;with&#8221;.</p>
<p>Unfortunately I kept hitting brick walls with <a href="http://perldoc.perl.org/functions/local.html">local</a> scoping.  I came to conclusion that I may have to use <a href="http://perldoc.perl.org/functions/eval.html">eval</a> or even go down a different track using an AUTOLOAD solution.</p>
<p>I left that weekend experiment with at least a very basic AUTOLOAD proof of concept:</p>
<p><pre class="brush: perl;">
use 5.012;
use warnings;

{
    package FileStuff;
    use Moose;
    
    has name =&gt; ( is =&gt; 'rw', isa =&gt; 'Str' );
    
    sub save { say &quot;save &quot;,   $_[0]-&gt;name }
    
    sub del  { say &quot;delete &quot;, $_[0]-&gt;name }
    
    sub mv {
        my ($self, $to) = @_;
        say &quot;rename &quot;, $self-&gt;name, &quot; to $to&quot;;
        $self-&gt;name( $to );
    }
    
    sub say { say &quot;Method say: &quot;, @_ }
}

my $f = FileStuff-&gt;new( name =&gt; 'foobar' );

# using $f {}
{
    local *AUTOLOAD = sub {
        our $AUTOLOAD;
        my $name = (split '::', $AUTOLOAD)[-1];
        die &quot;method $name NOT FOUND!&quot; unless $f-&gt;can( $name );
        $f-&gt;$name( @_ );
    };
    
    # my &quot;DSL&quot; stuff!
    save();               # $f-&gt;save();
    mv( &quot;barbaz&quot; );       # $f-&gt;mv( &quot;barbaz&quot; );
    del();                # parenthesis are always required
    say( &quot;builtin say&quot; ); # builtin wins over $f-&gt;say
}
</pre></p>
<p>I wasn&#8217;t sure if this would ever evolve into a robust solution.  However I found it reassuring to note that other people had similar problems trying to use local scoping in similar way when I came across this Perlmonks post <a href="http://perlmonks.org/?node_id=834137">A useful use of goto</a> a few days later.  So this gave the AUTOLOAD approach some merit.</p>
<p>Well chances are this blog post or any additional development may never made it any further.  What changed that was an email from <a href="http://search.cpan.org/~chocolate/">chocolateboy</a> this morning pointing me at his new <a href="http://search.cpan.org/dist/Scope-With/">Scope::With</a> CPAN module.</p>
<p>Blimey&#8230; Father Christmas had delivered my xmas pressie early and without even seeing my xmas list <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>chocolateboy&#8217;s first example of Scope::With even shows how it works with my <a href="http://search.cpan.org/dist/Builder/">Builder</a> module:</p>
<p><pre class="brush: perl;">
use Builder;
use Scope::With;

my $builder = Builder-&gt;new();
my $xml = $builder-&gt;block('Builder::XML');

with ($xml) {
    body(
        div(
            span({ id =&gt; 1 }, 'one'),
            span({ id =&gt; 2 }, 'two'),
        )
    );
}

say $builder-&gt;render();
</pre></p>
<p>Which is exactly why I wanted a &#8220;with&#8221; statement in Perl and it looked like chocolateboy also felt the same!</p>
<p>The documentation even gave an &#8220;using&#8221; example:</p>
<p><pre class="brush: perl;">
use Scope::With qw(using);

using ($xml) {
    div(
        span( ... ),
        span( ... ),
    );
}
</pre></p>
<p>Getting worried now someone was reading my mind <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>This is truly awesome stuff. chocolateboy has the habit of producing game changing CPAN modules. For eg. <a href="http://search.cpan.org/dist/autobox/">autobox</a> and <a href="http://search.cpan.org/dist/Method-Lexical/">Method::Lexical</a>.  To me <a href="http://search.cpan.org/dist/Scope-With/">Scope::With</a> is potentially another one that falls into this lofty category.  </p>
<p>I&#8217;m deeply honoured to find my Builder module being referenced and also glad to see that a &#8220;with&#8221; statement using <a href="http://search.cpan.org/dist/Devel-Declare/">Devel::Declare</a> and AUTOLOAD isn&#8217;t such a delusional idea <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>So many thanks to chocolateboy for writing Scope::With.  Keep up the good work.</p>
<p>/I3az/</p>
<p>PS. chocolateboy&#8217;s Scope::With docs also references a prior art module <a href="http://search.cpan.org/dist/with/">with</a> by <a href="http://search.cpan.org/~vpit/">Vincent Pit</a>. This module achieves similar things but in different way/implementation.</p>
<p>PPS. CPAN is so big and wonderful that its easy to miss or even forget whats on there!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/980/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/980/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/980/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/980/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/980/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/980/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/980/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/980/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/980/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/980/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/980/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/980/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/980/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/980/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&#038;blog=351142&#038;post=980&#038;subd=draegtun&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2010/05/15/perl-block-with-lexically-scoped-method-delegation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/29cb106071d163d703484e63839d89cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">draegtun</media:title>
		</media:content>
	</item>
		<item>
		<title>Couple of CPAN pressies</title>
		<link>http://transfixedbutnotdead.com/2009/12/26/couple-of-cpan-pressies/</link>
		<comments>http://transfixedbutnotdead.com/2009/12/26/couple-of-cpan-pressies/#comments</comments>
		<pubDate>Sat, 26 Dec 2009 23:17:02 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[CPAN]]></category>
		<category><![CDATA[Devel::Declare]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=783</guid>
		<description><![CDATA[Just uploaded a couple of new modules to CPAN: Acme::URL PerlX::QuoteOperator The first one you is just a small xmas cracker that you will already know about from my &#8220;bare URL&#8221; post from last week. Just pull, enjoy for a little while and then chuck away like most xmas cracker toys The second one is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&#038;blog=351142&#038;post=783&#038;subd=draegtun&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Just uploaded a couple of new modules to CPAN:</p>
<ul>
<li><a href="http://search.cpan.org/dist/Acme-URL/">Acme::URL</a>
<li><a href="http://search.cpan.org/dist/PerlX-QuoteOperator/">PerlX::QuoteOperator</a>
</ul>
<p>The first one you is just a small xmas cracker that you will already know about from my <a href="http://transfixedbutnotdead.com/2009/12/16/url-develdeclare-and-no-strings-attached/">&#8220;bare URL&#8221;</a> post from last week.  Just pull, enjoy for a little while and then chuck away like most xmas cracker toys <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The second one is a tad similar (ie. <a href="http://search.cpan.org/dist/Devel-Declare/">Devel::Declare</a>) but with a much more sympathetic syntax by allowing you to create new <a href="http://perldoc.perl.org/perlop.html#Quote-Like-Operators">Quote-Like Operators</a> in Perl with Perl.   </p>
<p>For a good end product exemplar have a look at <a href="http://search.cpan.org/dist/PerlX-QuoteOperator/lib/PerlX/QuoteOperator/URL.pm">PerlX::QuoteOperator::URL</a> included with the distribution:</p>
<p><pre class="brush: perl;">
use PerlX::QuoteOperator::URL 'qh';

my $content = qh{ http://transfixedbutnotdead.com/ };
</pre></p>
<p>There is no scary parsing going on with this example.  PerlX::QuoteOperator / Devel::Declare simply hits the &#8216;qh&#8217; keyword and converts it to:</p>
<p><pre class="brush: perl;">
qh qq{ http://transfixedbutnotdead.com/ }
</pre></p>
<p>And &#8216;qh&#8217; is a subroutine with a ($) prototype.   So nothing too sinister (unless you really do abhor subroutine prototypes!), just a simple macro which gives the appearance that we have new quote-like operators.</p>
<p>Certainly an interesting concept to be able to produce new quote-like operators in Perl.  So hopefully PerlX::QuoteOperator might find its way under a few xmas trees <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Merry Xmas &amp; happy St. Stephen&#8217;s day to everyone.</p>
<p>/I3az/</p>
<p>PS.  I chose the PerlX::* because <a href="http://search.cpan.org/~gugod/">gugod</a> as already provided some wonderful Devel::Declare Perl extending modules under this <a href="http://search.cpan.org/search?query=perlx%3A%3A*&amp;mode=distributions">namespace</a>.   I feel its in the same vein so hopefully a good place for it to live.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/783/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/783/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/783/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/783/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/783/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/783/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/783/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/783/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/783/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/783/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/783/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/783/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/783/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/783/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&#038;blog=351142&#038;post=783&#038;subd=draegtun&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2009/12/26/couple-of-cpan-pressies/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/29cb106071d163d703484e63839d89cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">draegtun</media:title>
		</media:content>
	</item>
		<item>
		<title>Contributing to a project on Github</title>
		<link>http://transfixedbutnotdead.com/2009/12/24/contributing-to-a-project-on-github/</link>
		<comments>http://transfixedbutnotdead.com/2009/12/24/contributing-to-a-project-on-github/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 21:20:43 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Devel::Declare]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=771</guid>
		<description><![CDATA[After getting my head into Devel::Declare::Context::Simple to create the bare URL for my last blog post I realised that I&#8217;ve percolated enough knowledge about the module that it would be a good idea to redirect it back to the project by at least doing the POD. This will be the first time I&#8217;ve &#8220;properly&#8221; contributed [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&#038;blog=351142&#038;post=771&#038;subd=draegtun&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>After getting my head into Devel::Declare::Context::Simple to create the bare URL for my <a href="http://transfixedbutnotdead.com/2009/12/16/url-develdeclare-and-no-strings-attached/">last blog post</a><br />
I realised that I&#8217;ve percolated enough knowledge about the module that it would be a good idea to redirect it back to the project by at least doing the POD.</p>
<p>This will be the first time I&#8217;ve &#8220;properly&#8221; contributed in some form to an opensource project (&#8220;properly&#8221; being via source control management &amp; a patch).</p>
<p><a href="http://search.cpan.org/dist/Devel-Declare/">Devel::Declare</a> is hosted on <a href="http://github.com/rafl/devel-declare">Github here</a>.   And the process to contribute back changes to a Github project is &#8220;forking&#8221; simple!</p>
<p>With <a href="http://git-scm.com/">Git</a> &amp; <a href="http://github.com">Github</a> its all about forking.  Here is a concise Github <a href="http://help.github.com/forking/">howto</a> (which I&#8217;ll regurgitate below with my own slant &amp; spiel!).</p>
<p>I only use Git for my Github projects, so forking etc is all new to me.  To break it down into a few simple steps the process to contribute would be:</p>
<ul>
<li>Fork project
<li>Clone your fork
<li>Make your changes
<li>Push changes back to you fork
<li>Send pull request back to original project
</ul>
<p>&nbsp;</p>
<p>Here are the minutiae steps I went through for Devel::Declare:</p>
<ul>
<li>Went to <a href="http://github.com/rafl/devel-declare">Devel::Declare project</a> on Github and forked it.
<li>Forked project now appears in my project list.
<li>Cloned my forked Devel::Declare (use &#8220;Your Clone URL&#8221; copy/paste)<br />
<code>git clone git@github.com:draegtun/devel-declare.git<br />
cd devel-declare/<br />
</code></p>
<li>Linked my forked project to original<br />
<code>git remote add upstream git://github.com/rafl/devel-declare.git<br />
git fetch upstream<br />
</code></p>
<li>Make my changes to project
<li>Run tests (in case I did something stupid!)<br />
<code>perl Makefile.PL<br />
make<br />
make test<br />
</code></p>
<li>Clear down build once happy<br />
<code>make realclean</code></p>
<li>Push my changes back to my fork<br />
<code>git commit -a -m "Documentation added to D::D::Context::Simple + examples directory"<br />
git push origin master<br />
</code>
</ul>
<p>Now on Github the changes can be seen in <a href="http://github.com/draegtun/devel-declare">my fork</a></p>
<p>All I do now is click the &#8220;pull request&#8221; button and off went a request of my changes to <a href="http://github.com/rafl">rafl</a>.  Fingers cross my documentation changes were good enough to be accepted.</p>
<p>So I&#8217;m no longer a virgin to contributing to this opensource malarky <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>/I3az/</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/771/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/771/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/771/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/771/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/771/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/771/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/771/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/771/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/771/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/771/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/771/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/771/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/771/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/771/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&#038;blog=351142&#038;post=771&#038;subd=draegtun&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2009/12/24/contributing-to-a-project-on-github/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/29cb106071d163d703484e63839d89cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">draegtun</media:title>
		</media:content>
	</item>
		<item>
		<title>URL, Devel::Declare and no strings attached</title>
		<link>http://transfixedbutnotdead.com/2009/12/16/url-develdeclare-and-no-strings-attached/</link>
		<comments>http://transfixedbutnotdead.com/2009/12/16/url-develdeclare-and-no-strings-attached/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 16:55:24 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Devel::Declare]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=753</guid>
		<description><![CDATA[This Hacker News article caught my eye yesterday which pointed to a blog post about having a raw URL within a Ruby program using some clever shenanigans. (They you go, I saved you having to click through to find all the links yourself!) Some examples from blog post: Very clever indeed. Of course there will [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&#038;blog=351142&#038;post=753&#038;subd=draegtun&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This <a href="http://news.ycombinator.com/item?id=994716">Hacker News article</a> caught my eye yesterday which pointed to a <a href="http://ozmm.org/posts/urls_in_ruby.html">blog post</a> about having a raw <a href="http://en.wikipedia.org/wiki/Uniform_Resource_Locator">URL</a> within a Ruby program using some <a href="http://gist.github.com/255948">clever shenanigans</a>. <em>(They you go, I saved you having to click through to find all the links yourself!)</em></p>
<p>Some examples from blog post:<br />
<pre class="brush: ruby;">
# display this JSON request
puts http://github.com/defunkt.json

# =&gt; &quot;He nose the truth.&quot;
require 'json'
url = http://twitter.com/statuses/show/6592721580.json
JSON.parse(url.to_s)['text']   
</pre></p>
<p>Very clever indeed.  Of course there will be some edge cases but it does show you how far you can stretch Ruby.  <em>NB. Didn&#8217;t work in 1.9 for me but worked with minor warning in 1.8.2 (yes I know it old but thats what shipped with Mac OSX Tiger).</em></p>
<p>So how far can we stretch Perl?  Well with <a href="http://search.cpan.org/dist/Devel-Declare/">Devel::Declare</a> it can be stretched all the way:</p>
<p><pre class="brush: perl;">
use Modern::Perl;
use JSON qw(decode_json);
use URLDSL;

# print the json
say http://twitter.com/statuses/show/6592721580.json;

# =&gt; &quot;He nose the truth.&quot;
say decode_json( http://twitter.com/statuses/show/6592721580.json )-&gt;{text};
</pre></p>
<p>So Perl also has no strings attached <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Devel::Declare allow us to extend the Perl syntax in a robust and easy fashion:<br />
<pre class="brush: perl;">
package URLDSL;
use Modern::Perl;
use Devel::Declare ();
use LWP::Simple ();
use base 'Devel::Declare::Context::Simple';

sub import {
    my $class  = shift;
    my $caller = caller;
    my $ctx    = __PACKAGE__-&gt;new;
    
    Devel::Declare-&gt;setup_for(
        $caller,
        { 
            http =&gt; { 
                const =&gt; sub { $ctx-&gt;parser(@_) },
            },
        },
    );
    
    no strict 'refs';
    *{$caller.'::http'} = sub ($) { LWP::Simple::get( $_[0] ) };
}

sub parser {
    my $self = shift;
    $self-&gt;init(@_);
    $self-&gt;skip_declarator;          # skip past &quot;http&quot;

    my $line = $self-&gt;get_linestr;   # get me current line of code
    my $pos  = $self-&gt;offset;        # position just after &quot;http&quot;
    my $url  = substr $line, $pos;   # url &amp; everything after &quot;http&quot;
    
    for my $c (split //, $url) {
        # if blank, semicolon or closing parenthesis then no longer a URL
        last if $c eq q{ };
        last if $c eq q{;};
        last if $c eq q{)};
        $pos++;
    }    
    
    # wrap the url with http() sub and quotes
    substr( $line, $pos,          0 ) = q{&quot;)};
    substr( $line, $self-&gt;offset, 0 ) = q{(&quot;http};
    
    # pass back changes to parser
    $self-&gt;set_linestr( $line );

    return;
}

1;
</pre></p>
<p>&nbsp;</p>
<p>Here is a low level synopsis of how it worked:</p>
<ul>
<li>Devel::Declare tells the perl parser to call parser() when it hits <code>http</code> keyword</li>
<li><code>http://twitter.com/statuses/show/6592721580.json</code> example triggers this (perl has no problem naturally distinguishing &#8220;http&#8221; keyword from &#8220;http://&#8221;)</li>
<li>Get line of code in question (<code>my $line = $self-&gt;get_linestr; </code>)
<li>parser() skips along the URL data following &#8220;http&#8221; (<code>$self-&gt;offset</code>) until it reaches a space, semicolon or a closing bracket (this covers &#8220;most&#8221; cases though comma could be an issue).  This marks the end of the URL data</li>
<li>We now in essence change <code>http://twitter.com/statuses/show/6592721580.json</code> into <code>http("http://twitter.com/statuses/show/6592721580.json")</code></li>
<li>And plonk the changed line back to the parser (<code>$self-&gt;set_linestr($line);</code>) and let it continue along its merry way</li>
<li>NB. http() sub was already imported into calling space and returns a <a href="http://search.cpan.org/dist/libwww-perl/lib/LWP/Simple.pm">LWP::Simple::get</a> of the requested URL</li>
</ul>
<p>&nbsp;</p>
<p>Now is URLDSL useful in the real world?   It can screw up syntax highlighting a bit (though WordPress highlighter coped extremely well, unlike <a href="http://gist.github.com/256992">Github</a> <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>Its robust enough to cope with normal string interpolation:<br />
<pre class="brush: perl;">
my $id = '6592721580';
say decode_json( http://twitter.com/statuses/show/$id.json )-&gt;{text};
</pre></p>
<p>So perhaps I should upload it to CPAN? (earmarked for ACME:: perhaps!?).  Anyone think this is useful little helper module then I wrap it up the next time I get a free moment.</p>
<p>The exercise as been fruitful in allowing me to get my head into the amazing Devel::Declare module for the first time. </p>
<p>I&#8217;ve used the Devel::Declare::Context::Simple class which simplifies things.  Unfortunately there isn&#8217;t a POD on this yet but the main Devel::Declare docs nicely demonstrate the correct mechanics so its easy to adapt from this.</p>
<p>I also found this <a href="http://lumberjaph.net/blog/index.php/2009/11/09/modules-i-like-develdeclare/">blog post by franck cuny</a> to be extremely helpful.  As was delving through the source code of <a href="http://cpants.perl.org/dist/used_by/Devel-Declare">other modules that used Devel::Declare</a></p>
<p>/I3az/</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/753/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/753/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/753/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/753/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/753/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/753/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/753/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/753/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/753/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/753/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/753/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/753/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/753/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/753/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&#038;blog=351142&#038;post=753&#038;subd=draegtun&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2009/12/16/url-develdeclare-and-no-strings-attached/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/29cb106071d163d703484e63839d89cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">draegtun</media:title>
		</media:content>
	</item>
	</channel>
</rss>
