Personally, I think one of the reasons Perl 5 OO has such a bad name is that you have to make such "manual" descisions yourself. In most other OO languages, these kind of descisions are made for you. This is excactly why I wrote Moose.
In this example, the a default constructor is already available for you (from Moose::Object), instance attributes are managed by the underlying metaclass (as they are in most all other OO languages) which means that and an accessor for the "time" slot has been created (which is read/write, will only accept "DateTime" objects, and has a default value of "DateTime->now").package Stopwatch; use strict; use warnings; use Moose; use DateTime; has 'timer' => ( is => 'rw', isa => 'DateTime', default => sub { DateTime->now } ); sub diff { my $self = shift; $self->time - DateTime->now; } # ... my $sw = Stopwatch->new; # ... wait a moment print $sw->diff;
The ugly details of how all this happens should be of no concern for your everyday programmer. The detail of the instance storage (it actually is a blessed HASH) should be of no conern to your everyday programmer either. In short, it should all Just Work.
In reply to Re: Your favorite objects NOT of the hashref phylum
by stvn
in thread Your favorite objects NOT of the hashref phylum
by blogical
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |