<?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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>HEAVYWORKS</title>
	
	<link>http://www.heavyworks.net</link>
	<description>Extreme Software Engineering</description>
	<lastBuildDate>Wed, 28 Jul 2010 20:39:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Heavyworks" /><feedburner:info uri="heavyworks" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>Heavyworks</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Don’t be ruthless to your processes</title>
		<link>http://feedproxy.google.com/~r/Heavyworks/~3/sRA2jyPaQzA/dont-be-ruthless-to-your-processes</link>
		<comments>http://www.heavyworks.net/blog/posts/dont-be-ruthless-to-your-processes#comments</comments>
		<pubDate>Wed, 28 Jul 2010 20:38:35 +0000</pubDate>
		<dc:creator>Jan Seidl</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[bsd]]></category>
		<category><![CDATA[kill]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[signal]]></category>
		<category><![CDATA[terminal]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.heavyworks.net/?p=544</guid>
		<description><![CDATA[It&#8217;s a common (while wrong) practice while administering *NIX/BSD servers (or even stations) to terminate hanged processes with the kill command. Although the name suggests that it will mercilessly murder the process, its more a signal thrower. Signals tell processes to take determinate actions such as pause, continue, terminate, interrupt current work etc. Anatomy of [...]


Related posts:<ol><li><a href='http://www.heavyworks.net/blog/posts/automating-human-intervention-tasks-with-expect' rel='bookmark' title='Permanent Link: Automating human intervention tasks with Expect'>Automating human intervention tasks with Expect</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[
<p>It&#8217;s a common (while wrong) practice while administering *NIX/BSD servers (or even stations) to terminate hanged processes with the <code>kill</code> command.</p>
<p>Although the name suggests that it will mercilessly murder the process, its more a <em>signal thrower</em>. Signals tell processes to take determinate actions such as pause, continue, terminate, interrupt current work etc.<br />
<span id="more-544"></span></p>
<h2>Anatomy of process&#8217; death process</h2>
<p>There are basically four forms to terminate a process:</p>
<div id="attachment_547" class="wp-caption aligncenter" style="width: 434px"><img src="http://www.heavyworks.net/wordpress/wp-content/uploads/killComputer.gif" alt="" title="Killing the PC" width="424" height="341" class="size-full wp-image-547" /><p class="wp-caption-text">Wrong way to terminate a process</p></div>
<h3>Signal 15, SIGTERM. The Terminate Call</h3>
<p>Tells your process to end the task cleanly, closing all network connections, file handles, notifying child processes, resetting the terminal and go home in peace. The first thing to do when it becomes unresponsive.</p>
<h3>Signal 2, SIGINT. The Interrupt &#8211; A slap on the face</h2>
<p>Tells your process to stop what it is doing right now and leave immediately. The only way when SIGTERM fails to do the job cleanly. <kbd>CTRL+C</kbd> will throw this signal too.</p>
<h3>Signal 1, SIGHUP. The Hangup &#8211; Kick on the tummy</h3>
<p>Quite similar to SIGINT, SIGHUP is one step up. The Hangup is equivalent to the physical medium holding the process going down. It was very common on dial-up lines to indicate that the line had hang-up the call thus terminating the connection.</p>
<h3>Signal 9, SIGKILL. The Kill &#8211; A bullet on process&#8217; head</h3>
<p>SIGKILL will wipe the process out from your tree leaving all the crap it was using. Its the worst way of terminating a process since it will keep your resources open and can lead to any sort of mayhem.</p>
<h2>So what to do?</h2>
<p>As mentioned on <a href="http://partmaps.org/era/unix/award.html#uuk9letter">UUK9 (Useless use of Kill -9)</a>:</p>
<blockquote><p>Generally, send 15 <em>(SIGTERM)</em>, and wait a second or two, and if that doesn&#8217;t<br />
work, send 2 <em>(SIGINT)</em>, and if that doesn&#8217;t work, send 1 <em>(SIGHUP)</em>.  If that doesn&#8217;t,<br />
REMOVE THE BINARY because the program is badly behaved!</p></blockquote>
<p>Quite extremist, but totally true!</p>
<p>Treat your processes with dignity, and they won&#8217;t betray you! HTH!</p>


<p>Related posts:<ol><li><a href='http://www.heavyworks.net/blog/posts/automating-human-intervention-tasks-with-expect' rel='bookmark' title='Permanent Link: Automating human intervention tasks with Expect'>Automating human intervention tasks with Expect</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.heavyworks.net/blog/posts/dont-be-ruthless-to-your-processes/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.heavyworks.net/blog/posts/dont-be-ruthless-to-your-processes</feedburner:origLink></item>
		<item>
		<title>Solving iPhone speed issues with dynamic paging</title>
		<link>http://feedproxy.google.com/~r/Heavyworks/~3/FQRJGdqBt8E/solving-iphone-speed-issues-with-dynamic-paging</link>
		<comments>http://www.heavyworks.net/blog/posts/solving-iphone-speed-issues-with-dynamic-paging#comments</comments>
		<pubDate>Tue, 27 Apr 2010 01:38:21 +0000</pubDate>
		<dc:creator>Jan Seidl</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[cydia]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[jailbreak]]></category>
		<category><![CDATA[page file]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[swap memory]]></category>
		<category><![CDATA[swapfile]]></category>
		<category><![CDATA[top]]></category>
		<category><![CDATA[virtual memory]]></category>

		<guid isPermaLink="false">http://www.heavyworks.net/?p=509</guid>
		<description><![CDATA[NOTE: This article does not teaches how to Jailbreak your iPhone nor how to install Cydia an its apps. There are plenty of info on that out there. Please do not insist. NOTE: As pointed by Robert, the amount of I/O generated by the swapfile may greatly decrease the iPhone&#8217;s SSD lifetime since is designed [...]


Related posts:<ol><li><a href='http://www.heavyworks.net/blog/posts/typing-less-with-ssh' rel='bookmark' title='Permanent Link: Typing less with SSH'>Typing less with SSH</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/sustainability-application-under-software-development' rel='bookmark' title='Permanent Link: Sustainability application under Software Development'>Sustainability application under Software Development</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[
<p><em>NOTE: This article does not teaches how to Jailbreak your iPhone nor how to install Cydia an its apps. There are plenty of info on that out there. Please do not insist.</em></p>
<p><em><strong>NOTE: As pointed by Robert, the amount of I/O generated by the swapfile may greatly decrease the iPhone&#8217;s SSD lifetime since is designed to be more at rest.</strong></em></p>
<p>After buying an iPhone 2G from a friend I just couldn&#8217;t resists and started hacking all that I could. Put on the latest firmware by the time (3.1.3) and Jailbroke it, put on <a href="http://www.saurik.com/">Saurik</a>&#8216;s <a href="http://cydia.saurik.com/info/cycorder/">Cycorder</a> via <a href="http://cydia.saurik.com/">Cydia</a> (apt<a title="apt @ Wikipedia" href="http://en.wikipedia.org/wiki/Apt" class="wikipedia" rel="external wikipedia"><sup>w</sup></a> repository) (also from Saurik) in order to gain movie recording ability but didn&#8217;t went any further than that.</p>
<p>Then, as I don&#8217;t have a 3G/EDGE plan on my carrier, I decided only to access internet over WiFI but even scrambling APN credentials my little buddy kept connecting to my carrier EDGE network (and thus spending my money on high taxes). </p>
<p>After reading a while I discovered that SBSSettings would give me the option to control EDGE status (enabled/disabled) instead of just jamming credentials (that were being ignored by the carrier anyway). Installed and it just worked like a charm&#8230;. BUT&#8230; I&#8217;ve noticed that my Spingboard page transitions became a little hangy and apps opening and closing started to take much longer than it usual. From SBSSettings pane, I constantly see my memory running out due to iPhone&#8217;s lack of memory and lots of apps in background struggling for resources.</p>
<p><img src="http://www.heavyworks.net/wordpress/wp-content/uploads/slow-iphone.jpg" alt="iPhone speed issues cartoon" title="iPhone speed issues" width="608" height="300" class="aligncenter size-full wp-image-511" /></p>
<p><span id="more-509"></span></p>
<p>Some apps are constantly running on background since boot: the Phone app (obviously) and the Mail app. I had to go often to the SBSSettings&#8217; process manager and shut down Safari or iPod app when they insisted in being kept alive even after being closed thus still consuming my precious resources. The &#8220;Free Memory&#8221; tool from SBSSettings also helped a (tiny [micro]) little bit.</p>
<h2>SBSSettings is your nerdy fat friend</h2>
<p>After some researching on iPhone speed issues I&#8217;ve <a href="http://www.theneocell.com/sbsettings-was-slowin-system-down.htm" title="SBSSettings was slowin system down @ NeoCell">found</a> that SBSSettings, just eats tons of memory from the device. This got some discussion because someone on behalf of BigBoss commented on a post on the same blog saying that SBSSettings did not ran in background, instead it would use some Springboard hooks that would activate the app when triggered and then destroyed the app instance when closed.</p>
<blockquote><p>Actually SBSettings does not run in the background (which I believe is the whole premise of this article). Instead, it hooks springboard functions. When certain events inside springboard occur, springboard (which is a single process already running without sbsettings installed) causes the sbsettings window to open. When you close the window, it goes back to being idle again. It uses about 1mb of memory to exist but does not run in the background, does not slow the device down, and does not cause performance issues. It would be very nice if some research could be done before posting these sorts of articles :(
</p></blockquote>
<p><em>some commenter entitled on behalf of BigBoss</em></p>
<p>Well, I decided that I didn&#8217;t want to write (or think) on things based on assumptions so I decided to take a look further into my device system, logged on by <acronym title="Secure Shell">SSH</acronym> and ran a <code>top</code> command (had to install the &#8220;Top&#8221; app from Cydia) and there he was:</p>
<p><img src="http://www.heavyworks.net/wordpress/wp-content/uploads/iphone-top.jpg" alt="" title="iPhone's top output" class="aligncenter size-full wp-image-513" /></p>
<p>So it is, <em>in some kind of way</em>, running on background on my iPhone since the SBSSettings tab wasn&#8217;t open.</p>
<h2>Proposed workaround</h2>
<p>The proposed workaround is to <a href="http://www.theneocell.com/say-yes-to-bossprefs-no-to-sbsettings.htm">install BossPrefs</a> (even not supporting WIFI toggling by its interface on the 3.0 firmware) that is SBSSettings &#8220;father&#8221; (where it derived from it) and is quite being discontinued by BigBoss. I haven&#8217;t tried that yet because I&#8217;m still tied emotionally with SBSSettings even with the speed issue.</p>
<h2>Even more heavy <strike>crap</strike> stuff</h2>
<p>As if I hadn&#8217;t enough problems, I also have installed Veency (for VNC<a title="VNC @ Wikipedia" href="http://en.wikipedia.org/wiki/Vnc" class="wikipedia" rel="external wikipedia"><sup>w</sup></a> support) and OpenSSH (for <acronym title="Secure Shell">SSH</acronym> support! RAH) and they just goes background and eat memory (and battery) as well.</p>
<p>Well, I benefit from <acronym title="Secure Shell">SSH</acronym> and Veency. From <acronym title="Secure Shell">SSH</acronym> because I&#8217;m an unix freak and like the lovely smell of an embedded shell under a *nix kernel. Besides syncing my iPhone wirelessly is convenient too. Veency is nice to read SMS messages, view notes or monitor any app from distance, for example: Working on your desktop/laptop computer and having the cellphone on your pocket or far from the table (I&#8217;m not lazy I just like connecting remotely to stuff!). They didn&#8217;t quite handle much trouble to memory because SBSSettings toggle buttons permit me to shut&#8217;em down and up whenever needed. It also keep my battery from draining unnecessarily.</p>
<h3>Now THAT&#8217;s heavy, baby!</h3>
<p>Going further down I managed to install Winterboard (also via Cydia) since I like custom themed things &#8211; just like Hell Angel&#8217;s likes custom bikes &#8211; that also runs backgrounded and thus handling the same side effects. That really lagged stuff out too.</p>
<p>Then I noticed things started to get very laggy, but the hell, I NEED (ok, maybe I don&#8217;t) those awesomeness! SBSSettings just give me fine control over my device. But I had to choose: look&#8217;n'feel + control or performance.</p>
<p>I really love all that Cydia awesomeness and when I was almost removing Winterboard, SBSSettings and such, I remembered that I&#8217;ve heard in some article that Apple didn&#8217;t allowed 3rd-party apps to be put in background because &#8220;it would drain the battery and the short iPhone memory (128M)&#8221; that struggles to support the Phone, and iPod app that are natively always running on background. So if I won&#8217;t reduce the number of backgrounded apps, the only thing left is to get more memory but, how?</p>
<p>I can&#8217;t solder more RAM, that would be too way extreme. Then it snapped the sysamin insde me and though that the memory issue could be aided with a swap/page file (virtual memory) but it wasn&#8217;t implemented natively on device (I believe in behalf of the type of Flash Memory on the device that lacks on read/write speed). But what the hell, why not?</p>
<h2>Dynamic paging</h2>
<p>Searching the internet I&#8217;ve found <a href="http://modmyi.com/forums/general-iphone-chat/681840-iphone-swap-space.html">this forum topic on modmyi.com</a> that discussed the implementation of swap file (dynamic paging) on the device and, guess what, <strong>they did it</strong>. It&#8217;s a single <strong>plist</strong> file and we get a sensible less lagging and gagging.</p>
<p>I just moved the <code>plist</code> file on the device over ssh and rebooted. It&#8217;s working quite better than before, so I don&#8217;t need to thrash my goods so soon.</p>
<p><img src="http://www.heavyworks.net/wordpress/wp-content/uploads/swap-iphone.jpg" alt="" title="iPhone using swap memory" class="aligncenter size-full wp-image-514" /></p>
<p>I forgot to measure my iPhone load values before enabling dynamic paging (shame on me) to compare the gains so if anyone has some, please share on the comments! Hope this works for you as well as it worked for me! Do you have any other performance tips?</p>


<p>Related posts:<ol><li><a href='http://www.heavyworks.net/blog/posts/typing-less-with-ssh' rel='bookmark' title='Permanent Link: Typing less with SSH'>Typing less with SSH</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/sustainability-application-under-software-development' rel='bookmark' title='Permanent Link: Sustainability application under Software Development'>Sustainability application under Software Development</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.heavyworks.net/blog/posts/solving-iphone-speed-issues-with-dynamic-paging/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.heavyworks.net/blog/posts/solving-iphone-speed-issues-with-dynamic-paging</feedburner:origLink></item>
		<item>
		<title>Automating human intervention tasks with Expect</title>
		<link>http://feedproxy.google.com/~r/Heavyworks/~3/cBtmEwkiFrQ/automating-human-intervention-tasks-with-expect</link>
		<comments>http://www.heavyworks.net/blog/posts/automating-human-intervention-tasks-with-expect#comments</comments>
		<pubDate>Thu, 22 Apr 2010 17:23:20 +0000</pubDate>
		<dc:creator>Jan Seidl</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[expect]]></category>
		<category><![CDATA[tcl]]></category>
		<category><![CDATA[tk]]></category>

		<guid isPermaLink="false">http://www.heavyworks.net/?p=495</guid>
		<description><![CDATA[I administrate several servers and SSH password management were a big issue until I changed all my servers logins to use private keys. Things got really nicer when I discovered SSH aliases. Some of my clients are hosted on shared hostings and some of them doesn&#8217;t allow password-less authentication &#8220;in behalf of my security&#8221;. Well, [...]


Related posts:<ol><li><a href='http://www.heavyworks.net/blog/posts/typing-less-with-ssh' rel='bookmark' title='Permanent Link: Typing less with SSH'>Typing less with SSH</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/smartly-resolving-your-wordpress-pages-and-posts-javascript-and-css-dependences' rel='bookmark' title='Permanent Link: Smartly resolving your WordPress pages and posts JavaScript and CSS dependences'>Smartly resolving your WordPress pages and posts JavaScript and CSS dependences</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/scytale-a-greek-cryptography-implemented-in-c' rel='bookmark' title='Permanent Link: Scytale, a greek cryptography system implemented in C++'>Scytale, a greek cryptography system implemented in C++</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[
<p>I administrate several servers and <acronym title="Secure Shell">SSH</acronym> password management were a big issue until I changed all my servers logins to use private keys. Things got really nicer when I discovered <a href="http://www.heavyworks.net/blog/posts/typing-less-with-ssh" title="Typing less with SSH @ Heavyworks"><acronym title="Secure Shell">SSH</acronym> aliases</a>.</p>
<p>Some of my clients are hosted on shared hostings and some of them doesn&#8217;t allow password-less authentication &#8220;in behalf of my security&#8221;. Well, I can&#8217;t really afford opening my password manager, locating it and then copying and pasting back to my terminal. Too much time spent.</p>
<p>I have heard about expect long ago but never quite stopped to look at it since I never really had that need but in this case it came quite handy. Hop in aboard the new less typing world!<br />
<span id="more-495"></span></p>
<blockquote><p>Expect is a Unix automation and testing tool, written by Don Libes [from <a href="http://www.nist.gov">NIST</a>] as an extension to the Tcl scripting language, for interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others. It uses Unix pseudo terminals to wrap up subprocesses transparently, allowing the automation of arbitrary applications that are accessed over a terminal. With Tk, interactive applications can be wrapped in X11 GUIs.</p></blockquote>
<p><em>Source: <a href="http://en.wikipedia.org/wiki/Expect">Wikipedia</a></em></p>
<h2>Expect Overview</h2>
<p>Expect runs a very simple kind of script file in its own language. This script contains the logic to perform the automation and it&#8217;s called as first argument to the <code>expect</code> command. For a argument-less script named <code>fake_ssh_host-x.exp</code> the call would be.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">expect fake_ssh_host-x.exp</pre></div></div>

<p>If it took any argument it would just be appended to the call:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">expect fake_ssh_host-x.exp somefile.txt</pre></div></div>

<h2>Quick Expect anatomy</h2>
<p>Expect directives are line-separated and not terminated. Expect even has some object-notation for grouping <code>expect</code> events.</p>
<h3>Variables</h3>
<p>Variables are assigned in expect with the <code>set</code> directive.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">set</span> pass <span style="color: #ff0000;">&quot;mypass&quot;</span></pre></div></div>

<p>Variables are further referenced prefixed with a dollar-sign <code>$</code>.</p>
<h3>Command-line arguments</h3>
<p>Command-line arguments are get through the <code>$argv</code> variable and the <code>lindex</code> call. Now that we have learned how to assign variables, let&#8217;s assign one with a command-line argument.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">set</span> filename <span style="color: #7a0874; font-weight: bold;">&#91;</span>lindex <span style="color: #007800;">$argv</span> <span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>Note that indexes start at <code>0</code>, as the standard.<br />
You can have multiple command-line arguments:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">set</span> filename <span style="color: #7a0874; font-weight: bold;">&#91;</span>lindex <span style="color: #007800;">$argv</span> <span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">set</span> user <span style="color: #7a0874; font-weight: bold;">&#91;</span>lindex <span style="color: #007800;">$argv</span> <span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">set</span> host <span style="color: #7a0874; font-weight: bold;">&#91;</span>lindex <span style="color: #007800;">$argv</span> <span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<h3>Spawning the tampered process</h3>
<p>We use Expect in order to tamper output from a process and thus automate the inputs. We need to tell our Expect script which process it must spawn. This is done though the <code>spawn</code> directive that prefixes a normal command-line statement for that call.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">spawn <span style="color: #c20cb9; font-weight: bold;">ssh</span> user<span style="color: #000000; font-weight: bold;">@</span>host</pre></div></div>

<h3>Timeout</h3>
<p>Timeout determines the number of seconds that the process has to respond before being considered jammed (or b0rked) and is set through the <code>timeout</code> directive, much like as a variable is.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># one minute timeout</span>
<span style="color: #000000; font-weight: bold;">set</span> timeout <span style="color: #000000;">60</span></pre></div></div>

<p><em><code>timeout</code> is disabled if set to <code>-1</code></em></p>
<h3>Observing data</h3>
<p>The <code>expect</code> directive tells the script which text to expect from standard output.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">expect <span style="color: #ff0000;">&quot;String&quot;</span></pre></div></div>

<h3>Posting data</h3>
<p>Posting data to standard input is done through the <code>send</code> directive. This statement when precede by an <code>expect</code> statement will execute only when given string matched.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">send <span style="color: #007800;">$pass</span></pre></div></div>

<h3>Grouping expectations and posts</h3>
<p>This kinda object-notation is used to couple <code>expect</code>/<code>send</code> statements.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">expect <span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #ff0000;">&quot;password:&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
		send <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pass</span><span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span> 
<span style="color: #666666; font-style: italic;"># or shortened</span>
expect <span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #ff0000;">&quot;password:&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> send <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pass</span><span style="color: #000099; font-weight: bold;">\r</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>If you need to append more commands on an <code>expect</code> block just append the line within the expected element closure (in expanded form) or append to the line in a semi-colon delimited form.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">expect <span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #ff0000;">&quot;password:&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
		send <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pass</span><span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
		<span style="color: #000000; font-weight: bold;">set</span> timeout <span style="color: #660033;">-1</span>
	<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span> 
<span style="color: #666666; font-style: italic;"># or shortened</span>
expect <span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #ff0000;">&quot;password:&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> send <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pass</span><span style="color: #000099; font-weight: bold;">\r</span>&quot;</span> ; <span style="color: #000000; font-weight: bold;">set</span> timeout <span style="color: #660033;">-1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>Coupling multiple expectations is also easy:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">expect <span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #ff0000;">&quot;yes/no):&quot;</span> send <span style="color: #ff0000;">&quot;yes&quot;</span>
	<span style="color: #ff0000;">&quot;password:&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> send <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pass</span><span style="color: #000099; font-weight: bold;">\r</span>&quot;</span> ; <span style="color: #000000; font-weight: bold;">set</span> timeout <span style="color: #660033;">-1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
	eof <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<h3>Printing messages on screen</h3>
<p>If you want to send messages to the standard output rather than the process&#8217; input, you do exactly like the <code>send</code> statement described earlier in this article but the command is <code>send_user</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">send_user <span style="color: #ff0000;">&quot;Connected to host&quot;</span></pre></div></div>

<h3>Outputting to a file</h3>
<p>Sure, you can always pipe the output natively from the shell with the output redirectors <code>&gt;</code> but we can also do that within Expect by setting the <code>file</code> variable coupled with the <code>open</code> directive.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># filename in example is set to expect.out</span>
<span style="color: #666666; font-style: italic;"># 'w' opens file for writing, 'a' to append</span>
<span style="color: #000000; font-weight: bold;">set</span> <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>open expect.out <span style="color: #c20cb9; font-weight: bold;">w</span><span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>More documentation can be found on <a href="http://expect.nist.gov/">Expect&#8217;s website</a> or in its man page.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">man</span> expect</pre></div></div>

<h2>Faking a private key authentication <acronym title="Also known as">AKA</acronym> Automating a password authentication</h2>
<p>For both examples we will need variables and the <code>expect</code>, <code>send</code> and <code>spawn</code> directives.</p>
<h3><acronym title="Secure Shell">SSH</acronym> Session</h3>
<p>We will expect the message asking us if we want to accept the key and send <code>yes</code> to accept it. Then we will expect for the <code>password:</code> string and send our password stored in a variable.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/expect -f</span>
<span style="color: #000000; font-weight: bold;">set</span> timeout <span style="color: #660033;">-1</span>
spawn <span style="color: #c20cb9; font-weight: bold;">ssh</span> username<span style="color: #000000; font-weight: bold;">@</span>host
<span style="color: #000000; font-weight: bold;">set</span> pass <span style="color: #ff0000;">&quot;mypass&quot;</span>
expect <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        password: <span style="color: #7a0874; font-weight: bold;">&#123;</span> send <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pass</span><span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>  <span style="color: #7a0874; font-weight: bold;">&#125;</span>
	<span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #ff0000;">&quot;yes/no)?&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
		send <span style="color: #ff0000;">&quot;yes<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
		<span style="color: #000000; font-weight: bold;">set</span> timeout <span style="color: #660033;">-1</span>
        <span style="color: #7a0874; font-weight: bold;">&#125;</span> eof <span style="color: #7a0874; font-weight: bold;">&#123;</span>
                <span style="color: #7a0874; font-weight: bold;">exit</span>
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<h3>SCP Session</h3>
<p>We will follow the same logic from the above example but we&#8217;ll add some command-line arguments to catch the filename and path to be transfered.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/expect -f</span>
<span style="color: #000000; font-weight: bold;">set</span> timeout <span style="color: #660033;">-1</span>
<span style="color: #000000; font-weight: bold;">set</span> filename <span style="color: #7a0874; font-weight: bold;">&#91;</span>lindex <span style="color: #007800;">$argv</span> <span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">set</span> dst_path <span style="color: #7a0874; font-weight: bold;">&#91;</span>lindex <span style="color: #007800;">$argv</span> <span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
spawn <span style="color: #c20cb9; font-weight: bold;">scp</span> <span style="color: #007800;">$filename</span> username<span style="color: #000000; font-weight: bold;">@</span>host:<span style="color: #007800;">$dst_path</span>
<span style="color: #000000; font-weight: bold;">set</span> pass <span style="color: #ff0000;">&quot;mypass&quot;</span>
expect <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        password: <span style="color: #7a0874; font-weight: bold;">&#123;</span> send <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pass</span><span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>  <span style="color: #7a0874; font-weight: bold;">&#125;</span>
	<span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #ff0000;">&quot;yes/no)?&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
		send <span style="color: #ff0000;">&quot;yes<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
		<span style="color: #000000; font-weight: bold;">set</span> timeout <span style="color: #660033;">-1</span>
        <span style="color: #7a0874; font-weight: bold;">&#125;</span> eof <span style="color: #7a0874; font-weight: bold;">&#123;</span>
                <span style="color: #7a0874; font-weight: bold;">exit</span>
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>And you&#8217;re done. No more typing and unnecessary time spending.</p>


<p>Related posts:<ol><li><a href='http://www.heavyworks.net/blog/posts/typing-less-with-ssh' rel='bookmark' title='Permanent Link: Typing less with SSH'>Typing less with SSH</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/smartly-resolving-your-wordpress-pages-and-posts-javascript-and-css-dependences' rel='bookmark' title='Permanent Link: Smartly resolving your WordPress pages and posts JavaScript and CSS dependences'>Smartly resolving your WordPress pages and posts JavaScript and CSS dependences</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/scytale-a-greek-cryptography-implemented-in-c' rel='bookmark' title='Permanent Link: Scytale, a greek cryptography system implemented in C++'>Scytale, a greek cryptography system implemented in C++</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.heavyworks.net/blog/posts/automating-human-intervention-tasks-with-expect/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.heavyworks.net/blog/posts/automating-human-intervention-tasks-with-expect</feedburner:origLink></item>
		<item>
		<title>Better error handling</title>
		<link>http://feedproxy.google.com/~r/Heavyworks/~3/FClCLvtUBIg/better-error-handling</link>
		<comments>http://www.heavyworks.net/blog/posts/better-error-handling#comments</comments>
		<pubDate>Tue, 20 Apr 2010 18:44:28 +0000</pubDate>
		<dc:creator>Jan Seidl</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[error handling]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.heavyworks.net/?p=440</guid>
		<description><![CDATA[Error handling and reporting was always a thing that I knew I wasn&#8217;t doing it right. Leaded by PHP&#8217;s on-the-fly type casting I used to make functions just return false or null but that was not right because I was hurting the return-type integrity of them. Let&#8217;s assume we have a function that returns users [...]


Related posts:<ol><li><a href='http://www.heavyworks.net/blog/posts/5-best-practices-on-developing-wordpress-plugins' rel='bookmark' title='Permanent Link: 5 best practices on developing WordPress plugins'>5 best practices on developing WordPress plugins</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/smartly-resolving-your-wordpress-pages-and-posts-javascript-and-css-dependences' rel='bookmark' title='Permanent Link: Smartly resolving your WordPress pages and posts JavaScript and CSS dependences'>Smartly resolving your WordPress pages and posts JavaScript and CSS dependences</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/scytale-a-greek-cryptography-implemented-in-c' rel='bookmark' title='Permanent Link: Scytale, a greek cryptography system implemented in C++'>Scytale, a greek cryptography system implemented in C++</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[
<p><img src="http://www.heavyworks.net/wordpress/wp-content/uploads/catcher.jpg" alt="Baseball Catcher" title="Better error handling" width="635" height="255" class="aligncenter size-full wp-image-489" /><br />
Error handling and reporting was always a thing that I knew I wasn&#8217;t doing it right. Leaded by <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s on-the-fly type casting I used to make functions just return <code>false</code> or <code>null</code> but that was not right because I was hurting the return-type integrity of them.<br />
<span id="more-440"></span><br />
Let&#8217;s assume we have a function that returns users IDs. What to expect for return type? Integers! Hell yeah! But what if the requested user ID is not found? Would you return <code>false</code>, <code>null</code>? Wrong!</p>
<p>In strict languages like C/C++ or Java, you can&#8217;t return a boolean in an integer function and so on. <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> gives the coder this &#8220;freedom&#8221; (that I interpret as &#8220;room for mistakes and laziness&#8221;). </p>
<p>You can go a little tidier when using type strict languages returning <code>0</code> or <code>-1</code> (thus needing a <code>signed int</code> return type).</p>
<p>Exceptions are handled in a different way (will explain further) and you won&#8217;t need anymore to chunk your funky error codes/messages into your return codes.</p>
<p><em>NOTE: All the examples are in <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> and functions are merely illustrative.</em></p>
<h2>Using <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s <code>trigger_error</code></h2>
<p>This function can help you to standardize your errors with <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s native errors like <strong>notices</strong> and <strong>fatal errors</strong>. I used this for long time attached to an <strong>error handler</strong> class. This worked out most for system errors and notices like &#8220;<em>unable to open file</em>&#8221; and such but they mostly were treated by issuing fatal errors and forcing user to a custom-built <strong>Error 500 &#8211; Internal Server Error</strong> page (in case of web code) or exiting to system with a custom error code.</p>
<h2>Testing with <code>if</code>/<code>else</code></h2>
<p>As mentioned above, I started handling errors with <strong>false-ing</strong> and <strong>null&#8217;ing</strong> the return of functions and checking them for that. Example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> find_id_by_name<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT id FROM users WHERE name = '<span style="color: #006699; font-weight: bold;">{$name}</span>'&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: function :: find_id_by_name</span></pre></div></div>

<p>And then checked when calling:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> find_id_by_name<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;John Doe&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
	<span style="color: #666666; font-style: italic;"># do stuff
</span><span style="color: #b1b100;">else</span><span style="color: #339933;">:</span>
	<span style="color: #666666; font-style: italic;"># print some message on screen
</span><span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span></pre></div></div>

<p><em>NOTE: Returning zero (0) would trigger a false condition too.</em></p>
<p>Although this works pretty well it has several problems:</p>
<h3>Obtrusion</h3>
<p>If the checking code block prints messages on screen directly with <code>print</code> or <code>echo</code> it will be obtrusive for other functions calling it.</p>
<h3>Multiple fail points</h3>
<p>If a function can fail many ways, if you take the <code>false</code>/<code>null</code> approach you will know only that the function has failed but will have to insert debugging checkpoints on the function to know where it failed exactly.</p>
<p>Returning error codes may aid this point but cannot be used in <code>integer</code> functions because it would mix up with valid return entries and would still hurt our return type integrity.</p>
<h2>Return Codes</h2>
<p>As mentioned above, one could return integer error codes in order to identify the type of error that occurred. Although is a great leap in front of the <code>null</code>/<code>false</code> initiative.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ERROR_NO_USER'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ERROR_INVALID_PASSWORD'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> auth_user<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> find_user<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> ERROR_NO_USER<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span> <span style="color: #339933;">!==</span> hash_function<span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> ERROR_INVALID_PASSWORD
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$user</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: function :: auth_user</span></pre></div></div>

<p>Note that this function returns an <code>Object</code> while it returns <code>integers</code> on failure.</p>
<p>To check if user has successfully authed, we would use something like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> auth_user<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jdoe&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;passw0rd&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #666666; font-style: italic;"># hmmm... error occurred!
</span><span style="color: #b1b100;">else</span><span style="color: #339933;">:</span>
	<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span></pre></div></div>

<p>But this still doesn&#8217;t seems right, and you still have to cluch a <code>switch</code> statement inside the error treating area to take actions depending on error code.</p>
<h3>Returning error codes in functions with integer return type</h3>
<p>In functions with integer return type you can&#8217;t return positive error codes because it would be confused with valid non-error entries unless you used negatively signed integers for that.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> find_id_by_name<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;"># ... some code to get user data ...
</span>
	<span style="color: #666666; font-style: italic;"># User entry exists?
</span>	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;"># User account is inactive?
</span>	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isInactive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: function :: find_id_by_name</span></pre></div></div>

<h2>Signaling failures in a return-safe manner</h2>
<p>You can always use a identifier of the same return type to illustrate failures. This technique can only be used to signalize entire function failure and does not gives you the fine grained error control that <code>try</code> gives.</p>
<h3>Integer return types</h3>
<p>Procedural languages like C has done this forever. In integer returned functions you can return <code>-1</code> to signalize function failure.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> find_id_by_name<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;"># ... some code
</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ids</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span>
		<span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: function :: find_id_by_name</span></pre></div></div>

<h3>Returning empty lists</h3>
<p>When dealing with function that returns lists (lists/arrays, dictionaries and such) you can also use an empty list in return signaling that no entries are found. This persists the function return-type and will flow naturally if parent function uses the returned value into a <code>foreach</code> or <code>for</code> statement.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> find_users_by_country<span style="color: #009900;">&#40;</span><span style="color: #000088;">$country_id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;"># ... some code
</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$users</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$users</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: function :: find_users_by_country</span></pre></div></div>

<h2>Using Try/Catch and Exceptions</h2>
<p>One of the greatest practices I learned in my life as a developer was to introduce the power of <code>try</code>/<code>catch</code> into my code. This long-known practice by Java users was introduced in <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> version 5 and I think it&#8217;s present in almost every OOP<a title="OOP @ Wikipedia" href="http://en.wikipedia.org/wiki/Object_Oriented_Programming" class="wikipedia" rel="external wikipedia"><sup>w</sup></a> implementation like Java, PHP5, C++, Python and even in Flash AS3 (since its inherited from Java structure).</p>
<p>It gives you the ability to raise real-time errors during execution without the need to interfere within your returns. It helps you keeping your functions returning only data its expecting.</p>
<h3>Try/Catch overview</h3>
<p>The <code>try</code> block surrounds a set of statements that must &#8220;not throw an exception&#8221;  to proceed. If any of the inner statements <code>throw</code>s any exception, function execution proceeds but its routed to the <code>catch</code> block corresponding to the kind of exception that was raised. </p>
<p>But hell, what are exceptions?</p>
<p>As corrected by <a href="http://blog.seanja.com/">SeanJA from SquirrelHacker</a> Exceptions are the Object-oriented form special errors that <strong>aren&#8217;t expected</strong> in the flow.</p>
<p>Exception are good to distinguish errors from each other from their class type. Exceptions can also have custom properties and methods for each type. You just have to define them in your exception class. They also extends a main Exception class with methods generally available to all classes such as the <code>getMessage</code> method in <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>.</p>
<p>In most implementation, exception classes often receives a custom message as first parameter that would be read later on by the corresponding <code>getMessage</code> method.</p>
<p>Example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
	do_something<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	do_other_thing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">print</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: try</span></pre></div></div>

<h3>Throwing exceptions (errors)</h3>
<p>Inside your function you can use the <code>throw</code> statement to generate exceptions that will be caught in the <code>catch</code> block of the caller function. Lets rewrite our function used in the early example with exceptions.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> find_id_by_name<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;"># ... some code to get user data ...
</span>
	<span style="color: #666666; font-style: italic;"># Connection is up?
</span>	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conn</span><span style="color: #009900;">&#41;</span>
		throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Connection was dropped.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: function :: find_id_by_name</span></pre></div></div>

<h3>Custom-built exceptions</h3>
<p>Working with generic exceptions is already very useful but it stills give you few control about the type of error generated. Generic exceptions are good if you will only catch and forward the error messages or if your error handling is independent of the error type.</p>
<p>You can easily generate custom exceptions by extending the main Exception class (varies according to the language). In <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>, this is the <code>Exception</code> class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> FileLockedException <span style="color: #000000; font-weight: bold;">extends</span> Exception <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: class :: FileLockedException</span></pre></div></div>

<p>That&#8217;s it, if you don&#8217;t need no additional methods or proprieties, you&#8217;re done! You may successful use your new exceptions while treating errors with <code>catch</code>. If you need it, just treat your Exception class as any normal OOP class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> FileLockedException <span style="color: #000000; font-weight: bold;">extends</span> Exception <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$timestamp</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: class :: FileLockedException</span>
&nbsp;
some_function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$ex</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FileLockedException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$ex</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timestamp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d H:i:s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	throw <span style="color: #000088;">$ex</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: function :: some_function</span></pre></div></div>

<h3>Catching exceptions</h3>
<p>As seen on the example on try/catch overview, catching just requires a <code>catch</code> statement for the corresponding Exception class. All exceptions that doesn&#8217;t have an <code>catch</code> statement for its own class type will fall back to the main exception class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
&nbsp;
	do_something<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	do_other_thing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>FileLockedException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Cannot open file for writing, locked!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">print</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: try</span></pre></div></div>

<h3>Rethrow, bubbling up exceptions</h3>
<p>Exceptions can be bubbled up by rethrowing with a <code>throw</code> statement under a <code>catch</code> block. This is used to delegate the control of parsing the error to a parent function and to force triggering the <code>catch</code> blocks on them.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> do_something<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	try <span style="color: #009900;">&#123;</span>
&nbsp;
		do_stuff<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		throw <span style="color: #000088;">$e</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: try</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: function :: do_something</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> do_stuff<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$somethingWentWrong</span><span style="color: #009900;">&#41;</span>
		throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Something went wrong!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: function :: do_stuff</span></pre></div></div>

<h2>Overhead &#038; Overkill or When to use exceptions?</h2>
<p>SeanJA wrote <a href="http://blog.seanja.com/2010/04/exceptions-are-not-for-flow-control/">an excellent article about that</a> clarifing some points (thanks SeanJA!).</p>
<p>To differ errors from exceptions, you need to point out if what you are returning is expected or not. You can achieve this by focusing on the purpose of your function.</p>
<p>To keep up with previous examples, lets assume our login function. Which is its purpose? The function is asked &#8220;This user credentials are valid?&#8221; and it expects &#8220;Yes&#8221; or &#8220;No&#8221;. </p>
<p>Credentials are a tuple of <strong>username</strong> and <strong>password</strong> so if one of these are wrong, your function should naturally return <code>false</code> because one of them are wrong, but that is expected.</p>
<p>Unexpected data is something that is beyond your control, like a locked file, a database timeout and such. Check out SeanJA if you need more clarification on this subject.</p>
<p>Once I got in love with Exceptions I can&#8217;t live without it. It gets you even more in depth within OOP, and I&#8217;m satisfied with it. </p>
<p>Treat your errors well and they will not betray you late in the nite!</p>


<p>Related posts:<ol><li><a href='http://www.heavyworks.net/blog/posts/5-best-practices-on-developing-wordpress-plugins' rel='bookmark' title='Permanent Link: 5 best practices on developing WordPress plugins'>5 best practices on developing WordPress plugins</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/smartly-resolving-your-wordpress-pages-and-posts-javascript-and-css-dependences' rel='bookmark' title='Permanent Link: Smartly resolving your WordPress pages and posts JavaScript and CSS dependences'>Smartly resolving your WordPress pages and posts JavaScript and CSS dependences</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/scytale-a-greek-cryptography-implemented-in-c' rel='bookmark' title='Permanent Link: Scytale, a greek cryptography system implemented in C++'>Scytale, a greek cryptography system implemented in C++</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.heavyworks.net/blog/posts/better-error-handling/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.heavyworks.net/blog/posts/better-error-handling</feedburner:origLink></item>
		<item>
		<title>Brazillian Portuguese (pt_BR) language file released for Simple:Press Forum WordPress plugin</title>
		<link>http://feedproxy.google.com/~r/Heavyworks/~3/OHEAES-JcAQ/brazillian-portuguese-pt_br-language-file-released-for-simplepress-forum-wordpress-plugin</link>
		<comments>http://www.heavyworks.net/blog/posts/brazillian-portuguese-pt_br-language-file-released-for-simplepress-forum-wordpress-plugin#comments</comments>
		<pubDate>Fri, 05 Mar 2010 16:58:35 +0000</pubDate>
		<dc:creator>Jan Seidl</dc:creator>
				<category><![CDATA[Translation]]></category>
		<category><![CDATA[forum]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[internationalization]]></category>
		<category><![CDATA[l10n]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.heavyworks.net/?p=435</guid>
		<description><![CDATA[We released today with the help of Bulli Castelo the internationalization file for this wonderful forum plugin. The language files can be found at the official Simple:Press Forum translations website. Enjoy! Related posts:Brazillian Portuguese (pt_BR) language file for WP-Filebase released 5 best practices on developing WordPress plugins Smartly resolving your WordPress pages and posts JavaScript [...]


Related posts:<ol><li><a href='http://www.heavyworks.net/blog/posts/brazillian-portuguese-pt_br-language-file-for-wp-filebase-released' rel='bookmark' title='Permanent Link: Brazillian Portuguese (pt_BR) language file for WP-Filebase released'>Brazillian Portuguese (pt_BR) language file for WP-Filebase released</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/5-best-practices-on-developing-wordpress-plugins' rel='bookmark' title='Permanent Link: 5 best practices on developing WordPress plugins'>5 best practices on developing WordPress plugins</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/smartly-resolving-your-wordpress-pages-and-posts-javascript-and-css-dependences' rel='bookmark' title='Permanent Link: Smartly resolving your WordPress pages and posts JavaScript and CSS dependences'>Smartly resolving your WordPress pages and posts JavaScript and CSS dependences</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[
<p>We released today with the help of <a href="http://www.bullicastelo.com">Bulli Castelo</a> the internationalization file for this wonderful WordPress<a title="WordPress @ Wikipedia" href="http://en.wikipedia.org/wiki/Wordpress" class="wikipedia" rel="external wikipedia"><sup>w</sup></a> forum plugin.</p>
<p>The language files can be found at the <a href="http://translate.simplepressforum.com/translations/brazilian/">official Simple:Press Forum translations website</a>.</p>
<p>Enjoy!</p>


<p>Related posts:<ol><li><a href='http://www.heavyworks.net/blog/posts/brazillian-portuguese-pt_br-language-file-for-wp-filebase-released' rel='bookmark' title='Permanent Link: Brazillian Portuguese (pt_BR) language file for WP-Filebase released'>Brazillian Portuguese (pt_BR) language file for WP-Filebase released</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/5-best-practices-on-developing-wordpress-plugins' rel='bookmark' title='Permanent Link: 5 best practices on developing WordPress plugins'>5 best practices on developing WordPress plugins</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/smartly-resolving-your-wordpress-pages-and-posts-javascript-and-css-dependences' rel='bookmark' title='Permanent Link: Smartly resolving your WordPress pages and posts JavaScript and CSS dependences'>Smartly resolving your WordPress pages and posts JavaScript and CSS dependences</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.heavyworks.net/blog/posts/brazillian-portuguese-pt_br-language-file-released-for-simplepress-forum-wordpress-plugin/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.heavyworks.net/blog/posts/brazillian-portuguese-pt_br-language-file-released-for-simplepress-forum-wordpress-plugin</feedburner:origLink></item>
		<item>
		<title>Typing less with SSH</title>
		<link>http://feedproxy.google.com/~r/Heavyworks/~3/-RDufOPbF0U/typing-less-with-ssh</link>
		<comments>http://www.heavyworks.net/blog/posts/typing-less-with-ssh#comments</comments>
		<pubDate>Thu, 04 Mar 2010 21:17:41 +0000</pubDate>
		<dc:creator>Jan Seidl</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[aliases]]></category>
		<category><![CDATA[scp]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://www.heavyworks.net/?p=419</guid>
		<description><![CDATA[For me as a user I always had to type long lines to start my sessions. It&#8217;s no rocket science, you know, but for he who types thousands of characters each day, each character save is golden. We usally start our SSH sessions by typing the following command string: ssh user@domain.tld I&#8217;ve tried to shorten [...]


No related posts.]]></description>
			<content:encoded><![CDATA[
<p><img src="http://www.heavyworks.net/wordpress/wp-content/uploads/221006_5456.jpg" alt="" title="" width="608" height="252" class="alignnone size-full wp-image-427" /></p>
<p>For me as a Linux<a title="Linux @ Wikipedia" href="http://en.wikipedia.org/wiki/Linux" class="wikipedia" rel="external wikipedia"><sup>w</sup></a> user I always had to type long lines to start my <acronym title="Secure Shell">SSH</acronym><a title="SSH @ Wikipedia" href="http://en.wikipedia.org/wiki/Ssh" class="wikipedia" rel="external wikipedia"><sup>w</sup></a> sessions. It&#8217;s no rocket science, you know, but for he who types thousands of characters each day, each character save is golden.</p>
<p>We usally start our <acronym title="Secure Shell">SSH</acronym> sessions by typing the following command string:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ssh</span> user<span style="color: #000000; font-weight: bold;">@</span>domain.tld</pre></div></div>

<p>I&#8217;ve tried to shorten this by adding the domain and its <acronym title="Top Level Domain">TLD</acronym><a title="TLD @ Wikipedia" href="http://en.wikipedia.org/wiki/Tld" class="wikipedia" rel="external wikipedia"><sup>w</sup></a> to my <code>hosts</code> file with a shorter alias like</p>
<pre>
hname hostname.tld
</pre>
<p>But obviously that crashed against Apache<a title="Apache @ Wikipedia" href="http://en.wikipedia.org/wiki/Apache" class="wikipedia" rel="external wikipedia"><sup>w</sup></a> Vhost<a title="Vhost @ Wikipedia" href="http://en.wikipedia.org/wiki/Vhost" class="wikipedia" rel="external wikipedia"><sup>w</sup></a> capability, the request never make it to the other end.</p>
<p>I personally have <acronym title="Secure Shell">SSH</acronym> keys on some of my servers to avoid typing passwords, that is good to prevent against MITM<a title="MITM @ Wikipedia" href="http://en.wikipedia.org/wiki/Mitm" class="wikipedia" rel="external wikipedia"><sup>w</sup></a> attacks and keyloggers, but I still have to type the string mentioned lines above.</p>
<p>Then, with 3 lines in a <acronym title="Secure Shell">SSH</acronym>&#8217;s configuration file on my home directory (yes, you don&#8217;t need to alter the server&#8217;s <acronym title="Secure Shell">SSH</acronym> configuration file and yes, you can do that on your shared hosting&#8217;s shell account).<br />
<span id="more-419"></span><br />
It&#8217;s very simple. <acronym title="Secure Shell">SSH</acronym> allows some user-based configuration in your <code>~/.ssh</code> directory (note that <code>~</code> stands for your home directory (usually <code>/home/yourusername</code>) and it also provides Aliases<a title="Aliases @ Wikipedia" href="http://en.wikipedia.org/wiki/Aliases" class="wikipedia" rel="external wikipedia"><sup>w</sup></a> capability.</p>
<p>The aliases can be configured in such a way that you won&#8217;t need to type neither the FQDN<a title="FQDN @ Wikipedia" href="http://en.wikipedia.org/wiki/Fqdn" class="wikipedia" rel="external wikipedia"><sup>w</sup></a> nor even type a single username EVER MORE! </p>
<p>Your old</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ssh</span> user<span style="color: #000000; font-weight: bold;">@</span>domain.tld</pre></div></div>

<p>will be shortened to</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ssh</span> aliasname</pre></div></div>

<h2>Ok, talk is cheap, show me the code!</h2>
<h3>Creating the need files and directories</h2>
<p>All the magic occurs on the <code>config</code> in your already-mentioned <code>~/.ssh/</code> directory. If you don&#8217;t have one, create it and secure it, as the following:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">chmod</span> <span style="color: #000000;">700</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>If the file does not exists, create it and secure it propperly, like:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">touch</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>config
<span style="color: #c20cb9; font-weight: bold;">chmod</span> <span style="color: #000000;">600</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>config</pre></div></div>

<h3>Syntax model</h3>
<pre>
Host [alias name]
HostName [host's fqdn]
User [username to log-in as]
</pre>
<p><em>Without the braces, please!</em></p>
<p>For a user called <code>jdoe</code> on <code>mydomain.com</code> being shorted <code>md</code>, the code would be the following:</p>
<pre>
Host md
HostName mydomain.com
User jdoe
</pre>
<p>Save the file and you can start typing less with SSH:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ssh</span> md</pre></div></div>

<p>And that&#8217;s IT!</p>
<h2>Wow that&#8217;s great! What else this beauty can do?</h2>
<h3>Omnipresence</h3>
<p>Do you have the same username in a large number of machines, like in a local network? So you have to love the <code>*</code> wildcard.</p>
<pre>
Host *
User jdoe
</pre>
<h3>I have many names</h3>
<p>Want to follow a naming pattern but also wants to have shorter ones? Give multiple aliases!</p>
<pre>
Host gdf gandalf
HostName gandalf.mydomain.com
User jdoe
</pre>
<h3>Avoid the front door</h3>
<p>Security through obscurity is always fun! Have your <acronym title="Secure Shell">SSH</acronym> aliases even in hosts with <code>sshd</code> running in a different port.</p>
<pre>
Host md
HostName mydomain.com
User jdoe
Port 11
</pre>
<h3>Be backwards compatible</h3>
<p>Have outdated <acronym title="Secure Shell">SSH</acronym> servers? I hope not! But if you do, you may also define a different protocol in a host-based manner.</p>
<pre>
Host md
HostName mydomain.com
User jdoe
Protocol 1
</pre>
<h3>Handle multiple keys</h3>
<p>You may specify the specific (what?) <acronym title="Secure Shell">SSH</acronym> key file for a host.</p>
<pre>
Host md
HostName mydomain.com
User jdoe
IdentityFile ~/mykey.pub
</pre>
<h2>And it also works on SCP!</h2>
<p>Oh yeah, file transfers are easier now!</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">scp</span> somefile.txt md:<span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>upload</pre></div></div>

<p>More parameters can be found on <acronym title="Secure Shell">SSH</acronym> manual, page 5.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">man</span> <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #000000;">5</span></pre></div></div>

<p>Phew! That&#8217;s all folks! Hope it helps someone as much as helped me! Anyone have any neat <acronym title="Secure Shell">SSH</acronym> trick?</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.heavyworks.net/blog/posts/typing-less-with-ssh/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.heavyworks.net/blog/posts/typing-less-with-ssh</feedburner:origLink></item>
		<item>
		<title>Smartly resolving your WordPress pages and posts JavaScript and CSS dependences</title>
		<link>http://feedproxy.google.com/~r/Heavyworks/~3/xRbxYPp2IIc/smartly-resolving-your-wordpress-pages-and-posts-javascript-and-css-dependences</link>
		<comments>http://www.heavyworks.net/blog/posts/smartly-resolving-your-wordpress-pages-and-posts-javascript-and-css-dependences#comments</comments>
		<pubDate>Tue, 23 Feb 2010 06:58:08 +0000</pubDate>
		<dc:creator>Jan Seidl</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[custom fields]]></category>
		<category><![CDATA[get_post_meta]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wp_enqueue_script]]></category>
		<category><![CDATA[wp_enqueue_style]]></category>

		<guid isPermaLink="false">http://www.heavyworks.net/?p=401</guid>
		<description><![CDATA[Sometimes you want to include page-specific or files and ended up in cluttered ifs and else ifs or even creating a creepy new page just for that. &#8216;s excellent plugin infrastructure with their hooks and actions provides us a beautiful and clean form of including our JavaScript in specific pages. In this tutorial, we will [...]


Related posts:<ol><li><a href='http://www.heavyworks.net/blog/posts/5-best-practices-on-developing-wordpress-plugins' rel='bookmark' title='Permanent Link: 5 best practices on developing WordPress plugins'>5 best practices on developing WordPress plugins</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/alternative-syntax-for-php-control-structures' rel='bookmark' title='Permanent Link: Alternative syntax for PHP control structures'>Alternative syntax for PHP control structures</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[
<p>Sometimes you want to include page-specific JavaScript<a title="JavaScript @ Wikipedia" href="http://en.wikipedia.org/wiki/Javascript" class="wikipedia" rel="external wikipedia"><sup>w</sup></a> or <acronym title="Cascading Style Sheets">CSS</acronym><a title="CSS @ Wikipedia" href="http://en.wikipedia.org/wiki/Css" class="wikipedia" rel="external wikipedia"><sup>w</sup></a> files and ended up in cluttered <code>if</code>s and <code>else if</code>s or even creating a creepy new page just for that.</p>
<p>WordPress<a title="WordPress @ Wikipedia" href="http://en.wikipedia.org/wiki/Wordpress" class="wikipedia" rel="external wikipedia"><sup>w</sup></a>&#8216;s excellent plugin infrastructure with their hooks and actions provides us a beautiful and clean form of including our JavaScript in specific pages.</p>
<p>In this tutorial, we will be using three WordPress functions: <code>get_post_meta()</code> and <code>wp_enqueue_script()</code> and <code>wp_enqueue_style()</code>.<br />
<span id="more-401"></span></p>
<h2>Custom Fields</h2>
<p>Well, you may have noticed that <code>get_post_meta()</code> is about the custom fields and you&#8217;re just right. </p>
<p>We&#8217;ll be using posts and pages custom field to add scripts and stylesheet files. I&#8217;m using the keys &#8216;js&#8217; and &#8216;css&#8217; to JavaScript and <acronym title="Cascading Style Sheets">CSS</acronym> files respectively.</p>
<p>If you don&#8217;t know how to use custom fields, please visit the <a href="http://codex.wordpress.org/Custom_Fields">WordPress documentation on Custom Fields</a>.</p>
<p>The documentation on these functions are available at WordPress documentation site:</p>
<dl>
<dt><code>get_post_meta</code></dt>
<dd><a href="http://codex.wordpress.org/Function_Reference/get_post_meta" title="get_post_meta documentation">http://codex.wordpress.org/Function_Reference/get_post_meta</a></dd>
<dt><code>wp_enqueue_style</code></dt>
<dd><a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_style" title="wp_enqueue_style documentation">http://codex.wordpress.org/Function_Reference/wp_enqueue_style</a></dd>
<dt><code>wp_enqueue_script</code></dt>
<dd><a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script" title="wp_enqueue_script documentation">http://codex.wordpress.org/Function_Reference/wp_enqueue_script</a></dd>
</dl>
<h2><code>get_post_meta</code></h2>
<p>This function is responsible by getting our custom field entries and returning them as an array.<br />
If one or more  entries are found, the array is returned with normal indexes (0..1&#8230;) and with the custom field values. If no entries are found, an empty array is returned.</p>
<p>Well, our return type will be always array so <code>foreach</code> will fit perfectly even on an empty set.</p>
<h2><code>header.php</code> file</h2>
<p>All code described in the following examples must be placed at least one line before the <code>wp_head()</code> function in your <code>header.php</code> file. if your <code>header.php</code> file is missing a <code>wp_head()</code> call, it shouldn&#8217;t. <code>wp_head()</code> function does lots of important things and many plugins use it. Please consider adding it to your code.</p>
<h2><code>wp_enqueue_script</code> and <code>wp_enqueue_style</code></h2>
<p>Some plugin developers may be not aware of these beautiful functions or even how scripts and stylesheets are managed by WordPress.</p>
<p>All scripts and style, when properly set, are enqueued and inserted automatically when <code>wp_head()</code> is called. Enqueuing your scripts and style are a better way other than just hooking <code>wp_head()</code> and printing to the output buffer.</p>
<p>This assures that our scripts and styles will be included only at the <code>wp_head()</code> call and will not be printed out from nowhere messing out peoples templates and plugins.</p>
<p><em>NOTE: Due the printing menace of <code>script</code> tags and JavaScript code chunks, <a href="http://www.heavyworks.net/blog/tag/wp-minify"><acronym title="WordPress">WP</acronym>-Minify</a> parses the ENTIRE site output in order to properly minify them.</em></p>
<p>This technique also prevents overlapping includes of twicely defined entries.</p>
<p><img src="http://www.heavyworks.net/wordpress/wp-content/uploads/queue.gif" alt="CSS and JavaScript files happy in the queue" title="CSS and JavaScript files happy in the queue" width="600" height="417" class="aligncenter size-full wp-image-413" /><br />
WordPress gives us respectively <code>wp_enqueue_script</code> and <code>wp_enqueue_style</code> for enqueueing scripts and stylesheets. Both functions receive a script or stylesheet unique identifier as the first parameter and the path as second (more parameters are available, please refer to the documentation for more understanding on this subject).</p>
<p>As this is an automated process, I&#8217;m using as unique identifier the md5 hash of the file&#8217;s path (the custom field value).</p>
<p><em>NOTE: This solution is made for JavaScript and <acronym title="Cascading Style Sheets">CSS</acronym> files that follows a unique parent directory. Please refer to the examples for more explanation.</em></p>
<p>So let&#8217;s get back to our <code>header.php</code> file just before the <code>wp_head()</code> call that we mentioned before. We also remember that the custom field key for JavaScript files that we&#8217;ll be using in this example is &#8216;js&#8217;.</p>
<h3>JavaScript files</h3>
<p>The following code will use <code>get_post_meta</code> to get the array (empty or not) of JavaScript files that have been set through custom fields under the <code>js</code> folder under our template directory.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$js</span> <span style="color: #339933;">=</span> get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'js'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$jscript</span><span style="color: #009900;">&#41;</span> wp_enqueue_script<span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$jscript</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>get_bloginfo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'template_directory'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/js/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$jscript</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3><acronym title="Cascading Style Sheets">CSS</acronym> files</h3>
<p>The following code will use <code>get_post_meta</code> to get the array (empty or not) of <acronym title="Cascading Style Sheets">CSS</acronym> files that have been set through custom fields under the <code>css</code> folder under our template directory.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$css</span> <span style="color: #339933;">=</span> get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'css'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$style</span><span style="color: #009900;">&#41;</span> wp_enqueue_style<span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$style</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>get_bloginfo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'template_directory'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/css/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$style</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Simple huh? One line each and your problems are solved.</p>
<p>You may now call <code>wp_head()</code> and go drink your coffee!</p>


<p>Related posts:<ol><li><a href='http://www.heavyworks.net/blog/posts/5-best-practices-on-developing-wordpress-plugins' rel='bookmark' title='Permanent Link: 5 best practices on developing WordPress plugins'>5 best practices on developing WordPress plugins</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/alternative-syntax-for-php-control-structures' rel='bookmark' title='Permanent Link: Alternative syntax for PHP control structures'>Alternative syntax for PHP control structures</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.heavyworks.net/blog/posts/smartly-resolving-your-wordpress-pages-and-posts-javascript-and-css-dependences/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.heavyworks.net/blog/posts/smartly-resolving-your-wordpress-pages-and-posts-javascript-and-css-dependences</feedburner:origLink></item>
		<item>
		<title>5 best practices on developing WordPress plugins</title>
		<link>http://feedproxy.google.com/~r/Heavyworks/~3/o46k-HAjz0o/5-best-practices-on-developing-wordpress-plugins</link>
		<comments>http://www.heavyworks.net/blog/posts/5-best-practices-on-developing-wordpress-plugins#comments</comments>
		<pubDate>Sun, 21 Feb 2010 19:34:20 +0000</pubDate>
		<dc:creator>Jan Seidl</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[cleanup]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[internationalization]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.heavyworks.net/?p=383</guid>
		<description><![CDATA[WordPress wonders me how it born as a Blogging platform and is growing a powerful CMS platform. Its plugin framework is full of hooks and actions and almost everything can be trapped and modified by plugins. As WordPress is a oriented platform the function wrapped to the hooks and actions have no scope when called [...]


Related posts:<ol><li><a href='http://www.heavyworks.net/blog/posts/brazillian-portuguese-pt_br-language-file-for-wp-filebase-released' rel='bookmark' title='Permanent Link: Brazillian Portuguese (pt_BR) language file for WP-Filebase released'>Brazillian Portuguese (pt_BR) language file for WP-Filebase released</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/sustainability-application-under-software-development' rel='bookmark' title='Permanent Link: Sustainability application under Software Development'>Sustainability application under Software Development</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[
<p><img src="http://www.heavyworks.net/wordpress/wp-content/uploads/plugin.jpg" alt="WordPress Plugins" title="WordPress Plugins" width="484" height="296" class="aligncenter size-full wp-image-395" /><br />
WordPress wonders me how it born as a Blogging platform and is growing a powerful <acronym title="Content Management System">CMS</acronym> platform. Its plugin framework is full of hooks and actions and almost everything can be trapped and modified by plugins.</p>
<p>As WordPress is a Procedural Programming<a title="Procedural Programming @ Wikipedia" href="http://en.wikipedia.org/wiki/Procedural_programming" class="wikipedia" rel="external wikipedia"><sup>w</sup></a> oriented platform the function wrapped to the hooks and actions have no scope when called except global this can be hell when developing plugins. We must take extra-care to not compromise our plugin security and compatibility.</p>
<p>In our road developing plugins we learned some lessons, here is our share.<span id="more-383"></span></p>
<h2>1. Enable your WP_DEBUG</h2>
<p>This caused me some nightmares the first time I turned it on. Lots of plugin developers doesn&#8217;t have the habit of working with <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s <code>error_reporting</code> at the <code>E_ALL</code> level (Maximum Level, including notices of undefined variables) and I god SEVERAL notices on screen.</p>
<p><code>WP_DEBUG</code> can be enabled by simply adding the following line to your <code>wp-config.php</code> file.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'WP_DEBUG'</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><acronym title="PHP: Hypertext Preprocessor">PHP</acronym> is a very friendly (if we can call this way) programming language that doesn&#8217;t cares very much to variable initialization but you should initialize all your variables and check if they are set with the language&#8217;s native <code>isset()</code> function.</p>
<p>If you have <code>WP_DEBUG</code> you will se NO errors at all so if your script breaks somewhere at the development, you&#8217;ll suffer lots of pain trying to blindly debug your code. Lots of experience had me finding unclosed brackets, parentheses and semicolons but, come on, I&#8217;m no masochist.</p>
<p>The best tip here is to have a development WordPress installation. If this is not possible (or you are just too lazy), as lots of plugin developers (very popular ones) aren&#8217;t caring about notices showing up to your visitors if you have WP_DEBUG enabled you can always use the good and old <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> code to enable debug only if you are the visitor.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># 200.200.200.200 is an example IP and should be replaced by your own
</span><span style="color: #666666; font-style: italic;"># if you don't know your IP you can visit www.whatismyip.com
</span><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'200.200.200.200'</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'WP_DEBUG'</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>2. Have a cleanup deactivation routine</h2>
<p>Had you ever took a look at your <code>wp_options</code> table after a long time activating and deactivating plugins? If you so you probably had seen the garbage left out there.</p>
<p>If you are going to write an option with <cod>add_option</code> or <code>update_option</code> please, please, please, be sure to remove your options upon plugin deactivation. I'm pretty sure no one wants your options leftovers as a souvenir.</p>
<p>To achieve such thing, you must register a deactivation hook by the <code>register_deactivation_hook</code> as following</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">register_deactivation_hook<span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'plugin_deactivate'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And obviously you will have to code an appropriate deactivation function, like <code>plugin_deactivate</code> in the example.</p>
<p><em>NOTE: Your deactivation function must be at the script's main <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> file.</em><br />
<em>NOTE: Your deactivation function may have any name.</em><br />
<em>NOTE: The <code>__FILE__</code> part specified at the deactivation hook is referencing your own script file.</em></p>
<p>I personally put all my options that goes to the <code>wp_options</code> database in a global associative array so if I add more options my cleanup function will still be compliant.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$aOptions</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$aOptions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'myplugin-default-lang'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'en_US'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I also put my deactivation and cleanup functions apart so I can call the cleanup from within the deactivation function. This is made so if I want to add more deactivation functionalities else than cleanup I can do this without polluting my cleanup function with non-cleanup procedures.</p>
<p>The <code>plugin_deactivate</code> function will look like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> plugin_deactivate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Cleanup</span>
    plugin_cleanup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: function :: plugin_deactivate</span></pre></div></div>

<p>And the cleanup function as it follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> plugin_cleanup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$aOptions</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Delete our options</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$aOptions</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$sOptionKey</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$sOptionValue</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
        delete_option<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sOptionKey</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: function :: plugin_cleanup</span></pre></div></div>

<p>This should be enough to keep your options table tidy.</p>
<h2>3. Protect files that shouldn't be accessed directly</h2>
<p>Almost all your plugin files shouldn't be access directly. Exception goes to scripts that receives data from outside the WordPress framework like thumbnail generators, image replacement techniques, <acronym title="Asynchronous JavaScript and XML">AJAX</acronym>, and such.</p>
<p>To disable direct access to your <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> files you must check if some WordPress function is present and if not, shutdown the script if a good and old <code>exit();</code> call.</p>
<p>The example goes as:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">function_exists</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'add_action'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> 
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Status: 403 Forbidden'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HTTP/1.1 403 Forbidden'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span></pre></div></div>

<p>The example above also sends a 403 header in both formats (Internet Explorer and decent browsers, respectively).</p>
<h2>4. Use internationalization even if you aren't going to translate yet</h2>
<p>Even if you are not planning release you plugin with many languages I strongly recommend that you build yours i18n-ready by wrapping your texts on <code>gettext<a title="gettext @ Wikipedia" href="http://en.wikipedia.org/wiki/Gettext" class="wikipedia" rel="external wikipedia"><sup>w</sup></a></code> functions so other users can help (or even you in the future) to translate your plugin.</p>
<h3>The basics</h3>
<p>Internationalization basics in <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> is very simple. There are mainly two functions: <code>__()</code> and <code>_e()</code>. The difference is that <code>__()</code> returns the internationalized string <code>_e()</code> <code>echo</code>es it.</p>
<p>When text is within a variable or returned by a function you should use the <code>__()</code> function and if you want to <code>echo</code> it, <code>_e()</code> instead.</p>
<h4>Echoing internationalized text in a template file</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;h2&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> _e<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'My text goes here'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'My text domain goes here'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/h2&gt;</pre></div></div>

<h4>Returning internationalized text in a function</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> somefunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'My text goes here'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'My text domain goes here'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;/</span>h2<span style="color: #339933;">&gt;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end :: function :: somefunction</span></pre></div></div>

<p>Internationalization is a wide subject and is covered greatly in the WordPress "<a href="http://codex.wordpress.org/Writing_a_Plugin#Internationalizing_Your_Plugin">Writing a Plugin</a>" page.</p>
<p>Internationalizing your plugin is also a good marketing strategy since if someone releases a language file for your plugin, is also making it available in a wider market-share slice.</p>
<h2>5. Employ current coding best practices</h2>
<p>There are some wide best practices that are not exclusively to the WordPress Plugin scope.</p>
<h3>Separate your code</h3>
<p>Applying this technique on WordPress Plugin development is such as having the plugin's main php file just making the calls and registering the hooks and have your functions properly laid under a <code>lib/</code> function or such. Same things goes to <code>images/</code>, <code>js/</code> (JavaScript) and <code>css/</code>.</p>
<h3>Document your code</h3>
<p>Ok, this is like telling grown-ups to not touch the hot oven or putting the finger in a wall socket but many people still lacks in documenting code.</p>
<p>I suggest to keep your functions, classes and files documented in the <a href="http://www.phpdoc.org/">phpDocumentor</a> syntax.</p>
<p>This kind of syntax is inherited from Java and other languages apply it since is very readable and parse-able.</p>
<p>It goes like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * My loved function
 *
 * This function collects some data and process in this way to give the desired result.
 *
 * @param    string   $varname    Description of the variable
 * @param    int   $othervarname    Description of the variable
 * @return    void   This function returns nothing
 */</span></pre></div></div>

<h3>Stay DRY</h3>
<p>Try to avoid two functions that do basically the same thing. If you have two functions in separate classes that to the same thing (like string sanitization), consider having a utility class.</p>
<h2>Bonus Tip!</h2>
<p> Avoid kidnapping people's <code>wp_head()</code> and <code>wp_footer()</code> with your plugin propaganda. If you want to make such thing, do as <acronym title="HyperText Markup Language">HTML</acronym> code comment or make this branding stuff optional. </p>
<p>If you are going to make your plugin free, make it free, not AD dependant. If you think your plugin should be paid, then make it paid. Placing plugin ADs in the themplate may scare out some users. Your free plugin shall make your own propaganda by being useful and rich-featured, not by printing it out. </p>
<p>What's your tip?</p>


<p>Related posts:<ol><li><a href='http://www.heavyworks.net/blog/posts/brazillian-portuguese-pt_br-language-file-for-wp-filebase-released' rel='bookmark' title='Permanent Link: Brazillian Portuguese (pt_BR) language file for WP-Filebase released'>Brazillian Portuguese (pt_BR) language file for WP-Filebase released</a></li>
<li><a href='http://www.heavyworks.net/blog/posts/sustainability-application-under-software-development' rel='bookmark' title='Permanent Link: Sustainability application under Software Development'>Sustainability application under Software Development</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.heavyworks.net/blog/posts/5-best-practices-on-developing-wordpress-plugins/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.heavyworks.net/blog/posts/5-best-practices-on-developing-wordpress-plugins</feedburner:origLink></item>
		<item>
		<title>Brazillian Portuguese (pt_BR) language file for WP-Filebase released</title>
		<link>http://feedproxy.google.com/~r/Heavyworks/~3/vfCrV1bDiJo/brazillian-portuguese-pt_br-language-file-for-wp-filebase-released</link>
		<comments>http://www.heavyworks.net/blog/posts/brazillian-portuguese-pt_br-language-file-for-wp-filebase-released#comments</comments>
		<pubDate>Fri, 19 Feb 2010 18:42:30 +0000</pubDate>
		<dc:creator>Jan Seidl</dc:creator>
				<category><![CDATA[Translation]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[internationalization]]></category>
		<category><![CDATA[localization]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wp-filebase]]></category>

		<guid isPermaLink="false">http://www.heavyworks.net/?p=362</guid>
		<description><![CDATA[We have just baked the Brazillian Portuguese pt_BR.po (and .mo) internationalization language files for the great WP-Filebase WordPress plugin from Fabian Schlieper. This plugin is a great download / file manager that covered just all holes that we need in our clients&#8217; projects. It features a highly configurable theming, internationalization, categorization, download counter, traffic/bitrate limiter [...]


No related posts.]]></description>
			<content:encoded><![CDATA[
<p>We have just baked the Brazillian Portuguese <code>pt_BR.po</code> (and <code>.mo</code>) internationalization language files for the great <a href="http://wordpress.org/extend/plugins/wp-filebase/"><acronym title="WordPress">WP</acronym>-Filebase</a> WordPress plugin from <a href="http://fabi.me">Fabian Schlieper</a>.</p>
<p>This plugin is a great download / file manager that covered just all holes that we need in our clients&#8217; projects. It features a highly configurable theming, internationalization, categorization, download counter, traffic/bitrate limiter and more. </p>
<p>This is certainly a recommended WordPress Plugin for all.<span id="more-362"></span></p>
<p>The language file was translated from the provided <code>template.po</code>. </p>
<p>Please submit any fixes for translation errors that you may find.</p>
<p><a href="http://www.heavyworks.net/wordpress/wp-content/uploads/wpfb-pt_BR.mo">Download <code>pt_BR.mo</code> language file for <acronym title="WordPress">WP</acronym>-Filebase</a><br />
<a href="http://www.heavyworks.net/wordpress/wp-content/uploads/wpfb-pt_BR.po">Optional download <code>pt_BR.po</code> language file for <acronym title="WordPress">WP</acronym>-Filebase</a></p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.heavyworks.net/blog/posts/brazillian-portuguese-pt_br-language-file-for-wp-filebase-released/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.heavyworks.net/blog/posts/brazillian-portuguese-pt_br-language-file-for-wp-filebase-released</feedburner:origLink></item>
		<item>
		<title>Cisco binary games kills your productivity while enriching knowledge</title>
		<link>http://feedproxy.google.com/~r/Heavyworks/~3/vdKmJ4uPOdM/cisco-binary-games-kills-your-productivity-while-enriching-knowledge</link>
		<comments>http://www.heavyworks.net/blog/posts/cisco-binary-games-kills-your-productivity-while-enriching-knowledge#comments</comments>
		<pubDate>Tue, 12 Jan 2010 05:23:33 +0000</pubDate>
		<dc:creator>Jan Seidl</dc:creator>
				<category><![CDATA[Computer Science & Math]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[cisco]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[game]]></category>

		<guid isPermaLink="false">http://www.heavyworks.net/?p=305</guid>
		<description><![CDATA[This is one of those Flash games that kills hours of productivity like Desktop Tower Defense, quite educative. The game released by Cisco as part of its CCNA training has the objective of training your binary skills. You will be asked for the bits corresponding to a certain value or vice-versa. Very interesting, can get [...]


No related posts.]]></description>
			<content:encoded><![CDATA[
<p>This is one of those Flash games that kills hours of productivity like <a href="http://www.handdrawngames.com/DesktopTD/Game.asp">Desktop Tower Defense</a>, quite educative.</p>
<p><img src="http://www.heavyworks.net/wordpress/wp-content/uploads/binary_banner.jpg" alt="Cisco Binary Game" title="Cisco Binary Game" width="600" height="215" class="aligncenter size-full wp-image-308" /><br />
<span id="more-305"></span><br />
The game released by Cisco as part of its CCNA training has the objective of training your binary skills. You will be asked for the bits corresponding to a certain value or vice-versa.</p>
<p>Very interesting, can get really naughty sometimes.</p>
<p><a href="http://forums.cisco.com/CertCom/game/binary_game_page.htm" title="Cisco Binary Game">Play</a> at your own risk at Cisco&#8217;s website.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.heavyworks.net/blog/posts/cisco-binary-games-kills-your-productivity-while-enriching-knowledge/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.heavyworks.net/blog/posts/cisco-binary-games-kills-your-productivity-while-enriching-knowledge</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 6.580 seconds. --><!-- Cached page generated by WP-Super-Cache on 2010-07-29 09:08:42 -->
