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

<channel>
	<title>Elliot Loves Perl &#187; coding style</title>
	<atom:link href="http://elliotlovesperl.com/tag/coding-style/feed/" rel="self" type="application/rss+xml" />
	<link>http://elliotlovesperl.com</link>
	<description>A boy and his language</description>
	<lastBuildDate>Wed, 17 Feb 2010 12:43:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='elliotlovesperl.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/7899c71f1e2dd5c5fa1b1ec047ffdf34?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>Elliot Loves Perl &#187; coding style</title>
		<link>http://elliotlovesperl.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://elliotlovesperl.com/osd.xml" title="Elliot Loves Perl" />
	<atom:link rel='hub' href='http://elliotlovesperl.com/?pushpress=hub'/>
		<item>
		<title>How to structure Perl programs.</title>
		<link>http://elliotlovesperl.com/2009/11/23/how-to-structure-perl-programs/</link>
		<comments>http://elliotlovesperl.com/2009/11/23/how-to-structure-perl-programs/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 20:15:51 +0000</pubDate>
		<dc:creator>elliotlovesperl</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coding style]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[programs]]></category>

		<guid isPermaLink="false">http://elliotlovesperl.wordpress.com/?p=148</guid>
		<description><![CDATA[Don&#8217;t put the implementation of your program in your program; put it in a module instead. There are at least three reasons for doing things this way: Your code is more testable. You can feed in arbitrary command lines and check the exit code. Your code is reusable. If you need to use your program [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=elliotlovesperl.com&amp;blog=7775865&amp;post=148&amp;subd=elliotlovesperl&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Don&#8217;t put the implementation of your program in your program; put it in a module instead.  There are at least three reasons for doing things this way:</p>
<ul>
<li>Your code is more testable.  You can feed in arbitrary command lines and check the exit code.</li>
<li>Your code is reusable.  If you need to use your program from another Perl program, there&#8217;s no need to use <code>system</code>, backticks, or <a href="http://search.cpan.org/perldoc/IPC::System::Simple">IPC::System::Simple</a>, just <code>use</code> your module and call the implementing subroutine.</li>
<li>Your programs can be better managed by the standard Perl toolchain.  Program versions are not checked by CPAN, et. al. so if you create a new version of an existing program, it won&#8217;t be upgraded.  If all of your code is in a module, then version changes will cause updates to be installed.</li>
</ul>
<p>A simple way of doing this would be to have a module that looks like</p>
<div style="background-color:#333;margin:1em 2em;">
<font face="monospace"><br />
<font color="#ff0">&nbsp;1 </font><font color="#f0e68c"><b>package</b></font><font color="#bdb76b"><b>&nbsp;ProgramImplementation;</b></font><br />
<font color="#ff0">&nbsp;2 </font><br />
<font color="#ff0">&nbsp;3 </font><font color="#f0e68c"><b>use utf8</b></font>;<br />
<font color="#ff0">&nbsp;4 </font><font color="#f0e68c"><b>use </b></font><font color="#ffa0a0">5.010</font>;<br />
<font color="#ff0">&nbsp;5 </font><br />
<font color="#ff0">&nbsp;6 </font><font color="#f0e68c"><b>use strict</b></font>;<br />
<font color="#ff0">&nbsp;7 </font><font color="#f0e68c"><b>use warnings</b></font>;<br />
<font color="#ff0">&nbsp;8 </font><br />
<font color="#ff0">&nbsp;9 </font><font color="#f0e68c"><b>use </b></font>Exporter <font color="#ffa0a0">qw&lt;</font><font color="#ffa0a0">&nbsp;import </font><font color="#ffa0a0">&gt;</font>;<br />
<font color="#ff0">10 </font><br />
<font color="#ff0">11 </font><font color="#f0e68c"><b>our</b></font>&nbsp;<font color="#98fb98">@EXPORT_OK</font>&nbsp;= <font color="#ffa0a0">qw&lt;</font><font color="#ffa0a0">&nbsp;run </font><font color="#ffa0a0">&gt;</font>;<br />
<font color="#ff0">12 </font><br />
<font color="#ff0">13 </font><br />
<font color="#ff0">14 </font><font color="#f0e68c"><b>sub</b></font><font color="#98fb98">&nbsp;</font><font color="#98fb98">run </font>{<br />
<font color="#ff0">15 </font>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#f0e68c"><b>my</b></font>&nbsp;(<font color="#98fb98">@argv</font>) = <font color="#98fb98">@_</font>;<br />
<font color="#ff0">16 </font><br />
<font color="#ff0">17 </font>&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<br />
<font color="#ff0">18 </font><br />
<font color="#ff0">19 </font>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#f0e68c"><b>return</b></font>&nbsp;<font color="#ffa0a0">0</font>;<br />
<font color="#ff0">20 </font>}<br />
<font color="#ff0">21 </font><br />
<font color="#ff0">22 </font><font color="#ffa0a0">1</font>;<br />
</font>
</div>
<p>and then use that like this:</p>
<div style="background-color:#333;margin:1em 2em;">
<font face="monospace"><br />
<font color="#ff0">&nbsp;1 </font><font color="#f0e68c"><b>use utf8</b></font>;<br />
<font color="#ff0">&nbsp;2 </font><font color="#f0e68c"><b>use </b></font><font color="#ffa0a0">5.010</font>;<br />
<font color="#ff0">&nbsp;3 </font><br />
<font color="#ff0">&nbsp;4 </font><font color="#f0e68c"><b>use strict</b></font>;<br />
<font color="#ff0">&nbsp;5 </font><font color="#f0e68c"><b>use warnings</b></font>;<br />
<font color="#ff0">&nbsp;6 </font><br />
<font color="#ff0">&nbsp;7 </font><font color="#f0e68c"><b>use </b></font>ProgramImplementation <font color="#ffa0a0">qw&lt;</font><font color="#ffa0a0">&nbsp;run </font><font color="#ffa0a0">&gt;</font>;<br />
<font color="#ff0">&nbsp;8 </font><br />
<font color="#ff0">&nbsp;9 </font><font color="#f0e68c"><b>return</b></font>&nbsp;<font color="#ffa0a0">1</font>&nbsp;<font color="#f0e68c"><b>if</b></font>&nbsp;<font color="#f0e68c"><b>caller</b></font>;<br />
<font color="#ff0">10 </font><font color="#f0e68c"><b>exit</b></font>&nbsp;run(<font color="#98fb98">@ARGV</font>);<br />
</font>
</div>
<p>Having the &#8220;<code>return 1 if caller</code>&#8221; in there means that the program can be <code>require</code>d without causing the requiring program to exit.  (Whether you ought to actually do that is another matter.)</p>
<p>If you want things to be even more reusable, make your program implementation an object with attributes for the standard file handles:</p>
<div style="background-color:#333;margin:1em 2em;">
<font face="monospace"><br />
<font color="#ff0">&nbsp;1 </font><font color="#f0e68c"><b>package</b></font><font color="#bdb76b"><b>&nbsp;ObjectProgramImplementation;</b></font><br />
<font color="#ff0">&nbsp;2 </font><br />
<font color="#ff0">&nbsp;3 </font><font color="#f0e68c"><b>use utf8</b></font>;<br />
<font color="#ff0">&nbsp;4 </font><font color="#f0e68c"><b>use </b></font><font color="#ffa0a0">5.010</font>;<br />
<font color="#ff0">&nbsp;5 </font><br />
<font color="#ff0">&nbsp;6 </font><font color="#f0e68c"><b>use </b></font>Moose;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#87ceeb"># Or your favorite object module.</font><br />
<font color="#ff0">&nbsp;7 </font><br />
<font color="#ff0">&nbsp;8 </font>has <font color="#ffa0a0">stdout </font>=&gt; (<br />
<font color="#ff0">&nbsp;9 </font>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ffa0a0">isa </font>=&gt; <font color="#ffa0a0">&#8216;</font><font color="#ffa0a0">FileHandle</font><font color="#ffa0a0">&#8216;</font>,<br />
<font color="#ff0">10 </font>&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<br />
<font color="#ff0">11 </font>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ffa0a0">default </font>=&gt;<font color="#98fb98">&nbsp;</font><font color="#f0e68c"><b>sub</b></font><font color="#98fb98">&nbsp;</font>{ \*STDOUT },<br />
<font color="#ff0">12 </font>);<br />
<font color="#ff0">13 </font><br />
<font color="#ff0">14 </font>has <font color="#ffa0a0">stderr </font>=&gt; (<br />
<font color="#ff0">15 </font>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ffa0a0">isa </font>=&gt; <font color="#ffa0a0">&#8216;</font><font color="#ffa0a0">FileHandle</font><font color="#ffa0a0">&#8216;</font>,<br />
<font color="#ff0">16 </font>&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<br />
<font color="#ff0">17 </font>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ffa0a0">default </font>=&gt;<font color="#98fb98">&nbsp;</font><font color="#f0e68c"><b>sub</b></font><font color="#98fb98">&nbsp;</font>{ \*STDERR },<br />
<font color="#ff0">18 </font>);<br />
<font color="#ff0">19 </font><br />
<font color="#ff0">20 </font><br />
<font color="#ff0">21 </font><font color="#f0e68c"><b>sub</b></font><font color="#98fb98">&nbsp;</font><font color="#98fb98">run </font>{<br />
<font color="#ff0">22 </font>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#f0e68c"><b>my</b></font>&nbsp;(<font color="#98fb98">$self</font>, <font color="#98fb98">@argv</font>) = <font color="#98fb98">@_</font>;<br />
<font color="#ff0">23 </font><br />
<font color="#ff0">24 </font>&nbsp;&nbsp;&nbsp;&nbsp;say { <font color="#98fb98">$self</font>-&gt;stdout() } <font color="#ffa0a0">&#8216;</font><font color="#ffa0a0">Hello there!</font><font color="#ffa0a0">&#8216;</font>;<br />
<font color="#ff0">25 </font>&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<br />
<font color="#ff0">26 </font><br />
<font color="#ff0">27 </font>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#f0e68c"><b>return</b></font>&nbsp;<font color="#ffa0a0">0</font>;<br />
<font color="#ff0">28 </font>}<br />
<font color="#ff0">29 </font><br />
<font color="#ff0">30 </font><font color="#ffa0a0">1</font>;<br />
</font>
</div>
<p>Your regular program would then look like this:</p>
<div style="background-color:#333;margin:1em 2em;">
<font face="monospace"><br />
<font color="#ff0">&nbsp;1 </font><font color="#f0e68c"><b>use utf8</b></font>;<br />
<font color="#ff0">&nbsp;2 </font><font color="#f0e68c"><b>use </b></font><font color="#ffa0a0">5.010</font>;<br />
<font color="#ff0">&nbsp;3 </font><br />
<font color="#ff0">&nbsp;4 </font><font color="#f0e68c"><b>use strict</b></font>;<br />
<font color="#ff0">&nbsp;5 </font><font color="#f0e68c"><b>use warnings</b></font>;<br />
<font color="#ff0">&nbsp;6 </font><br />
<font color="#ff0">&nbsp;7 </font><font color="#f0e68c"><b>use </b></font>ObjectProgramImplementation <font color="#ffa0a0">qw&lt;</font><font color="#ffa0a0">&nbsp;</font><font color="#ffa0a0">&gt;</font>;<br />
<font color="#ff0">&nbsp;8 </font><br />
<font color="#ff0">&nbsp;9 </font><font color="#f0e68c"><b>return</b></font>&nbsp;<font color="#ffa0a0">1</font>&nbsp;<font color="#f0e68c"><b>if</b></font>&nbsp;<font color="#f0e68c"><b>caller</b></font>;<br />
<font color="#ff0">10 </font><font color="#f0e68c"><b>exit</b></font>&nbsp;ObjectProgramImplementation-&gt;<font color="#f0e68c"><b>new</b></font>()-&gt;run(<font color="#98fb98">@ARGV</font>);<br />
</font>
</div>
<p>But you can have other uses like</p>
<div style="background-color:#333;margin:1em 2em;">
<font face="monospace"><br />
<font color="#ffff00">&nbsp;1 </font><font color="#f0e68c"><b>my</b></font>&nbsp;<font color="#98fb98">$stdout</font>;<br />
<font color="#ffff00">&nbsp;2 </font><font color="#f0e68c"><b>my</b></font>&nbsp;<font color="#98fb98">$stderr</font>;<br />
<font color="#ffff00">&nbsp;3 </font><font color="#f0e68c"><b>open</b></font>&nbsp;<font color="#f0e68c"><b>my</b></font>&nbsp;<font color="#98fb98">$stdout_handle</font>, <font color="#ffa0a0">&#8216;</font><font color="#ffa0a0">&gt;</font><font color="#ffa0a0">&#8216;</font>, <font color="#98fb98">\$stdout</font>;<br />
<font color="#ffff00">&nbsp;4 </font><font color="#f0e68c"><b>open</b></font>&nbsp;<font color="#f0e68c"><b>my</b></font>&nbsp;<font color="#98fb98">$stderr_handle</font>, <font color="#ffa0a0">&#8216;</font><font color="#ffa0a0">&gt;</font><font color="#ffa0a0">&#8216;</font>, <font color="#98fb98">\$stderr</font>;<br />
<font color="#ffff00">&nbsp;5 </font><br />
<font color="#ffff00">&nbsp;6 </font><font color="#f0e68c"><b>my</b></font>&nbsp;<font color="#98fb98">$program</font>&nbsp;=<br />
<font color="#ffff00">&nbsp;7 </font>&nbsp;&nbsp;&nbsp;&nbsp;ObjectProgramImplementation-&gt;<font color="#f0e68c"><b>new</b></font>(<font color="#ffa0a0">stdout </font>=&gt; <font color="#98fb98">$stdout</font>, <font color="#ffa0a0">stderr </font>=&gt; <font color="#98fb98">$stderr</font>);<br />
<font color="#ffff00">&nbsp;8 </font><font color="#f0e68c"><b>my</b></font>&nbsp;<font color="#98fb98">$exit_code</font>&nbsp;= <font color="#98fb98">$program</font>-&gt;run(<font color="#98fb98">@ARGV</font>);<br />
<font color="#ffff00">&nbsp;9 </font><br />
<font color="#ffff00">10 </font><font color="#87ceeb"># Do something with $stdout/$stderr</font><br />
</font>
</div>
<br /> Tagged: coding style, development, programs <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/elliotlovesperl.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/elliotlovesperl.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/elliotlovesperl.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/elliotlovesperl.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/elliotlovesperl.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/elliotlovesperl.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/elliotlovesperl.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/elliotlovesperl.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/elliotlovesperl.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/elliotlovesperl.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/elliotlovesperl.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/elliotlovesperl.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/elliotlovesperl.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/elliotlovesperl.wordpress.com/148/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=elliotlovesperl.com&amp;blog=7775865&amp;post=148&amp;subd=elliotlovesperl&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://elliotlovesperl.com/2009/11/23/how-to-structure-perl-programs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e2e2e6430cc24b91f3c25f25fba6d9e6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">elliotlovesperl</media:title>
		</media:content>
	</item>
	</channel>
</rss>