<?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; Object-Oriented</title>
	<atom:link href="http://jeremysmyth.com/tag/object-oriented/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>
		<item>
		<title>Object-Oriented Perl: Inheritance, and why it&#8217;s not Java</title>
		<link>http://jeremysmyth.com/2008/10/19/object-oriented-perl-inheritance-and-why-its-not-java/</link>
		<comments>http://jeremysmyth.com/2008/10/19/object-oriented-perl-inheritance-and-why-its-not-java/#comments</comments>
		<pubDate>Sun, 19 Oct 2008 21:42:19 +0000</pubDate>
		<dc:creator>Jeremy Smyth</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[Object-Oriented]]></category>

		<guid isPermaLink="false">http://jeremysmyth.com/?p=22</guid>
		<description><![CDATA[I&#8217;m quite a fan of both Java and Perl, for quite opposite reasons; Java is very strict, syntactically, and is rather good in collaborative environments as a result, where Perl is entirely the opposite of strict (even with use strict; in operation), and is therefore a delight to program in.
One thing they share is the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m quite a fan of both Java and Perl, for quite opposite reasons; Java is very strict, syntactically, and is rather good in collaborative environments as a result, where Perl is entirely the opposite of strict (even with <tt>use strict;</tt> in operation), and is therefore a delight to program in.</p>
<p>One thing they share is the ability to work with objects. However, this gives rise to an interesting difference: Java is object-oriented, where Perl is not.</p>
<p>In Java, you call a &#8220;function&#8221; by using its name, just like in any other language.</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="co1">// declare the function</span>
<span class="kw4">double</span> square<span class="br0">&#40;</span><span class="kw4">int</span> x<span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; &nbsp;<span class="kw1">return</span> x <span class="sy0">*</span> x<span class="sy0">;</span>
<span class="br0">&#125;</span>


<span class="co1">//somewhere else, we call it</span>
<span class="kw4">double</span> y <span class="sy0">=</span> square<span class="br0">&#40;</span>x<span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
</pre>
<p>Ditto, Perl:</p>
<pre>
<div class="codesnip-container" >
<div class="perl codesnip" style="font-family:monospace;"><span class="co1"># declare the sub</span>
<span class="kw2">sub</span> square<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; &nbsp;<span class="kw1">my</span> <span class="re0">$x</span> <span class="sy0">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span class="kw3">shift</span></a><span class="sy0">;</span>
&nbsp; &nbsp;<a href="http://perldoc.perl.org/functions/return.html"><span class="kw3">return</span></a> <span class="re0">$x</span> <span class="sy0">*</span> <span class="re0">$x</span><span class="sy0">;</span>
<span class="br0">&#125;</span>

<span class="co1"># somewhere else, we call it</span>
<span class="kw1">my</span> <span class="re0">$y</span> <span class="sy0">=</span> square<span class="br0">&#40;</span><span class="re0">$x</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
</pre>
<p>All very well, until we get into inheritance.</p>
<p><img src="http://jeremysmyth.com/wp-content/uploads/2009/06/perl-oo-1.png" alt="perl-oo-1" title="perl-oo-1" width="189" height="164" class="alignnone size-full wp-image-23" /><br />
In Java, if the function &#8220;square&#8221; is declared in a superclass of the calling code, the above snippet will still work perfectly well; if we run <tt>double y = square(x);</tt> in code in class Banana, it will happily execute <tt>square()</tt> as if it were a local function.</p>
<p>Perl, on the other hand, is fundamentally a procedural language, so it assumes that function calls as shown above are internal to the current package. This means that if the sub &#8220;square&#8221; as shown above is in a superclass of the calling code, it won&#8217;t work.</p>
<p>This is where we have to _tell_ Perl that it&#8217;s using objects.</p>
<p>Firstly, some background. Inheritance in Perl is pretty easy: you create the inheriting package in the usual way, and you populate the @ISA array (pronounced &#8220;is-a&#8221;) with a list of packages to inherit, with the highest priority/affinity first. Yes, Perl supports multiple inheritance, which Java does not. As an example:
<pre>
<div class="codesnip-container" >
<div class="perl codesnip" style="font-family:monospace;"><a href="http://perldoc.perl.org/functions/package.html"><span class="kw3">package</span></a> <span class="st_h">'Banana'</span><span class="sy0">;</span>
<span class="re0">@ISA</span> <span class="sy0">=</span> <span class="br0">&#40;</span><span class="st_h">'Aardvark'</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
</pre>
<p>If package Banana contains the line of code <tt>my $y = square($x);</tt>, it will cause an error on execution, because Perl doesn&#8217;t know that <tt>square</tt> should be treated as an OO function, but rather it looks in the current scope for that function.</p>
<p>Now, if the calling code above is in a _different_ package, and contained an object reference to our package Banana in a variable <tt>$b</tt>, then we could get it to work by doing this:<br />
<tt>$my $y = $b->square($x);</tt></p>
<p>Interestingly, this will work whether <tt>square()</tt> is implemented in A or B. Go figure.</p>
<p>So, to get over this, we call the function via an object reference. In Perl, when an instance method is called (e.g. <tt>$b->square($x);</tt>), the first parameter to the function is the object reference. Conventionally, this is often written into a variable called <tt>$self</tt>. </p>
<p>If we need to call another function from that one, while retaining OO techniques, we use <tt>$self->otherfunction()</tt>. This solves our problem with inheritance, and is not a million miles removed from Java&#8217;s <tt>this</tt> keyword, although is far from implicit; remember, Java is fundamentally object-oriented, where Perl needs to be reminded.</p>
<p>Code samples:</p>
<pre>
<div class="codesnip-container" >
<div class="perl codesnip" style="font-family:monospace;"><a href="http://perldoc.perl.org/functions/package.html"><span class="kw3">package</span></a> Aardvark<span class="sy0">;</span>

<span class="kw2">sub</span> square<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; &nbsp; <span class="kw1">my</span><span class="br0">&#40;</span><span class="re0">$self</span><span class="sy0">,</span> <span class="re0">$x</span><span class="br0">&#41;</span> <span class="sy0">=</span> <span class="co5">@_</span><span class="sy0">;</span>
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span class="kw3">return</span></a> <span class="re0">$x</span> <span class="sy0">*</span> <span class="re0">$x</span><span class="sy0">;</span>
<span class="br0">&#125;</span>

<a href="http://perldoc.perl.org/functions/package.html"><span class="kw3">package</span></a> Banana<span class="sy0">;</span>

<span class="kw2">use</span> Aardvark<span class="sy0">;</span>
<span class="kw1">our</span> <span class="re0">@ISA</span> <span class="sy0">=</span> <span class="br0">&#40;</span><span class="st_h">'Aardvark'</span><span class="br0">&#41;</span><span class="sy0">;</span>


<span class="kw2">sub</span> <span class="kw2">new</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; <span class="kw1">my</span> <span class="re0">$type</span><span class="sy0">=</span><a href="http://perldoc.perl.org/functions/shift.html"><span class="kw3">shift</span></a><span class="sy0">;</span>
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span class="kw3">return</span></a> <a href="http://perldoc.perl.org/functions/bless.html"><span class="kw3">bless</span></a> <span class="br0">&#123;</span><span class="br0">&#125;</span><span class="sy0">,</span> <span class="re0">$type</span><span class="sy0">;</span>
<span class="br0">&#125;</span>

<span class="co1"># test calling an inherited instance method</span>
<span class="kw2">sub</span> printstuff <span class="br0">&#123;</span>
&nbsp; &nbsp; <span class="kw1">my</span> <span class="re0">$self</span><span class="sy0">=</span><a href="http://perldoc.perl.org/functions/shift.html"><span class="kw3">shift</span></a><span class="sy0">;</span>
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span class="kw3">print</span></a> <span class="re0">$self</span><span class="sy0">-&gt;</span><span class="me1">square</span><span class="br0">&#40;</span><span class="nu0">5.2</span><span class="br0">&#41;</span> <span class="sy0">.</span> <span class="st0">&quot;<span class="es0">\n</span>&quot;</span><span class="sy0">;</span>
<span class="br0">&#125;</span>


<a href="http://perldoc.perl.org/functions/package.html"><span class="kw3">package</span></a> main<span class="sy0">;</span>

<span class="kw2">use</span> Banana<span class="sy0">;</span>
<span class="kw1">my</span> <span class="re0">$b</span> <span class="sy0">=</span> Banana<span class="sy0">-&gt;</span><span class="me1">new</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>

<span class="co1"># see if square works when called from Banana</span>
<span class="re0">$b</span><span class="sy0">-&gt;</span><span class="me1">printstuff</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>

<span class="co1"># see if square works when called from here, via Banana</span>
<a href="http://perldoc.perl.org/functions/print.html"><span class="kw3">print</span></a> <span class="re0">$b</span><span class="sy0">-&gt;</span><span class="me1">square</span><span class="br0">&#40;</span><span class="nu0">1.5</span><span class="br0">&#41;</span> <span class="sy0">.</span> <span class="st0">&quot;<span class="es0">\n</span>&quot;</span><span class="sy0">;</span></div>
</div>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://jeremysmyth.com/2008/10/19/object-oriented-perl-inheritance-and-why-its-not-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
