Data::Object::Attributes

Attribute Builder for Perl 5

package Example;

use Moo;

use Data::Object::Attributes;

has 'data';

package main;

my $example = Example->new;

This package provides options for defining class attributes. Specifically, this package wraps the has attribute keyword and adds shortcuts and enhancements. If no directives are specified, the attribute is declared as read-write and optional.

This package supports the has keyword function and all of its configurations. See the Moo documentation for more details.

package Example::Has;

use Moo;

has 'data' => (
  is => 'ro',
  isa => sub { die }
);

package Example::HasData;

use Moo;

use Data::Object::Attributes;

extends 'Example::Has';

has '+data' => (
  is => 'ro',
  isa => sub { 1 }
);

package main;

my $example = Example::HasData->new(data => time);

This package supports the is directive, used to denote whether the attribute is read-only or read-write. See the Moo documentation for more details.

package Example::HasIs;

use Moo;

use Data::Object::Attributes;

has data => (
  is => 'ro'
);

package main;

my $example = Example::HasIs->new(data => time);

This package supports the isa directive, used to define the type constraint to validate the attribute against. See the Moo documentation for more details.

package Example::HasIsa;

use Moo;
use registry;

use Data::Object::Attributes;

has data => (
  is => 'ro',
  isa => 'Str' # e.g. Types::Standard::Str
);

package main;

my $example = Example::HasIsa->new(data => time);

This package supports the req and required directives, used to denote if an attribute is required or optional. See the Moo documentation for more details.

package Example::HasReq;

use Moo;

use Data::Object::Attributes;

has data => (
  is => 'ro',
  req => 1 # required
);

package main;

my $example = Example::HasReq->new(data => time);

This package supports the opt and optional directives, used to denote if an attribute is optional or required. See the Moo documentation for more details.

package Example::HasOpt;

use Moo;

use Data::Object::Attributes;

has data => (
  is => 'ro',
  opt => 1
);

package main;

my $example = Example::HasOpt->new(data => time);

This package supports the bld and builder directives, expects a 1, a method name, or coderef and builds the attribute value if it wasn't provided to the constructor. See the Moo documentation for more details.

package Example::HasBld;

use Moo;
use routines;

use Data::Object::Attributes;

has data => (
  is => 'ro',
  bld => 1
);

method _build_data() {

  return time;
}

package main;

my $example = Example::HasBld->new;

This package supports the clr and clearer directives expects a 1 or a method name of the clearer method. See the Moo documentation for more details.

package Example::HasClr;

use Moo;

use Data::Object::Attributes;

has data => (
  is => 'ro',
  clr => 1
);

package main;

my $example = Example::HasClr->new(data => time);

# $example->clear_data;

This package supports the crc and coerce directives denotes whether an attribute's value should be automatically coerced. See the Moo documentation for more details.

package Example::HasCrc;

use Moo;

use Data::Object::Attributes;

has data => (
  is => 'ro',
  crc => sub {'0'}
);

package main;

my $example = Example::HasCrc->new(data => time);

This package supports the def and default directives expects a non-reference or a coderef to be used to build a default value if one is not provided to the constructor. See the Moo documentation for more details.

package Example::HasDef;

use Moo;

use Data::Object::Attributes;

has data => (
  is => 'ro',
  def => '0'
);

package main;

my $example = Example::HasDef->new;

This package supports the mod and modify directives denotes whether a pre-existing attribute's definition is being modified. This ability is not supported by the Moo object superclass.

package Example::HasNomod;

use Moo;

use Data::Object::Attributes;

has data => (
  is => 'rw',
  opt => 1
);

package Example::HasMod;

use Moo;

use Data::Object::Attributes;

extends 'Example::HasNomod';

has data => (
  is => 'ro',
  req => 1,
  mod => 1
);

package main;

my $example = Example::HasMod->new;

This package supports the hnd and handles directives denotes the methods created on the object which dispatch to methods available on the attribute's object. See the Moo documentation for more details.

package Example::Time;

use Moo;
use routines;

method maketime() {

  return time;
}

package Example::HasHnd;

use Moo;

use Data::Object::Attributes;

has data => (
  is => 'ro',
  hnd => ['maketime']
);

package main;

my $example = Example::HasHnd->new(data => Example::Time->new);

This package supports the lzy and lazy directives denotes whether the attribute will be constructed on-demand, or on-construction. See the Moo documentation for more details.

package Example::HasLzy;

use Moo;

use Data::Object::Attributes;

has data => (
  is => 'ro',
  def => sub {time},
  lzy => 1
);

package main;

my $example = Example::HasLzy->new;

This package supports the new directive, if truthy, denotes that the attribute will be constructed on-demand, i.e. is lazy, with a builder named new_{attribute}. This ability is not supported by the Moo object superclass.

package Example::HasNew;

use Moo;
use routines;

use Data::Object::Attributes;

has data => (
  is => 'ro',
  new => 1
);

fun new_data($self) {

  return time;
}

package main;

my $example = Example::HasNew->new(data => time);

This package supports the pre and predicate directives expects a 1 or a method name and generates a method for checking the existance of the attribute. See the Moo documentation for more details.

package Example::HasPre;

use Moo;

use Data::Object::Attributes;

has data => (
  is => 'ro',
  pre => 1
);

package main;

my $example = Example::HasPre->new(data => time);

This package supports the rdr and reader directives denotes the name of the method to be used to "read" and return the attribute's value. See the Moo documentation for more details.

package Example::HasRdr;

use Moo;

use Data::Object::Attributes;

has data => (
  is => 'ro',
  rdr => 'get_data'
);

package main;

my $example = Example::HasRdr->new(data => time);

This package supports the tgr and trigger directives expects a 1 or a coderef and is executed whenever the attribute's value is changed. See the Moo documentation for more details.

package Example::HasTgr;

use Moo;
use routines;

use Data::Object::Attributes;

has data => (
  is => 'ro',
  tgr => 1
);

method _trigger_data() {
  $self->{triggered} = 1;

  return $self;
}

package main;

my $example = Example::HasTgr->new(data => time);

This package supports the use directive denotes that the attribute will be constructed on-demand, i.e. is lazy, using a custom builder meant to perform service construction. This directive exists to provide a simple dependency injection mechanism for class attributes. This ability is not supported by the Moo object superclass.

package Example::HasUse;

use Moo;
use routines;

use Data::Object::Attributes;

has data => (
  is => 'ro',
  use => ['service', 'time']
);

method service($type, @args) {
  $self->{serviced} = 1;

  return time if $type eq 'time';
}

package main;

my $example = Example::HasUse->new;

This package supports the wkr and weak_ref directives is used to denote if the attribute's value should be weakened. See the Moo documentation for more details.

package Example::HasWkr;

use Moo;

use Data::Object::Attributes;

has data => (
  is => 'ro',
  wkr => 1
);

package main;

my $data = do {
  my ($a, $b);

  $a = { time => time };
  $b = { time => $a };

  $a->{time} = $b;
  $a
};

my $example = Example::HasWkr->new(data => $data);

This package supports the wrt and writer directives denotes the name of the method to be used to "write" and return the attribute's value. See the Moo documentation for more details.

package Example::HasWrt;

use Moo;

use Data::Object::Attributes;

has data => (
  is => 'ro',
  wrt => 'set_data'
);

package main;

my $example = Example::HasWrt->new;

42 POD Errors

The following errors were encountered while parsing the POD:

Around line 10:

Unknown directive: =name

Around line 16:

Unknown directive: =abstract

Around line 22:

Unknown directive: =synopsis

Around line 38:

Unknown directive: =description

Around line 47:

Unknown directive: =scenario

Around line 52:

Unknown directive: =example

Around line 82:

Unknown directive: =scenario

Around line 87:

Unknown directive: =example

Around line 105:

Unknown directive: =scenario

Around line 111:

Unknown directive: =example

Around line 131:

Unknown directive: =scenario

Around line 137:

Unknown directive: =example

Around line 156:

Unknown directive: =scenario

Around line 162:

Unknown directive: =example

Around line 181:

Unknown directive: =scenario

Around line 187:

Unknown directive: =example

Around line 212:

Unknown directive: =scenario

Around line 218:

Unknown directive: =example

Around line 239:

Unknown directive: =scenario

Around line 245:

Unknown directive: =example

Around line 264:

Unknown directive: =scenario

Around line 270:

Unknown directive: =example

Around line 289:

Unknown directive: =scenario

Around line 295:

Unknown directive: =example

Around line 328:

Unknown directive: =scenario

Around line 334:

Unknown directive: =example

Around line 363:

Unknown directive: =scenario

Around line 369:

Unknown directive: =example

Around line 389:

Unknown directive: =scenario

Around line 395:

Unknown directive: =example

Around line 420:

Unknown directive: =scenario

Around line 426:

Unknown directive: =example

Around line 445:

Unknown directive: =scenario

Around line 451:

Unknown directive: =example

Around line 470:

Unknown directive: =scenario

Around line 476:

Unknown directive: =example

Around line 502:

Unknown directive: =scenario

Around line 510:

Unknown directive: =example

Around line 536:

Unknown directive: =scenario

Around line 542:

Unknown directive: =example

Around line 571:

Unknown directive: =scenario

Around line 577:

Unknown directive: =example