# Programming with Moose/Problems solved/Accessors

Perl has two aggregate (non-scalar) data types: arrays and hashes (i.e., associative arrays) which are commonly used as the base data structure for objects. A more serious problem is that neither of these data types is an object. For instance, native array and hash support doesn't allow you to receive an event trigger when you write a new key-value pair to a hash, or when you push a new element onto an array. It's possible to use Perl's tie mechanism to get this, but tie comes with a serious performance penalty. Perl does enhance the C-style array of fixed size - a simple contiguous block of memory, addressed via indexing - to allow it to grow or shrink as needed, but chose for reasons of efficiency to implement only basic operations.
One of the two might be better for one task, and not applicable for another, but that doesn't matter. With Moose, your program isn't directly tied to a Perl data structure.

## The old way

Traditionally, Class::Accessor, has done the most for arena, we'll see that soon. The problem it solved was a big and simple one, and it didn't overachieve one bit: if you've got a hash and you don't want a key: foobar to be inserted, how do you go about it? You simply bless the hash into an object and say what accessors should be generated. Now this "hash" acts as watered down model that your module can wrap to achieve the goals of the program. When an unrelated function is called perl realizes it can't resolve the method to a matching sub and errors.

### An Example

Examine the terseness of this code.
Here we create an object, essentially a hash that permits only three keys. The getters and setters for this hash directly correlate to the keys in the underlying perl hash. Remember, that a Perl object is very simple. complexities are left as an exercise to the programmer.

## Invoking the Moose

Truth be told, Moose is more than a replacement for Class::Accessor. So, off the shelf, it won't compete with the simplicity of Class::Accessor. In this section however, we will see Moose's analog.

### An Example

Pretty simple. No syntax thus far really needs explaining -- but in the next section we will anyway.

## New Syntax

For clarity and those who just can't learn by example:

### has

has is one of few Moose keywords, it begins a Moose expression that declares a statement.

### isa

    See also: has
isa in the context of has refers to the TypeConstraint of the object. Moose has a built in type system, and this keyword, which will be explained further in the next chapter specifies what values this attribute is to accept.

### is

    See also: has
is in the context of has refers to the type of accessors to be created. A value of rw will instruct Moose to forge a setter and getter, while a value of ro will only yield a get'er.

## Footnotes

* ^ Unless of course you have explicitly crafted a sub AUTOLOAD, let's not be bothered with this exception though.
* ^ By fixed we mean you can push onto both sides of the array. There is no guarantee new data will be contiguous to the old data. When you program with perl you aren't supposed to think at a low level, don't worry about what happens just know it'll work.
* ^ Of course, with Moose::Tiny you will probably get a more powerful and more terse alternative to Class::Accessor but we will address MooseX:: and alternatives at a later point in this book.