<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>A Foolish Manifesto</title>
	
	<link>http://blog.afoolishmanifesto.com</link>
	<description>fREWdiculous!</description>
	<lastBuildDate>Wed, 16 May 2012 00:22:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/AFoolishManifesto" /><feedburner:info uri="afoolishmanifesto" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license><image><link>http://creativecommons.org/licenses/by-sa/3.0/</link><url>http://creativecommons.org/images/public/somerights20.gif</url><title>Some Rights Reserved</title></image><feedburner:feedFlare href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare href="http://feeds.my.aol.com/add.jsp?url=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://o.aolcdn.com/favorites.my.aol.com/webmaster/ffclient/webroot/locale/en-US/images/myAOLButtonSmall.gif">Subscribe with My AOL</feedburner:feedFlare><feedburner:feedFlare href="http://www.bloglines.com/sub/http://feeds.feedburner.com/AFoolishManifesto" src="http://www.bloglines.com/images/sub_modern11.gif">Subscribe with Bloglines</feedburner:feedFlare><feedburner:feedFlare href="http://www.netvibes.com/subscribe.php?url=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://www.netvibes.com/img/add2netvibes.gif">Subscribe with Netvibes</feedburner:feedFlare><feedburner:feedFlare href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare href="http://www.pageflakes.com/subscribe.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://www.pageflakes.com/ImageFile.ashx?instanceId=Static_4&amp;fileName=ATP_blu_91x17.gif">Subscribe with Pageflakes</feedburner:feedFlare><feedburner:feedFlare href="http://www.plusmo.com/add?url=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://plusmo.com/res/graphics/fbplusmo.gif">Subscribe with Plusmo</feedburner:feedFlare><feedburner:feedFlare href="http://www.thefreedictionary.com/_/hp/AddRSS.aspx?http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://img.tfd.com/hp/addToTheFreeDictionary.gif">Subscribe with The Free Dictionary</feedburner:feedFlare><feedburner:feedFlare href="http://www.bitty.com/manual/?contenttype=rssfeed&amp;contentvalue=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://www.bitty.com/img/bittychicklet_91x17.gif">Subscribe with Bitty Browser</feedburner:feedFlare><feedburner:feedFlare href="http://www.newsalloy.com/?rss=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://www.newsalloy.com/subrss3.gif">Subscribe with NewsAlloy</feedburner:feedFlare><feedburner:feedFlare href="http://www.live.com/?add=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://tkfiles.storage.msn.com/x1piYkpqHC_35nIp1gLE68-wvzLZO8iXl_JMledmJQXP-XTBOLfmQv4zhj4MhcWEJh_GtoBIiAl1Mjh-ndp9k47If7hTaFno0mxW9_i3p_5qQw">Subscribe with Live.com</feedburner:feedFlare><feedburner:feedFlare href="http://mix.excite.eu/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://image.excite.co.uk/mix/addtomix.gif">Subscribe with Excite MIX</feedburner:feedFlare><feedburner:feedFlare href="http://www.yourminis.com/subscribe.aspx?u=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://www.yourminis.com/images/addtoyourminisbadge.gif">Subscribe with Yourminis.com</feedburner:feedFlare><feedburner:feedFlare href="http://download.attensa.com/app/get_attensa.html?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://www.attensa.com/blogs/attensa/WindowsLiveWriter/BadgeredintoBadges_10C02/attensa_feed_button5.gif">Subscribe with Attensa for Outlook</feedburner:feedFlare><feedburner:feedFlare href="http://www.webwag.com/wwgthis.php?url=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://www.webwag.com/images/wwgthis.gif">Subscribe with Webwag</feedburner:feedFlare><feedburner:feedFlare href="http://hub.netomat.net/account/account.autoSubscribe.jspa?urls=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://www.netomat.net/blogger/images/icon_netomat_feedbutton.gif">Subscribe with netomat Hub</feedburner:feedFlare><feedburner:feedFlare href="http://www.podcastready.com/oneclick_bookmark.php?url=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://www.podcastready.com/images/podcastready_button.gif">Subscribe with Podcast Ready</feedburner:feedFlare><feedburner:feedFlare href="http://www.flurry.com/pushRssFeed.do?r=fb&amp;url=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://www.flurry.com/images/flurry_rss_logo2.gif">Subscribe with Flurry</feedburner:feedFlare><feedburner:feedFlare href="http://www.wikio.com/subscribe?url=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://www.wikio.com/shared/img/add2wikio.gif">Subscribe with Wikio</feedburner:feedFlare><feedburner:feedFlare href="http://www.dailyrotation.com/index.php?feed=http%3A%2F%2Ffeeds.feedburner.com%2FAFoolishManifesto" src="http://www.dailyrotation.com/rss-dr2.gif">Subscribe with Daily Rotation</feedburner:feedFlare><item>
		<title>Using search.cpan.org AND metacpan</title>
		<link>http://feedproxy.google.com/~r/AFoolishManifesto/~3/g-6gWkc9D4g/1692</link>
		<comments>http://blog.afoolishmanifesto.com/archives/1692#comments</comments>
		<pubDate>Wed, 16 May 2012 00:22:00 +0000</pubDate>
		<dc:creator>fREW Schmidt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[metacpan]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[sco]]></category>
		<category><![CDATA[search.cpan.org]]></category>

		<guid isPermaLink="false">http://blog.afoolishmanifesto.com/?p=1692</guid>
		<description><![CDATA[I appreciate the effort and openness of metacpan, but their search is still pretty bad. To be clear, compare the results of the search for DBIx:Class::Source on SCO and metacpan. That&#8217;s why I made the following greasemonkey/dotjs script: 123456$&#40;'a'&#41;.each&#40;function&#40;i,x&#41;&#123; &#160; &#160;var obj = $&#40;this&#41;; &#160; &#160;var href = obj.attr&#40;'href'&#41;; &#160; &#160;var re = new RegExp&#40;'^/~([^/]+)/(.*)$'&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>I appreciate the effort and openness of <a href="http://metacpan.org">metacpan</a>, but their search is still pretty bad.  To be clear, compare the results of the search for DBIx:Class::Source on <a href="http://search.cpan.org/search?query=dbix%3Aclass%3A%3Asource&#038;mode=all">SCO</a> and <a href="https://metacpan.org/search?q=DBIx%3AClass%3A%3ASource">metacpan</a>.  That&#8217;s why I made the following greasemonkey/dotjs script:</p>
<div class="codecolorer-container javascript vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span>x<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> href <span style="color: #339933;">=</span> obj.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> re <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'^/~([^/]+)/(.*)$'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">href</span> <span style="color: #339933;">=</span> href.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>re<span style="color: #339933;">,</span> <span style="color: #3366CC;">'https://metacpan.org/module/$1/$2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p>Put this in ~/.js/search.cpan.org.js to install it with dotjs.  Feel free to extend it to work for more than just modules.</p>
<div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://blog.afoolishmanifesto.com/archives/1627" rel="bookmark" class="crp_title">Weekly Status Report 2</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1640" rel="bookmark" class="crp_title">Weekly Status Report 3</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1595" rel="bookmark" class="crp_title">Weekly Status Report 1</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1601" rel="bookmark" class="crp_title">New Stuff in Class::C3::Componentised 1.001000</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1643" rel="bookmark" class="crp_title">New Stuff in Catalyst::ActionRole::PseudoCache 1.000001</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div><img src="http://feeds.feedburner.com/~r/AFoolishManifesto/~4/g-6gWkc9D4g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.afoolishmanifesto.com/archives/1692/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.afoolishmanifesto.com/archives/1692</feedburner:origLink></item>
		<item>
		<title>The Rise and Fall of Event Loops (in one very small place of my code)</title>
		<link>http://feedproxy.google.com/~r/AFoolishManifesto/~3/2bMoL6hV02o/1687</link>
		<comments>http://blog.afoolishmanifesto.com/archives/1687#comments</comments>
		<pubDate>Wed, 07 Mar 2012 01:22:18 +0000</pubDate>
		<dc:creator>fREW Schmidt</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[AnyEvent]]></category>
		<category><![CDATA[POE]]></category>
		<category><![CDATA[while(1)]]></category>

		<guid isPermaLink="false">http://blog.afoolishmanifesto.com/?p=1687</guid>
		<description><![CDATA[In the spirit of one of my other posts I&#8217;ve decided to chronicle my path with at least a couple event loops. More than eighteen months ago I documented my decision to start using an event loop as it would handle things I may not have considered, the example mentioned specifically in that post being [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.afoolishmanifesto.com/archives/1303">In the spirit of one of my other posts</a> I&#8217;ve decided to chronicle my path with at least a <a href="https://metacpan.org/module/AnyEvent">couple</a> <a href="https://metacpan.org/module/POE">event loops</a>.</p>
<p>More than eighteen months ago I <a href="http://blog.afoolishmanifesto.com/archives/1525">documented</a> my decision to start using an event loop as it would handle things I may not have considered, the example mentioned specifically in that post being exceptions.  Things went well!  I used the code I documented in that post for a long time with no issues until recently.  It turns out that the event loop I was using didn&#8217;t actually handle exceptions at all, thus completely nullifying my reason to use it.</p>
<p>So I looked elsewhere.  <a href="http://blog.afoolishmanifesto.com/archives/1682">I looked at the grandfather of event loops, POE</a>.  I like a lot of the components that have been written on top of POE, but POE itself is frustratingly low level.  That&#8217;s a topic for another post though (yes I looked at Reflex.)</p>
<p>After my last post and speaking with Rocco Caputo, auther of our venerable POE, I came up with the following runner role:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> Lynx<span style="color: #339933;">::</span><span style="color: #006600;">SMS</span><span style="color: #339933;">::</span><span style="color: #006600;">DoesRun</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> Moose<span style="color: #339933;">::</span><span style="color: #006600;">Role</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> POE<span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># this merely uses our logger etc</span><br />
with <span style="color: #ff0000;">'Lynx::SMS::HandlesDieForPOE'</span><span style="color: #339933;">;</span><br />
<br />
requires <span style="color: #ff0000;">'single_run'</span><span style="color: #339933;">;</span><br />
<br />
has period <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp;is <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'ro'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;required <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
has schema <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp;is <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'ro'</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> run <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;POE<span style="color: #339933;">::</span><span style="color: #006600;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">create</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; inline_states <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_start <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span>KERNEL<span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">sig</span><span style="color: #009900;">&#40;</span> DIE <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'sig_DIE'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span>KERNEL<span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">yield</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'loop'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sig_DIE <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;die_handler</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;loop <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span>KERNEL<span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">delay</span><span style="color: #009900;">&#40;</span> loop <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">period</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">single_run</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;POE<span style="color: #339933;">::</span><span style="color: #006600;">Kernel</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">run</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/no.html"><span style="color: #000066;">no</span></a> Moose<span style="color: #339933;">::</span><span style="color: #006600;">Role</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This works fine.  It&#8217;s (to me) a little ugly, but I imagine that I&#8217;d get used to it if I were to write much more POE.  But then Rocco pointed out that maybe I&#8217;m just wasting my time with event loops for this use case.  Ultimately using POE as a glorified Try::Tiny is stupid and really not even the goal.  So finally I&#8217;ve ended up just a few steps beyond where I started:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> Lynx<span style="color: #339933;">::</span><span style="color: #006600;">SMS</span><span style="color: #339933;">::</span><span style="color: #006600;">DoesRun</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> Moose<span style="color: #339933;">::</span><span style="color: #006600;">Role</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Try<span style="color: #339933;">::</span><span style="color: #006600;">Tiny</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Log<span style="color: #339933;">::</span><span style="color: #006600;">Contextual</span> <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span><span style="color: #339933;">:</span><a href="http://perldoc.perl.org/functions/log.html"><span style="color: #000066;">log</span></a> <span style="color: #339933;">:</span>dlog<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
requires <span style="color: #ff0000;">'single_run'</span><span style="color: #339933;">;</span><br />
<br />
has period <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp;is <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'ro'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;required <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
has schema <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp;is <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'ro'</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> run <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; try <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">single_run</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$error</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;log_error <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$error</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/sleep.html"><span style="color: #000066;">sleep</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">period</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/no.html"><span style="color: #000066;">no</span></a> Moose<span style="color: #339933;">::</span><span style="color: #006600;">Role</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The observant reader will notice that despite me mentioning the above use case, which is really the only important one for me given that our actual server will run all of our services in separate processes, there is still the benefit of Event Loops mentioned in the first post for development purposes (starting all services in a single program.)  I have indeed converted that to POE, but that probably doesn&#8217;t matter.  I run my unified service script maybe once or twice a year at this point.  Here it is if anyone is interested:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> Lynx<span style="color: #339933;">::</span><span style="color: #006600;">SMS</span><span style="color: #339933;">::</span><span style="color: #006600;">Runner</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> Moose<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> POE<span style="color: #339933;">;</span><br />
<br />
with <span style="color: #ff0000;">'Lynx::SMS::HandlesDieForPOE'</span><span style="color: #339933;">;</span><br />
<br />
has tasks <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp;is <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'ro'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;default <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> run <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;POE<span style="color: #339933;">::</span><span style="color: #006600;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">create</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; inline_states <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_start <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span>KERNEL<span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">sig</span><span style="color: #009900;">&#40;</span> DIE <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'sig_DIE'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">create_children_sessions</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sig_DIE <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;die_handler</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;POE<span style="color: #339933;">::</span><span style="color: #006600;">Kernel</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">run</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> create_children_sessions <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@tasks</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">tasks</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$task</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@tasks</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; POE<span style="color: #339933;">::</span><span style="color: #006600;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">create</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inline_states <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _start <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span>KERNEL<span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">delay</span><span style="color: #009900;">&#40;</span>loop <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$x</span><span style="color: #339933;">++</span> <span style="color: #339933;">/</span> <span style="color: #0000ff;">@tasks</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loop <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span>KERNEL<span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">delay</span><span style="color: #009900;">&#40;</span> loop <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$task</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">period</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">$task</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">single_run</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/no.html"><span style="color: #000066;">no</span></a> Moose<span style="color: #339933;">;</span><br />
<br />
__PACKAGE__<span style="color: #339933;">-&gt;</span><span style="color: #006600;">meta</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">make_immutable</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>I look forward to using POE for actual heavy-lifting in another one of our projects, and will post about the experience when I get there.</p>
<div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://blog.afoolishmanifesto.com/archives/1525" rel="bookmark" class="crp_title">Event Loops are better than while (1)</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1682" rel="bookmark" class="crp_title">Perl Event Loop</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1448" rel="bookmark" class="crp_title">Sensible database testing using Catalyst</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1679" rel="bookmark" class="crp_title">Using Catalyst::Plugin::Authentication with an old setup</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1595" rel="bookmark" class="crp_title">Weekly Status Report 1</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div><img src="http://feeds.feedburner.com/~r/AFoolishManifesto/~4/2bMoL6hV02o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.afoolishmanifesto.com/archives/1687/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.afoolishmanifesto.com/archives/1687</feedburner:origLink></item>
		<item>
		<title>Perl Event Loop</title>
		<link>http://feedproxy.google.com/~r/AFoolishManifesto/~3/pgrqsnSOpw0/1682</link>
		<comments>http://blog.afoolishmanifesto.com/archives/1682#comments</comments>
		<pubDate>Sun, 04 Mar 2012 17:09:16 +0000</pubDate>
		<dc:creator>fREW Schmidt</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[AnyEvent]]></category>
		<category><![CDATA[event loops]]></category>
		<category><![CDATA[POE]]></category>

		<guid isPermaLink="false">http://blog.afoolishmanifesto.com/?p=1682</guid>
		<description><![CDATA[I have some extremely basic code using AnyEvent but I recently found out that I was doing it wrong. That is, the entire reason I am using an event loop is to catch errors, log them, and keep going. That&#8217;s one of the great benefits that Catalyst gives me; I override one thing and I [...]]]></description>
			<content:encoded><![CDATA[<p>I have some extremely basic code using <a href="https://metacpan.org/module/AnyEvent">AnyEvent</a> but I recently found out that I was doing it wrong.  That is, the <strong>entire reason</strong> I am using an event loop is to catch errors, log them, and keep going.  That&#8217;s one of the great benefits that <a href="https://metacpan.org/module/Catalyst">Catalyst</a> gives me; I override one thing and I get universal error logging.  The problem is that AnyEvent <a href="https://metacpan.org/module/AnyEvent::FAQ#My-callback-dies-and...">specifically does not handle this use case</a>.</p>
<p>I have a working solution, but as I am planning on rewriting our services in evented code this prohibition makes me <strong>really</strong> worried.  The problem is that you can&#8217;t just know your code won&#8217;t die.  Exceptions happen and as a developer of a language that&#8217;s not Java or C# I don&#8217;t know where they come from.  My current solution is ok, but I don&#8217;t think it&#8217;s really viable long term.  Here&#8217;s my current code:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/usr/bin/env perl</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> AnyEvent<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Try<span style="color: #339933;">::</span><span style="color: #006600;">Tiny</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> event <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;looped<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">&quot;lol&quot;</span> <span style="color: #b1b100;">if</span> <a href="http://perldoc.perl.org/functions/rand.html"><span style="color: #000066;">rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #339933;">.</span>5<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> NEVER_DIE <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$code</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; try \<span style="color: #0000ff;">&amp;$code</span><span style="color: #339933;">,</span> catch <span style="color: #009900;">&#123;</span> <a href="http://perldoc.perl.org/functions/warn.html"><span style="color: #000066;">warn</span></a> <span style="color: #0000ff;">$_</span> <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;"># &lt;-- this should be logging, you get the idea</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cv</span> <span style="color: #339933;">=</span> AE<span style="color: #339933;">::</span><span style="color: #006600;">cv</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$w</span> <span style="color: #339933;">=</span> AE<span style="color: #339933;">::</span><span style="color: #006600;">timer</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> NEVER_DIE<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">\&amp;event</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #0000ff;">$cv</span><span style="color: #339933;">-&gt;</span><a href="http://perldoc.perl.org/functions/recv.html"><span style="color: #000066;">recv</span></a><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This works for simple cases, but if I chose to go down this route in the long term I&#8217;d have to wrap every single code ref in NEVER_DIE, which is pretty lame.</p>
<p>I looked at <a href="https://metacpan.org/modules/POE">POE</a> as it may support my use case better but as far as I can tell it&#8217;s support is WORSE.  Here&#8217;s what I came up with:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> POE<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Try<span style="color: #339933;">::</span><span style="color: #006600;">Tiny</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> handler_start <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$kernel</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$heap</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$session</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #009900;">&#91;</span>KERNEL<span style="color: #339933;">,</span> HEAP<span style="color: #339933;">,</span> SESSION<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #0000ff;">$kernel</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">yield</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'event'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> NEVER_DIE <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$code</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; try \<span style="color: #0000ff;">&amp;$code</span><span style="color: #339933;">,</span> catch <span style="color: #009900;">&#123;</span> <a href="http://perldoc.perl.org/functions/warn.html"><span style="color: #000066;">warn</span></a> <span style="color: #0000ff;">$_</span> <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;"># &lt;-- this should be logging, you get the idea</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> event <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;looped<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">&quot;lol&quot;</span> <span style="color: #b1b100;">if</span> <a href="http://perldoc.perl.org/functions/rand.html"><span style="color: #000066;">rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #339933;">.</span>5<span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span>KERNEL<span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">delay_add</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'event'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
POE<span style="color: #339933;">::</span><span style="color: #006600;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">create</span><span style="color: #009900;">&#40;</span><br />
&nbsp;inline_states <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;_start &nbsp; &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;handler_start</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;event &nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> NEVER_DIE<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">\&amp;event</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;_stop &nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">sub</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
POE<span style="color: #339933;">::</span><span style="color: #006600;">Kernel</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>So I still have to use NEVER_DIE, so that&#8217;s a lose, and worse, if event dies before the call to delay_add we end anyway.  Sure, I could put delay_add at the beginning of event, but that brings me to another thing that really bothers me about the &#8220;POE Way&#8221; (my own terminology, I may just not be getting it), for my AnyEvent code I can add a bunch of things and they don&#8217;t have to know about each other.  The loop handles calling the events.  With POE it seems like I have to manually tell it &#8220;call this, now call this.&#8221;  That seems to defeat the entire purpose!  What am I missing here?</p>
<p>If anyone knows an event loop I should consider (MUST RUN WELL ON WINDOWS) or maybe some setting in POE and some kind of POE timer thing, or some way of safely overriding how AE calls it&#8217;s events, please, comment and let me know.</p>
<div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://blog.afoolishmanifesto.com/archives/1687" rel="bookmark" class="crp_title">The Rise and Fall of Event Loops (in one very small place of my code)</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1525" rel="bookmark" class="crp_title">Event Loops are better than while (1)</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1643" rel="bookmark" class="crp_title">New Stuff in Catalyst::ActionRole::PseudoCache 1.000001</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1595" rel="bookmark" class="crp_title">Weekly Status Report 1</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1601" rel="bookmark" class="crp_title">New Stuff in Class::C3::Componentised 1.001000</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div><img src="http://feeds.feedburner.com/~r/AFoolishManifesto/~4/pgrqsnSOpw0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.afoolishmanifesto.com/archives/1682/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://blog.afoolishmanifesto.com/archives/1682</feedburner:origLink></item>
		<item>
		<title>Using Catalyst::Plugin::Authentication with an old setup</title>
		<link>http://feedproxy.google.com/~r/AFoolishManifesto/~3/K1NEvMeGjQk/1679</link>
		<comments>http://blog.afoolishmanifesto.com/archives/1679#comments</comments>
		<pubDate>Wed, 18 Jan 2012 01:04:37 +0000</pubDate>
		<dc:creator>fREW Schmidt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[catalyst]]></category>

		<guid isPermaLink="false">http://blog.afoolishmanifesto.com/?p=1679</guid>
		<description><![CDATA[Recently I took it upon myself to make Catalyst::Plugin::Authentication know users had logged in after users had logged in in a completely non-Catalyst part of our app. After LOTS of frustration, code spelunking, and bugging a couple people in #catalyst (hobbs and t0m) I got it working. Basically what I did was have the session [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I took it upon myself to make Catalyst::Plugin::Authentication know users had logged in after users had logged in in a completely non-Catalyst part of our app.  After LOTS of frustration, code spelunking, and bugging a couple people in #catalyst (hobbs and t0m) I got it working.</p>
<p>Basically what I did was have the session plugin look at a different cookie and load information from our own strange brew of session table.  It&#8217;s not perfect, but I&#8217;m much happier with it than I was before.  Here&#8217;s the code:</p>
<p>First, you need to create your own Session Store, our app is called Lynx, so the namespace reflects that:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> Lynx<span style="color: #339933;">::</span><span style="color: #006600;">Session</span><span style="color: #339933;">::</span><span style="color: #006600;">Store</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> base <span style="color: #009966; font-style: italic;">qw/Catalyst::Plugin::Session::Store/</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> DateTime<span style="color: #339933;">::</span><span style="color: #006600;">Format</span><span style="color: #339933;">::</span><span style="color: #006600;">MSSQL</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Catalyst<span style="color: #339933;">::</span><span style="color: #006600;">Authentication</span><span style="color: #339933;">::</span><span style="color: #006600;">Store</span><span style="color: #339933;">::</span><span style="color: #006600;">DBIx</span><span style="color: #339933;">::</span><span style="color: #006600;">Class</span><span style="color: #339933;">::</span><span style="color: #006600;">User</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">sub</span> get_session_data <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$c</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$key</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$k</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/split.html"><span style="color: #000066;">split</span></a> <span style="color: #009966; font-style: italic;">/:/</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$key</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$k</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">'session'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$login</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">model</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'DB::Login'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">single</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> access_num <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$v</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; __user_realm <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'default'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; __user &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;user <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$login</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">userid</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$k</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">'expires'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cookie</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">cookie</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Access_Num'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$login</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">model</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'DB::Login'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">single</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> access_num <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$v</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ex</span> <span style="color: #339933;">=</span> DateTime<span style="color: #339933;">::</span><span style="color: #006600;">Format</span><span style="color: #339933;">::</span><span style="color: #006600;">MSSQL</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">parse_datetime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$login</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">last_accessed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">epoch</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">720</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">-</span> DateTime<span style="color: #339933;">-&gt;</span><span style="color: #006600;">now</span><span style="color: #009900;">&#40;</span>time_zone <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'local'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">offset</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #0000ff;">$ex</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> store_session_data <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">sub</span> delete_session_data <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">sub</span> delete_expired_sessions <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>We have stub methods for the session stuff that we don&#8217;t support.  Eventually I may fill those out, but what&#8217;s more likely is that we remove this code entirely and just use what&#8217;s provided by CPA.</p>
<p>Next is get_session_data, which gets arguments like session:1234 and expires:1234.  They are meant to return the session data and the expiry time (seconds since epoch) respectively.  Clearly I had to do a lot of really weird stuff with datetime to get that expiration date from our database, but it works, so that&#8217;s cool.  You may store your expiration directly. Who knows.</p>
<p>So far, so weird.  Then I had to figure out how to &#8220;inflate&#8221; the session.  The keys __user_realm and __user are hardcoded in CPA, and I kinda think they should change to just current_user_realm and current_user, or maybe catalyst-plugin-authentication-user.  Whatever.  But the fact is they are what they are.  The value for __user_realm is which realm is currently selected.  I imagine the vast majority of people should have that set to default, as they typically only have a single realm (we actually have two, but I didn&#8217;t realize till this code broke in a special way.)  The value for __user is <strong>not</strong> a user object, but instead what get&#8217;s passed to the auth store&#8217;s from_session method.  I am <strong>mostly</strong> sure about that, but it&#8217;s a pretty deep stack trace at that point.</p>
<p>Next up I made a Session subclass:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> Lynx<span style="color: #339933;">::</span><span style="color: #006600;">Session</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> base <span style="color: #009966; font-style: italic;">qw/Catalyst::Plugin::Session/</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> sessionid <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$c</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$access_num</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cookie</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">cookie</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Access_Num'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$access_num</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$cookie</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">value</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #0000ff;">$access_num</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #cc66cc;">1</span></div></td></tr></tbody></table></div>
<p>This is clearly pretty basic.  I just overrode sessionid to look at our cookie to get the sessionid.</p>
<p>After that I just loaded the plugins I needed and configured CPA:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">...</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Catalyst <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp;Authentication<br />
&nbsp; &nbsp;<span style="color: #339933;">+</span>Lynx<span style="color: #339933;">::</span><span style="color: #006600;">Session</span><br />
&nbsp; &nbsp;Session<span style="color: #339933;">::</span><span style="color: #006600;">State</span><span style="color: #339933;">::</span><span style="color: #006600;">Cookie</span><br />
&nbsp; &nbsp;<span style="color: #339933;">+</span>Lynx<span style="color: #339933;">::</span><span style="color: #006600;">Session</span><span style="color: #339933;">::</span><span style="color: #006600;">Store</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #339933;">...</span><br />
&nbsp; &nbsp;<span style="color: #ff0000;">'Plugin::Authentication'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; default <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;credential <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Password'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; password_field <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'password'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; password_type <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'clear'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;store <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'DBIx::Class'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user_model <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'DB::User'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #339933;">...</span></div></td></tr></tbody></table></div>
<p>Note that the credential is unused in my use case as catalyst doesn&#8217;t do the actual authentication at all.</p>
<p>Hope this helps someone!</p>
<div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://blog.afoolishmanifesto.com/archives/1658" rel="bookmark" class="crp_title">Weekly Status Report 4</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1286" rel="bookmark" class="crp_title">Do Passwords Right</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/913" rel="bookmark" class="crp_title">OpenID with Catalyst and more</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/168" rel="bookmark" class="crp_title">Ext JS Conference</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1682" rel="bookmark" class="crp_title">Perl Event Loop</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div><img src="http://feeds.feedburner.com/~r/AFoolishManifesto/~4/K1NEvMeGjQk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.afoolishmanifesto.com/archives/1679/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.afoolishmanifesto.com/archives/1679</feedburner:origLink></item>
		<item>
		<title>Cloning Objects in Perl</title>
		<link>http://feedproxy.google.com/~r/AFoolishManifesto/~3/RuaLheYQB1c/1675</link>
		<comments>http://blog.afoolishmanifesto.com/archives/1675#comments</comments>
		<pubDate>Tue, 20 Sep 2011 06:59:37 +0000</pubDate>
		<dc:creator>fREW Schmidt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[clone]]></category>
		<category><![CDATA[Clone::Hooker]]></category>
		<category><![CDATA[moose]]></category>
		<category><![CDATA[MooseX::Clone]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[storable]]></category>

		<guid isPermaLink="false">http://blog.afoolishmanifesto.com/?p=1675</guid>
		<description><![CDATA[Recently I needed to do some deep cloning of some objects at work. I think I ended up looking at all of the major ways to do it, and I figure I might as well discuss them here. What is deep cloning? Nearly everyone should be able to answer this, but it doesn&#8217;t hurt to [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I needed to do some deep cloning of some objects at work.  I think I ended up looking at all of the major ways to do it, and I figure I might as well discuss them here.</p>
<h1>What is deep cloning?</h1>
<p>Nearly everyone should be able to answer this, but it doesn&#8217;t hurt to define it anyway.  Deep cloning means you clone other things the current object is related to, recursively.  So while a shallow clone of a hashref (in Perl) would be merely:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$clone</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$other_hash_ref</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>That doesn&#8217;t do if the things in the hash get mutated and are also references, because in that case you&#8217;ll be modifying parts of the other hash, possibly surprisingly.</p>
<h1>Isn&#8217;t this solved?</h1>
<p>Well yes.  If it&#8217;s something as basic as a simple data structure you can just use <a href="https://metacpan.org/module/Storable">Storable</a>.  The code for above would become:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">use</span> Storable <span style="color: #ff0000;">'dclone'</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$clone</span> <span style="color: #339933;">=</span> dclone<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$other_hash_ref</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Storable has been core enough for long enough that if it&#8217;s not core you need to upgrade <img src='http://blog.afoolishmanifesto.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<h1>What&#8217;s your problem?</h1>
<p>Sadly just default Storable isn&#8217;t good enough.  I needed to deeply clone the objects, but <strong>not</strong> clone any related schemata.  That is, the objects had a <a href="https://metacpan.org/module/DBIx::Class::Schema">DBIx::Class::Schema</a> object attached to them and for various reasons I do not want to clone that at all.  The correct way to deal with such an issue is to define the two Storable hooks as follows:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@stack</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">sub</span> STORABLE_freeze <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$cloning</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <a href="http://perldoc.perl.org/functions/q.html"><span style="color: #000066;">q</span></a><span style="color: #009900;">&#40;</span>you can<span style="color: #ff0000;">'t freeze this thing silly!) unless $cloning;<br />
<br />
&nbsp; &nbsp;my %ret = %$self;<br />
<br />
&nbsp; &nbsp;my %frame;<br />
&nbsp; &nbsp;$frame{schema} = delete $ret{schema};<br />
&nbsp; &nbsp;push @stack, \%frame;<br />
<br />
&nbsp; &nbsp;return \%ret<br />
}<br />
<br />
sub STORABLE_thaw {<br />
&nbsp; &nbsp;my ($self, $cloning, $ice) = @_;<br />
<br />
&nbsp; &nbsp;die q(you can'</span>t thaw this thing silly<span style="color: #339933;">!</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">unless</span> <span style="color: #0000ff;">$cloning</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%frame</span> <span style="color: #339933;">=</span> <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span><a href="http://perldoc.perl.org/functions/pop.html"><span style="color: #000066;">pop</span></a> <span style="color: #0000ff;">@stack</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$new</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #000000; font-weight: bold;">new</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">%$self</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/map.html"><span style="color: #000066;">map</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">$_</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$frame</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <a href="http://perldoc.perl.org/functions/keys.html"><span style="color: #000066;">keys</span></a> <span style="color: #0000ff;">%frame</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0000ff;">%$self</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">%$new</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>This is a little more generic than you probably need, and came from my prototype module, <a href="https://github.com/frioux/Clone-Hooker/blob/master/lib/Clone/Hooker.pm">Clone::Hooker</a>, but I gave up on that as well as Storable.</p>
<h1>Why did you give up on Storable?</h1>
<p>Two reasons; first, defining the hooks above might be a bad thing.  Storable is something that someone other than me may use, and by defining the hooks above I am changing the relatively generic interface of Storable for my module.  Second, there&#8217;s a better alternative that I ended up using.</p>
<h1>WHAT DID YOU DO?!</h1>
<p>I ended up settling on the handy <a href="https://metacpan.org/module/MooseX::Clone">MooseX::Clone</a>.  Obviously it is for Moose modules only, but all of my modules are Moose objects in this case.  It&#8217;s very simple to use, here&#8217;s how it works for me:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> Dashboard<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">use</span> Moose<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;with <span style="color: #ff0000;">'MooseX::Clone'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;has gadgets <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; is <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'rw'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; isa <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'ArrayRef'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; traits <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span><a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span>Clone<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> Gadget<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">use</span> Moose<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;with <span style="color: #ff0000;">'MooseX::Clone'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;has schema <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; is <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'ro'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$d</span> <span style="color: #339933;">=</span> Dashboard<span style="color: #339933;">-&gt;</span><span style="color: #000000; font-weight: bold;">new</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; gadgets <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Gadget<span style="color: #339933;">-&gt;</span><span style="color: #000000; font-weight: bold;">new</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; schema <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$schema</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cloned_d</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$d</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">clone</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This avoids the &#8220;global&#8221; nature of changing the interface of Storable, is fairly unobtrusive in my code, and works well.</p>
<div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://blog.afoolishmanifesto.com/archives/1601" rel="bookmark" class="crp_title">New Stuff in Class::C3::Componentised 1.001000</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1428" rel="bookmark" class="crp_title">Try Out Color Coded SQL</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1386" rel="bookmark" class="crp_title">git-svn for the win</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1349" rel="bookmark" class="crp_title">DBIx::Class has migrated to git!</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1627" rel="bookmark" class="crp_title">Weekly Status Report 2</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div><img src="http://feeds.feedburner.com/~r/AFoolishManifesto/~4/RuaLheYQB1c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.afoolishmanifesto.com/archives/1675/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.afoolishmanifesto.com/archives/1675</feedburner:origLink></item>
		<item>
		<title>Shortcut Constructor Method &amp; Conversion</title>
		<link>http://feedproxy.google.com/~r/AFoolishManifesto/~3/EJ8SXlF1PgY/1672</link>
		<comments>http://blog.afoolishmanifesto.com/archives/1672#comments</comments>
		<pubDate>Wed, 07 Sep 2011 17:31:41 +0000</pubDate>
		<dc:creator>fREW Schmidt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[smalltalk]]></category>

		<guid isPermaLink="false">http://blog.afoolishmanifesto.com/?p=1672</guid>
		<description><![CDATA[I left my book and notes at work yesterday, hence the late post. Shortcut Constructor Method What is the external interface for creating a new object when a Constructor Method is too wordy? Sometimes creating an object is exorbitantly wordy. The example that the author gives (in javascript) is the following: 1var p = new [...]]]></description>
			<content:encoded><![CDATA[<p>I left my book and notes at work yesterday, hence the late post.</p>
<h1>Shortcut Constructor Method</h1>
<p><strong>What is the external interface for creating a new object when a Constructor Method is too wordy?</strong></p>
<p>Sometimes creating an object is exorbitantly wordy.  The example that the author gives (in javascript) is the following:</p>
<div class="codecolorer-container javascript vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> p <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Point<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> x<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p><strong>Add methods to a lower level object that can construct your objects.  Take care to only do this rarely.</strong></p>
<p>This can&#8217;t be done with the example given in javascript, but the idea is to do something like the following:</p>
<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var p = ( 1 x 2 )</div></td></tr></tbody></table></div>
<p>Personally, I&#8217;m very wary of this idea.  I see the value, but even operator overloading, which is a step HIGHER level than this, is usually viewed skeptically.  I <strong>do</strong> think it&#8217;s a good idea to make shortcut methods to instantiate related objects, but that&#8217;s a far sight better than creating a method on all integers.  If you <strong>do</strong> monkey-patch something like integer, it would be best if it were done dynamically, so only the code in your own project sees it.</p>
<h1>Conversion</h1>
<p><strong>How do you convert an object&#8217;s format to another object&#8217;s format?</strong></p>
<p>This is (at least to me) quite obvious.  Some would think that they should add methods to every object to convert to other formats.  So one might monkey-patch the DOM stuff to return a jquery DOM thing with the asJQDom method or something like that.  Of course doing that means you&#8217;re going to end up with a ton of random conversion methods.</p>
<p><strong>Convert objects by merely instantiating the second object type</strong></p>
<p>This just seems so obvious I almost feel bad even writing it&#8230;</p>
<div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://blog.afoolishmanifesto.com/archives/1663" rel="bookmark" class="crp_title">Smalltalk Best Practice Patterns &#8211; Chapter 3 &#8211; Behavior &#8211; Methods</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1665" rel="bookmark" class="crp_title">Smalltalk Best Practice Patterns: Constructor Method</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1648" rel="bookmark" class="crp_title">Refactoring Dispatch Tables into Objects</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1670" rel="bookmark" class="crp_title">Smalltalk Best Practice Patterns &#8211; Constructor Parameter Method</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/304" rel="bookmark" class="crp_title">Javascript scope</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div><img src="http://feeds.feedburner.com/~r/AFoolishManifesto/~4/EJ8SXlF1PgY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.afoolishmanifesto.com/archives/1672/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.afoolishmanifesto.com/archives/1672</feedburner:origLink></item>
		<item>
		<title>Creating a pseudo attribute with DBIx::Class</title>
		<link>http://feedproxy.google.com/~r/AFoolishManifesto/~3/gYuMjLFUtC4/1668</link>
		<comments>http://blog.afoolishmanifesto.com/archives/1668#comments</comments>
		<pubDate>Sun, 04 Sep 2011 06:59:25 +0000</pubDate>
		<dc:creator>fREW Schmidt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dbix::class]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://blog.afoolishmanifesto.com/?p=1668</guid>
		<description><![CDATA[I&#8217;m surprised I haven&#8217;t actually blogged this before. I had to do it recently for the first time in a long time and I figured I&#8217;d share the secret sauce. At work we just added a complete permission system on top of our existing user system, but we didn&#8217;t want to make the UI as [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m surprised I haven&#8217;t actually blogged this before.  I had to do it recently for the first time in a long time and I figured I&#8217;d share the secret sauce.</p>
<p>At work we just added a complete permission system on top of our existing user system, but we didn&#8217;t want to make the UI as flexible as the underlying code.  We ended up making a single role (which has all permissions) called &#8220;Full Control&#8221;.  Without that role all you get is the stuff configured directly for your user; that is, your user gets a dashboard.  So instead of making a grid of roles etc etc we just made a single checkbox on the user edit form.  Of course I could have put in controller code to handle this special case, but I&#8217;m trying to get better at factoring code correctly.  (As an aside: two years ago I would have also put all of this in the model; the frustrating thing is that Fat Model Skinny Controller only really works for relatively small apps.  I&#8217;ll try to do a blog post on why I think that at another point later <img src='http://blog.afoolishmanifesto.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  )</p>
<p>Anyway, first off, here&#8217;s the full_control accessor I made:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">sub</span> full_control <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/exists.html"><span style="color: #000066;">exists</span></a> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$full_control</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$full_control</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">set_roles</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> name <span style="color: #339933;">=&gt;</span> FULL_CONTROL <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">user_roles</span><span style="color: #339933;">-&gt;</span><a href="http://perldoc.perl.org/functions/delete.html"><span style="color: #000066;">delete</span></a><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #0000ff;">$full_control</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">roles</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">search</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> name <span style="color: #339933;">=&gt;</span> FULL_CONTROL <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">count</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Not a whole lot going on.  If an argument is passed we set the user&#8217;s roles based on the truthiness of the argument.  Because the system is currently just the one role we delete all roles for clearing it.  Later on if we make the system more full featured we&#8217;ll have to change this up a bit of course.  If no argument is passed we just return the count of full control roles, as that approximates truthiness just fine.</p>
<p>Next up are the &#8220;insert&#8221; and update wrappers.  I quote insert because I actually override new:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">sub</span> update <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$args</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">@rest</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$full_control</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/delete.html"><span style="color: #000066;">delete</span></a> <span style="color: #0000ff;">$args</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>full_control<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #b1b100;">next</span><span style="color: #339933;">::</span><span style="color: #006600;">method</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$args</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">@rest</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0000ff;">$ret</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">full_control</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$full_control</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #0000ff;">$ret</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$args</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">@rest</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$full_control</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/delete.html"><span style="color: #000066;">delete</span></a> <span style="color: #0000ff;">$args</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>full_control<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0000ff;">$args</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>user_roles<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span> <span style="color: #009900;">&#123;</span> role <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span> name <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Full Control'</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$full_control</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #b1b100;">next</span><span style="color: #339933;">::</span><span style="color: #006600;">method</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$args</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">@rest</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #0000ff;">$ret</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The code for update should be abundantly clear.  We just update the object, calling our accessor afterwards.  The new code is a little bit more messy.  Basically, instead of trying to use the accessor on new (which is wrong as new doesn&#8217;t actually imply an insert) we just leverage the excellent MultiCreate which DBIx::Class provides for us.</p>
<p>And that&#8217;s it!  I hope this helps you get your job done that much faster/better <img src='http://blog.afoolishmanifesto.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://blog.afoolishmanifesto.com/archives/709" rel="bookmark" class="crp_title">DBIC&#8217;d</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1582" rel="bookmark" class="crp_title">DBIx::Class Extended Relationships</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1315" rel="bookmark" class="crp_title">Delegation via Roles</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/259" rel="bookmark" class="crp_title">The Beginning of a Roles Based Authorization System for Perl</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1490" rel="bookmark" class="crp_title">Predefined Schema Additions for DBIx::Class</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div><img src="http://feeds.feedburner.com/~r/AFoolishManifesto/~4/gYuMjLFUtC4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.afoolishmanifesto.com/archives/1668/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.afoolishmanifesto.com/archives/1668</feedburner:origLink></item>
		<item>
		<title>Smalltalk Best Practice Patterns – Constructor Parameter Method</title>
		<link>http://feedproxy.google.com/~r/AFoolishManifesto/~3/2MaEl285Ryk/1670</link>
		<comments>http://blog.afoolishmanifesto.com/archives/1670#comments</comments>
		<pubDate>Sat, 03 Sep 2011 23:10:06 +0000</pubDate>
		<dc:creator>fREW Schmidt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[constructor parameter method]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[smalltalk]]></category>

		<guid isPermaLink="false">http://blog.afoolishmanifesto.com/?p=1670</guid>
		<description><![CDATA[How do you set instance variables from a constructor method? The fundamental issue here is that often validation is bypassed at construction time, for whatever reason. So one&#8217;s accessor may look something like this: 12345678910111213141516171819sub x &#123; &#160; &#160;my $self = shift; &#160; &#160;if &#40;$self-&#62;constructing&#41; &#123; &#160; &#160; &#160;if &#40;exists $_&#91;0&#93;&#41; &#123; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>How do you set instance variables from a constructor method?</p>
<p>The fundamental issue here is that often validation is bypassed at construction time, for whatever reason.  So one&#8217;s accessor may look something like this:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">sub</span> x <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">constructing</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/exists.html"><span style="color: #000066;">exists</span></a> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>x<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>x<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/exists.html"><span style="color: #000066;">exists</span></a> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">'too high!'</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">'too low!'</span> &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>x<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>x<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> &nbsp; &nbsp; <br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Clearly this method is just doing to much.  To solve this we make special set methods that are entirely to be used during construction.  So in Perl this might look like the following:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">sub</span> _set_x <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>x<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$x</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Interestingly, with Moose we happily side-step this issue, as the default constructor doesn&#8217;t go through the accessors and already sets the raw values.</p>
<hr />
<p>Ok, so I think I may start trying to apply this stuff to JavaScript instead of Perl.  I almost feel like the fact that I have Moose in Perl is cheating.  I know that there is Joose in JavaScript, but I&#8217;ve yet to use that in production, and I find that I have a harder time making well factored code in JavaScript than Perl.  Part of that is that the underlying libraries I use in JS (ExtJS 3) are not really well factored either, but I still struggle with overall structure.</p>
<div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://blog.afoolishmanifesto.com/archives/1665" rel="bookmark" class="crp_title">Smalltalk Best Practice Patterns: Constructor Method</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1672" rel="bookmark" class="crp_title">Shortcut Constructor Method &#038; Conversion</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1663" rel="bookmark" class="crp_title">Smalltalk Best Practice Patterns &#8211; Chapter 3 &#8211; Behavior &#8211; Methods</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1660" rel="bookmark" class="crp_title">Smalltalk Best Practice Patterns, Chapters 1 and 2</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1668" rel="bookmark" class="crp_title">Creating a pseudo attribute with DBIx::Class</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div><img src="http://feeds.feedburner.com/~r/AFoolishManifesto/~4/2MaEl285Ryk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.afoolishmanifesto.com/archives/1670/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blog.afoolishmanifesto.com/archives/1670</feedburner:origLink></item>
		<item>
		<title>Smalltalk Best Practice Patterns: Constructor Method</title>
		<link>http://feedproxy.google.com/~r/AFoolishManifesto/~3/PMhWSHFK3Hk/1665</link>
		<comments>http://blog.afoolishmanifesto.com/archives/1665#comments</comments>
		<pubDate>Thu, 01 Sep 2011 23:28:43 +0000</pubDate>
		<dc:creator>fREW Schmidt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[constructor method]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[smalltalk]]></category>

		<guid isPermaLink="false">http://blog.afoolishmanifesto.com/?p=1665</guid>
		<description><![CDATA[Sadly reading is going slower than expected due to being so busy with various things in life. Oh well, just a single pattern today. Constructor Method How do you represent instantiation? In addition to a vanilla constructor, add methods for common cases to instantiate typical objects. For strange cases allow the use of accessors. Using [...]]]></description>
			<content:encoded><![CDATA[<p>Sadly reading is going slower than expected due to being so busy with various things in life.  Oh well, just a single pattern today.</p>
<h1>Constructor Method</h1>
<p><strong>How do you represent instantiation?</strong></p>
<p>In addition to a vanilla constructor, add methods for common cases to instantiate typical objects.  For strange cases allow the use of accessors.</p>
<p>Using Perl (with Moose) an example might be:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> Point<span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> Moose<span style="color: #339933;">;</span><br />
<br />
has x <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>is <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'ro'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
has <a href="http://perldoc.perl.org/functions/y.html"><span style="color: #000066;">y</span></a> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>is <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'ro'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> r_theta <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$class</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$r</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$theta</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #000000; font-weight: bold;">new</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; x <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$r</span> <span style="color: #339933;">*</span> <a href="http://perldoc.perl.org/functions/cos.html"><span style="color: #000066;">cos</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$theta</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/y.html"><span style="color: #000066;">y</span></a> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$r</span> <span style="color: #339933;">*</span> <a href="http://perldoc.perl.org/functions/sin.html"><span style="color: #000066;">sin</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$theta</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>So now both of the following work:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$p</span> <span style="color: #339933;">=</span> Point<span style="color: #339933;">-&gt;</span><span style="color: #000000; font-weight: bold;">new</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$v</span> <span style="color: #339933;">=</span> Point<span style="color: #339933;">-&gt;</span><span style="color: #006600;">r_theta</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1.4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://blog.afoolishmanifesto.com/archives/1670" rel="bookmark" class="crp_title">Smalltalk Best Practice Patterns &#8211; Constructor Parameter Method</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1672" rel="bookmark" class="crp_title">Shortcut Constructor Method &#038; Conversion</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1660" rel="bookmark" class="crp_title">Smalltalk Best Practice Patterns, Chapters 1 and 2</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1648" rel="bookmark" class="crp_title">Refactoring Dispatch Tables into Objects</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1663" rel="bookmark" class="crp_title">Smalltalk Best Practice Patterns &#8211; Chapter 3 &#8211; Behavior &#8211; Methods</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div><img src="http://feeds.feedburner.com/~r/AFoolishManifesto/~4/PMhWSHFK3Hk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.afoolishmanifesto.com/archives/1665/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blog.afoolishmanifesto.com/archives/1665</feedburner:origLink></item>
		<item>
		<title>Smalltalk Best Practice Patterns – Chapter 3 – Behavior – Methods</title>
		<link>http://feedproxy.google.com/~r/AFoolishManifesto/~3/mQS1R-USVMU/1663</link>
		<comments>http://blog.afoolishmanifesto.com/archives/1663#comments</comments>
		<pubDate>Thu, 01 Sep 2011 00:05:58 +0000</pubDate>
		<dc:creator>fREW Schmidt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[behavior]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[methods]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[smalltalk]]></category>

		<guid isPermaLink="false">http://blog.afoolishmanifesto.com/?p=1663</guid>
		<description><![CDATA[Today I had to spend time taking care of passport stuff for my upcoming honeymoon, so I only got to read a handful of pages. I&#8217;ll post my notes nonetheless. Methods are more important that state because, correctly factored, methods paper over any changes in state over time. Most of us who took OO classes [...]]]></description>
			<content:encoded><![CDATA[<p>Today I had to spend time taking care of passport stuff for my upcoming honeymoon, so I only got to read a handful of pages.  I&#8217;ll post my notes nonetheless.</p>
<p>Methods are more important that state because, correctly factored, methods paper over any changes in state over time.  Most of us who took OO classes in college had this hammered into our brains <img src='http://blog.afoolishmanifesto.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Methods should be written to get something done, but should also be written to communicate with the reader.  Method names like &#8220;task_1&#8243;, &#8220;task_2&#8243;, etc are completely useless for a regular person, and should be named as to what they actually do.</p>
<p>Small methods are expensive in that they cost more CPU cycles and typically cause the novice trouble in following the structure of a program.  On the other hand, more methods means more human readable names, easier maintenance (pinpointing changes,) and method overrideability is much more feasible with small methods.</p>
<h1>Composed Method</h1>
<p><strong>How do you split your program into methods?</strong></p>
<p>As already mentioned, large methods are faster and easier for the reader to follow, but small methods with good names work well in the long run.  A seasoned programmer is able to see a method and assume what it does without needing to read the code for it.  On top of that, small methods with good names allow you to communicate the structure of your code to the reader.  Also, small methods are a must for inheritance.</p>
<p><strong>Split your program into methods that do a single identifiable task.</strong></p>
<p>A Perl example might be something like:</p>
<div class="codecolorer-container perl vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">sub</span> run_app <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">intialize_app</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">app_loop</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">shutdown_app</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The <strong>Composed Method</strong> patter can be used in a top down fashion, that is, write your higher level methods in an almost pseudo-code fashion, and then fill in the details of the lower level methods as you work.  You may also opt to use the bottom up approach of writing a larger method and splitting it into smaller methods as you notice repetition or other reusable structures.  Or lastly (and I think the most new idea to me) you can use this to find holes in your API.  So if an object is calling more than one method on another object, the second object probably needs to implement a method that will encapsulate the multiple calls.</p>
<div id="crp_related"><h2>Related Posts:</h2><ul><li><a href="http://blog.afoolishmanifesto.com/archives/1672" rel="bookmark" class="crp_title">Shortcut Constructor Method &#038; Conversion</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1648" rel="bookmark" class="crp_title">Refactoring Dispatch Tables into Objects</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1660" rel="bookmark" class="crp_title">Smalltalk Best Practice Patterns, Chapters 1 and 2</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/940" rel="bookmark" class="crp_title">July 2009, DFW.p6m</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/706" rel="bookmark" class="crp_title">Dallas.p6m: May 2009</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div><img src="http://feeds.feedburner.com/~r/AFoolishManifesto/~4/mQS1R-USVMU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.afoolishmanifesto.com/archives/1663/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blog.afoolishmanifesto.com/archives/1663</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 1.719 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-05-15 17:28:45 -->

