{
    "mode": "perldoc",
    "parameter": "Moose::Meta::Attribute::Native",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AMeta%3A%3AAttribute%3A%3ANative/json",
    "generated": "2026-06-11T14:26:13Z",
    "synopsis": "package MyClass;\nuse Moose;\nhas 'mapping' => (\ntraits  => ['Hash'],\nis      => 'rw',\nisa     => 'HashRef[Str]',\ndefault => sub { {} },\nhandles => {\nexistsinmapping => 'exists',\nidsinmapping    => 'keys',\ngetmapping       => 'get',\nsetmapping       => 'set',\nsetquantity      => [ set => 'quantity' ],\n},\n);\nmy $obj = MyClass->new;\n$obj->setquantity(10);      # quantity => 10\n$obj->setmapping('foo', 4); # foo => 4\n$obj->setmapping('bar', 5); # bar => 5\n$obj->setmapping('baz', 6); # baz => 6\n# prints 5\nprint $obj->getmapping('bar') if $obj->existsinmapping('bar');\n# prints 'quantity, foo, bar, baz'\nprint join ', ', $obj->idsinmapping;",
    "sections": {
        "NAME": {
            "content": "Moose::Meta::Attribute::Native - Delegate to native Perl types\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 2.2200\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package MyClass;\nuse Moose;\n\nhas 'mapping' => (\ntraits  => ['Hash'],\nis      => 'rw',\nisa     => 'HashRef[Str]',\ndefault => sub { {} },\nhandles => {\nexistsinmapping => 'exists',\nidsinmapping    => 'keys',\ngetmapping       => 'get',\nsetmapping       => 'set',\nsetquantity      => [ set => 'quantity' ],\n},\n);\n\nmy $obj = MyClass->new;\n$obj->setquantity(10);      # quantity => 10\n$obj->setmapping('foo', 4); # foo => 4\n$obj->setmapping('bar', 5); # bar => 5\n$obj->setmapping('baz', 6); # baz => 6\n\n# prints 5\nprint $obj->getmapping('bar') if $obj->existsinmapping('bar');\n\n# prints 'quantity, foo, bar, baz'\nprint join ', ', $obj->idsinmapping;\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Native delegations allow you to delegate to native Perl data structures as if they were objects.\nFor example, in the \"SYNOPSIS\" you can see a hash reference being treated as if it has methods\nnamed \"exists()\", \"keys()\", \"get()\", and \"set()\".\n\nThe delegation methods (mostly) map to Perl builtins and operators. The return values of these\ndelegations should be the same as the corresponding Perl operation. Any deviations will be\nexplicitly documented.\n",
            "subsections": []
        },
        "API": {
            "content": "Native delegations are enabled by passing certain options to \"has\" when creating an attribute.\n\ntraits\nTo enable this feature, pass the appropriate name in the \"traits\" array reference for the\nattribute. For example, to enable this feature for hash reference, we include 'Hash' in the list\nof traits.\n\nisa\nYou will need to make sure that the attribute has an appropriate type. For example, to use this\nwith a Hash you must specify that your attribute is some sort of \"HashRef\".\n\nhandles\nThis is just like any other delegation, but only a hash reference is allowed when defining\nnative delegations. The keys are the methods to be created in the class which contains the\nattribute. The values are the methods provided by the associated trait. Currying works the same\nway as it does with any other delegation.\n\nSee the docs for each native trait for details on what methods are available.\n",
            "subsections": []
        },
        "TRAITS FOR NATIVE DELEGATIONS": {
            "content": "Below are some simple examples of each native trait. More features are available than what is\nshown here; this is just a quick synopsis.\n\nArray (Moose::Meta::Attribute::Native::Trait::Array)\nhas 'queue' => (\ntraits  => ['Array'],\nis      => 'ro',\nisa     => 'ArrayRef[Str]',\ndefault => sub { [] },\nhandles => {\nadditem  => 'push',\nnextitem => 'shift',\n# ...\n}\n);\n\nBool (Moose::Meta::Attribute::Native::Trait::Bool)\nhas 'islit' => (\ntraits  => ['Bool'],\nis      => 'ro',\nisa     => 'Bool',\ndefault => 0,\nhandles => {\nilluminate  => 'set',\ndarken      => 'unset',\nflipswitch => 'toggle',\nisdark     => 'not',\n# ...\n}\n);\n\nCode (Moose::Meta::Attribute::Native::Trait::Code)\nhas 'callback' => (\ntraits  => ['Code'],\nis      => 'ro',\nisa     => 'CodeRef',\ndefault => sub {\nsub {'called'}\n},\nhandles => {\ncall => 'execute',\n# ...\n}\n);\n\nCounter (Moose::Meta::Attribute::Native::Trait::Counter)\nhas 'counter' => (\ntraits  => ['Counter'],\nis      => 'ro',\nisa     => 'Num',\ndefault => 0,\nhandles => {\ninccounter   => 'inc',\ndeccounter   => 'dec',\nresetcounter => 'reset',\n# ...\n}\n);\n\nHash (Moose::Meta::Attribute::Native::Trait::Hash)\nhas 'options' => (\ntraits  => ['Hash'],\nis      => 'ro',\nisa     => 'HashRef[Str]',\ndefault => sub { {} },\nhandles => {\nsetoption => 'set',\ngetoption => 'get',\nhasoption => 'exists',\n# ...\n}\n);\n\nNumber (Moose::Meta::Attribute::Native::Trait::Number)\nhas 'integer' => (\ntraits  => ['Number'],\nis      => 'ro',\nisa     => 'Int',\ndefault => 5,\nhandles => {\nset => 'set',\nadd => 'add',\nsub => 'sub',\nmul => 'mul',\ndiv => 'div',\nmod => 'mod',\nabs => 'abs',\n# ...\n}\n);\n\nString (Moose::Meta::Attribute::Native::Trait::String)\nhas 'text' => (\ntraits  => ['String'],\nis      => 'ro',\nisa     => 'Str',\ndefault => q{},\nhandles => {\naddtext     => 'append',\nreplacetext => 'replace',\n# ...\n}\n);\n\nCOMPATIBILITY WITH MooseX::AttributeHelpers\nThis feature used to be a separated CPAN distribution called MooseX::AttributeHelpers.\n\nWhen the feature was incorporated into the Moose core, some of the API details were changed. The\nunderlying capabilities are the same, but some details of the API were changed.\n",
            "subsections": []
        },
        "BUGS": {
            "content": "See \"BUGS\" in Moose for details on reporting bugs.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "*   Stevan Little <stevan@cpan.org>\n\n*   Dave Rolsky <autarch@urth.org>\n\n*   Jesse Luehrs <doy@cpan.org>\n\n*   Shawn M Moore <sartak@cpan.org>\n\n*   יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>\n\n*   Karen Etheridge <ether@cpan.org>\n\n*   Florian Ragwitz <rafl@debian.org>\n\n*   Hans Dieter Pearcey <hdp@cpan.org>\n\n*   Chris Prather <chris@prather.org>\n\n*   Matt S Trout <mstrout@cpan.org>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is copyright (c) 2006 by Infinity Interactive, Inc.\n\nThis is free software; you can redistribute it and/or modify it under the same terms as the Perl\n5 programming language system itself.\n",
            "subsections": []
        }
    },
    "summary": "Moose::Meta::Attribute::Native - Delegate to native Perl types",
    "flags": [],
    "examples": [],
    "see_also": []
}