Coder Social home page Coder Social logo

moox-const's Introduction

NAME

MooX::Const - Syntactic sugar for constant and write-once Moo attributes

VERSION

version v0.6.2

SYNOPSIS

use Moo;
use MooX::Const;

use Types::Standard -types;

has thing => (
  is  => 'const',
  isa => ArrayRef[HashRef],
);

DESCRIPTION

This is syntactic sugar for using Types::Const with Moo. The SYNOPSIS above is equivalent to:

use Types::Const -types;

has thing => (
  is     => 'ro',
  isa    => Const[ArrayRef[HashRef]],
  coerce => 1,
);

It modifies the has function to support "const" attributes. These are read-only ("ro") attributes for references, where the underlying data structure has been set as read-only.

This will return an error if there is no "isa", the "isa" is not a Type::Tiny type, if it is not a reference, or if it is blessed object.

Simple value types such as Int or Str are silently converted to read-only attributes.

As of v0.5.0, it also supports write-once ("once") attributes for references:

has setting => (
  is  => 'once',
  isa => HashRef,
);

This allows you to set the attribute once. The value is coerced into a constant, and cannot be changed again.

Note that "wo" is a removed synonym for "once". It no longer works in v0.6.0, since "wo" is used for "write-only" in some Moose-like extensions.

As of v0.4.0, this now supports the strict setting:

has thing => (
  is     => 'const',
  isa    => ArrayRef[HashRef],
  strict => 0,
);

When this is set to a false value, then the read-only constraint will only be applied when running in strict mode, see Devel::StrictMode.

If omitted, strict is assumed to be true.

KNOWN ISSUES

Accessing non-existent keys for hash references will throw an error. This is a feature, not a bug, of read-only hash references, and it can be used to catch mistakes in code that refer to non-existent keys.

Unfortunately, this behaviour is not replicated with array references.

See Types::Const for other known issues related to the Const type.

Using with Moose and Mouse

This module appears to work with Moose, and there is now a small test suite.

It does not work with Mouse. Pull requests are welcome.

SUPPORT FOR OLDER PERL VERSIONS

Since v0.6.0, the this module requires Perl v5.14 or later.

Future releases may only support Perl versions released in the last ten years.

If you need this module on Perl v5.10, please use one of the v0.5.x versions of this module. Significant bug or security fixes may be backported to those versions.

SEE ALSO

Const::Fast

Devel::StrictMode

Moo

MooseX::SetOnce

Sub::Trigger::Lock

Types::Const

Type::Tiny

SOURCE

The development version is on github at https://github.com/robrwo/MooX-Const and may be cloned from git://github.com/robrwo/MooX-Const.git

BUGS

Please report any bugs or feature requests on the bugtracker website https://github.com/robrwo/MooX-Const/issues

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

AUTHOR

Robert Rothenberg [email protected]

This module was inspired by suggestions from Kang-min Liu 劉康民 [email protected] in a blog post.

CONTRIBUTOR

Kang-min Liu 劉康民 [email protected]

COPYRIGHT AND LICENSE

This software is Copyright (c) 2018-2023 by Robert Rothenberg.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)

moox-const's People

Contributors

robrwo avatar

Watchers

 avatar  avatar

moox-const's Issues

Should "wo" (write once) be renamed?

Class::Accessor "antlers" / "moose-like" mode uses "wo" for "write-only". So should "wo" be renamed to something else, e.g. "w1" or "write-once" ?

t/21-moox-typetiny.t not correctly skipped or dependency spec missing

t/21-moox-typetiny.t fails if MooX::TypeTiny is not installed:

Can't locate MooX/TypeTiny.pm in @INC (you may need to install the MooX::TypeTiny module) (@INC contains: ...  .) at /usr/local/lib/perl5/5.30/if.pm line 15.
BEGIN failed--compilation aborted at t/lib/MooTest.pm line 5.
Compilation failed in require at t/21-moox-typetiny.t line 10.
BEGIN failed--compilation aborted at t/21-moox-typetiny.t line 10.
t/21-moox-typetiny.t ......... 
Dubious, test returned 2 (wstat 512, 0x200)
No subtests run 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.