Acme-Has-Tiny

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

NAME
    Acme::Has::Tiny - tiny implementation of Moose-like "has" keyword

SYNOPSIS
       package Person;
   
       use Acme::Has::Tiny qw(new has);
       use Types::Standard -types;
   
       has name => (isa => Str);
       has age  => (isa => Num);

DESCRIPTION
    Acme::Has::Tiny provides a Moose-like `has` function. It is not
    particularly full-featured, providing just enough to be useful for small
    OO projects.

    Generally speaking, I'd recommend using Moo or Moose instead, but if you
    want to use this then I'm fairly unlikely to hunt you down with dogs.

    This module was originally written for Type::Tiny, but turned out to be
    just a smidgen slower than the system it was replacing, so was abandoned.

  Methods
    `has \@attrs, %spec`
    `has $attr, %spec`
        Create an attribute. This method can also be exported as a usable
        function.

        The specification supports the following options:

        `is => "ro" | "rw" | "rwp"`
            Defaults to "ro".

        `required => 1`
        `default => $coderef`
            Defaults are always eager (not lazy).

        `builder => $coderef | $method_name | 1`
            Builders are always lazy.

        `predicate => $method_name | 1`
        `isa => $type`
            Type constraint (use Types::Standard or another
            Type::Library-based type constraint library).

    `create_constructor $method_name, %options`
        If you want a constructor, then you could call this after defining
        your attributes. (Or you could just import `new` from this module.)

           package Person;
   
           use Acme::Has::Tiny qw(has);
           use Types::Standard -types;
   
           has name => (isa => Str);
           has age  => (isa => Num);
   
           Acme::Has::Tiny->create_constructor("new");
           Acme::Has::Tiny->create_constructor(
              "new_from_arrayref",
              buildargs => sub {
                 my ($class, $aref) = @_;
                 return { name => $aref->[0], age => $aref->[1] };
              },
           );

        Currently supported options:

        `buildargs => $coderef | $method_name`
        `build => $coderef | $method_name`
        `class => $class_name`
            Package to build a constructor for; if omitted, uses the caller.

        `replace => $bool`
            Allow `create_constructor` to overwrite an existing method.

        There's no law that says you have to use `create_constructor`. You can
        write your own constructor if you like. In which case, you might like
        to make use of...

    `assert_valid($class, \%params)`
        Check that a hash of parameters is valid according to type constraints
        and required attributes of $class and any classes it inherits from.

        Returns the hashref or dies.

           sub new {
              my ($class, %params) = @_;
              ...; # other stuff here
              my $self = bless(
                 Acme::Has::Tiny->assert_valid($class, \%params),
                 $class,
              );
              ...; # other stuff here
              return $self;



( run in 1.600 second using v1.01-cache-2.11-cpan-d8267643d1d )