<?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>Jeremy Smyth's Blog &#187; Private</title>
	<atom:link href="http://jeremysmyth.com/tag/private/feed/" rel="self" type="application/rss+xml" />
	<link>http://jeremysmyth.com</link>
	<description></description>
	<lastBuildDate>Tue, 10 Aug 2010 12:07:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Faking &#8220;Private&#8221; methods with Perl</title>
		<link>http://jeremysmyth.com/2008/12/14/faking-private-methods-with-perl/</link>
		<comments>http://jeremysmyth.com/2008/12/14/faking-private-methods-with-perl/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 17:03:10 +0000</pubDate>
		<dc:creator>Jeremy Smyth</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Object-Oriented]]></category>
		<category><![CDATA[Private]]></category>

		<guid isPermaLink="false">http://jeremysmyth.com/?p=28</guid>
		<description><![CDATA[Although Perl doesn&#8217;t really do the whole OO concept of private/public access modifiers, it&#8217;s somewhat possible to approximate them using subroutine references.


my $print_rev = sub &#123; print scalar reverse $_&#91;0&#93;; &#125;;


Now we have a lexically scoped variable, visible only within its package, containing a reference to an anonymous sub.
Within our class/package, we call it by [...]]]></description>
			<content:encoded><![CDATA[<p>Although Perl doesn&#8217;t really do the whole OO concept of private/public access modifiers, it&#8217;s somewhat possible to approximate them using subroutine references.</p>
<pre>
<div class="codesnip-container" >
<div class="perl codesnip" style="font-family:monospace;"><span class="kw1">my</span> <span class="re0">$print_rev</span> <span class="sy0">=</span> <span class="kw2">sub</span> <span class="br0">&#123;</span> <a href="http://perldoc.perl.org/functions/print.html"><span class="kw3">print</span></a> <a href="http://perldoc.perl.org/functions/scalar.html"><span class="kw3">scalar</span></a> <a href="http://perldoc.perl.org/functions/reverse.html"><span class="kw3">reverse</span></a> <span class="co5">$_</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span><span class="sy0">;</span> <span class="br0">&#125;</span><span class="sy0">;</span></div>
</div>
</pre>
<p>Now we have a lexically scoped variable, visible only within its package, containing a reference to an anonymous sub.</p>
<p>Within our class/package, we call it by doing something like:</p>
<pre>
<div class="codesnip-container" >
<div class="perl codesnip" style="font-family:monospace;"><span class="re0">&amp;$print_rev</span><span class="br0">&#40;</span><span class="st0">&quot;Reversed!&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
</pre>
<p>Voila! Instant private method!</p>
<p>note: this is partway to creating closures, which are functions declared at runtime with, with variables passed as parameter. I might mention them some other time.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeremysmyth.com/2008/12/14/faking-private-methods-with-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
