<?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>Sat, 28 Jan 2012 17:34:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</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 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/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/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>
		<item>
		<title>Smalltalk Best Practice Patterns, Chapters 1 and 2</title>
		<link>http://feedproxy.google.com/~r/AFoolishManifesto/~3/QGq6QhDvDYc/1660</link>
		<comments>http://blog.afoolishmanifesto.com/archives/1660#comments</comments>
		<pubDate>Tue, 30 Aug 2011 23:29:23 +0000</pubDate>
		<dc:creator>fREW Schmidt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[smalltalk]]></category>

		<guid isPermaLink="false">http://blog.afoolishmanifesto.com/?p=1660</guid>
		<description><![CDATA[For work I decided I&#8217;d start reading some technical books, taking notes, and then trying to reiterate what I&#8217;ve learned. Yesterday I read the Preface and Chapter 1 and today I read Chapter 2, but sadly it&#8217;s all still introductory. I might as well discuss what I&#8217;ve read nonetheless. First though, I should say that [...]]]></description>
			<content:encoded><![CDATA[<p>For work I decided I&#8217;d start reading some technical books, taking notes, and then trying to reiterate what I&#8217;ve learned.  Yesterday I read the Preface and Chapter 1 and today I read Chapter 2, but sadly it&#8217;s all still introductory.  I might as well discuss what I&#8217;ve read nonetheless.</p>
<p>First though, I should say that I am reading the book because Thomas Doran of Catalyst development recommended it, and it clearly applies to Perl with Moose.  I am sure some of it won&#8217;t be applicable (the whitespace formatting for example) but hopefully it can make me a better programmer.</p>
<h1>Preface</h1>
<p>The main point of the book is to go over things that programmers deal with all the time, examples given are naming, where to split up methods, and how to make comprehensible code.</p>
<p>The overall structure of a pattern is:</p>
<ul>
<li>Recurring daily programming problem</li>
<li>Tradeoffs affecting the solution</li>
<li>Recipe for solution</li>
</ul>
<p>Patterns (especially the ones in this book) are good for pretty much everyone.  Beginners, obviously, as they don&#8217;t have experience or habits yet and these are &#8220;blessed&#8221; habits that they can start using immediately.  Professional programmers may get a fresh perspective or even more importantly a shared vocabulary for what they already do.  Teachers can use patterns as stepping stones to teach students or even just ideas for material to teach.  Managers, lastly, can use patterns for communication, so that they can actually understand what their programmers are doing.</p>
<h1>Chapter 1 &#8211; Introduction</h1>
<p>The title, Smalltalk Best Practice Patterns, can be broken into three parts.  Smalltalk is the language used in the book.  Best Practice is a legal term which basically means you did what a professional would do, without neglect.  And Patterns are, according to the book, decisions that experts make over and over.</p>
<p>Interestingly, the distinction is made between coding and programming, which I always thought was largely archaic.  The fundamental point that Beck makes remains though.  There is a time for planning, and a time for implementation, and if you run into an issue during implementation <strong>change the plan</strong>.</p>
<p>When programs get huge, hard to understand, and generally unwieldy, the information you should take from this is that you need to fix whatever issue is causing these problems, don&#8217;t just assume that the plan is set in stone and leave it forever.</p>
<p>The patterns described in the book should help you decrease duplicate code, remove conditional logic (not really sure what he means by that yet,) simplify complex methods, and clean up &#8220;structural code,&#8221; that is, using objects as data structures.</p>
<p>Good programs balance speed of development, project mutability over the lifetime of the system, and &#8220;copy pasta,&#8221; which is my term for making code that should be ok to be cargo culted.</p>
<p>Keep in mind that some patterns on help some of the above items listed.  That&#8217;s reality and part of becoming a great programmer is knowing which thing to optimize for when.</p>
<p>Another thing stressed in the book is that code should be written to be read.  That doesn&#8217;t mean write lots of comments (this is my interpretation) but it means name variables well, name methods well, and don&#8217;t do confusing things in the code if possible.</p>
<p>The things that all patterns take into account are productivity, life cycle cost (not making something so unmaintainable that you can&#8217;t do other work too,) time to market, and risk.  Again, these things need to be balanced.</p>
<p>According to Beck, good style is not repeating yourself, writing short methods and small objects, things should be modular and replaceable, low coupling which again leads to replaceability, and lastly, segmentation based on rates of change.  The last bit I&#8217;d never really considered before.  A perfect example is Catalyst vs CGI::Application.  With Catalyst you have a &#8220;context&#8221; that changes for each action and gets passed to the controllers.  The context has a request and a response.  With CGI::Application, on the other hand, the request and response are part of the controller, so the controller gets mutated on each response at a higher rate of change than the controller really needs.</p>
<p>A number of issues aren&#8217;t discussed in the book.  Exception handling is one, as it is confusing and the author didn&#8217;t have a signification amount of experience with it.  Copying objects isn&#8217;t really discussed as it&#8217;s usually a performance suck.  Become (which I&#8217;m guessing is a Smalltalk feature) isn&#8217;t discussed because it&#8217;s confusing.  Performance isn&#8217;t discussed as it&#8217;s typically not worth considering.  Overall design isn&#8217;t discussed; he points to the Gang of Four book for that.  Modelling and Requirements are entire books in themselves and thus left out.  And lastly UI design is left out, as it&#8217;s probably an entire field in itself.</p>
<p>The book is organized into 6 parts, philosophy of patterns, patterns for methods, patterns for state, patterns for collections, a brief overview of classes (in Smalltalk I&#8217;m guessing,) and formatting (which I might not even read.)</p>
<p>There are three methods for adoption of patterns listed in the book.  First is the Bag of Tricks method, where you pick and choose the few that you&#8217;ll start to implement over time.  Next is as a fairly large style guide, complete with justifications.  You pick the patterns for your organization, have programmers read them, and then ensure that people use them.  Last is the Whole Hog method, where you keep the book in your lap and every time you need to make a choice the book discusses, you look up the appropriate pattern and apply it.  I&#8217;m hoping to try the latter method, but we&#8217;ll see if I can keep to that.</p>
<p>To learn a pattern you should focus on the context (patterns before and after the pattern.)  You should read the example.  Focus on the problem shown and the solution shown.  If possible try to find examples of the problem in your own codebase.  Lastly, write your own example.  I fully intend on writing an example of each pattern in Perl.</p>
<h1>Chapter 2 &#8211; Patterns</h1>
<p>One interesting point is that patterns are supposed to work where code reuse doesn&#8217;t.  Another interesting fact is that a lot of these patterns are so small that reuse would be meaningless, for example, how do you &#8220;reuse&#8221; the naming of a method?</p>
<p>One of the main goals for patterns is to increase communication.  To increase communication you either need to say more, or have the speaker and listener share more ground so words mean more.  The latter is what patterns try to achieve.</p>
<p>By knowing patterns you should be able to understand code that uses such patterns better.  Patterns will also help you answer questions that come up constantly during development.  Shared patterns also help reduce the friction involved with code review as you just request that a pattern be implemented in a give place.  Patterns can also reduce documentation as it is more shared by the writer and reader.  Lastly, when joining an existing project, rote application of a handful of patterns can often help general understanding of the project as well as some basic cleanup.</p>
<p>The format for all patterns is: a title/name for the pattern, optionally any patterns that should be read before the current pattern, the problem that the pattern solves or maybe the question it answers, the forces that constrain the pattern, the solution to the problem, discussion or practical application of the pattern, and lastly, any patterns that should be read after the current pattern.</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/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/1670" rel="bookmark" class="crp_title">Smalltalk Best Practice Patterns &#8211; Constructor Parameter Method</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/977" rel="bookmark" class="crp_title">Initial Impressions of Catalyst Book</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/899" rel="bookmark" class="crp_title">Model Based Security</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/QGq6QhDvDYc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.afoolishmanifesto.com/archives/1660/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://blog.afoolishmanifesto.com/archives/1660</feedburner:origLink></item>
		<item>
		<title>Weekly Status Report 4</title>
		<link>http://feedproxy.google.com/~r/AFoolishManifesto/~3/EBCkQV7FCZ0/1658</link>
		<comments>http://blog.afoolishmanifesto.com/archives/1658#comments</comments>
		<pubDate>Mon, 29 Aug 2011 23:31:15 +0000</pubDate>
		<dc:creator>fREW Schmidt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[status report]]></category>

		<guid isPermaLink="false">http://blog.afoolishmanifesto.com/?p=1658</guid>
		<description><![CDATA[This week I finished the following three git conversions: Catalyst-Authentication-Store-DBIx-Class Catalyst-Authentication-Credential-FBConnect Catalyst-Authentication-Credential-OAuth And I started on the following four git conversions (I think they are ready but I want t0m to sign off on them) : Task-Catalyst Catalyst-Plugin-Authentication HTTP-Request-AsCGI Catalyst-Action-RenderView Related Posts:Using Catalyst::Plugin::Authentication with an old setupDo Passwords RightCatalyst Git ConversionNew Stuff in Catalyst::ActionRole::PseudoCache 1.000001Future [...]]]></description>
			<content:encoded><![CDATA[<p>This week I finished the following three git conversions:</p>
<ul>
<li>Catalyst-Authentication-Store-DBIx-Class</li>
<li>Catalyst-Authentication-Credential-FBConnect</li>
<li>Catalyst-Authentication-Credential-OAuth</li>
</ul>
<p>And I started on the following four git conversions (I think they are ready but I want t0m to sign off on them) :</p>
<ul>
<li>Task-Catalyst</li>
<li>Catalyst-Plugin-Authentication</li>
<li>HTTP-Request-AsCGI</li>
<li>Catalyst-Action-RenderView</li>
</ul>
<div id="crp_related"><h2>Related Posts:</h2><ul><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/1286" rel="bookmark" class="crp_title">Do Passwords Right</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1509" rel="bookmark" class="crp_title">Catalyst Git Conversion</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/762" rel="bookmark" class="crp_title">Future Post: Compare and Contrast CGIApp and Catalyst</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/EBCkQV7FCZ0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.afoolishmanifesto.com/archives/1658/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.afoolishmanifesto.com/archives/1658</feedburner:origLink></item>
		<item>
		<title>Stop accidentally committing all with git</title>
		<link>http://feedproxy.google.com/~r/AFoolishManifesto/~3/v7bhCiEuseA/1653</link>
		<comments>http://blog.afoolishmanifesto.com/archives/1653#comments</comments>
		<pubDate>Mon, 29 Aug 2011 06:59:49 +0000</pubDate>
		<dc:creator>fREW Schmidt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://blog.afoolishmanifesto.com/?p=1653</guid>
		<description><![CDATA[One of the things that annoys me a lot when using git is if I go through a lot of work to stage some changes, probably using `git add -p` to stage parts of files, and then from muscle memory I type `git ci -am &#8216;lolol I dummy&#8217;`. If you didn&#8217;t know the -a says [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things that annoys me a lot when using git is if I go through a lot of work to stage some changes, probably using `git add -p` to stage parts of files, and then from muscle memory I type `git ci -am &#8216;lolol I dummy&#8217;`.  If you didn&#8217;t know the -a says commit everything, so then of my painstaking staging is gone.</p>
<p>Well, on Thursday I finally fixed this problem.  I wrote the following, very basic, git wrapper.  All it does is:</p>
<ul>
<li>Find all aliases for commit</li>
<li>Check if the current command is a commit or commit alias</li>
<li>Check if the current arguments have -a or &#8211;all</li>
<li>Check if there are staged modifications</li>
<li>And if all of those conditions are true, it prompts the user to ensure that they actually want to commit all.</li>
</ul>
<p>I&#8217;m fairly happy with the alias detection;  the only thing it should also do is introspect the arguments in the values of the alias as well as the current command.  I don&#8217;t have any aliases like that, but if I wanted to make this a canned solution that would be a must.</p>
<p>The arguments detection is actually <strong>very</strong> dumb.  It wouldn&#8217;t work if you did `git ci -m &#8216;Foo&#8217; -a`.  I&#8217;m ok with that because this is to battle my own muscle memory and I would never type that.  But it is definitely a spot for improvement.</p>
<p>The staged checking I am very happy with.  It <strong>only</strong> checks for staged modifications.  So if you add a new file or delete a file and then do `git ci -am &#8220;station&#8221;` it will happily go on it&#8217;s way, which I like.</p>
<p>Anyway, here&#8217;s the script.  To install it just put it somewhere in your path as wrap-git (I use ~/bin) and alias git=wrap-git</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"><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: #b1b100;">my</span> <span style="color: #0000ff;">%aliases</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/map.html"><span style="color: #000066;">map</span></a> <span style="color: #009900;">&#123;</span> <a href="http://perldoc.perl.org/functions/split.html"><span style="color: #000066;">split</span></a><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\n/</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/split.html"><span style="color: #000066;">split</span></a> <span style="color: #009966; font-style: italic;">/\0/</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;<span style="color: #ff0000;">`git config -z --get-regexp alias<span style="color: #000099; font-weight: bold;">\\</span>.`</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%commit_aliases</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span> commit <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/map.html"><span style="color: #000066;">map</span></a> <span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">s/alias\.//</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$_</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/grep.html"><span style="color: #000066;">grep</span></a> <span style="color: #0000ff;">$aliases</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/^commit\b/</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/keys.html"><span style="color: #000066;">keys</span></a> <span style="color: #0000ff;">%aliases</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$command</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">@args</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$commit_aliases</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$command</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #0000ff;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/^-a|^--all/</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@staged</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/grep.html"><span style="color: #000066;">grep</span></a> <span style="color: #009966; font-style: italic;">/^M/</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;">/\0/</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">`git status -z`</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@staged</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;There are staged changes, are you sure you want to commit all? (y/N) &quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/chomp.html"><span style="color: #000066;">chomp</span></a><span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$answer</span> <span style="color: #339933;">=</span> <span style="color: #009999;">&lt;STDIN&gt;</span><span style="color: #009900;">&#41;</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;">$answer</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/^y/i</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;run_command<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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; run_command<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;run_command<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> run_command <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/system.html"><span style="color: #000066;">system</span></a> <span style="color: #ff0000;">'git'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$command</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">@args</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a> <span style="color: #0000ff;">$?</span> <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>One thing I think would be <strong>really</strong> cool would be to make a WrapGit.pm and wrap-git would just be coderefs passed to WrapGit.pm.  I&#8217;d love to have full introspection of all git commands and arguments.  It would let me do things like keep statistics about how you use git, maybe make a powerful achievement system, make more commands prompt the way this one does.  Anyway, I&#8217;ll probably do that one of these days when I finish all the other stuff on my list <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/1620" rel="bookmark" class="crp_title">Git aliases for your life</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/1616" rel="bookmark" class="crp_title">You should be using git grep</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1518" rel="bookmark" class="crp_title">Git 1.7.5.1 from git on ubuntu</a></li><li><a href="http://blog.afoolishmanifesto.com/archives/1547" rel="bookmark" class="crp_title">Nicer git remote URLs</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/v7bhCiEuseA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.afoolishmanifesto.com/archives/1653/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blog.afoolishmanifesto.com/archives/1653</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 2.290 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-01-31 22:04:48 -->

