Looks like we’re rolling with Moose…

After the nth person telling me to try Moose, I finally gave in.

The first thing I wanted to know was how to handle private variables, hash table properties, and object type properties. The Moose Quick-Ref Card lists the built-in Data Type constraints. I think Randal L. Schwartz has written the definitive introduction, though. (His follow up goes into more detail about custom types….)

We can see below that the read-only field on the Example object needed to have a writer defined for it. Even though the method is named private_set_x, it can still be called from the outside world. Also, we can set the “read only” member via the constructor hash! Hash and object property types seem to work just fine. The only real trick there was to use an in-line sub to initialize them.

package Example;
use Moose;

has 'x' => (isa => 'Int', is => 'ro', writer => 'private_set_x', default => 21);

sub test {
    my $self = shift;
    my $x = $self->x();
    print "My x is currently a $x\n";

sub setit {
    my $self = shift;

package Another;
use Moose;
extends 'Example';

has 'hashtest' => (isa => 'HashRef', is => 'rw', default => sub { my %hash; return \%hash; } );
has 'exampletest' => (isa => 'Example', is => 'rw', default => sub { Example->new(); } );

after 'setit' => sub {
    my $self = shift;
    print "The after method is getting called now.\n";

my $thing = Example->new();
$thing->test();  #My x is currently a 21
$thing->test();  #My x is currently a 42
$thing->test();  #My x is currently a 84

my $otherthing = Example->new(x => 168);
$otherthing->test();  # My x is currently a 168

my $another = Another->new();
$another->setit();  # The after method is getting called now.

my $h = $another->hashtest();
$h->{"One"} = "Hello";
my $v = $another->hashtest()->{"One"};
print "The hash value is $v\n";  # The hash value is Hello

my $e = $another->exampletest();
$e->test();  # My x is currently a 21

One Response to “Looks like we’re rolling with Moose…”

  1. draegtun Says:

    Moose is definitely the way to go. However understanding how normal Perl OO works will help provide more insight into how it all hangs together.

    Perl/Moose don’t really provide mechanisms for private methods & variables. However the Perl convetions are prefix private methods with “_” ( eg. $self->_private_set_x(42); ) and to use closures if you really need your variables to be private.

    It confused me when I first looked at Moose but the only difference (that I know of!) between is => 'ro' & is => 'rw' is that the second one produces a write accessor.

    Simple package to explain this….

    package OO;
    use Moose;
    has 'ro' => ( isa => 'Int', is => 'ro' );
    has 'rw' => ( isa => 'Int', is => 'rw' );
    no Moose;

    Below you can see what happens…..

    use OO;
    my $oo = OO->new( ro => 100, rw => 200 );
    say $oo->ro;        # => 100
    say $oo->rw;        # => 200
    $oo->ro( 101 );     # => throws read-only accessor error
    $oo->rw( 201 );     # => 201
    $oo->{ ro } = 102;  # => 102
    $oo->{ rw } = 202;  # => 202


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: