I've been doing Perl for a while now, and one of the things I've meant to get to grips with, is 'object oriented' Perl. I think I'm getting there now, so I'm writing up what I've got so far in the hopes that it'll be a helpful reference.

First, the basics - object oriented languages encapsulate code into 'objects'. The object has an internal state, and it's own 'code' for handling that state.

The reason that's useful, is because if I write a module for you to use - then all you need to know is how to run the internal code, and don't have to worry about what goes on inside.

That's why it's widely used in modules on CPAN, and chances are pretty good that you've already used it if you've used any CPAN modules.

The way perl 'does' objects, is that it treats each object as a hash. The 'attributes' or 'internal state' are hash keys.

As a start point (save as 'MyObject.pm')

#!/usr/bin/perl use strict; use warnings; package MyObject; sub new { print "method 'new' called with parameters: ", join ("\n", @_ ), "\n +"; my ( $class, @args ) = @_; my $self = {}; $self -> {state} = "newly created"; bless $self, $class; return $self; } sub set_state { my ( $self, $new_state ) = @_; $self -> {state} = $new_state; } sub get_state { my ( $self ) = @_; return $self -> {state}; }

You can then 'drive' this module with:

# use_oo.pl #!/usr/bin/perl use strict; use warnings; use MyObject my $object_instance = MyObject -> new(); print "Object is: ", $object_instance,"\n"; $object_instance -> set_state ( "here is a new state" ); print "Object state: ", $object_instance -> get_state(),"\n";

It's fairly simple, but what it means is that I can 'build in' all sorts of code and state variables into 'MyObject' - for example, validating when you 'set state' to ensure that it's not forcing something odd to happen. But this can happen transparently, and your code snippet doesn't actually need to worry about it.

What you may not have seen before is the 'bless' keyword - when you create '$self' then you create a hash reference - which you could (and essentially, do) treat it as any other hash reference. What 'bless' does is 'mark' the reference as a specific type of object. So you'll see when you print '$object_instance' that it looks something like:

MyObject=HASH(x012345)

That's key, because it lets perl identify where it should look when you use a method - e.g. 'get_state' - it knows to look inside the 'MyObject' module explicitly.

$object_instance -> set_state ( "here is a new state" );

is translated by perl (because it knows it's a 'MyObject' because of that 'bless') into:

MyObject::set_state ( $object_instance, "here is a new state" );

(Passing $object_instance is necessary, because that has it's own internal state, and we need to know which internal state to modify).

I wouldn't say it's something I'll always use as a technique, but it's a useful extra tool - if I've got a set of scripts that all do similar and related things, then there may well be value in creating a module for them to all use... and if you do, it's certainly worth considering using an object oriented module instead.


In reply to Object Oriented Perl - very basic guide by Preceptor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.