Squatting::H - a slot-based object that's vaguely reminiscent of Camping::H


Behold, a glorified hashref that you can treat like an object:

  my $cat = Squatting::H->new({
    name => 'kurochan',
    meow => sub { "me" . "o" x length($_[0]->name) . "w" }
  my $kitty = $cat->clone({ name => 'max' });

  $cat->name;                     # "kurochan"
  $kitty->name;                   # "max"
  $cat->meow;                     # "meoooooooow"
  $kitty->meow;                   # "meooow"
  $cat->age(3);                   # 3
  $kitty->age(2);                 # 2
  $kitty->slots;                  # qw(name meow age)


This module implements a simple slot-based object system. Objects in this system are blessed hashrefs whose keys (aka slots) can be accessed by calling methods with the same name as the key. You can also assign coderefs to a slot which will let you define custom methods for an object.

This object system does not implement inheritance, but you can create derivatives of an object using the clone() method which creates a deep copy of your object.


Object Construction

$object = Squatting::H->new(\%attributes)

This method is used to construct a new object. A hashref of attributes may be passed to this method to initialize the object. A shallow copy of \%attributes will then be created and blessed before being returned.

$object = Squatting::H->bless(\%attributes)

This is like new(), but it doesn't bother making a shallow copy of \%attributes.

$object = $object->extend(\%attributes)

This method will add new attributes to an object. If the attributes already existed, the new values will replace the old values.

$clone = $object->clone(\%attributes)

This method will create a deep clone of the object. You may also pass in a hashref of attributes that the cloned object should have.



UNIVERSAL::can has been overridden to be aware of the conventions used by Squatting::H objects. If a slot has been defined for the method that's passed in, this method will return true.

@slot_names = $object->slots;

This method gives you a list of all the slots that have been defined for this object. It's essentially the same as saying:

  keys %$object

$value = $object->$slot

$value = $object->$slot($value)

This method lets you get and set the value of a slot.

  $object->foo;         # 5

If you pass in a coderef, it'll be treated as a method for your object.

  $object->double(sub {
    my ($self, $x) = @_;
    $x * 2;
  $object->double(16)   # 32