The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

decorators::providers::constructor - A set of decorators to generate BUILDARG methods

VERSION

version 0.01

SYNOPSIS

  use decorators ':constructor';

  # accepts /no/ arguments
  sub BUILDARGS : strict;

  # accept *only* the following arguments
  sub BUILDARGS : strict(
      foo  => _foo,      # required key
      bar? => _bar       # optional
      baz? => super(baz) # delegate to the superclass
  );

DESCRIPTION

strict( arg_key => slot_name, ... )

This is a trait that is exclusively applied to the BUILDARGS method. This is a means for generating a strict interface for the BUILDARGS method that will map a set of constructor parameters to a set of given slots. This is useful for maintaining encapsulation for things like a private slot with a different public name.

    # declare a slot with a private name
    use slots (_bar => sub {});

    # map the `foo` key to the `_bar` slot
    sub BUILDARGS : strict( foo => _bar );

All other parameters will be rejected and an exception thrown. If you wish to have an optional parameter, simply follow the parameter name with a question mark, like so:

    # declare a slot with a private name
    use slots (_bar => sub {});

    # the `foo` key is optional, but if
    # given, will store in the `_bar` slot
    sub BUILDARGS : strict( foo? => _bar );

If you wish to accept parameters for your superclass's constructor but do not want to specify storage location because of encapsulation concerns, simply use the super designator, like so:

    # map the `foo` key to the local `_bar` slot
    # with the `bar` key, let the superclass decide ...
    sub BUILDARGS : strict(
        foo => _bar,
        bar => super(bar)
    );

If you wish to have a constructor that accepts no parameters at all, then simply do this.

    sub BUILDARGS : strict;

And the constructor will throw an exception if any arguments at all are passed in.

AUTHOR

Stevan Little <stevan@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2018 by Stevan Little.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 227:

You forgot a '=back' before '=head1'