{"id":20482966,"url":"https://github.com/rsrchboy/moosex-attributeshortcuts","last_synced_at":"2025-07-11T02:39:34.343Z","repository":{"id":56832812,"uuid":"1532685","full_name":"rsrchboy/moosex-attributeshortcuts","owner":"rsrchboy","description":"Shorthand for common attribute options","archived":false,"fork":false,"pushed_at":"2019-04-18T14:58:30.000Z","size":565,"stargazers_count":3,"open_issues_count":6,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T14:42:23.341Z","etag":null,"topics":["modern-perl","moosex","perl","traits"],"latest_commit_sha":null,"homepage":"http://metacpan.org/release/MooseX-AttributeShortcuts/","language":"Perl","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rsrchboy.png","metadata":{"files":{"readme":"README.mkdn","changelog":"Changes","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-03-27T13:48:32.000Z","updated_at":"2019-04-18T14:58:32.000Z","dependencies_parsed_at":"2022-09-07T20:10:41.593Z","dependency_job_id":null,"html_url":"https://github.com/rsrchboy/moosex-attributeshortcuts","commit_stats":null,"previous_names":[],"tags_count":40,"template":false,"template_full_name":null,"purl":"pkg:github/rsrchboy/moosex-attributeshortcuts","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rsrchboy%2Fmoosex-attributeshortcuts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rsrchboy%2Fmoosex-attributeshortcuts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rsrchboy%2Fmoosex-attributeshortcuts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rsrchboy%2Fmoosex-attributeshortcuts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rsrchboy","download_url":"https://codeload.github.com/rsrchboy/moosex-attributeshortcuts/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rsrchboy%2Fmoosex-attributeshortcuts/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264713815,"owners_count":23652907,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["modern-perl","moosex","perl","traits"],"created_at":"2024-11-15T16:15:23.223Z","updated_at":"2025-07-11T02:39:34.280Z","avatar_url":"https://github.com/rsrchboy.png","language":"Perl","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/RsrchBoy/moosex-attributeshortcuts.svg?branch=master)](https://travis-ci.org/RsrchBoy/moosex-attributeshortcuts)\n[![Kwalitee status](http://cpants.cpanauthors.org/dist/MooseX-AttributeShortcuts.png)](http://cpants.charsbar.org/dist/overview/MooseX-AttributeShortcuts)\n[![Coverage Status](https://coveralls.io/repos/RsrchBoy/moosex-attributeshortcuts/badge.svg?branch=master)](https://coveralls.io/r/RsrchBoy/moosex-attributeshortcuts?branch=master)\n\n# NAME\n\nMooseX::AttributeShortcuts - Shorthand for common attribute options\n\n# VERSION\n\nThis document describes version 0.037 of MooseX::AttributeShortcuts - released November 20, 2017 as part of MooseX-AttributeShortcuts.\n\n# SYNOPSIS\n\n    package Some::Class;\n\n    use Moose;\n    use MooseX::AttributeShortcuts;\n\n    # same as:\n    #   is =\u003e 'ro', lazy =\u003e 1, builder =\u003e '_build_foo'\n    has foo =\u003e (is =\u003e 'lazy');\n\n    # same as: is =\u003e 'ro', writer =\u003e '_set_foo'\n    has foo =\u003e (is =\u003e 'rwp');\n\n    # same as: is =\u003e 'ro', builder =\u003e '_build_bar'\n    has bar =\u003e (is =\u003e 'ro', builder =\u003e 1);\n\n    # same as: is =\u003e 'ro', clearer =\u003e 'clear_bar'\n    has bar =\u003e (is =\u003e 'ro', clearer =\u003e 1);\n\n    # same as: is =\u003e 'ro', predicate =\u003e 'has_bar'\n    has bar =\u003e (is =\u003e 'ro', predicate =\u003e 1);\n\n    # works as you'd expect for \"private\": predicate =\u003e '_has_bar'\n    has _bar =\u003e (is =\u003e 'ro', predicate =\u003e 1);\n\n    # extending? Use the \"Shortcuts\" trait alias\n    extends 'Some::OtherClass';\n    has '+bar' =\u003e (traits =\u003e [Shortcuts], builder =\u003e 1, ...);\n\n# DESCRIPTION\n\nEver find yourself repeatedly specifying writers and builders, because there's\nno good shortcut to specifying them?  Sometimes you want an attribute to have\na read-only public interface, but a private writer.  And wouldn't it be easier\nto just say `builder =\u003e 1` and have the attribute construct the canonical\n`_build_$name` builder name for you?\n\nThis package causes an attribute trait to be applied to all attributes defined\nto the using class.  This trait extends the attribute option processing to\nhandle the above variations.  All attribute options as described in [Moose](https://metacpan.org/pod/Moose)\nor [Class::MOP::Attribute](https://metacpan.org/pod/Class::MOP::Attribute) remain usable, just as when this trait is not\napplied.\n\n## Some Notes On History\n\nMoose has long had a [lazy\\_build attribute option](https://metacpan.org/pod/Moose#lazy_build).  It was\nonce considered a best practice, but that has, ah, changed.  This trait began\nas a desire to still leverage bits of `lazy_build` (and a tacit\nacknowledgment that fat-finger bugs rank among the most embarrassing, right up\nthere with \"the TV was unplugged the entire time\").\n\nThis author does not recommend you use `lazy_build`, unless you know exactly\nwhat you're doing (probably) and that it's a good idea (probably not).\n\nNonetheless, this `lazy_build` option is why we set certain options the way\nwe do below; while `lazy_build` in its entirety is not optimal, it had the\nright idea: regular, predictable accessor names for regular, predictable\nattribute options.\n\nAs an example, just looking at the below it doesn't seem logical that:\n\n    has _foo =\u003e (is =\u003e 'ro', clearer =\u003e 1);\n\n...becomes:\n\n    has _foo =\u003e (is =\u003e 'ro', clearer =\u003e '_clear_foo');\n\nAfter reading the [lazy\\_build attribute option](https://metacpan.org/pod/Moose#lazy_build),\nhowever, we see that the choice had already been made for us.\n\n# USAGE\n\nThis package automatically applies an attribute metaclass trait.  Simply using\nthis package causes the trait to be applied by default to your attribute's\nmetaclasses.\n\n# EXTENDING A CLASS\n\nIf you're extending a class and trying to extend its attributes as well,\nyou'll find out that the trait is only applied to attributes defined locally\nin the class.  This package exports a trait shortcut function `Shortcuts`\nthat will help you apply this to the extended attribute:\n\n    has '+something' =\u003e (traits =\u003e [Shortcuts], ...);\n\n# NEW ATTRIBUTE OPTIONS\n\nUnless specified here, all options defined by [Moose::Meta::Attribute](https://metacpan.org/pod/Moose::Meta::Attribute) and\n[Class::MOP::Attribute](https://metacpan.org/pod/Class::MOP::Attribute) remain unchanged.\n\nWant to see additional options?  Ask, or better yet, fork on GitHub and send\na pull request.  If the shortcuts you're asking for already exist in [Moo](https://metacpan.org/pod/Moo) or\n[Mouse](https://metacpan.org/pod/Mouse) or elsewhere, please note that as it will carry significant weight.\n\nFor the following, `$name` should be read as the attribute name; and the\nvarious prefixes should be read using the defaults.\n\n## is =\u003e 'rwp'\n\nSpecifying `is =\u003e 'rwp'` will cause the following options to be set:\n\n    is     =\u003e 'ro'\n    writer =\u003e \"_set_$name\"\n\nrwp can be read as \"read + write private\".\n\n## is =\u003e 'lazy'\n\nSpecifying `is =\u003e 'lazy'` will cause the following options to be set:\n\n    is       =\u003e 'ro'\n    builder  =\u003e \"_build_$name\"\n    lazy     =\u003e 1\n\n**NOTE:** Since 0.009 we no longer set `init_arg =\u003e undef` if no `init_arg`\nis explicitly provided.  This is a change made in parallel with [Moo](https://metacpan.org/pod/Moo), based\non a large number of people surprised that lazy also made one's `init_def`\nundefined.\n\n## is =\u003e 'lazy', default =\u003e ...\n\nSpecifying `is =\u003e 'lazy'` and a default will cause the following options to be\nset:\n\n    is       =\u003e 'ro'\n    lazy     =\u003e 1\n    default  =\u003e ... # as provided\n\nThat is, if you specify `is =\u003e 'lazy'` and also provide a `default`, then\nwe won't try to set a builder, as well.\n\n## builder =\u003e 1\n\nSpecifying `builder =\u003e 1` will cause the following options to be set:\n\n    builder =\u003e \"_build_$name\"\n\n## builder =\u003e sub { ... }\n\nPassing a coderef to builder will cause that coderef to be installed in the\nclass this attribute is associated with the name you'd expect, and\n`builder =\u003e 1` to be set.\n\ne.g., in your class (or role),\n\n    has foo =\u003e (is =\u003e 'ro', builder =\u003e sub { 'bar!' });\n\n...is effectively the same as...\n\n    has foo =\u003e (is =\u003e 'ro', builder =\u003e '_build_foo');\n    sub _build_foo { 'bar!' }\n\nThe behaviour of this option in roles changed in 0.030, and the builder\nmethods will be installed in the role itself.  This means you can\nalias/exclude/etc builder methods in roles, just as you can with any other\nmethod.\n\n## clearer =\u003e 1\n\nSpecifying `clearer =\u003e 1` will cause the following options to be set:\n\n    clearer =\u003e \"clear_$name\"\n\nor, if your attribute name begins with an underscore:\n\n    clearer =\u003e \"_clear$name\"\n\n(that is, an attribute named `_foo` would get `_clear_foo`)\n\n## predicate =\u003e 1\n\nSpecifying `predicate =\u003e 1` will cause the following options to be set:\n\n    predicate =\u003e \"has_$name\"\n\nor, if your attribute name begins with an underscore:\n\n    predicate =\u003e \"_has$name\"\n\n(that is, an attribute named `_foo` would get `_has_foo`)\n\n## init\\_arg =\u003e 1 / -1\n\nThis is a somewhat esoteric shortcut; you probably don't want to use this (or\neven read this section).\n\nSpecifying `init_arg =\u003e 1` will cause the following options to be set:\n\n    # attribute: \"name\"\n    init_arg =\u003e 'name'\n\n    # or, attribute: \"_name\"\n    init_arg =\u003e '_name'\n\n...while `init_arg =\u003e -1` will cause the following options to be set:\n\n    # attribute: \"name\"\n    init_arg =\u003e '_name'\n\n    # or, attribute: \"_name\"\n    init_arg =\u003e 'name'\n\n## trigger =\u003e 1\n\nSpecifying `trigger =\u003e 1` will cause the attribute to be created with a trigger\nthat calls a named method in the class with the options passed to the trigger.\nBy default, the method name the trigger calls is the name of the attribute\nprefixed with `_trigger_`.\n\ne.g., for an attribute named `foo` this would be equivalent to:\n\n    trigger =\u003e sub { shift-\u003e_trigger_foo(@_) }\n\nFor an attribute named `_foo`:\n\n    trigger =\u003e sub { shift-\u003e_trigger__foo(@_) }\n\nThis naming scheme, in which the trigger is always private, is the same as the\nbuilder naming scheme (just with a different prefix).\n\n## handles =\u003e { foo =\u003e sub { ... }, ... }\n\nCreating a delegation with a coderef will now create a new, \"custom accessor\"\nfor the attribute.  These coderefs will be installed and called as methods on\nthe associated class (just as readers, writers, and other accessors are), and\nwill have the attribute metaclass available in `$_`.  Anything the accessor\nis called with it will have access to in `@_`, just as you'd expect of a\nmethod.\n\ne.g., the following example creates an attribute named `bar` with a standard\nreader accessor named `bar` and two custom accessors named `foo` and\n`foo_too`.\n\n    has bar =\u003e (\n\n        is      =\u003e 'ro',\n        isa     =\u003e 'Int',\n        handles =\u003e {\n\n            foo =\u003e sub {\n                my $self = shift @_;\n\n                return $_-\u003eget_value($self) + 1;\n            },\n\n            foo_too =\u003e sub {\n                my $self = shift @_;\n\n                return $self-\u003ebar + 1;\n            },\n\n            # ...as you'd expect.\n            bar =\u003e 'bar',\n        },\n    );\n\n...and later,\n\nNote that in this example both foo() and foo\\_too() do effectively the same\nthing: return the attribute's current value plus 1.  However, foo() accesses\nthe attribute value directly through the metaclass, the pros and cons of\nwhich this author leaves as an exercise for the reader to determine.\n\nYou may choose to use the installed accessors to get at the attribute's value,\nor use the direct metaclass access, your choice.\n\n# ANONYMOUS SUBTYPING AND COERCION\n\n    \"Abusus non tollit usum.\"\n\nNote that we create new, anonymous subtypes whenever the constraint or\ncoercion options are specified in such a way that the Shortcuts trait (this\none) is invoked.  It's fully supported to use both constraint and coerce\noptions at the same time.\n\nThis facility is intended to assist with the creation of one-off type\nconstraints and coercions.  It is not possible to deliberately reuse the\nsubtypes we create, and if you find yourself using a particular isa /\nconstraint / coerce option triplet in more than one place you should really\nthink about creating a type that you can reuse.  [MooseX::Types](https://metacpan.org/pod/MooseX::Types) provides\nthe facilities to easily do this, or even a simple [constant](https://metacpan.org/pod/constant) definition at\nthe package level with an anonymous type stashed away for local use.\n\n## isa =\u003e sub { ... }\n\n    has foo =\u003e (\n        is  =\u003e 'rw',\n        # $_ == $_[0] == the value to be validated\n        isa =\u003e sub { die unless $_[0] == 1 },\n    );\n\n    # passes constraint\n    $thing-\u003efoo(1);\n\n    # fails constraint\n    $thing-\u003efoo(5);\n\nGiven a coderef, create a type constraint for the attribute.  This constraint\nwill fail if the coderef dies, and pass otherwise.\n\nAstute users will note that this is the same way [Moo](https://metacpan.org/pod/Moo) constraints work; we\nuse [MooseX::Meta::TypeConstraint::Mooish](https://metacpan.org/pod/MooseX::Meta::TypeConstraint::Mooish) to implement the constraint.\n\n## isa\\_instance\\_of =\u003e ...\n\nGiven a package name, this option will create an `isa` type constraint that\nrequires the value of the attribute be an instance of the class (or a\ndescendant class) given.  That is,\n\n    has foo =\u003e (is =\u003e 'ro', isa_instance_of =\u003e 'SomeThing');\n\n...is effectively the same as:\n\n    use Moose::TypeConstraints 'class_type';\n    has foo =\u003e (\n        is  =\u003e 'ro',\n        isa =\u003e class_type('SomeThing'),\n    );\n\n...but a touch less awkward.\n\n## isa =\u003e ..., constraint =\u003e sub { ... }\n\nSpecifying the constraint option with a coderef will cause a new subtype\nconstraint to be created, with the parent type being the type specified in the\n`isa` option and the constraint being the coderef supplied here.\n\nFor example, only integers greater than 10 will pass this attribute's type\nconstraint:\n\n    # value must be an integer greater than 10 to pass the constraint\n    has thinger =\u003e (\n        isa        =\u003e 'Int',\n        constraint =\u003e sub { $_ \u003e 10 },\n        # ...\n    );\n\nNote that if you supply a constraint, you must also provide an `isa`.\n\n## isa =\u003e ..., constraint =\u003e sub { ... }, coerce =\u003e 1\n\nSupplying a constraint and asking for coercion will \"Just Work\", that is, any\ncoercions that the `isa` type has will still work.\n\nFor example, let's say that you're using the `File` type constraint from\n[MooseX::Types::Path::Class](https://metacpan.org/pod/MooseX::Types::Path::Class), and you want an additional constraint that the\nfile must exist:\n\n    has thinger =\u003e (\n        is         =\u003e 'ro',\n        isa        =\u003e File,\n        constraint =\u003e sub { !! $_-\u003estat },\n        coerce     =\u003e 1,\n    );\n\n`thinger` will correctly coerce the string \"/etc/passwd\" to a\n`Path::Class:File`, and will only accept the coerced result as a value if\nthe file exists.\n\n## coerce =\u003e \\[ Type =\u003e sub { ...coerce... }, ... \\]\n\nSpecifying the coerce option with a hashref will cause a new subtype to be\ncreated and used (just as with the constraint option, above), with the\nspecified coercions added to the list.  In the passed hashref, the keys are\nMoose types (well, strings resolvable to Moose types), and the values are\ncoderefs that will coerce a given type to our type.\n\n    has bar =\u003e (\n        is     =\u003e 'ro',\n        isa    =\u003e 'Str',\n        coerce =\u003e [\n            Int    =\u003e sub { \"$_\"                       },\n            Object =\u003e sub { 'An instance of ' . ref $_ },\n        ],\n    );\n\n# INTERACTIONS WITH OTHER ATTRIBUTE TRAITS\n\nSometimes attribute traits interact in surprising ways.  This trait is well\nbehaved; if you have discovered any interactions with other traits (good, bad,\nindifferent, etc), please\n[report this](https://github.com/RsrchBoy/moosex-attributeshortcuts/issues/new)\nso that it can be worked around, fixed, or documented, as appropriate.\n\n## MooseX::SemiAffordanceAccessor\n\n[MooseX::SemiAffordanceAccessor](https://metacpan.org/pod/MooseX::SemiAffordanceAccessor) changes how the `is =\u003e 'rw'` and\n`accessor =\u003e ...` attribute options work.  If our trait detects that an\nattribute has had the\n[MooseX::SemiAffordanceAccessor attribute trait](https://metacpan.org/pod/MooseX::SemiAffordanceAccessor::Role::Attribute)\napplied, then we change our behaviour to conform to its expectations:\n\n- `is =\u003e 'rwp'`\n\n    This:\n\n        has  foo =\u003e (is =\u003e 'rwp');\n        has _bar =\u003e (is =\u003e 'rwp');\n\n    ...is now effectively equivalent to:\n\n        has foo  =\u003e (is =\u003e 'ro', writer =\u003e '_set_foo');\n        has _bar =\u003e (is =\u003e 'ro', writer =\u003e '_set_bar')\n\n- `-writer_prefix` is ignored\n\n    ...as MooseX::SemiAffordanceAccessor has its own specific ideas as to how\n    writers should look.\n\n# SEE ALSO\n\nPlease see those modules/websites for more information related to this module.\n\n- [Moo](https://metacpan.org/pod/Moo)\n- [MooseX::Types](https://metacpan.org/pod/MooseX::Types)\n- [MooseX::SemiAffordanceAccessor](https://metacpan.org/pod/MooseX::SemiAffordanceAccessor)\n\n# BUGS\n\nPlease report any bugs or feature requests on the bugtracker website\n[https://github.com/RsrchBoy/moosex-attributeshortcuts/issues](https://github.com/RsrchBoy/moosex-attributeshortcuts/issues)\n\nWhen submitting a bug or request, please include a test-file or a\npatch to an existing test-file that illustrates the bug or desired\nfeature.\n\n# AUTHOR\n\nChris Weyl \u003ccweyl@alumni.drew.edu\u003e\n\n# CONTRIBUTORS\n\n- David Steinbrunner \u003cdsteinbrunner@pobox.com\u003e\n- Graham Knop \u003chaarg@haarg.org\u003e\n- Karen Etheridge \u003cether@cpan.org\u003e\n- Olaf Alders \u003colaf@wundersolutions.com\u003e\n\n# COPYRIGHT AND LICENSE\n\nThis software is Copyright (c) 2017, 2015, 2014, 2013, 2012, 2011 by Chris Weyl.\n\nThis is free software, licensed under:\n\n    The GNU Lesser General Public License, Version 2.1, February 1999\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frsrchboy%2Fmoosex-attributeshortcuts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frsrchboy%2Fmoosex-attributeshortcuts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frsrchboy%2Fmoosex-attributeshortcuts/lists"}