Object::Pad::FieldAttr::LazyInit(3pm)

NAME
"Object::Pad::FieldAttr::LazyInit" - lazily initialise "Object::Pad" fields at first read

SYNOPSIS
   use Object::Pad;
   use Object::Pad::FieldAttr::LazyInit;

   class Item {
      field $uuid :reader :param :LazyInit(_make_uuid);

      method _make_uuid {
         require Data::GUID;
         return Data::GUID->new->as_string;
      }
   }

DESCRIPTION
This module provides a third-party field attribute for Object::Pad-based classes, which declares that the field it is attached to has a lazy initialisation method, which will be called the first time the field's value is read from.

WARNING The ability for Object::Pad to take third-party field attributes is still new and highly experimental, and subject to much API change in future. As a result, this module should be considered equally experimental.

FIELD ATTRIBUTES

  :LazyInit
   field $name :LazyInit(NAME) ...;

Declares that if the field variable is read from before it has been otherwise initialised, then the named method will be called first to create an initial value for it. Initialisation by either by a ":param" declaration, explicit assignment into it, or the use of a ":writer" accessor will set the value for the field and mean the lazy initialiser will not be invoked.

After it has been invoked, the value is stored by the field and thereafter it will behave as normal; subsequent reads will return the current value, and the initialiser method will not be invoked again.

In order to avoid the possibility of accidental recursion when generating the value, it is recommended that the logic in the lazy initialisation method be as self-contained as possible; ideally not invoking any methods on $self, and only making use of other fields already declared before the field being initialised. By placing the initialiser method immediately after the field declaration, before any other fields, you can reduce the possibility of getting stuck in such a manner.

   field $field_zero :param;

   field $field_one :LazyInit(_make_one);
   method _make_one {
      # we can safely use $field_zero in here
   }

   field $field_two :LazyInit(_make_two);
   method _make_two {
      # we can safely use $field_zero and $field_one
   }

AUTHOR
Paul Evans <leonerd@leonerd.org.uk>