<?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>xterm.it &#187; perl</title>
	<atom:link href="http://www.xterm.it/blog/tag/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xterm.it/blog</link>
	<description>time to think -  by Marco Campana</description>
	<lastBuildDate>Sun, 18 Oct 2009 20:59:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Adding attribute accessors to a Perl class &#8211; rubifying perl</title>
		<link>http://www.xterm.it/blog/2009/02/adding-attribute-accessors-to-a-perl-class-rubifying-perl/</link>
		<comments>http://www.xterm.it/blog/2009/02/adding-attribute-accessors-to-a-perl-class-rubifying-perl/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 22:19:03 +0000</pubDate>
		<dc:creator>marco</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.xterm.it/blog/?p=73</guid>
		<description><![CDATA[I can't help it, even when I'm working with Perl I always think of Ruby The idea is to add to a Perl package behavior similar to a Ruby class, when you can specify attribute accessors (getters and setters) like this:   my $class = shift;   bless {}, $class; ATT ]]></description>
			<content:encoded><![CDATA[<p>This small hack adds attribute accessors to a Perl class. The idea is to add to a Perl package behavior similar to a Ruby class, when you can specify attribute accessors (getters and setters) like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">attr_accessor <span style="color:#ff3333; font-weight:bold;">:attr_name</span></pre></div></div>

<p>Here is the code we have written. Basically all you need to do is add the sub attr_accessor to your Perl class, and then call this method with the attribute you want the getter and setter for.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">package</span> MyPackage<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$class</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">bless</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$class</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> attr_accessor <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$attr</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">eval</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt;ATT;
  sub $attr {
    my \$self = shift;
    my \$value = shift;
    if( \$value ) {
      my \$old_value = \$self-&gt;{ $attr };
      \$self-&gt;{ $attr } = \$value;
      return \$old_value;
      } else {
        return \$self-&gt;{ $attr };
      }
    }
ATT</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
attr_accessor <span style="color: #ff0000;">'name'</span><span style="color: #339933;">;</span></pre></div></div>

<p>In the code above we have added accessors methods for the attribute &#8216;name&#8217;. We can then set and get the &#8216;name&#8217; attribute as in the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$t</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyPackage<span style="color: #339933;">;</span>
<span style="color: #0000ff;">$t</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">name</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'marco'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># setter method</span>
<span style="color: #000066;">print</span> <span style="color: #0000ff;">$t</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">name</span> <span style="color: #666666; font-style: italic;"># this will get and print 'marco'</span></pre></div></div>

<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.xterm.it/blog/2009/02/adding-attribute-accessors-to-a-perl-class-rubifying-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>London, the Perl city</title>
		<link>http://www.xterm.it/blog/2009/01/london-the-perl-city/</link>
		<comments>http://www.xterm.it/blog/2009/01/london-the-perl-city/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 22:47:35 +0000</pubDate>
		<dc:creator>marco</dc:creator>
				<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://www.xterm.it/blog/?p=54</guid>
		<description><![CDATA[If you are a Perl developer (or just passionate about this language), then London is definitely the city to live in The Perl community in the city is quite active, as you can see from the number of free events available every couple of months. ]]></description>
			<content:encoded><![CDATA[<p>If you are a Perl developer (or just passionate about this language), then London is definitely the city to live in. The Perl community in the city is quite active, as you can see from the number of free events available every couple of months. Last November I attended the London Perl Workshop, where a former colleague from Yahoo was giving some talks about <a href="http://www.iinteractive.com/moose/">Moose</a>. The next month, February 19th, I will attend the <a title="London.pm technical talk" href="http://londonpmtech.appspot.com/">London.pm technical meeting</a> where, guess what, <a title="Moose a postmodern object system" href="http://www.iinteractive.com/moose/">Moose</a> will be the star again. The event will take place at the BBC&#8217;s offices near White City from 7pm to 9pm. If you are in town and you want to join, make sure to register as places are limited.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.xterm.it/blog/2009/01/london-the-perl-city/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
