An OO module *is* a library. The words module and library can be used for each other interchangeably. For OO, you must have a module/library file. This file contains the "class" that your objects will be blessed into. To make things more confusing, a class is called a "Package" in Perl. In the below example, you'll see what I mean.

new() is a class method that instantiates, configures and returns an object of the class. In its most basic form, it can look like this: sub new {bless {}, shift;}. That blesses a hash reference into the class, and implicitly returns the new object. You can use any name for this method as you like, but the de-facto unwritten rule standard is and always has been new(). I know some devs who use init() or other things, but because of the unforced standard, I always expect to see a new() method if I'm looking at someone elses code.

bless() is the class method that actually makes a standard perl reference type an object of your class. It provides you the ability to call methods against it etc. This method is internal to perl, and can not be changed. You can bless any reference type you want. The de-facto standard is a hash reference, but there are a few reasons why you'd want to use other types. Hashes make it easy to use the attributes/members of the object (example code below unverified and untested):

package My::Class; use warnings; use strict; use Carp qw(croak); sub new { my ($class, %args) = @_; my $self = bless {%args}, $class; $self->_validate_args; return $self; } sub swear { my ($self, $word) = @_; croak "swear() needs word param sent in" if ! defined $word; if ($self->can_swear) { print "$word\n"; } } sub can_swear { # getter/setter, and returns the value of the 'can_swear' attribut +e. # This attribute determines whether the object is allowed to use f +oul language my ($self, $can_swear) = @_; $self->{can_swear} = $can_swear if defined $can_swear; # If can_swear wasn't set via this method, or wasn't set # within the %args parameters hash to new(), we default # to not allowing the object to swear return $self->{can_swear} // 0; } sub _validate_args { my ($self) = @_; if (! exists $self->{not_provided_in_new_args}) { # do stuff } }

Script:

use My::Class; my %args = ( swear => 1, emphasis => 1, }; my $object = My::Class->new(%args); $object->swear('damnit');

$self is the actual object itself. We (pretty well) always refer to an object of the class as self while working with it within the class. Again, this is not a hard rule, but just stick with it so others reading your code won't be confused.

When you call an object's method, the object is inserted at the beginning of the parameter list into the method. This happens automatically behind the scenes. So this:

$object->swear('damnit');

Is converted to:

swear($object, 'damnit');

When our swear() method is called, we shift off the object and the swear word into the variables $self and $word. We're then able to call other methods or do other work on the $self object. Here's what the above looks like when I display the contents of the @_ array in the method. The @_ array contains the full parameter list to the method:

$VAR1 = [ bless( {}, 'My::Class' ), 'damnit', ];

Another note... methods that begin with an underscore are "private" methods. Ones that typically do maintenance and validation type work, or are chunks of larger public methods that perform specific pieces of functionality. These methods are not designed for use by the users of your objects. An example is the _validate_args() in the above example. This rule is also unenforced, but is a very long-standing standard. Try not to use common names for these, use long, descriptive ones.


In reply to Re: OO modules by stevieb
in thread OO modules by BernieC

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.