<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mike Gerwitz</title>
	<atom:link href="http://mikegerwitz.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://mikegerwitz.com</link>
	<description>Free Software Hacker</description>
	<lastBuildDate>Wed, 04 Jan 2012 23:15:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Value of Vim Swap Files</title>
		<link>http://mikegerwitz.com/2011/05/22/value-of-vim-swap-files/</link>
		<comments>http://mikegerwitz.com/2011/05/22/value-of-vim-swap-files/#comments</comments>
		<pubDate>Sun, 22 May 2011 16:04:51 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://mikegerwitz.com/?p=526</guid>
		<description><![CDATA[My nightly development routine is fairly consistent. I hack some code, commit it, hack some. Before bed, I take a look at the commits and see if everything is in a stable state that actually makes sense to push. If so, I push it. Otherwise, I save it for another day. I then suspend my [...]]]></description>
			<content:encoded><![CDATA[<p>My nightly development routine is fairly consistent. I hack some code, commit it, hack some. Before bed, I take a look at the commits and see if everything is in a stable state that actually makes sense to push. If so, I push it. Otherwise, I save it for another day. I then suspend my PC and head off to bed. If I find myself in bed, having forgotten that last step, I&#8217;ll hit a button on my N900 which runs a script to put my PC to sleep with me. I&#8217;ll then lay awake for at least a half hour pondering the same problem I was working to solve before bed, mulling over the source code in my head.</p>
<p>Last night was no different. I was working on finishing up some final features before the release of version 0.1.0 of ease.js. It wasn&#8217;t a horribly productive day (code-wise), but it was still time that I had to squeeze in amongst other errands and the rest of my life. As usual, I committed my changes, found that I wasn&#8217;t in a state I wanted to push, suspended my PC and went off to bed. When I awoke in the morning, I found that I wasn&#8217;t able to do anything without filesystem errors. None of my drives were readable or writable.</p>
<p>Alright. It&#8217;s happened before. I&#8217;ll just reboot, run fsck and hope nothing important was corrupted. I wasn&#8217;t able to reboot properly (due to filesystem errors), so I just did a hard reset. To my relief, the PC booted back up just fine. I was able to reconnect to the network, restore my screen session on my server for <acronym title="Internet Relay Chat">IRC</acronym> and re-open vim to begin hacking. All seemed well.</p>
<p>Normally, when I open vim after shutting down without giving it a chance to close, I&#8217;m spammed with messages regarding its swap files. While vim is running, it writes your changes to a swap file, one per open file, just in case something happens that causes vim to shut down without being able to save your changes. Next time you open that file, vim will notice that there&#8217;s already a swap file an will prompt the user, asking what should be done. Should vim recover from the swap file, delete it, or ignore it and edit the file anyway? Normally, I delete them all and start fresh, because everything is normally committed in Git.</p>
<p>But I noticed something odd. My shell, when in a Git project dir, will add additional information to the prompt (see git-ps1). Rather than saying &#8220;master&#8221; as it should of, it said &#8220;HEAD&#8221;. Sensing a problem, I didn&#8217;t delete the swap files. Instead, I typed &#8216;git status&#8217; and was rather discomforted to find that every file was listed as modified and no previous commits were recognized.</p>
<p>Okay. That&#8217;s happened before too. Somehow, the HEAD was lost, likely due to corruption. Since most commands fail with an invalid HEAD, I just cloned my repository from GitHub and put the SHA-1 hash of its head into .git/HEAD. Worked like a charm. Unfortunately, <tt>`git reflog`</tt> was not displaying anything useful, so I couldn&#8217;t just check out one of my more recent commits. I then ran <tt>`git fsck --lost-found`</tt> to hopefully recover the commits I had made since I last pushed. It complained with a fatal error, stating that a commit was corrupt.</p>
<p>Suffice it to say that many commits were corrupt. After a while of trying every trick in the book for recovering lost commits (which I&#8217;m not going to go over here), I decided that it was a lost cause. Not having run <tt>`git gc`</tt> since the start of the project, there were files everywhere. There was no way I was going to manually sort through these to figure out my commit, and <tt>`git fsck`</tt> was useless because, every time I recovered or removed corrupt commits, another one was a problem. It was clear that this repository was a lost cause.</p>
<p>No developer likes to lose hours of work. As I said, I have to squeeze the time in when I can. Lost time is truly lost. I only have so much time in my day and, if I couldn&#8217;t recover that work, I may not be able to make the v0.1.0 release that I had hope to this weekend. Or at least get as close as possible to it. So, naturally, that was very disappointing.</p>
<p>Recovering the exact commits would have been ideal. The timestamps, original commit messages, etc are nice to have. With that lost, I at least wanted to try to recover the code. If I could do that, I could split it into a number of commits and hopefully get back to where I was before. So, I looked back to vim. Because I was unable to shut down the PC properly, none of the swap files were cleaned up. Therefore, I moved my old, corrupt repository to a temporary location, moved the new clone into the same dir as the original, and then chose the &#8220;recover&#8221; option for each of the files in my session and wrote them all to disk. That recovered most of my old files, which was very exciting.</p>
<p>Before I had shut down, I attempted to use <tt>:mksess</tt> to create a session to restore all my open files after reboot. However, since the disk was not writable, I was unable to do so. Therefore, I was stuck with an old session, which did not reload many of the files I had open. Some of those files didn&#8217;t even exist in the newly cloned repository. Fortunately, simply opening those non-existent files in vim would cause the swap file to be recognized. Vim would prompt, I would recover from the swap file, write it to disk and have my old file back.</p>
<p>Though I lost the commits, I now had all of my lost code back where it belongs. After recommitting everything, I didn&#8217;t have as many commits as I had previously, nor did I have the original messages, timestamps, etc, but at least I didn&#8217;t lose any actual work. Just a little bit of time, for which I was able to share an interesting experience. That has not been the only time that vim&#8217;s swap files have saved me from strangling myself. While it&#8217;s not good to rely on them for a backup solution, they are able to help in situations where you&#8217;re left with no other choice.</p>
<p>Had the entire disk been corrupted, I still would have been okay. My repository is on a separate physical disk than my home directory. Using the <tt>directory</tt> option in vim, you can set a different location for your swap files (it&#8217;s in the same directory as the file, by default). I set mine to ~/.vim-tmp by default. Had they been in the same directory, it&#8217;s likely that they may have been accidently lost while I was screwing around with Git trying to recover everything. If the entire disk did fail, I would have still had my work in the swap files, so I would have lost only the commits, not the code.</p>
<p>Moral of the story &#8211; store your swap files wisely. They&#8217;re an annoying pest when you don&#8217;t need them, but highly valuable in the off chance that they are needed.</p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fmikegerwitz.com%2F2011%2F05%2F22%2Fvalue-of-vim-swap-files%2F&amp;linkname=Value%20of%20Vim%20Swap%20Files">Share/Bookmark</a>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/2011/05/22/value-of-vim-swap-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IE toString() Not Enumerable: Workaround</title>
		<link>http://mikegerwitz.com/2011/03/06/ie-tostring-not-enumerable-workaround/</link>
		<comments>http://mikegerwitz.com/2011/03/06/ie-tostring-not-enumerable-workaround/#comments</comments>
		<pubDate>Sun, 06 Mar 2011 16:21:50 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://mikegerwitz.com/?p=520</guid>
		<description><![CDATA[While working on a project of mine, I noticed that overriding toString() on an object would not cause it to show up when looping through the object&#8217;s properties with for in Internet Explorer 6 (I have not tested what other versions are affected). My suspicious were verified on StackOverflow:
http://stackoverflow.com/questions/3679233/are-there-still-ecmascript-3-implementation-differences-in-major-browsers/3679293#3679293
To clarify, this is the problem:

?View Code [...]]]></description>
			<content:encoded><![CDATA[<p>While working on a project of mine, I noticed that overriding toString() on an object would not cause it to show up when looping through the object&#8217;s properties with <tt>for</tt> in Internet Explorer 6 (I have not tested what other versions are affected). My suspicious were verified on StackOverflow:</p>
<p><a href="http://stackoverflow.com/questions/3679233/are-there-still-ecmascript-3-implementation-differences-in-major-browsers/3679293#3679293">http://stackoverflow.com/questions/3679233/are-there-still-ecmascript-3-implementation-differences-in-major-browsers/3679293#3679293</a></p>
<p>To clarify, this is the problem:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p520code3'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5203"><td class="code" id="p520code3"><pre class="javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    obj.<span style="color: #660066;">toString</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> prop <span style="color: #000066; font-weight: bold;">in</span> obj <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// &quot;prop&quot; will never show 'toString' in IE</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I needed some way to work around this. The solution needed to be fairly elegant &#8211; that is, I don&#8217;t want to have to pass in an argument stating &#8220;yes, I set the toString property&#8221;. My workaround, though not pretty, was to test to see if toString was set:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p520code4'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5204"><td class="code" id="p520code4"><pre class="javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">function</span> hasToString<span style="color: #009900;">&#40;</span> obj <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span> obj.<span style="color: #660066;">toString</span> <span style="color: #339933;">===</span> Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span> <span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">?</span> <span style="color: #003366; font-weight: bold;">false</span>
            <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span>
        <span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If the <tt>toString</tt> property of the object is equal to that of its prototype, then it&#8217;s safe to say that the property was not overwritten. However, there is a problem. We&#8217;re using <tt>Object.prototype</tt> for this test. We are not checking the parent. This is because <acronym title="Internet Explorer">IE</acronym> does not have a <tt>__proto__</tt> property like other browsers such as FF or Chromium. So we cannot test against the actual prototype.</p>
<p>This means that <em>the above function will give you an incorrect result if the <tt>toString</tt> property was previously overridden</em>.</p>
<p>Fortunately, in my use case, that will never be an issue. If it is an issue for you, then make sure you replace <tt>Object</tt> with whatever <tt>obj</tt> is an instance of.</p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fmikegerwitz.com%2F2011%2F03%2F06%2Fie-tostring-not-enumerable-workaround%2F&amp;linkname=IE%20toString%28%29%20Not%20Enumerable%3A%20Workaround">Share/Bookmark</a>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/2011/03/06/ie-tostring-not-enumerable-workaround/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GNU GPL / Copyleft: Guardian or Curse?</title>
		<link>http://mikegerwitz.com/2010/12/11/gnu-gpl-copyleft-guardian-or-curse/</link>
		<comments>http://mikegerwitz.com/2010/12/11/gnu-gpl-copyleft-guardian-or-curse/#comments</comments>
		<pubDate>Sat, 11 Dec 2010 15:07:32 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://mikegerwitz.com/?p=444</guid>
		<description><![CDATA[The GNU General Public License is a free software license (one of many) created to protect the four essential freedoms of software users. Those freedoms are:

Freedom 0. the freedom to use the software for any purpose,
Freedom 1. the freedom to change the software to suit your needs,
Freedom 2. the freedom to share the software with [...]]]></description>
			<content:encoded><![CDATA[<p>The <a id="gpl" href="http://www.gnu.org/licenses/gpl.html"><acronym title="GNU's Not Unix!">GNU</acronym> General Public License</a> is a <a href="http://www.gnu.org/philosophy/free-sw.html">free software</a> license (<a href="http://www.gnu.org/licenses/license-list.html">one of many</a>) created to protect the <a href="http://www.gnu.org/licenses/quick-guide-gplv3.html#the-foundations-of-the-gpl">four essential freedoms</a> of software users. Those freedoms are:</p>
<ul id="four-freedoms">
<li><strong>Freedom 0.</strong> the freedom to use the software for any purpose,</li>
<li><strong>Freedom 1.</strong> the freedom to change the software to suit your needs,</li>
<li><strong>Freedom 2.</strong> the freedom to share the software with your friends and neighbors, and</li>
<li><strong>Freedom 3.</strong> the freedom to share the changes you make.</li>
</ul>
<p>The license is the child of <a href="http://stallman.org/">Richard M. Stallman</a> &#8211; the founder of the <a href="http://en.wikipedia.org/wiki/Free_software_movement">free software movement</a> &#8211; and the <a href="http://fsf.org">Free Software Foundation</a> &#8211; the not-for-profit organization that was born from his vision. There are multiple incarnations of the license, each with their own distinct purposes:</p>
<ul id="gpl-licenses">
<li><strong><acronym title="GNU's Not Unix!">GNU</acronym> General Public License (<acronym title="GNU General Public License">GPL</acronym>)</strong> &#8211; Designed to protect and enforce the aforementioned freedoms. All derivative works must also be licensed under the <acronym title="GNU General Public License">GPL</acronym> or a <a href="http://www.gnu.org/licenses/license-list.html#GPLCompatibleLicenses"><acronym title="GNU General Public License">GPL</acronym>-compatible</a> license.</li>
<li><strong><acronym title="GNU's Not Unix!">GNU</acronym> Lesser General Public License (<a href="http://www.gnu.org/licenses/lgpl.html"><acronym title="GNU Lesser General Public License">LGPL</acronym></a>)</strong> &#8211; Designed to protect the four freedoms, but <a href="http://www.gnu.org/licenses/why-not-lgpl.html">permits usage in proprietary applications to a limited degree</a>. Derivative works must still be licensed under a <acronym title="GNU General Public License">GPL</acronym>-compatible license.</li>
<li><strong><acronym title="GNU's Not Unix!">GNU</acronym> Affero General Public License (<a href="http://www.gnu.org/licenses/agpl.html">AGPL</a>)</strong> &#8211; The most noble and powerful of the <acronym title="GNU General Public License">GPL</acronym> licenses. It is an extension of the <acronym title="GNU's Not Unix!">GNU</acronym> <acronym title="GNU General Public License">GPL</acronym>, with an added clause requiring that <a href="http://www.gnu.org/licenses/why-affero-gpl.html">users accessing software through a server must have access to the software&#8217;s source code</a> (e.g. a website/web service). All derivative works must be licensed under the AGPL or a compatible license.</li>
</ul>
<p>In addition to the above licenses, the <a href="http://www.gnu.org/licenses/fdl.html"><acronym title="GNU's Not Unix!">GNU</acronym> Free Documentation License (<acronym title="GNU Free Documentation License">FDL</acronym>)</a> is also available for documentation and other written works.</p>
<p>The licenses attempt to defend a very noble cause &#8211; <a href="http://en.wikipedia.org/wiki/Free_software_movement">the free software movement</a>. You will notice that all of the above licenses have something in common &#8211; they permit derivative works <em>only if they are licensed under a <a href="http://www.gnu.org/licenses/license-list.html#GPLCompatibleLicenses">compatible license</a></em>. This ensure that <em>the software will forever remain <a href="http://www.gnu.org/philosophy/free-sw.html">free software</a></em> and will never be a part of proprietary software, which <a href="http://www.gnu.org/philosophy/categories.html#ProprietarySoftware">is not tolerated by the free software community</a>. This is known as a <a href="http://www.gnu.org/copyleft/copyleft.html">copyleft</a>.</p>
<p>And this is where, for some, the problem begins.</p>
<h3 id="why-copyleft">Why Copyleft?</h3>
<p>As mentioned above, the <acronym title="GNU's Not Unix!">GNU</acronym> licenses are all <a href="http://www.gnu.org/copyleft/copyleft.html">copyleft</a>. Copyleft is a strong alternative to other methods of making software <a href="http://www.gnu.org/philosophy/categories.html#FreeSoftware">free</a>, such as putting it into the <a href="http://www.gnu.org/philosophy/categories.html#PublicDomainSoftware">public domain</a>, that <a href="http://www.gnu.org/philosophy/pragmatic.html">provides incentive to improve upon free software</a>. By placing software into the public domain, for example, the author allows it to be incorporated into proprietary software, or even public the <em>exact code</em> as proprietary software. There are no restrictions in place and the freedoms of the users remain unprotected.</p>
<p>Simply applying a free software license to software doesn&#8217;t solve the issue. Consider, for example, one of the most permissive software licenses &#8211; the <a href="http://www.opensource.org/licenses/mit-license.php"><acronym title="Massachusetts Institute of Technology">MIT</acronym>/Expat license</a>. It is a <a href="http://www.gnu.org/philosophy/categories.html#Non-CopyleftedFreeSoftware">non-copyleft</a> license, permitting the use of the code within proprietary software so long as the license text is distributed with the resulting software. This is a problem. Should the code be included in proprietary software, it violates all of the four freedoms:</p>
<ul>
<li>The software could only be used under the terms permitted by the license of the proprietary software vendor,</li>
<li>the software could not be altered to suit your needs, as you would not have access to the source code,</li>
<li>the vendor may prevent you from sharing the software with your friends and neighbors, and</li>
<li>it would be illegal to share your changes.</li>
</ul>
<p>It is exactly this that the free software community is against and what <a href="http://www.gnu.org/philosophy/x.html">copyleft aims to prevent</a>. In fact, by using a non-copyleft license, the author of the software is <a href="http://www.gnu.org/philosophy/why-copyleft.html">failing to protect the freedom of his/her users</a> by permitting derivative works to completely trash the user&#8217;s freedoms.</p>
<h3 id="why-not-copyleft">Why [Not] Copyleft?</h3>
<p>There are many <a href="http://blogofbile.com/2009/08/05/how-richard-stallmans-gpl-platform-backfires-on-the-free-software-movement/">opponents to the <acronym title="GNU General Public License">GPL</acronym> and copyleft</a>. They argue that copyleft imposes unnecessary restrictions on developers by preventing them from using free software as they wish, wish some consider unethical. Denying a developer a right to use code however they please is denying a freedom. Copyleft has been deemed &#8220;viral&#8221; (by all means of the word, it is &#8211; all derivative works must also be copyleft, but the term &#8220;viral&#8221; has a highly negative connotation). Terms such as the <acronym title="GNU's Not Unix!">GNU</acronym> <a href="http://catb.org/esr/jargon/oldversions/jarg221.txt">&#8220;General Public Virus&#8221;</a> emerged shortly after its release.</p>
<p>It is noble to release your code into the wild with no restrictions, saying &#8220;I made this for the world and the world may use it as it pleases.&#8221; Your mind is likely in the right place. However, as mentioned above, <a href="http://www.gnu.org/philosophy/why-copyleft.html">you are failing to protect the freedoms of your users</a>. Consider, for example, the development of the nuclear fission. Let&#8217;s say that you were the one to discover the splitting an atom releases vast amounts of energy. You think to yourself that this has incredible implications for humanity. A viable, clean energy source &#8211; nuclear power. You release this information to the world with excellent intentions &#8211; to better humanity. Your intention was totally selfless. You&#8217;re asking nothing in return for this incredible discovery.</p>
<p>Months later, you hear that a city was completely annihilated. They used a bomb that exploited your discovery, calling it the &#8220;atomic bomb&#8221;. Your wonderful discovery has just been used in a way unimaginable, taking advantage of all your hard work to do harm to others.</p>
<p>Proprietary software is by no means comparable to the death of millions of individuals (in my opinion). It is, however, comparable to taking your beautiful, hard work that you released for the better of humanity, and using it to control users. This is what you are potentially dooming users of your software to by using a non-copyleft license.</p>
<div id="freedom-user-not-developer">Essentially, <a href="http://www.gnu.org/philosophy/misinterpreting-copyright.html">copyright is uses to protect the interests of corporations</a>, which <a href="http://www.gnu.org/philosophy/copyright-versus-community.html">limits user freedom</a>, where copyleft is designed to <em>ensure</em> freedom of the user. It is a trade off. By distributing your software as free software under a copyleft license such as the <acronym title="GNU General Public License">GPL</acronym>, you are surrendering your freedom as a developer to control your users. To play god in the world of software. The choice of whether or not to use copyleft is fairly simple:</p>
<ul>
<li>If you wish to protect the freedom of <em>developers</em> and allow your software to be used in any manner, choose <a href="http://www.gnu.org/philosophy/categories.html#Non-CopyleftedFreeSoftware">non-copyleft</a>.</li>
<li>If you wish to protect the freedom of your users at all costs, choose <a href="http://www.gnu.org/copyleft/copyleft.html">copyleft</a>.</li>
</ul>
</div>
<p>Some also argue that <a href="http://www.osnews.com/story/21192/ESR_GPL_No_Longer_Needed">copyleft is no longer needed</a>, because taking free software and using it in proprietary software has become a greatly unethical practice. That is true within many communities. But that will not stop others from <a href="http://www.softwarefreedom.org/resources/">taking your software and using it unjustly</a> or <a href="http://www.gnu.org/licenses/rms-why-gplv3.html">attempting to circumvent licensing terms to use your software in a manner that is non-free</a>.</p>
<h3>Copyleft Resistance</h3>
<p>This article was prompted by a couple of factors within a single day. The main reason was that I noticed strong resistance to the <acronym title="GNU General Public License">GPL</acronym> within a piece of software I was looking at to determine if I needed to develop a <a href="http://www.gnu.org/philosophy/categories.html#FreeSoftware">free</a> alternative for myself and others. I noticed the following in the licensing terms:</p>
<blockquote><p>
<tt><br />
##  LICENSE:<br />
##  <strong>1. You may not make this <acronym title="GNU General Public License">GPL</acronym> ever. I want my code to be freely</strong><br />
##&nbsp;&nbsp;&nbsp;&nbsp;<strong>available to everyone forever. The <acronym title="GNU General Public License">GPL</acronym> restricts freedom.</strong><br />
##  2. Copyright notices must remain intact and distributed with<br />
##&nbsp;&nbsp;&nbsp;&nbsp;the program.  This includes the contributors list.<br />
##  3. No warantee is present, whether express or implied.  Use at<br />
##&nbsp;&nbsp;&nbsp;&nbsp;your own risk.<br />
##  4. This license must be included with all distributions of this<br />
##&nbsp;&nbsp;&nbsp;&nbsp;program and no modifications to this license are allowed.<br />
##&nbsp;&nbsp;&nbsp;&nbsp;<strong>This implicitly makes this program <acronym title="GNU General Public License">GPL</acronym> incompatible but</strong><br />
##&nbsp;&nbsp;&nbsp;&nbsp;<strong>compatible with virtually every other OSI approved license.</strong><br />
##  5. You are otherwise free to distribute this program, modify it<br />
##&nbsp;&nbsp;&nbsp;&nbsp;and distribute those modified works.<br />
</tt>
</p></blockquote>
<p>I took the time to post a response on this person&#8217;s blog (the text below contains only minor grammatical fixes from the original and hyperlinks):</p>
<blockquote><p>
I noticed the following comment in your license:</p>
<p><tt>"The <acronym title="GNU General Public License">GPL</acronym> restricts freedom."</tt></p>
<p>This seems like a contradictory statement and brings into question your<br />
definition of &#8220;freedom&#8221;. Had you used the <a href="#gpl"><acronym title="GNU General Public License">GPL</acronym></a> in order to license this work, you<br />
would have ensured that the source code will be freely available indefinitely. You would<br />
have in fact been protecting the <a href="#four-freedoms">freedom of your users</a>.</p>
<p>You impose only restrictions on your license. You impose no restrictions<br />
whatsoever on the source code itself. This means that someone would be able take<br />
your script and use it in their own proprietary project, obfuscating or<br />
otherwise compiling the code, and still follow the license terms so long as they<br />
distribute your license and do not modify it. In doing so, <a href="#why-copyleft">the freedoms of your<br />
users have been assaulted</a>. Should they receive a copy of this modified<br />
software, they are not free to study and alter the source code &#8211; it is in a<br />
state that makes doing so very difficult.</p>
<p>By your statement, I assume you are referring to the <a href="#why-not-gpl">&#8220;viral&#8221; nature of the <acronym title="GNU's Not Unix!">GNU</acronym><br />
General Public license</a> &#8211; that it <a href="#gpl-licenses">forces any derivative works to also be free,<br />
released under a compatible license</a>. Some people see this as an assault on their<br />
freedom to distribute and use the software however they please. But <a href="#freedom-user-not-developer">the <acronym title="GNU's Not Unix!">GNU</acronym> <acronym title="GNU General Public License">GPL</acronym><br />
protects the <em>user</em>, not the developer</a> &#8211; ensuring that users are not bullied and<br />
taken advantage of. With your license, are protecting the developer (permitting<br />
them to do whatever they wish with your code), but dooming the users of your<br />
software.</p>
<p>It should also be noted that your software is stand-alone. It is not a library.<br />
Had it been released under the terms of the <acronym title="GNU General Public License">GPL</acronym>, you would have protected the<br />
<a href="#four-freedoms">four essential freedoms</a>. You also would have permitted developers to use your<br />
software in proprietary software, because they could simply call your script and<br />
use its output in their proprietary software.</p>
<p>Therefore, you have gained nothing here by disallowing the <acronym title="GNU General Public License">GPL</acronym>, but instead have<br />
forfeited the four essential freedoms.
</p></blockquote>
<p>When I returned to the blog to see if the author had responded, I found that my post had been deleted. The author either did not care about the freedom of his/her users or did not understand the implications of his actions or copyleft.</p>
<p>What the author of this software had done was develop his own <a href="http://www.gnu.org/philosophy/categories.html#Non-CopyleftedFreeSoftware>non-copyleft license</a> very similar to the aforementioned <a href="http://www.opensource.org/licenses/mit-license.php">Expat license</a> or other highly permissive licenses (such as the <a href="http://www.gnu.org/philosophy/bsd.html">original/modified BSD</a>). He/she places restrictions only on the license itself &#8211; not the source code. The user was free to distribute and modify the source code in any shape or form, including proprietary.</p>
<p>Rather than simply using an <a href="http://www.gnu.org/licenses/license-list.html">existing permissive license</a>, the author decided to explicitly state that the license should not be compatible with the <acronym title="GNU General Public License">GPL</acronym>. Therefore, ironically, <em>the author was placing more restrictions on the users than virtually any other permissive license</em>.</p>
<blockquote><p><tt>"[...] I want my code to be freely available to everyone forever."</tt></p></blockquote>
<p>The author failed in doing so. By stating that the code cannot ever be licensed under the <acronym title="GNU General Public License">GPL</acronym>, he/she ensures that the code can never be incorporated into any projects using the <a href="http://johnhaller.com/jh/useful_stuff/open_source_license_popularity/">most popular free software license in the world</a>. There are many other licenses, but the author has placed favor on proprietary software by disallowing so many free software projects the right to use his/her code.</p>
<blockquote><p><tt>"[...] The <acronym title="GNU General Public License">GPL</acronym> restricts freedom."</tt></p></blockquote>
<p>This was the line that really got to me. It is a fascinating line, because it represents a large difference in opinion &#8211; the difference in what we consider &#8220;freedom&#8221; to be. <a href="http://www.free-soft.org/gpl_history/">The <acronym title="GNU General Public License">GPL</acronym> was created to protect users&#8217; freedoms</a>. Why, then, does the author claim that the <acronym title="GNU General Public License">GPL</acronym> restricts freedom? As the author did not respond, I can only speculate. Likely, it represents the concept mentioned above &#8211; that the <a href="#freedom-user-not-developer"><acronym title="GNU General Public License">GPL</acronym> protects freedoms of the <em>user</em>, not the developer</a>. The author decided that it was more important to him/her that his/her code be able to be used anywhere and specifically singled out the license designed to protect the freedom of users. Therefore, the author decided that it was ethical to allow derivative works to control its users.</p>
<h3>Guardian or Curse</h3>
<p>Copyleft (and consequently, the <acronym title="GNU General Public License">GPL</acronym>) is something people tend to have strong feelings for on either side &#8211; they love it, or they hate it. I&#8217;m not one to state that someone else&#8217;s opinions are incorrect. I personally strongly support copyleft and <a href="http://www.gnu.org/philosophy/">philosophy of the <acronym title="GNU's Not Unix!">GNU</acronym> Project</a>. The <acronym title="GNU General Public License">GPL</acronym> has been the force driving the free software movement for two just over two decades. It allows individuals to develop software that respects the freedom of users &#8211; ensures that they cannot be <a href="http://www.defectivebydesign.org/">controlled and bullied</a> &#8211; and ensures that their software will never fall and surrender those freedoms by enforcing those freedoms with copyright.</p>
<p>At the same time, the <acronym title="GNU General Public License">GPL</acronym> is an assault on freedom to developers. <em>And that is a good thing!</em> Large corporations use their proprietary software to control their users. For example, <a href="http://www.defectivebydesign.org/amazon-kindle-swindle">Amazon used <acronym title="Digital Restrictions Management">DRM</acronym> on the Kindle to remove copies of the book 1984 from users&#8217; devices</a> &#8211; something that would not have been possible with free software. <a href="http://www.gnu.org/philosophy/opposing-drm.html"><acronym title="Digital Restrictions Management">DRM</acronym></a> cannot exist within free software. Users would simply remove the offending code from the software and likely fork the project, leaving the original author in shame.</p>
<p>Proprietary software can also be used to <a href="http://www.defectivebydesign.org/blog/1313">spy on your personal information</a>, with no way to disable it. In fact, devices like the iPhone/iPod/iPad cannot be modified at all. They must be &#8220;jailbroken&#8221; in order to do anything that Apple does not want you to do. It is the ultimate form of control over users. And who&#8217;s controlling the users? The developers.</p>
<p><strong>The <acronym title="GNU General Public License">GPL</acronym> is a guardian to those who believe that the user should, above all else, be free to use, alter and distribute the program as they wish. The <acronym title="GNU General Public License">GPL</acronym> is a curse to those who wish to take advantage of their users by creating proprietary software in order to control and exploit them.</strong></p>
<h3>More Information</h3>
<p>For more information on free software, <acronym title="GNU's Not Unix!">GNU</acronym> <acronym title="GNU General Public License">GPL</acronym> and copyleft, please see the links throughout this article. For your convenience, many have been summarized in the list below:</p>
<ul>
<li><a href="http://fsf.org">Free Software Foundation</a></li>
<li><a href="http://gnu.org">The <acronym title="GNU's Not Unix!">GNU</acronym> Operating System</a></li>
<li><a href="http://www.gnu.org/philosophy/free-sw.html">The Free Software Definition</a></li>
<li><a href="http://www.gnu.org/copyleft/copyleft.html">Copyleft</a></li>
<li><a href="http://www.gnu.org/philosophy/why-copyleft.html">Why Copyleft?</a></li>
<li><a href="http://www.gnu.org/philosophy/pragmatic.html">Copyleft: Pragmatic Idealism</a></li>
<li><a href="http://www.gnu.org/licenses/license-list.html">List of Software Licenses</a></li>
<li><a href="http://www.gnu.org/licenses/rms-why-gplv3.html">Why Upgrade to GPLv3</a></li>
<li><a href="http://www.gnu.org/philosophy/categories.html">Categories of free and nonfree software</a></li>
<li><a href="http://www.gnu.org/philosophy/x.html">The X Window System Trap</a></li>
<li><a href="http://www.gnu.org/philosophy/misinterpreting-copyright.html">Misinterpreting Copyright &#8211; A Series of Errors</a></li>
<li><a href="http://www.gnu.org/philosophy/copyright-versus-community.html">Copyright versus Community in the Age of Computer Networks</a></li>
<li><a href="http://en.wikipedia.org/wiki/Free_software_movement">Free Software Movement (Wikipedia)</a></li>
<li><a href="http://www.softwarefreedom.org/">Software Freedom Law Center</a></li>
<li><a href="http://stallman.org/">Richard M. Stallman</a></li>
</ul>
<p>I also encourage you to take a look at the following related websites::</p>
<ul>
<li><strong><a href="http://patentabsurdity.com/">Patent Absurdity</a></strong></li>
<li><a href="http://endsoftpatents.org/">End Software Patents</a></li>
<li><a href="http://libreplanet.org/">LibrePlanet</a></li>
<li><a href="http://www.defectivebydesign.org/">Defective By Design</a></li>
<li><a href="http://www.gnu.org/philosophy/opposing-drm.html">Opposing Digital Rights Management</a></li>
<li><a href="http://en.windows7sins.org/">Windows 7 Sins</a></li>
<li><a href="https://my.fsf.org/donate/directed-donations/riaa/">RIAA Lawsuits &#8211; Expert Witnesses Fund</a></li>
<li><a href="http://www.fsf.org/campaigns/acta/">Campaign Against the ACTA</a></li>
<li><a href="http://www.fsf.org/resources/hw">Hardware Devices that Support Free <acronym title="GNU's Not Unix!">GNU</acronym>/Linux</a></li>
<li><a href="http://www.fsf.org/campaigns/priority-projects/">High Priority Free Software Projects</a></li>
<li><a href="https://my.fsf.org/associate/support_freedom/join_fsf?referrer=5804">Join the Free Software Foundation</a></li>
</ul>
<p><em><strong>Disclaimer:</strong> No attempt was made to provide an unbiased article regarding the <acronym title="GNU's Not Unix!">GNU</acronym> <acronym title="GNU General Public License">GPL</acronym> and copyleft. It can be made very apparent from the rest of this website that I am a strong supporter of free software and copyleft. This article is intended to support the <acronym title="GNU General Public License">GPL</acronym> and copyleft. I am an <a href="https://my.fsf.org/associate/support_freedom/join_fsf?referrer=5804">associate member of the Free Software Foundation</a> and a free software developer and user.</em></p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fmikegerwitz.com%2F2010%2F12%2F11%2Fgnu-gpl-copyleft-guardian-or-curse%2F&amp;linkname=GNU%20GPL%20%2F%20Copyleft%3A%20Guardian%20or%20Curse%3F">Share/Bookmark</a>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/2010/12/11/gnu-gpl-copyleft-guardian-or-curse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Node.js modules and instanceof operator</title>
		<link>http://mikegerwitz.com/2010/11/09/node-js-modules-and-instanceof-operator/</link>
		<comments>http://mikegerwitz.com/2010/11/09/node-js-modules-and-instanceof-operator/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 03:19:19 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Node.js]]></category>
		<category><![CDATA[node]]></category>

		<guid isPermaLink="false">http://mikegerwitz.com/?p=426</guid>
		<description><![CDATA[There are many moments in a developer&#8217;s life where things simply don&#8217;t make sense. You test even the most obscure solutions to try to figure out the problem, when the actual solution ends up making perfect sense (though it may be subtle). The case of modules in Node.js with use of the instanceof operator was [...]]]></description>
			<content:encoded><![CDATA[<p>There are many moments in a developer&#8217;s life where things simply don&#8217;t make sense. You test even the most obscure solutions to try to figure out the problem, when the actual solution ends up making perfect sense (though it may be subtle). The case of modules in <a href="http://nodejs.org">Node.js</a> with use of the <tt>instanceof</tt> operator was one of those instances.</p>
<p>Modules are brought into scope with the <tt>require</tt> function. You then have access to the exports of that module. In the example below, we&#8217;re interested in the <tt>Foo</tt> class of the <tt>foobar</tt> module.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p426code10'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p42610"><td class="code" id="p426code10"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Load the &quot;Foo&quot; class from the &quot;foobar&quot; module</span>
<span style="color: #003366; font-weight: bold;">var</span> Foo <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'./foobar'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">Foo</span><span style="color: #339933;">,</span>
    obj <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// we would expect the following to be true, and we are correct</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> obj <span style="color: #000066; font-weight: bold;">instanceof</span> Foo <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span></pre></td></tr></table></div>

<p>Obviously, <tt>obj</tt> is an instance of <tt>Foo</tt>. Pretty straightforward. If you use the module elsewhere in your code, you would expect that <tt>obj</tt> is still an instance of <tt>Foo</tt>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p426code11'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p42611"><td class="code" id="p426code11"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// ...</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span> obj <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span> obj <span style="color: #000066; font-weight: bold;">instanceof</span> require<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'foobar'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">Foo</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> foo<span style="color: #009900;">&#40;</span> obj <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span></pre></td></tr></table></div>

<p>Huh? That above function, <tt>foo</tt>, is returning <tt>false</tt>! That did not make sense to me. As it turns out, the problem was with how I was including the module. Early on in the project, I included the module as above &#8211; by prepending <tt>./</tt> to the path. However, a more convenient method is to simply add it to the include path:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p426code12'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p42612"><td class="code" id="p426code12"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// add $CWD to the include path</span>
require.<span style="color: #660066;">paths</span>.<span style="color: #660066;">unshift</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'.'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You will notice that, in <tt>foo()</tt>, we were using a different path for the module. They may <em>resolve</em> to the same path ultimately, but Node seems to keep track of them differently.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p426code13'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p42613"><td class="code" id="p426code13"><pre class="javascript" style="font-family:monospace;">require.<span style="color: #660066;">paths</span>.<span style="color: #660066;">unshift</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'.'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> Foo1 <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'./foobar'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">Foo</span><span style="color: #339933;">,</span>
    Foo2 <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'./foobar'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">Foo</span><span style="color: #339933;">,</span>
    Foo3 <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'foobar'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">Foo</span><span style="color: #339933;">,</span>
    obj  <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Foo1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> obj <span style="color: #000066; font-weight: bold;">instanceof</span> Foo1 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> obj <span style="color: #000066; font-weight: bold;">instanceof</span> Foo2 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> obj <span style="color: #000066; font-weight: bold;">instanceof</span> Foo3 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span></pre></td></tr></table></div>

<p>This was tested with node v0.3.1-pre. As far as I am concerned, this is a bug. <tt>Foo1</tt>, <tt>Foo2</tt> and <tt>Foo3</tt> are all in fact the same object, however Node will reimport the module when it is included via a relative path in <tt>require.paths</tt>. <em>If you use an absolute path in <tt>require.paths</tt>, this is not an issue.</em> To demonstrate this, consider the following complete example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p426code14'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p42614"><td class="code" id="p426code14"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * foobar.js
 */</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'&gt;&gt;&gt; foobar module loaded'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
exports.<span style="color: #660066;">Foo</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #006600; font-style: italic;">/**
 * test.js
 */</span>
require.<span style="color: #660066;">paths</span>.<span style="color: #660066;">unshift</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'.'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> paths <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
    <span style="color: #3366CC;">'./foobar'</span><span style="color: #339933;">,</span>
    __dirname <span style="color: #339933;">+</span> <span style="color: #3366CC;">'/foobar'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'././foobar'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'../module-oddity/foobar'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'foobar'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> funcs <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    objs  <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    i     <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// load module and instantiate objects</span>
paths.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> path <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    funcs<span style="color: #009900;">&#91;</span> i <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span> path <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">Foo</span><span style="color: #339933;">;</span>
    objs<span style="color: #009900;">&#91;</span> i <span style="color: #009900;">&#93;</span>  <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> funcs<span style="color: #009900;">&#91;</span> i <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;Foo%d = %s&quot;</span><span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> path <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    i<span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// compare objects</span>
<span style="color: #003366; font-weight: bold;">var</span> foo_i <span style="color: #339933;">=</span> obj_i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
funcs.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> foo <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    foo_i<span style="color: #339933;">++;</span>
    obj_i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    objs.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> obj <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;Foo%d instanceof obj%d: %s&quot;</span><span style="color: #339933;">,</span>
            foo_i<span style="color: #339933;">,</span>
            <span style="color: #339933;">++</span>obj_i<span style="color: #339933;">,</span>
            <span style="color: #009900;">&#40;</span> obj <span style="color: #000066; font-weight: bold;">instanceof</span> foo <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<blockquote><p>
$ node test.js<br />
>>> foobar module loaded<br />
Foo0 = ./foobar<br />
Foo1 = ./foobar<br />
Foo2 = ././foobar<br />
Foo3 = ../module-oddity/foobar<br />
>>> foobar module loaded<br />
Foo4 = foobar</p>
<p>Foo1 instanceof obj1: true<br />
Foo1 instanceof obj2: true<br />
Foo1 instanceof obj3: true<br />
Foo1 instanceof obj4: true<br />
Foo1 instanceof obj5: false</p>
<p>Foo2 instanceof obj1: true<br />
Foo2 instanceof obj2: true<br />
Foo2 instanceof obj3: true<br />
Foo2 instanceof obj4: true<br />
Foo2 instanceof obj5: false</p>
<p>Foo3 instanceof obj1: true<br />
Foo3 instanceof obj2: true<br />
Foo3 instanceof obj3: true<br />
Foo3 instanceof obj4: true<br />
Foo3 instanceof obj5: false</p>
<p>Foo4 instanceof obj1: true<br />
Foo4 instanceof obj2: true<br />
Foo4 instanceof obj3: true<br />
Foo4 instanceof obj4: true<br />
Foo4 instanceof obj5: false</p>
<p>Foo5 instanceof obj1: false<br />
Foo5 instanceof obj2: false<br />
Foo5 instanceof obj3: false<br />
Foo5 instanceof obj4: false<br />
Foo5 instanceof obj5: true
</p></blockquote>
<p>According to the above test, you&#8217;ll notice that every relative path is considered to be an instance of the same Function. However, the single Function loaded from a <acronym title="Uniform Resource Locator">URL</acronym> that must be resolved though <tt>require.paths</tt> is considered to be a different Function. Also notice that the module is re-imported. Be sure to keep your naming conventions consistent.</p>
<p>So, there are two solutions to this issue:</p>
<ul>
<li><strong>Use absolute paths.</strong> You can avoid this issue by using <tt>require( __dirname + '/path' )</tt> rather than <tt>require( './path' )</tt>.</li>
<li><strong>Access modules consistently.</strong> If you use <tt>./path</tt>, always use that path when accessing the module.</li>
</ul>
<p>Hopefully in future versions of Node, this will not be an issue (I&#8217;m looking forwarding to putting a strikethru throughout this entire post). Until then, hopefully this saves you a confusing game of cat and mouse.</p>
<p>A patch for this problem is <a href="https://github.com/mikegerwitz/node/commit/46e909ac98c827bd2d4097a853289bd5e6676eb9">available on github</a>.</p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fmikegerwitz.com%2F2010%2F11%2F09%2Fnode-js-modules-and-instanceof-operator%2F&amp;linkname=Node.js%20modules%20and%20instanceof%20operator">Share/Bookmark</a>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/2010/11/09/node-js-modules-and-instanceof-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SSH public key still prompting for password</title>
		<link>http://mikegerwitz.com/2010/11/02/ssh-public-key-still-prompting-for-password/</link>
		<comments>http://mikegerwitz.com/2010/11/02/ssh-public-key-still-prompting-for-password/#comments</comments>
		<pubDate>Tue, 02 Nov 2010 21:57:42 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[SSH]]></category>

		<guid isPermaLink="false">http://mikegerwitz.com/?p=401</guid>
		<description><![CDATA[This was one of those problems that irritated me considerably, since the solution was the last thing that I tried. The issue was that, I noticed, my private key for one of my users on one of my boxes was no longer being accepted. It was still prompting for the password. I tried regenerating the [...]]]></description>
			<content:encoded><![CDATA[<p>This was one of those problems that irritated me considerably, since the solution was the last thing that I tried. The issue was that, I noticed, my private key for one of my users on one of my boxes was no longer being accepted. It was still prompting for the password. I tried regenerating the key, clearing out <tt>~/.ssh/authorized_keys</tt>, reconfiguring ssh, reinstalling <acronym title="Secure Shell">SSH</acronym>&#8230;of course, the logical thing to do would have been to check the logs. But for whatever reason, I saved that for last.</p>
<p>When attempting to log into the server, I was prompted for a password as if the server did not have my public key. It certainly did. Checking <tt>/var/log/auth.log</tt>, I noticed this:</p>
<blockquote><p>
# /var/log/auth.log<br />
Nov  2 17:47:37 hostname sshd[2053]: Authentication refused: bad ownership or modes for directory /home/myuser
</p></blockquote>
<p>Somehow (likely due to user error), the permissions for <tt>/home/myuser</tt> was set to <tt>0777</tt>!</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p401code18'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p40118"><td class="code" id="p401code18"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-lah</span> <span style="color: #000000; font-weight: bold;">/</span>home <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> myuser
drwxrwxrwx <span style="color: #000000;">119</span> myuser myuser 8.0K <span style="color: #000000;">2010</span>-<span style="color: #000000;">11</span>-02 <span style="color: #000000;">17</span>:<span style="color: #000000;">45</span> myuser</pre></td></tr></table></div>

<p>The solution was to restore the directory permissions to what they should be &#8211; r/w access for the owner only.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p401code19'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p40119"><td class="code" id="p401code19"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">chmod</span> 0755 ~myuser
$ <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-lah</span> <span style="color: #000000; font-weight: bold;">/</span>home <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> myuser
drwxr-xr-x <span style="color: #000000;">119</span> myuser myuser 8.0K <span style="color: #000000;">2010</span>-<span style="color: #000000;">11</span>-02 <span style="color: #000000;">17</span>:<span style="color: #000000;">45</span> myuser</pre></td></tr></table></div>

<p>Hopefully now you&#8217;ll be able to log in with your private key.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p401code20'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p40120"><td class="code" id="p401code20"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ssh</span> myhost
$ <span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>auth.log
Nov  2 17:50:36 <span style="color: #c20cb9; font-weight: bold;">hostname</span> sshd<span style="color: #7a0874; font-weight: bold;">&#91;</span>2212<span style="color: #7a0874; font-weight: bold;">&#93;</span>: Accepted publickey <span style="color: #000000; font-weight: bold;">for</span> myuser from 192.168.1.XXX port XXXXX ssh2</pre></td></tr></table></div>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fmikegerwitz.com%2F2010%2F11%2F02%2Fssh-public-key-still-prompting-for-password%2F&amp;linkname=SSH%20public%20key%20still%20prompting%20for%20password">Share/Bookmark</a>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/2010/11/02/ssh-public-key-still-prompting-for-password/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OSCON Attendees Use Primarily Proprietary Devices / Software</title>
		<link>http://mikegerwitz.com/2010/07/21/oscon-attendees-use-primarily-proprietary-devices-software/</link>
		<comments>http://mikegerwitz.com/2010/07/21/oscon-attendees-use-primarily-proprietary-devices-software/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 03:38:02 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Free Software]]></category>
		<category><![CDATA[open source]]></category>

		<guid isPermaLink="false">http://mikegerwitz.com/?p=353</guid>
		<description><![CDATA[I&#8217;d just like to comment on some irony I&#8217;ve seen while attending the O&#8217;Reilly Open Source Convention (OSCON). I&#8217;m fully aware that this conference makes no attempt to represent purely free software. However, it is still an &#8220;open source&#8221; conference. Why, then, do the vast majority of attendees bring in laptops running proprietary operating systems [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d just like to comment on some irony I&#8217;ve seen while attending the <a href="http://oscon.com">O&#8217;Reilly Open Source Convention (OSCON)</a>. I&#8217;m fully aware that this conference makes no attempt to represent purely <a href="http://www.fsf.org/about/what-is-free-software">free software</a>. However, it is still an &#8220;open source&#8221; conference. Why, then, do the vast majority of attendees bring in laptops running proprietary operating systems such as Windows and OSX, as well as use proprietary devices such as the iPhone, iPad, Blackberries, etc? There&#8217;s some slight irony in that.</p>
<p>During the keynotes this morning, <a href="http://www.oscon.com/oscon2010/public/schedule/detail/13426">one of the presenters</a> (who is a part of the <a href="http://foundation.gnome.org/">GNOME foundation</a>) stated how, in the past, she would bring a laptop running Microsoft Windows into work. It would boot up and make that distinctive noise and everyone would turn to her. They would ask why she was running a proprietary <acronym title="Operating System">OS</acronym>, and she would make excuses for it. But not everyone made such excuses. She stated that it is because of those who decided to make such sacrifices &#8211; to spend hours getting things to work using free software &#8211; that free software is where it is today. She encouraged attendees to use free software themselves. Yet, they sit there typing notes in Microsoft Word. Do the attendees not understand the values of free software? Do they even understand the values of open source? I know some of them do &#8211; there are some there that have <acronym title="GNU's Not Unix!">GNU</acronym> stickers on their laptops, run <acronym title="GNU's Not Unix!">GNU</acronym>/Linux operating systems, etc. But not enough. It is astonishing and shameful.</p>
<p>The conference does little to encourage free software either. Yes, there are some presenters that make the distinction between free software and open source. However, there are also sessions that are essentially advertisements for proprietary solutions! For example, a vendor may have an &#8220;open source&#8221; or &#8220;community&#8221; version of their product, which they advertise as a trial. One such phrase I heard in one of the sessions was &#8220;we encourage you to at least try the community version&#8221;, or to try the community version &#8220;first&#8221;. They would then go through a list of <a href="http://www.fsf.org/bulletin/2007/fall/antifeatures/">anti-features</a> in the &#8220;open source&#8221; version that essentially make it useless in any performance-intensive or enterprise settings, forcing users to upgrade to their &#8220;enterprise&#8221; versions. This is an excellent example of the distinction between the free software philosophy and simply making your product &#8220;open source&#8221;. In this instance, it appears to be a sales pitch. The vendor then went on to state that they have patents pending for their software. Let&#8217;s not even get me started on that. <em>Why would you have such a session at an open source conference?!</em></p>
<p>Clearly, while many of the attendees and presenters do recognize software freedom and understand concepts that give the meaning behind the term &#8220;open&#8221;, there are many that do not. OSCON also meets the needs of business owners. I am willing to bet that the vast majority of those using proprietary devices were there for their companies. Hell, I&#8217;m there because my employer funded it &#8211; it&#8217;s an expensive conference. However, this is also a conference I would love to go to on my own. I understand the core principles behind free software. I understand the open source movement. But if your employer simply sees open source as a benefit to business and sends you there, and you are just an employee&#8230;well, of course you&#8217;re not going to care. You&#8217;re just there to see what new and exciting technologies exist to help your employer or your own business.</p>
<p>I wish during the tutorial sessions the first two days that I took the time to count the number of laptops and devices in each of the areas I entered in order to give an actual figure. Now that the tutorial days are over, there are much less devices. Even still, out of a handful of 20 or so laptops, I&#8217;m lucky to see a single one running a free operating system. Even the majority of the presenters used Macs. However, some of those presenters had no idea what they were doing on a Mac, leading me to believe that OSCON supplied them! That is even worse! It left a very bad impression. For those attendees that I did see at OSCON that understood and embodied the spirit of the open source movement (or even better, the free software movement), I was very pleased. For everyone else &#8211; very disappointed. I hope that they are able to take something away from those speakers that do mention free software and do explain the values of open source.</p>
<p>This also brings up another thought. Lack of presence from members of the <a href="http://fsf.org">Free Software Foundation</a>. The most I saw of them was a single booth in the Expo hall. Where are the speakers? Where are the sessions? (In their defense, I understand that there were hundreds of session proposals that were unable to make it into the conference. Maybe someone tried. I&#8217;ll ask FSF directly.) There are little tags you can put on your badges, such as &#8220;Git&#8221;, &#8220;Desperate <acronym title="Practical Extraction and Report Language">Perl</acronym> Hacker&#8221;, etc. Why is there no &#8220;Free Software&#8221; one? Why isn&#8217;t Richard Stallman himself helping to spread the word of free software at such a large conference? Now, don&#8217;t get me wrong &#8211; I completely understand him not being there. If I were Stallman I&#8217;d probably have a heart attack from what I saw there (metaphorically speaking &#8211; I almost did). But that is not the point. The point is that there are plenty of people there who have been introduced to this concept &#8211; may even be new to the concept &#8211; of &#8220;open source&#8221;. They may not understand what free software is, or why it is important. But they may be interested in it. How often do you see such a large group of software enthusiasts and developers in a single location for a conference focusing on openness in software? This would have been an excellent opportunity for the Free Software Foundation to spread its values. If anything, help combat the vendors that are lacing their &#8220;open source&#8221; products with proprietary bits. Give out CDs containing free <acronym title="GNU's Not Unix!">GNU</acronym>/Linux distributions. Explain our core values. Ask people to join. I was very disappointed by FSF&#8217;s presence at that conference. I&#8217;ll bring that up in the appropriate channels &#8211; I just wanted to mention it publically as well.</p>
<p>Overall, OSCON has been an interesting experience thus far. I&#8217;m not trying to recognize it as a free software conference &#8211; it clearly isn&#8217;t, and I never expected it to be. However, it is an excellent learning experience and there are many free software projects represented there.</p>
<p>Oh, and a last note that just puts the icing on the cake: After the conference, I went on a walking tour of Portland. The tour guide saw an iPhone and stated, &#8220;Isn&#8217;t this an open source conference? Aren&#8217;t iPhones kind of anti-open source?&#8221; (I quoted him to the best degree I possibly could, though those may not have been his exact words.) Yeah, that&#8217;s just great. A tour guide was able to recognize that. Yet the group of OSCON attendees that were asked that question remained silent. Swell.</p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fmikegerwitz.com%2F2010%2F07%2F21%2Foscon-attendees-use-primarily-proprietary-devices-software%2F&amp;linkname=OSCON%20Attendees%20Use%20Primarily%20Proprietary%20Devices%20%2F%20Software">Share/Bookmark</a>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/2010/07/21/oscon-attendees-use-primarily-proprietary-devices-software/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Performance: Array Iteration</title>
		<link>http://mikegerwitz.com/2010/03/28/php-performance-array-iteration/</link>
		<comments>http://mikegerwitz.com/2010/03/28/php-performance-array-iteration/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 12:32:11 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Performance]]></category>

		<guid isPermaLink="false">http://www.mikegerwitz.com/?p=273</guid>
		<description><![CDATA[In PHP, arrays are used for virtually everything (I&#8217;ll spare my rant on that), therefore it&#8217;s likely that you&#8217;re going to use them abundantly in your scripts. The issue we&#8217;re going to be focusing on here isn&#8217;t with the arrays themselves &#8211; it&#8217;s the simple fact that you&#8217;re probably going to have to iterate over [...]]]></description>
			<content:encoded><![CDATA[<p>In <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>, arrays are used for virtually everything (I&#8217;ll spare my rant on that), therefore it&#8217;s likely that you&#8217;re going to use them abundantly in your scripts. The issue we&#8217;re going to be focusing on here isn&#8217;t with the arrays themselves &#8211; it&#8217;s the simple fact that you&#8217;re probably going to have to iterate over them in order to get the data you want. <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> offers a wide range of methods for doing so, so which do we use?</p>
<p>First, let&#8217;s generate an array to work with. We&#8217;re going for quantity here, not size of data. Therefore, we&#8217;ll simply generate an array containing a list of integers. <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> offers a quick and easy way to do this using the <a href="http://us.php.net/manual/en/function.array_fill.php"><tt>array_fill()</tt></a> function:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code34'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27334"><td class="code" id="p273code34"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// generate an array containing 100,000 elements, each with the value of 1</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_fill"><span style="color: #990000;">array_fill</span></a><span style="color: #009900;">&#40;</span> 0<span style="color: #339933;">,</span> 100000<span style="color: #339933;">,</span> 1 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We want to use the <tt>array_fill()</tt> function rather than <tt>range()</tt> here because we will be summing up the values in the examples below. Range will generate incremental values, leading to integer overflows and inconsistent results across various architectures.</p>
<p><strong>Note:</strong> This article assumes that the reader is running at least <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 5.0.0. If you are running anything else, you should understand that <a href=http://www.php.net/archive/2008.php#id2008-08-07-1">official support for <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 4 ended back in 2008</a> and you should consider upgrading immediately.</p>
<h3><tt>for</tt> loop</h3>
<p>Those coming from a procedural background may be most familiar with the <a href="http://us2.php.net/manual/en/control-structures.for.php"><tt>for</tt> loop</a> as a means of iterating through an array. Since it is so common, let&#8217;s take a look at that first:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code35'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27335"><td class="code" id="p273code35"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$time_start</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$total</span>      <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// loop through each of the array items and sum them up</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$total</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span> <span style="color: #000088;">$i</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// calculate total running time and output the result</span>
<span style="color: #000088;">$time_end</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$time_start</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/printf"><span style="color: #990000;">printf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Total: <span style="color: #009933; font-weight: bold;">%d</span><span style="color: #000099; font-weight: bold;">\n</span>loop time: <span style="color: #009933; font-weight: bold;">%f</span>s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time_end</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Let&#8217;s run that test and see what type of results we get:</p>
<blockquote><p>
Total: 100000<br />
for loop time: 0.127712s
</p></blockquote>
<p>Ouch. The time that is returned from the above test will depend on the speed of your system. I do know that, on my system, that&#8217;s a lot of time to spend simply adding up elements of an array. Let&#8217;s see how we may be able to improve that a bit.</p>
<p>The first thing that may jump out is the use of the <tt>count</tt> method in the condition of the <tt>for</tt> clause:</p>
<blockquote><p><tt>for ( $i = 0; $i &lt; <b>count( $data )</b>; $i++ )</tt></p></blockquote>
<p>It may help to understand exactly how <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> executes the above statement. The assignment portion of the loop is executed once before any iterations begin. The step/count, which is the <tt>$i++</tt> part, is executed after each iteration. The test expression (middle) is executed before each iteration to determine if the step should be executed and if we should continue with the loop.</p>
<p>Alright, that&#8217;s pretty straightforward. But that is the issue. That <tt>count</tt> function is being executed <em>for every iteration</em>. <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> is not caching the result of that function. That gets pretty expensive considering that it would be calling the function no less than 100,000 times.</p>
<p>Let&#8217;s see what happens when we change it up a bit:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code36'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27336"><td class="code" id="p273code36"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// ...</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// loop through each of the array items and sum them up</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> 0<span style="color: #339933;">,</span> <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$count</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$total</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span> <span style="color: #000088;">$i</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// ...</span></pre></td></tr></table></div>

<blockquote><p>
Total: 100000<br />
for loop time: 0.046609s
</p></blockquote>
<p>Wow &#8211; that is a huge improvement! We nearly tripled the speed of the iteration simply by having the conditional check a precalculated value, rather than calling the function with each iteration. That&#8217;s pretty good, right?</p>
<p>No; it&#8217;s still pretty slow. We can do much better.</p>
<h3><tt>while</tt> loop and <tt>current()</tt></h3>
<p>Some people may prefer to use a <a href="http://us2.php.net/manual/en/control-structures.while.php"><tt>while</tt> loop</a> in order to loop through the elements of an array. One of the most common ways of doing that is to use the <a href="http://us2.php.net/manual/en/function.current.php"><tt>current()</tt></a> function in conjunction with <a href="http://us2.php.net/manual/en/function.next.php"><tt>next()</tt></a>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code37'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27337"><td class="code" id="p273code37"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// loop through each of the array items and sum them up</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$val</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/current"><span style="color: #990000;">current</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$total</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$val</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/next"><span style="color: #990000;">next</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So how does that fair speed wise. It certainly <em>looks</em> a little less complicated than the <tt>for</tt> loop&#8230;that means it&#8217;s faster, right?</p>
<blockquote><p>
Total: 100000<br />
for loop time: 0.175094s
</p></blockquote>
<p>Yikes. We took a step in the wrong direction. Let&#8217;s pretend we didn&#8217;t see this.</p>
<p><a id="foreach"></a></p>
<h3><tt>foreach</tt> loop</h3>
<p><acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 4 introduced the <a href="http://us2.php.net/manual/en/control-structures.foreach.php"><tt>foreach</tt> construct</a> which provides a much more simple means of iterating through an array. Surely if it was designed for arrays, it should be faster than <tt>for</tt>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code38'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27338"><td class="code" id="p273code38"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// loop through each of the array items and sum them up</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$total</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$val</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Notice that, in the above snippet, we are also avoiding the use of a temporary variable <tt>$i</tt> to store the current index. While <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> is undoubtedly doing so internally, we&#8217;re talking about a difference between compiled and optimized C code versus interpreted, sluggish <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> code. So that alone will provide us with a bit of a benefit, however slight.</p>
<p>Let&#8217;s see the result:</p>
<blockquote><p>
Total: 100000<br />
for loop time: 0.037076s
</p></blockquote>
<p>Not too bad&#8230;we managed to shave off nearly 1/4th the time of the <tt>for</tt> loop. But I&#8217;m not satisfied.</p>
<h3>The Problem</h3>
<p>What do all of the above have in common? They&#8217;re looping structures. Great, so what&#8217;s the issue? If you&#8217;re coming from a compiled, high-performance language like C, there is no issue. Let&#8217;s see how long it&#8217;d take to iterate through an array containing 100,000 elements in C:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code39'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27339"><td class="code" id="p273code39"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;sys/time.h&gt;</span>
&nbsp;
<span style="color: #339933;">#define DATALEN 100000</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span>            data<span style="color: #009900;">&#91;</span> DATALEN <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">register</span> <span style="color: #993333;">long</span>  i<span style="color: #339933;">;</span>  
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span>  total <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">struct</span> timeval time_start<span style="color: #339933;">,</span> time_end<span style="color: #339933;">;</span>
    <span style="color: #993333;">long</span>           elapsed<span style="color: #339933;">,</span> uelapsed<span style="color: #339933;">;</span> 
    <span style="color: #993333;">double</span>         elapsed_total<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// create our data array</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> DATALEN<span style="color: #339933;">;</span> data<span style="color: #009900;">&#91;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> 1 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// get current time</span>
    gettimeofday<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>time_start<span style="color: #339933;">,</span> NULL <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
    <span style="color: #666666; font-style: italic;">// loop through the array summing up the values</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> DATALEN<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>   
        total <span style="color: #339933;">+=</span> data<span style="color: #009900;">&#91;</span> i <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>   
&nbsp;
    <span style="color: #666666; font-style: italic;">// get total running time</span>
    gettimeofday<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>time_end<span style="color: #339933;">,</span> NULL <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    elapsed       <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> time_end.<span style="color: #202020;">tv_sec</span> <span style="color: #339933;">-</span> time_start.<span style="color: #202020;">tv_sec</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    uelapsed      <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> time_end.<span style="color: #202020;">tv_usec</span> <span style="color: #339933;">-</span> time_start.<span style="color: #202020;">tv_usec</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    elapsed_total <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> elapsed <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span> uelapsed <span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> 1000000 <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// output total and loop time</span>
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;Total: %ld<span style="color: #000099; font-weight: bold;">\n</span>loop time: %fs<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
        total<span style="color: #339933;">,</span> elapsed_total
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<blockquote><p>
Total: 100000<br />
loop time: 0.000273s
</p></blockquote>
<p>Well, then. The point here is &#8211; <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> is a slow language, <em>especially</em> for looping. Wherever possible, you want to avoid it.</p>
<p>But you need to iterate through your arrays somehow! Does that mean you are stuck with <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s slow loops?</p>
<p><a id="builtin"></a></p>
<h3>Use Built-In Functions</h3>
<p>Fortunately, there&#8217;s certain functions available to you that can drastically improve the speed of array processing. In fact, <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> has a whole slew of <a href="http://us2.php.net/manual/en/ref.array.php">functions devoted to processing and altering arrays</a>.</p>
<p>Programmers coming form other languages such as C may prefer to write their own loops for processing data, because it is faster than a function call. This is not true at all with <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>; in fact, much of <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s power comes from it&#8217;s built-in functions. <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> itself and its libraries are written in C. Therefore, if you can use a built-in <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> function that will do the looping for you, that loop will be executed much more quickly.</p>
<p>Let&#8217;s take another look at our above example &#8211; summing up 100,000 values of an array. Is there a <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> function that could do this for us? <a href="http://us2.php.net/manual/en/function.array-sum.php"><tt>array_sum()</tt></a> perhaps?</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code40'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27340"><td class="code" id="p273code40"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$time_start</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// sum up the values in the array</span>
<span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_sum"><span style="color: #990000;">array_sum</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// calculate total running time and output the result</span>
<span style="color: #000088;">$time_end</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$time_start</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/printf"><span style="color: #990000;">printf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Total: <span style="color: #009933; font-weight: bold;">%d</span><span style="color: #000099; font-weight: bold;">\n</span>for loop time: <span style="color: #009933; font-weight: bold;">%f</span>s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time_end</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Above, we are making a single function call to <tt>array_sum()</tt> rather than going in a loop and manually adding everything up. Let&#8217;s see how this method does:</p>
<blockquote><p>
Total: 100000<br />
for loop time: 0.012571s
</p></blockquote>
<p>Not bad. Using everything discussed here, we&#8217;ve cut our time down from the original 0.127712s to a mere 0.012571s. It&#8217;s still no C code, but our loop is <em>10 times faster</em> than it was originally. It&#8217;s still written in <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>; we&#8217;re just using the tools we were given.</p>
<p>But wait &#8211; I said that <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s libraries were written in C. Why isn&#8217;t <tt>array_sum()</tt> just as fast as the C example provided above? Unfortunately, we&#8217;re still dealing with <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s variables and other internal representations here. It&#8217;s not just jumping to a position in memory like it does with an array in C. The <tt>array_sum()</tt> function still has to scan the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> array to retrieve all the values. There&#8217;s no getting away from that.</p>
<h3>Expense of Function Calls</h3>
<p>One must also be mindful of <em>what</em> their loop is doing. Take a look at the following example. It calls a function in order to count all the values in an array that contain the number 2.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code41'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27341"><td class="code" id="p273code41"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> check_value<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$val</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: #000088;">$val</span> <span style="color: #339933;">===</span> 2 <span style="color: #009900;">&#41;</span>
        ? 1
        <span style="color: #339933;">:</span> 0
    <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// generate an array with 1/3 values containing 1, the rest containing 2</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>
    <a href="http://www.php.net/array_fill"><span style="color: #990000;">array_fill</span></a><span style="color: #009900;">&#40;</span> 0<span style="color: #339933;">,</span> 10000<span style="color: #339933;">,</span> 1 <span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">+</span> <a href="http://www.php.net/array_fill"><span style="color: #990000;">array_fill</span></a><span style="color: #009900;">&#40;</span> 10000<span style="color: #339933;">,</span> 20000<span style="color: #339933;">,</span> 2 <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$time_start</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// count the values in the array greater than 50000</span>
<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$count</span> <span style="color: #339933;">+=</span> check_value<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// calculate total running time and output the result</span>
<span style="color: #000088;">$time_end</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$time_start</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/printf"><span style="color: #990000;">printf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Count: <span style="color: #009933; font-weight: bold;">%d</span><span style="color: #000099; font-weight: bold;">\n</span>for loop time: <span style="color: #009933; font-weight: bold;">%f</span>s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$count</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time_end</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<blockquote><p>
Count: 20000<br />
for loop time: 0.043597s
</p></blockquote>
<p>Above, we called the <tt>check_value()</tt> function 30,000 times. Functions do an excellent job at improving code clarity and reducing duplicate code. However, function and method calls are expensive. Consider what happens when we get rid of the function and do all the processing in the body of the loop:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code42'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27342"><td class="code" id="p273code42"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// count the values in the array greater than 50000</span>
<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$val</span> <span style="color: #339933;">===</span> 2 <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$count</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If we run it again using that loop instead of the previous one, we get the following result:</p>
<blockquote><p>
Count: 20000<br />
for loop time: 0.013397s
</p></blockquote>
<p>That&#8217;s a significant speed improvement. Now, I&#8217;m not advocating that you get rid of all of your functions (though I would advocate preprocessor macros in <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>). However, consider the performance implications of excessive function or method calls where it is important. In most situations, the benefits you gain from legibility will outweigh the performance pitfalls.</p>
<p>It should also be noted that under many circumstances, the overhead of the function call may be insignificant. If your method takes 0.3 seconds to execute, you&#8217;re not going to care about milliseconds of overhead.</p>
<h3>Memory Tradeoff &#038; Apparent Processing</h3>
<p>Let&#8217;s continue with the example from the previous section (counting the number of elements in an array that contain the value <tt>2</tt>). <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> does not contain a function that can explicitly search an array for the given value and return the number of results. It does, however, have something very similar &#8211; <a href="http://us.php.net/manual/en/function.array-count-values.php"><tt>array_count_values()</tt></a>.</p>
<p>The <tt>array_count_values()</tt> function will loop through the array and maintain a count of each of the values. It will then return an array containing the results. Therefore, in our above example, it would return an array much like the following:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code43'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27343"><td class="code" id="p273code43"><pre class="php" style="font-family:monospace;"><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10000</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// there are 10,000 1s in our data array</span>
    <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">20000</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// there are 20,000 2s in our data array</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That&#8217;s a bit more information than we want. In fact, what if we had an array containing hundreds of different values? Wouldn&#8217;t it take longer to count every element than a single one (well, no, but one might assume)? That would account for a bit more memory usage as well, even though it does give us the answer we are looking for. Which is a more elegant solution?</p>
<p>When attempting to optimize code, developers must often have to choose between CPU time and memory footprint. This is one of those situations due to language limitations. Let&#8217;s take a look at how much time it takes for <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> to count the values for us:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code44'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27344"><td class="code" id="p273code44"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// count the values in the array greater than 50000</span>
<span style="color: #000088;">$count_data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_count_values"><span style="color: #990000;">array_count_values</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$count</span>      <span style="color: #339933;">=</span> <span style="color: #000088;">$count_data</span><span style="color: #009900;">&#91;</span> 2 <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// output memory consumption</span>
<a href="http://www.php.net/printf"><span style="color: #990000;">printf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Memory Consumption: <span style="color: #009933; font-weight: bold;">%d</span> bytes<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/memory_get_usage"><span style="color: #990000;">memory_get_usage</span></a><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<blockquote><p>
Memory Consumption: 6815744 bytes<br />
Count: 20000<br />
for loop time: 0.003805s
</p></blockquote>
<p>Even though we&#8217;re gathering more information, it&#8217;s still 3.5x faster than counting only a single value manually. In fact, I&#8217;m willing to bet that it even consumed less memory than our previous example, simply due to the fact that we aren&#8217;t having to deal with userland variables and function calls:</p>
<blockquote><p>
(Our example using check_value(), with memory output)</p>
<p>Memory Consumption: 7077888 bytes<br />
Count: 20000<br />
for loop time: 0.043128s
</p></blockquote>
<p>It seems that counting a single element using our <tt>check_value()</tt> method used <tt>262144</tt> more bytes than <tt>array_count_values()</tt>! Damn you, <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>! Yes, it&#8217;s unfortunate that considerations like this must be made, but you&#8217;d be surprised how many internal methods really do provide both strong memory and CPU benefits even though they &#8220;do more work&#8221;. Userland consumes considerable amounts of memory.</p>
<p>That isn&#8217;t to say that <tt>array_count_values()</tt> can&#8217;t consume more memory. Like I said previously &#8211; if it returns an array with thousands of values, it&#8217;s going to offset that memory savings.</p>
<h3>ArrayObjects and Iterators</h3>
<p><acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 5 began a strong move toward Object-Oriented programming. It provides a number of powerful components that deal with iteration &#8211; specifically <a href="http://us.php.net/manual/en/class.traversable.php"><tt>Traversable</tt></a> objects and <a href="http://us.php.net/manual/en/class.iterator.php"><tt>Iterators</tt></a>. They provide a benefit to developers in the sense that it makes code much easier to understand and allows for powerful OO implementations, but does it provide any performance benefit?</p>
<p>Firstly, let&#8217;s see what happens when we loop through an <a href="http://us.php.net/manual/en/class.arrayobject.php"><tt>ArrayObject</tt></a> rather than a normal <tt>array</tt>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code45'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27345"><td class="code" id="p273code45"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// generate an array containing 100,000 elements, each with the value of 1</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayObject<span style="color: #009900;">&#40;</span> <a href="http://www.php.net/array_fill"><span style="color: #990000;">array_fill</span></a><span style="color: #009900;">&#40;</span> 0<span style="color: #339933;">,</span> 100000<span style="color: #339933;">,</span> 1 <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<blockquote><p>
Total: 100000<br />
loop time: 0.061663s
</p></blockquote>
<p>This is compared to our <tt>foreach</tt> example with a result of <tt>0.037076s</tt>. So we did loose a bit of time there. However, that does not mean you shouldn&#8217;t use <tt>ArrayObject</tt>s. In fact, I use them a great deal in OO scripts, because <tt>ArrayObject</tt>s coupled with <tt>Iterator</tt>s provide powerful designs that aren&#8217;t quite as elegant when using procedural code. However, when performance is a requirement, I have to steer clear of them.</p>
<p>You will find that the <tt>Iterator</tt> object provides very little benefit over a conventional <tt>foreach</tt> loop as well. The only time I make use of an <tt>Iterator</tt> is when it contains additional logic that determines what results it should return (e.g. to iterate only over odd numbers), or if the implementation allows for an <tt>Iterator</tt> to be passed in at runtime to determine what results should be returned.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code46'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27346"><td class="code" id="p273code46"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// loop through each of the array items and sum them up</span>
<span style="color: #000088;">$iterator</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">valid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$total</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<blockquote><p>
Total: 100000<br />
loop time: 0.291542s
</p></blockquote>
<p>In fact, that&#8217;s our slowest method yet. So, just be wise when weighing the costs and benefits. If performance doesn&#8217;t matter &#8211; go ahead. Use whatever implementation is best for you. That&#8217;s why you have the option. If performance <em>is</em> a factor, don&#8217;t even glance at those objects. Maybe future versions of <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> will enhance the functionality to the point where their speeds are equivalent to the loop structures.</p>
<h3>Wall Of Text Summary</h3>
<p><acronym title="PHP: Hypertext Preprocessor">PHP</acronym> provides a number of ways to iterate through arrays &#8211; rest assured that we haven&#8217;t nearly covered them all here. Out of all the loop structures available, <a href="#foreach"><tt>foreach</tt></a> is the fastest for iteration. However, it comes up short when compared to <a href="#builtin">built-in functions</a> designed for array processing. Where possible, those built-in functions should be used, even where it may seem at first glance that it may be slower. They often provide a very strong performance benefit &#8211; both CPU and memory wise.</p>
<p><acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 5 introduced a number of new means of iteration through the use of objects. Certain implementations may do well to improve code legibility, and may enable the developer to implement some fairly powerful object-oriented designs, but when performance is a factor, you want to steer clear of them.</p>
<p>Hopefully this article helped to clear up some questions. I expect not to see any more <tt>for</tt> loops with <tt>count()</tt> in the loop test expression!</p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fmikegerwitz.com%2F2010%2F03%2F28%2Fphp-performance-array-iteration%2F&amp;linkname=PHP%20Performance%3A%20Array%20Iteration">Share/Bookmark</a>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/2010/03/28/php-performance-array-iteration/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>False Freedom in Wordpress Themes</title>
		<link>http://mikegerwitz.com/2010/02/06/false-freedom-in-wordpress-themes/</link>
		<comments>http://mikegerwitz.com/2010/02/06/false-freedom-in-wordpress-themes/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 01:09:08 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Free Software]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.mikegerwitz.com/?p=235</guid>
		<description><![CDATA[I&#8217;ve always had one major issue with some of the &#8220;free&#8221; Wordpress themes out there &#8211; the encryption of footer data. I&#8217;ve seen countless themes licensed under the GNU GPL that violate the very principles for which it exists.
Someone took the time to make their theme available under the GPL &#8211; that&#8217;s an excellent, noble [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve always had one major issue with some of the &#8220;free&#8221; Wordpress themes out there &#8211; the encryption of footer data. I&#8217;ve seen countless themes licensed under the <a href="http://www.gnu.org/licenses/gpl.html"><acronym title="GNU's Not Unix!">GNU</acronym> <acronym title="GNU General Public License">GPL</acronym></a> that violate the very principles for which it exists.</p>
<p>Someone took the time to make their theme available under the <acronym title="GNU General Public License">GPL</acronym> &#8211; that&#8217;s an excellent, noble thing. However, the encryption of footer data shows a complete lack of understanding of the philosophy behind <a href="http://www.gnu.org/philosophy/free-sw.html">Free Software</a> and the <acronym title="GNU General Public License">GPL</acronym>.</p>
<p>Freedom #1 states:</p>
<blockquote><p>The freedom to study how the program works, and change it to make it do what you wish (freedom 1). Access to the source code is a precondition for this.</p></blockquote>
<p>Here is an example footer from one of the themes I found (most of the content has been removed and what remains has been altered to (a) not throw anyone under a bus and (b) reduce the size of this post):</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p235code48'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23548"><td class="code" id="p235code48"><pre class="php" style="font-family:monospace;"><a href="http://www.php.net/eval"><span style="color: #990000;">eval</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/gzinflate"><span style="color: #990000;">gzinflate</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/base64_decode"><span style="color: #990000;">base64_decode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gfE8f[...]T8='</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* WARNING: This file is protected by copyright law. To reverse engineer or decode this file is strictly prohibited. */</span>
<span style="color: #000088;">$o</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ZnvJF[...]Mn=&quot;</span><span style="color: #339933;">;</span>eval<span style="color: #009900;">&#40;</span><a href="http://www.php.net/base64_decode"><span style="color: #990000;">base64_decode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Xvxsbe0wO2V2YWwoYmFzZT[...]GxsfW9cnw==&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>return<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Clearly, this is in violation of Freedom #1 for the following reasons:</p>
<ol>
<li><strong>The only way to gain access to the source code is to decrypt (reverse engineer) the above content.</strong> The fact that it is a very simple matter to do so is irrelevant &#8211; the source code should be clearly visible.</li>
<li><strong>There is a comment in the code that clearly states that any attempt to reverse engineer the code is illegal.</strong> This is in direct conflict with the <acronym title="GNU General Public License">GPL</acronym> &#8211; in fact, it&#8217;s completely incompatible. So that statement is actually negated by the license. You&#8217;re more than welcome to do so under terms of the <acronym title="GNU General Public License">GPL</acronym>.</li>
<li><strong>Since you do not have access to the original source code without decrypting the data, you are not free to modify it.</strong> Many try to decrypt the data and are unable to do so because they do are familiar with the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> language or are otherwise unable to figure out how to decrypt it. If you do decrypt the above, you will find that the majority of it is <acronym title="HyperText Markup Language">HTML</acronym>, with one call to <tt>wp_footer()</tt>. This means that, were it not encrypted, users would need only know <acronym title="HyperText Markup Language">HTML</acronym> to safely modify it.</li>
</ol>
<p>But those aren&#8217;t the only issues. There&#8217;s also some other considerations:</p>
<ol>
<li>The encrypted footer is <strong>7743 bytes</strong>. Decrypted, it is roughly <strong>996 bytes</strong>. That is a <em>huge</em> size difference &#8211; a lot of wasted bytes to secure the footer.</li>
<li>There are <strong>a great number of function calls in order to decrypt the footer</strong>. You can see some of them above, but the eval()&#8217;d code contains additional methods to decrypt further encrypted and obfuscated code. That&#8217;s unnecessary overhead. Not to mention that <strong>eval() in itself is very slow</strong>. <em>So, not only did the author prevent you from modifying the footer, you&#8217;re taking a performance hit because of it!</em></li>
</ol>
<p>I can understand why the author would want to encrypt the footer. They probably felt that, if they&#8217;re releasing their source code free of charge to the public, they should at least be allowed to keep their copyright notice in tact. And yes, they should be able to. In fact, the copyright notice should never be removed on <acronym title="GNU General Public License">GPL</acronym>&#8217;d code &#8211; it&#8217;s a legal requirement to keep it there. It is still the author&#8217;s work, and it is a copyrighted work, so the notice must remain in tact. But yes, people do remove it anyway, even though they shouldn&#8217;t.</p>
<p>But when an author takes the time to not only prevent me from modifying the source, but in doing so causes me to incur a performance hit, <em>and</em> on top of it licenses the theme under the <acronym title="GNU General Public License">GPL</acronym> &#8211; I see some very large ethical issues. Especially considering that all I wanted to do was add my own link, and its placement required being able to modify the encrypted <acronym title="HyperText Markup Language">HTML</acronym>.</p>
<p>I propose that all those who support free software boycott such themes and send a notice to the authors when they come by such themes (or send them a link to this post). Furthermore, do not let developers who try to restrict your freedom push you around. I do not propose you remove the copyright notice &#8211; in fact, I would never, ever suggest that someone do that; but I do propose that, if the theme is licensed under the <acronym title="GNU General Public License">GPL</acronym>, you decrypt the footer regardless of any notices that state you otherwise cannot. The license clearly states you have access to the source code, and if you are not given such access, the code either needs to be relicensed, or the developer needs to comply with its terms.</p>
<p>As there are also many themes out there that encrypt their data and are <em>not</em> licensed under the <acronym title="GNU General Public License">GPL</acronym> or other free software licenses, I&#8217;m not going to provide any methods of decrypting the data. I do not support proprietary software, but I also do not encourage license violations.</p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fmikegerwitz.com%2F2010%2F02%2F06%2Ffalse-freedom-in-wordpress-themes%2F&amp;linkname=False%20Freedom%20in%20Wordpress%20Themes">Share/Bookmark</a>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/2010/02/06/false-freedom-in-wordpress-themes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft: Hijacking Your Data</title>
		<link>http://mikegerwitz.com/2010/01/03/microsoft-hijacking-your-data/</link>
		<comments>http://mikegerwitz.com/2010/01/03/microsoft-hijacking-your-data/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 03:32:13 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[drm]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[privacy]]></category>

		<guid isPermaLink="false">http://www.mikegerwitz.com/?p=224</guid>
		<description><![CDATA[I recently read this story on Slashdot:
Microsoft Seeks Patent On Shaming Fat Gamers
The idea is that Microsoft wants to create a system whereby it will gather physical statistics on a user and alter the game play around those characteristics. For example, as the article mentioned, an obese player may be cut off from playing. I [...]]]></description>
			<content:encoded><![CDATA[<p>I recently read this story on Slashdot:</p>
<p><a href="http://games.slashdot.org/story/09/12/18/1649253/Microsoft-Seeks-Patent-On-Shaming-Fat-Gamers">Microsoft Seeks Patent On Shaming Fat Gamers</a></p>
<p>The idea is that Microsoft wants to create a system whereby it will gather physical statistics on a user and alter the game play around those characteristics. For example, as the article mentioned, an obese player may be cut off from playing. I suppose the idea could have benefits &#8211; such as helping players get into shape if they can&#8217;t wait to get back on their consoles. But this raises some serious ethical issues. What, for example, about those players who are unable to lose weight for genetic reasons? Or what ever happened to those who play video games to escape from the real world? Hell, that&#8217;s half the fun in my opinion.</p>
<blockquote><p>Microsoft also proposes shaping gaming experiences by using &#8216;psychological and demographic information such as education level, geographic location, age, sex, intelligence quotient, socioeconomic class, occupation, marital/relationship status, religious belief, political affiliation, etc.&#8217;</p></blockquote>
<p>This to me raises some serious privacy issues. I would never wish a video game or vendor to have <em>that</em> much personal information on myself. In fact, I feel the name on a credit card is too much information for the vendor or any party to have. But the traits listed above? Look, placing yourself in a video game is a pretty cool idea if you consent to it. For those who do want to give up all of that personal information, good for them. But basing game play off of factors like physical appearance, sex, religion, politically affiliation, etc could turn into a discriminatory battle.</p>
<p>Furthermore, you know that corporations will use this information for other statistical purposes, or to sell to other companies. Would you want a company like Microsoft to know more about you than you probably do? The only way I would subscribe to such a system is if it (a) used an open protocol, (b) was subject to very strict privacy standards and policies that are heavily enforced and investigated, (c) we were able to strictly state what information we wanted to give to the vendor, (d) we were able to choose what remote server to upload such statistical data to (i.e. one we may be able to trust) and (e) if the video game provided this is an <em>option</em> rather than a requirement. The patent does use the word &#8220;nonvolitionally&#8221; when referencing information gathered from the user, but we&#8217;ll see how that goes. Vendors can still state, &#8220;oh, uh, you can only play this game if you give us this, this, this and this.&#8221;</p>
<p>&#8230;surprise, the girl you&#8217;ve been lusting over in your video game is actually a guy, and this will prove it.</p>
<p>The patent states:</p>
<blockquote><p>[0009]In yet a further aspect, an apparatus is provided for interacting with a virtual gaming environment. An information source is accessed for receiving a health information nonvolitionally obtained from a user. These sources are at least two of a a physiological sensor, a networked healthcare information repository, and a healthcare smart card.</p></blockquote>
<p>I would be highly against anyone having access to information on that type of level &#8211; as everyone else should be. This is just one example of how vendors like Microsoft may try to steal information from you. If they were able to have access to heathcare records, you wouldn&#8217;t be able to say &#8220;I only want you to see this&#8221;. No, they&#8217;d probably be able to see it all.</p>
<p>Again, I&#8217;m not saying it&#8217;s a horrible idea. I&#8217;ve often fantasized about placing yourself in a virtual reality, where the game actually rendered you in the virtual environment. But considering Microsoft&#8217;s track record, they are not the company to implement or control this.</p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fmikegerwitz.com%2F2010%2F01%2F03%2Fmicrosoft-hijacking-your-data%2F&amp;linkname=Microsoft%3A%20Hijacking%20Your%20Data">Share/Bookmark</a>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/2010/01/03/microsoft-hijacking-your-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grub 2 Hangs on Boot</title>
		<link>http://mikegerwitz.com/2009/11/01/grub-2-hangs-on-boot/</link>
		<comments>http://mikegerwitz.com/2009/11/01/grub-2-hangs-on-boot/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 00:10:36 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[grub]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.mikegerwitz.com/2009/11/01/grub-2-hangs-on-boot/</guid>
		<description><![CDATA[When BETA testing Ubuntu Karmic (9.10), I noticed that the whole boot process wasn&#8217;t so fast. This was primarily due to Grub 2 hanging for nearly 35 seconds each boot.
I haven&#8217;t the time to figure out what Grub is trying to do in that amount of time, but I did notice a solution. I have [...]]]></description>
			<content:encoded><![CDATA[<p>When BETA testing Ubuntu Karmic (9.10), I noticed that the whole boot process wasn&#8217;t so fast. This was primarily due to Grub 2 hanging for nearly 35 seconds each boot.</p>
<p>I haven&#8217;t the time to figure out what Grub is trying to do in that amount of time, but I did notice a solution. I have three HDDs. In the BIOS, it was set as the third boot device. By moving it to the top, Grub quickly moved along to boot Ubuntu.</p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fmikegerwitz.com%2F2009%2F11%2F01%2Fgrub-2-hangs-on-boot%2F&amp;linkname=Grub%202%20Hangs%20on%20Boot">Share/Bookmark</a>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/2009/11/01/grub-2-hangs-on-boot/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
