<p>Perl has OOP features, but they are somewhat rarely used. How do you create and use Perl objects with methods and properties?</p>

[{'answer_id': 7960, 'author': 'Bruce Alderman', 'author_id': 311, 'author_profile': 'https://Stackoverflow.com/users/311', 'pm_score': 2, 'selected': False, 'text': '<p>The official tutorial on the <a href="http://search.cpan.org/dist/perl/pod/perltoot.pod" rel="nofollow noreferrer">CPAN site</a> is good.</p>\n\n<p>There\'s also a good article called <a href="http://www.codeproject.com/KB/perl/camel_poop.aspx" rel="nofollow noreferrer">Camel POOP</a> at CodeProject.</p>\n'}, {'answer_id': 8668, 'author': 'Pat', 'author_id': 238, 'author_profile': 'https://Stackoverflow.com/users/238', 'pm_score': 6, 'selected': True, 'text': '<p>You should definitely take a look at <a href="http://search.cpan.org/perldoc?Moose" rel="noreferrer"><code>Moose</code></a>.</p>\n\n<pre><code>package Point;\nuse Moose; # automatically turns on strict and warnings\n\nhas \'x\' =&gt; (is =&gt; \'rw\', isa =&gt; \'Int\');\nhas \'y\' =&gt; (is =&gt; \'rw\', isa =&gt; \'Int\');\n\nsub clear {\n    my $self = shift;\n    $self-&gt;x(0);\n    $self-&gt;y(0);\n}\n</code></pre>\n\n<p>Moose gives you (among other things) a constructor, accessor methods, and type checking for free! </p>\n\n<p>So in your code you can:</p>\n\n<pre><code>my $p = Point-&gt;new({x=&gt;10 , y=&gt;20}); # Free constructor\n$p-&gt;x(15);     # Free setter\nprint $p-&gt;x(); # Free getter\n$p-&gt;clear();\n$p-&gt;x(15.5);   # FAILS! Free type check.\n</code></pre>\n\n<p>A good starting point is <a href="http://search.cpan.org/perldoc?Moose::Manual" rel="noreferrer"><code>Moose::Manual</code></a> and <a href="http://search.cpan.org/perldoc?Moose::Cookbook" rel="noreferrer"><code>Moose::Cookbook</code></a></p>\n\n<p>If you just need the basic stuff you can also use <a href="http://search.cpan.org/perldoc?Mouse" rel="noreferrer"><code>Mouse</code></a> which is not as complete, but without most of the compile time penalty.</p>\n'}, {'answer_id': 36795, 'author': 'Matthew Watson', 'author_id': 3839, 'author_profile': 'https://Stackoverflow.com/users/3839', 'pm_score': 3, 'selected': False, 'text': '<p>Currently I use <a href="http://search.cpan.org/dist/Object-InsideOut/" rel="noreferrer" title="Object::InsideOut">Object::InsideOut</a> whenever I want objects, its quite nice and will give you a lot of features over standard blessed hash objects. Having said that, if I was starting a new project I would seriously look at Moose.</p>\n\n<p>While it is good to read the official PERL documentation, I would <strong>NOT</strong> recommend trying to role your own object framework, or building objects using hashes, its far to tempting to take the easy road and "peak" directly into the objects "private" variables completely breaking encapsulation, this will come back to bite you when you want to refactor the object.</p>\n'}, {'answer_id': 63230, 'author': 'Michael Cramer', 'author_id': 1496728, 'author_profile': 'https://Stackoverflow.com/users/1496728', 'pm_score': 3, 'selected': False, 'text': '<p>Perl objects are NOT just blessed hashes. They are blessed REFERENCES. They can be (and most often are) blessed hash references, but they could just as easily be blessed scalar or array references.</p>\n'}, {'answer_id': 75767, 'author': 'Penfold', 'author_id': 11952, 'author_profile': 'https://Stackoverflow.com/users/11952', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://search.cpan.org/dist/Moose" rel="noreferrer">Moose</a>, definitely.</p>\n\n<pre><code>package Person;\nuse Moose;\nhas age =&gt; ( isa =&gt; Int, is =&gt; \'rw\'); \nhas name =&gt; ( isa =&gt; Str, is =&gt; \'rw\'); \n1;\n</code></pre>\n\n<p>Immediately, you have for free a new() method, and accessor methods for the attributes you just defined with \'has\'. So, you can say:</p>\n\n<pre><code>my $person = Person-&gt;new();\n$person-&gt;age(34);\n$person-&gt;name(\'Mike\');\nprint $person-&gt;name, "\\n";\n</code></pre>\n\n<p>and so on. Not only that, but your accessor methods come type-checked for free (and you can define your own types as well as the standard ones). Plus you get \'extends\' for subclassing, \'with\' for roles/traits, and all manner of other great stuff that allows you to get on with the real job of writing good robust maintainable OO code.</p>\n\n<p>TMTOWTDI, but this one works.</p>\n'}, {'answer_id': 329945, 'author': 'ysth', 'author_id': 17389, 'author_profile': 'https://Stackoverflow.com/users/17389', 'pm_score': 1, 'selected': False, 'text': '<p>On one foot, each class is a package; you establish (multiple, if desired) inheritance by setting the package variable <a href="http://perldoc.perl.org/perltoot.html#Inheritance" rel="nofollow noreferrer">@ISA</a> (preferably at compile time); you create an object from an existing piece of data (often, but not always, an anonymous hash used to store instance variables) with <a href="http://perldoc.perl.org/functions/bless.html" rel="nofollow noreferrer">bless</a>(REFERENCE [, CLASSNAME]); you call object methods like $obj->methodname(@ARGS) and class methods like "CLASSNAME"->methodname(@ARGS).\nMultiple inheritance method resolution order can be altered using <a href="http://search.cpan.org/perldoc/mro" rel="nofollow noreferrer">mro</a>.</p>\n\n<p>Because this is somewhat minimalistic and doesn\'t force encapsulation, there are many different modules that provide more or different functionality.</p>\n'}, {'answer_id': 3339553, 'author': 'mmmpork', 'author_id': 402758, 'author_profile': 'https://Stackoverflow.com/users/402758', 'pm_score': 2, 'selected': False, 'text': '<p>I highly recommend taking a look at Moose if you want to do OO in Perl. However, it\'s not very useful if you don\'t understand what OO in Perl means. To better understand how Perl OO works under the hood, I wrote an overview on my blog: <a href="http://augustinalareina.wordpress.com/2010/06/06/an-introduction-to-object-oriented-perl/" rel="nofollow noreferrer">http://augustinalareina.wordpress.com/2010/06/06/an-introduction-to-object-oriented-perl/</a></p>\n\n<p>From a data structure point of view, an Object is reference with a few extra features. The interpreter knows to treat these special references as Objects because they have been "blessed" with the keyword "bless". Blessed references contain a flag indicating they are an Object. Essentially this means you can define and call methods on them.</p>\n\n<p>For instance if you created a basic hashref, this wouldn\'t work:\n$hashref->foo(); </p>\n\n<p>But if you create a blessed hashref (aka an Object) this does work:\n$blessed_hashref->foo();</p>\n\n<p>Moose is an excellent module for OOP in Perl because it creates an enforceable OO layer AND automagically handles accessor methods so you don\'t have to define a bunch of getters and setters. If you\'re interested in using Devel::Peak to see how the Perl interpreter stores objects, follow the link to the blog entry I posted above. </p>\n'}]
['https://Stackoverflow.com/questions/7885', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/432/']