in reply to OO best practice basic questions

First of all, is there a preferred style for "new" constructors? Specifically one which will be taking hash value/key pairs as attributes?

A summary of Perl Best Practices (TM) can be found on the Best Practices(TM) reference card. It does not make any pronouncements on the argument list for a constructor. The key-value syntax is a good choice, but you probably want to include a mod 2 test (die "Uneven list" if @_ % 2) to make sure the list is actually balanced. A single hash-ref is also a popular choice. See Moose::Manual for how the most standard issue object object system does it.

Secondly, is there a way to tell how many objects have been created/exist in a particular class?
You could add a counter to your constructor, and then provide an accessor to the counter, e.g.
my $obj_count = 0; sub new { my $class=shift; my %params=@_; my $self={}; $self->{$_}=$params{$_} foreach keys(%params); bless($self, $class); $obj_count++; return $self; } sub get_count { return $obj_count; }
with the possibility of also adding a decrement to the destructor.
And lastly, what is the best way to access individual object attributes? Always through a method or can you access them directly?
This one, best practices does weigh in on. Always through a accessor. That allows for a layer of abstraction between the object itself, makes you insensitive to implementation changes, and allows for side-effects and internal mutations of the class if the designer needs them.

Side note: if this is not just a learning exercise, consider using a declarative object system, like Moose or one of its lighter-weight derivatives.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: OO best practice basic questions
by Amblikai (Scribe) on Aug 28, 2014 at 10:09 UTC

    Thanks, i've added the uneven list test into my constructor.

    When using the obj_count, is there a way to make it a method of the object? I don't know if that makes sense but in order to call it i guess i have to use Numbers::get_count() for example. But then if i attach it to each object i'm guessing the object will only have the value at that time, so i'd have to be sure to only check the value in the last object created. Is that right?

    Another question i have is with the value "result"

    It is an internally created value. So do i initialise the value in the constructor? Or the first time it is used? For example if i call the method add_nums, the result is stored in result. Should that variable already exist?

    What we be an example of a destructor?

    Thanks!

      is there a way to make it a method of the object?
      Why not, just try
      $obj->ob_count;
      i guess i have to use Numbers::get_count()

      No, the class methods are usually called like

      my $count = 'Numbers'->get_count;

      It just returns a value of a lexical variable in the package, so it's not attached to an object, but to the class.

      Regarding the result: it depends. The direct initialization is usually used when it's cheap or when you know you'll use the value sooner or later. For the other cases, the "lazy" approach is more appropriate, i.e. create the result only when asked to do so.

      What we be an example of a destructor?
      In this case, you should probably do
      sub DESTROY { $obj_count-- }

      Note that you don't have to call $obj->DESTROY, you just undef $obj.

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ