{
    "content": [
        {
            "type": "text",
            "text": "# Class::Accessor (perldoc)\n\n**Summary:** Class::Accessor - Automated accessor generation\n\n**Synopsis:** package Foo;\nuse base qw(Class::Accessor);\nFoo->followbestpractice;\nFoo->mkaccessors(qw(name role salary));\n# or if you prefer a Moose-like interface...\npackage Foo;\nuse Class::Accessor \"antlers\";\nhas name => ( is => \"rw\", isa => \"Str\" );\nhas role => ( is => \"rw\", isa => \"Str\" );\nhas salary => ( is => \"rw\", isa => \"Num\" );\n# Meanwhile, in a nearby piece of code!\n# Class::Accessor provides new().\nmy $mp = Foo->new({ name => \"Marty\", role => \"JAPH\" });\nmy $job = $mp->role;  # gets $mp->{role}\n$mp->salary(400000);  # sets $mp->{salary} = 400000 # I wish\n# like my @info = @{$mp}{qw(name role)}\nmy @info = $mp->get(qw(name role));\n# $mp->{salary} = 400000\n$mp->set('salary', 400000);\n\n## Examples\n\n- `Here's an example of generating an accessor for every public field of your class.`\n- `package Altoids;`\n- `use base qw(Class::Accessor Class::Fields);`\n- `use fields qw(curiously strong mints);`\n- `Altoids->mkaccessors( Altoids->showfields('Public') );`\n- `sub new {`\n- `my $proto = shift;`\n- `my $class = ref $proto || $proto;`\n- `return fields::new($class);`\n- `my Altoids $tin = Altoids->new;`\n- `$tin->curiously('Curiouser and curiouser');`\n- `print $tin->{curiously};    # prints 'Curiouser and curiouser'`\n- `# Subclassing works, too.`\n- `package Mint::Snuff;`\n- `use base qw(Altoids);`\n- `my Mint::Snuff $pouch = Mint::Snuff->new;`\n- `$pouch->strong('Blow your head off!');`\n- `print $pouch->{strong};     # prints 'Blow your head off!'`\n- `Here's a simple example of altering the behavior of your accessors.`\n- `package Foo;`\n- `use base qw(Class::Accessor);`\n- `Foo->mkaccessors(qw(this that up down));`\n- `sub get {`\n- `my $self = shift;`\n- `# Note every time someone gets some data.`\n- `print STDERR \"Getting @\\n\";`\n- `$self->SUPER::get(@);`\n- `sub set {`\n- `my ($self, $key) = splice(@, 0, 2);`\n- `# Note every time someone sets some data.`\n- `print STDERR \"Setting $key to @\\n\";`\n- `$self->SUPER::set($key, @);`\n\n## Section Outline\n\n- **NAME** (2 lines)\n- **SYNOPSIS** (26 lines)\n- **DESCRIPTION** (28 lines) — 2 subsections\n  - mk_accessors (19 lines)\n  - Moose-like (10 lines)\n- **CONSTRUCTOR** (22 lines)\n- **MAKING ACCESSORS** (88 lines)\n- **DETAILS** (17 lines) — 3 subsections\n  - Modifying the behavior of the accessor (9 lines)\n  - set (6 lines)\n  - get (27 lines)\n- **EXCEPTIONS** (4 lines)\n- **EFFICIENCY** (34 lines)\n- **EXAMPLES** (52 lines)\n- **CAVEATS AND TRICKS** (5 lines) — 2 subsections\n  - Don't make a field called DESTROY (3 lines)\n  - Overriding autogenerated accessors (51 lines)\n- **AUTHORS** (15 lines)\n- **SEE ALSO** (8 lines)\n\n## Full Content\n\n### NAME\n\nClass::Accessor - Automated accessor generation\n\n### SYNOPSIS\n\npackage Foo;\nuse base qw(Class::Accessor);\nFoo->followbestpractice;\nFoo->mkaccessors(qw(name role salary));\n\n# or if you prefer a Moose-like interface...\n\npackage Foo;\nuse Class::Accessor \"antlers\";\nhas name => ( is => \"rw\", isa => \"Str\" );\nhas role => ( is => \"rw\", isa => \"Str\" );\nhas salary => ( is => \"rw\", isa => \"Num\" );\n\n# Meanwhile, in a nearby piece of code!\n# Class::Accessor provides new().\nmy $mp = Foo->new({ name => \"Marty\", role => \"JAPH\" });\n\nmy $job = $mp->role;  # gets $mp->{role}\n$mp->salary(400000);  # sets $mp->{salary} = 400000 # I wish\n\n# like my @info = @{$mp}{qw(name role)}\nmy @info = $mp->get(qw(name role));\n\n# $mp->{salary} = 400000\n$mp->set('salary', 400000);\n\n### DESCRIPTION\n\nThis module automagically generates accessors/mutators for your class.\n\nMost of the time, writing accessors is an exercise in cutting and pasting. You usually wind up\nwith a series of methods like this:\n\nsub name {\nmy $self = shift;\nif(@) {\n$self->{name} = $[0];\n}\nreturn $self->{name};\n}\n\nsub salary {\nmy $self = shift;\nif(@) {\n$self->{salary} = $[0];\n}\nreturn $self->{salary};\n}\n\n# etc...\n\nOne for each piece of data in your object. While some will be unique, doing value checks and\nspecial storage tricks, most will simply be exercises in repetition. Not only is it Bad Style to\nhave a bunch of repetitious code, but it's also simply not lazy, which is the real tragedy.\n\nIf you make your module a subclass of Class::Accessor and declare your accessor fields with\n\n#### mk_accessors\n\ncan even be customized!\n\nThe basic set up is very simple:\n\npackage Foo;\nuse base qw(Class::Accessor);\nFoo->mkaccessors( qw(far bar car) );\n\nDone. Foo now has simple far(), bar() and car() accessors defined.\n\nAlternatively, if you want to follow Damian's *best practice* guidelines you can use:\n\npackage Foo;\nuse base qw(Class::Accessor);\nFoo->followbestpractice;\nFoo->mkaccessors( qw(far bar car) );\n\nNote: you must call \"followbestpractice\" before calling \"mkaccessors\".\n\n#### Moose-like\n\nBy popular demand we now have a simple Moose-like interface. You can now do:\n\npackage Foo;\nuse Class::Accessor \"antlers\";\nhas far => ( is => \"rw\" );\nhas bar => ( is => \"rw\" );\nhas car => ( is => \"rw\" );\n\nCurrently only the \"is\" attribute is supported.\n\n### CONSTRUCTOR\n\nClass::Accessor provides a basic constructor, \"new\". It generates a hash-based object and can be\ncalled as either a class method or an object method.\n\nnew\nmy $obj = Foo->new;\nmy $obj = $otherobj->new;\n\nmy $obj = Foo->new(\\%fields);\nmy $obj = $otherobj->new(\\%fields);\n\nIt takes an optional %fields hash which is used to initialize the object (handy if you use\nread-only accessors). The fields of the hash correspond to the names of your accessors, so...\n\npackage Foo;\nuse base qw(Class::Accessor);\nFoo->mkaccessors('foo');\n\nmy $obj = Foo->new({ foo => 42 });\nprint $obj->foo;    # 42\n\nhowever %fields can contain anything, new() will shove them all into your object.\n\n### MAKING ACCESSORS\n\nfollowbestpractice\nIn Damian's Perl Best Practices book he recommends separate get and set methods with the prefix\nset and get to make it explicit what you intend to do. If you want to create those accessor\nmethods instead of the default ones, call:\n\nPACKAGE->followbestpractice\n\nbefore you call any of the accessor-making methods.\n\naccessornamefor / mutatornamefor\nYou may have your own crazy ideas for the names of the accessors, so you can make those happen\nby overriding \"accessornamefor\" and \"mutatornamefor\" in your subclass. (I copied that idea\nfrom Class::DBI.)\n\nmkaccessors\nPACKAGE->mkaccessors(@fields);\n\nThis creates accessor/mutator methods for each named field given in @fields. Foreach field in\n@fields it will generate two accessors. One called \"field()\" and the other called\n\"fieldaccessor()\". For example:\n\n# Generates foo(), fooaccessor(), bar() and baraccessor().\nPACKAGE->mkaccessors(qw(foo bar));\n\nSee \"Overriding autogenerated accessors\" in CAVEATS AND TRICKS for details.\n\nmkroaccessors\nPACKAGE->mkroaccessors(@readonlyfields);\n\nSame as mkaccessors() except it will generate read-only accessors (ie. true accessors). If you\nattempt to set a value with these accessors it will throw an exception. It only uses get() and\nnot set().\n\npackage Foo;\nuse base qw(Class::Accessor);\nFoo->mkroaccessors(qw(foo bar));\n\n# Let's assume we have an object $foo of class Foo...\nprint $foo->foo;  # ok, prints whatever the value of $foo->{foo} is\n$foo->foo(42);    # BOOM!  Naughty you.\n\nmkwoaccessors\nPACKAGE->mkwoaccessors(@writeonlyfields);\n\nSame as mkaccessors() except it will generate write-only accessors (ie. mutators). If you\nattempt to read a value with these accessors it will throw an exception. It only uses set() and\nnot get().\n\nNOTE I'm not entirely sure why this is useful, but I'm sure someone will need it. If you've\nfound a use, let me know. Right now it's here for orthogonality and because it's easy to\nimplement.\n\npackage Foo;\nuse base qw(Class::Accessor);\nFoo->mkwoaccessors(qw(foo bar));\n\n# Let's assume we have an object $foo of class Foo...\n$foo->foo(42);      # OK.  Sets $self->{foo} = 42\nprint $foo->foo;    # BOOM!  Can't read from this accessor.\n\nMoose!\nIf you prefer a Moose-like interface to create accessors, you can use \"has\" by importing this\nmodule like this:\n\nuse Class::Accessor \"antlers\";\n\nor\n\nuse Class::Accessor \"moose-like\";\n\nThen you can declare accessors like this:\n\nhas alpha => ( is => \"rw\", isa => \"Str\" );\nhas beta  => ( is => \"ro\", isa => \"Str\" );\nhas gamma => ( is => \"wo\", isa => \"Str\" );\n\nCurrently only the \"is\" attribute is supported. And our \"is\" also supports the \"wo\" value to\nmake a write-only accessor.\n\nIf you are using the Moose-like interface then you should use the \"extends\" rather than tweaking\nyour @ISA directly. Basically, replace\n\n@ISA = qw/Foo Bar/;\n\nwith\n\nextends(qw/Foo Bar/);\n\n### DETAILS\n\nAn accessor generated by Class::Accessor looks something like this:\n\n# Your foo may vary.\nsub foo {\nmy($self) = shift;\nif(@) {    # set\nreturn $self->set('foo', @);\n}\nelse {\nreturn $self->get('foo');\n}\n}\n\nVery simple. All it does is determine if you're wanting to set a value or get a value and calls\nthe appropriate method. Class::Accessor provides default get() and set() methods which your\nclass can override. They're detailed later.\n\n#### Modifying the behavior of the accessor\n\nRather than actually modifying the accessor itself, it is much more sensible to simply override\nthe two key methods which the accessor calls. Namely set() and get().\n\nIf you -really- want to, you can override makeaccessor().\n\nset\n$obj->set($key, $value);\n$obj->set($key, @values);\n\n#### set\n\noverride this method to change how data is stored by your accessors.\n\nget\n$value  = $obj->get($key);\n@values = $obj->get(@keys);\n\n#### get\n\noverride this method to change how it is retrieved.\n\nmakeaccessor\n$accessor = PACKAGE->makeaccessor($field);\n\nGenerates a subroutine reference which acts as an accessor for the given $field. It calls get()\nand set().\n\nIf you wish to change the behavior of your accessors, try overriding get() and set() before you\nstart mucking with makeaccessor().\n\nmakeroaccessor\n$readonlyaccessor = PACKAGE->makeroaccessor($field);\n\nGenerates a subroutine reference which acts as a read-only accessor for the given $field. It\nonly calls get().\n\nOverride get() to change the behavior of your accessors.\n\nmakewoaccessor\n$writeonlyaccessor = PACKAGE->makewoaccessor($field);\n\nGenerates a subroutine reference which acts as a write-only accessor (mutator) for the given\n$field. It only calls set().\n\nOverride set() to change the behavior of your accessors.\n\n### EXCEPTIONS\n\nIf something goes wrong Class::Accessor will warn or die by calling Carp::carp or Carp::croak.\nIf you don't like this you can override carp() and croak() in your subclass and do whatever\nelse you want.\n\n### EFFICIENCY\n\nClass::Accessor does not employ an autoloader, thus it is much faster than you'd think. Its\ngenerated methods incur no special penalty over ones you'd write yourself.\n\naccessors:\nRate  Basic   Fast Faster Direct\nBasic   367589/s     --   -51%   -55%   -89%\nFast    747964/s   103%     --    -9%   -77%\nFaster  819199/s   123%    10%     --   -75%\nDirect 3245887/s   783%   334%   296%     --\n\nmutators:\nRate    Acc   Fast Faster Direct\nAcc     265564/s     --   -54%   -63%   -91%\nFast    573439/s   116%     --   -21%   -80%\nFaster  724710/s   173%    26%     --   -75%\nDirect 2860979/s   977%   399%   295%     --\n\nClass::Accessor::Fast is faster than methods written by an average programmer (where \"average\"\nis based on Schwern's example code).\n\nClass::Accessor is slower than average, but more flexible.\n\nClass::Accessor::Faster is even faster than Class::Accessor::Fast. It uses an array internally,\nnot a hash. This could be a good or bad feature depending on your point of view.\n\nDirect hash access is, of course, much faster than all of these, but it provides no\nencapsulation.\n\nOf course, it's not as simple as saying \"Class::Accessor is slower than average\". These are\nbenchmarks for a simple accessor. If your accessors do any sort of complicated work (such as\ntalking to a database or writing to a file) the time spent doing that work will quickly swamp\nthe time spend just calling the accessor. In that case, Class::Accessor and the ones you write\nwill be roughly the same speed.\n\n### EXAMPLES\n\nHere's an example of generating an accessor for every public field of your class.\n\npackage Altoids;\n\nuse base qw(Class::Accessor Class::Fields);\nuse fields qw(curiously strong mints);\nAltoids->mkaccessors( Altoids->showfields('Public') );\n\nsub new {\nmy $proto = shift;\nmy $class = ref $proto || $proto;\nreturn fields::new($class);\n}\n\nmy Altoids $tin = Altoids->new;\n\n$tin->curiously('Curiouser and curiouser');\nprint $tin->{curiously};    # prints 'Curiouser and curiouser'\n\n\n# Subclassing works, too.\npackage Mint::Snuff;\nuse base qw(Altoids);\n\nmy Mint::Snuff $pouch = Mint::Snuff->new;\n$pouch->strong('Blow your head off!');\nprint $pouch->{strong};     # prints 'Blow your head off!'\n\nHere's a simple example of altering the behavior of your accessors.\n\npackage Foo;\nuse base qw(Class::Accessor);\nFoo->mkaccessors(qw(this that up down));\n\nsub get {\nmy $self = shift;\n\n# Note every time someone gets some data.\nprint STDERR \"Getting @\\n\";\n\n$self->SUPER::get(@);\n}\n\nsub set {\nmy ($self, $key) = splice(@, 0, 2);\n\n# Note every time someone sets some data.\nprint STDERR \"Setting $key to @\\n\";\n\n$self->SUPER::set($key, @);\n}\n\n### CAVEATS AND TRICKS\n\nClass::Accessor has to do some internal wackiness to get its job done quickly and efficiently.\nBecause of this, there's a few tricks and traps one must know about.\n\nHey, nothing's perfect.\n\n#### Don't make a field called DESTROY\n\nThis is bad. Since DESTROY is a magical method it would be bad for us to define an accessor\nusing that name. Class::Accessor will carp if you try to use it with a field named \"DESTROY\".\n\n#### Overriding autogenerated accessors\n\nYou may want to override the autogenerated accessor with your own, yet have your custom accessor\ncall the default one. For instance, maybe you want to have an accessor which checks its input.\nNormally, one would expect this to work:\n\npackage Foo;\nuse base qw(Class::Accessor);\nFoo->mkaccessors(qw(email this that whatever));\n\n# Only accept addresses which look valid.\nsub email {\nmy($self) = shift;\nmy($email) = @;\n\nif( @ ) {  # Setting\nrequire Email::Valid;\nunless( Email::Valid->address($email) ) {\ncarp(\"$email doesn't look like a valid address.\");\nreturn;\n}\n}\n\nreturn $self->SUPER::email(@);\n}\n\nThere's a subtle problem in the last example, and it's in this line:\n\nreturn $self->SUPER::email(@);\n\nIf we look at how Foo was defined, it called mkaccessors() which stuck email() right into Foo's\nnamespace. There *is* no SUPER::email() to delegate to! Two ways around this... first is to make\na \"pure\" base class for Foo. This pure class will generate the accessors and provide the\nnecessary super class for Foo to use:\n\npackage Pure::Organic::Foo;\nuse base qw(Class::Accessor);\nPure::Organic::Foo->mkaccessors(qw(email this that whatever));\n\npackage Foo;\nuse base qw(Pure::Organic::Foo);\n\nAnd now Foo::email() can override the generated Pure::Organic::Foo::email() and use it as\nSUPER::email().\n\nThis is probably the most obvious solution to everyone but me. Instead, what first made sense to\nme was for mkaccessors() to define an alias of email(), emailaccessor(). Using this solution,\nFoo::email() would be written with:\n\nreturn $self->emailaccessor(@);\n\ninstead of the expected SUPER::email().\n\n### AUTHORS\n\nCopyright 2017 Marty Pauley <marty+perl@martian.org>\n\nThis program is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself. That means either (a) the GNU General Public License or (b) the Artistic License.\n\nORIGINAL AUTHOR\nMichael G Schwern <schwern@pobox.com>\n\nTHANKS\nLiz and RUZ for performance tweaks.\n\nTels, for his big feature request/bug report.\n\nVarious presenters at YAPC::Asia 2009 for criticising the non-Moose interface.\n\n### SEE ALSO\n\nSee Class::Accessor::Fast and Class::Accessor::Faster if speed is more important than\nflexibility.\n\nThese are some modules which do similar things in different ways Class::Struct,\nClass::Methodmaker, Class::Generate, Class::Class, Class::Contract, Moose, Mouse\n\nSee Class::DBI for an example of this module in use.\n\n"
        }
    ],
    "structuredContent": {
        "command": "Class::Accessor",
        "section": "",
        "mode": "perldoc",
        "summary": "Class::Accessor - Automated accessor generation",
        "synopsis": "package Foo;\nuse base qw(Class::Accessor);\nFoo->followbestpractice;\nFoo->mkaccessors(qw(name role salary));\n# or if you prefer a Moose-like interface...\npackage Foo;\nuse Class::Accessor \"antlers\";\nhas name => ( is => \"rw\", isa => \"Str\" );\nhas role => ( is => \"rw\", isa => \"Str\" );\nhas salary => ( is => \"rw\", isa => \"Num\" );\n# Meanwhile, in a nearby piece of code!\n# Class::Accessor provides new().\nmy $mp = Foo->new({ name => \"Marty\", role => \"JAPH\" });\nmy $job = $mp->role;  # gets $mp->{role}\n$mp->salary(400000);  # sets $mp->{salary} = 400000 # I wish\n# like my @info = @{$mp}{qw(name role)}\nmy @info = $mp->get(qw(name role));\n# $mp->{salary} = 400000\n$mp->set('salary', 400000);",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [
            "Here's an example of generating an accessor for every public field of your class.",
            "package Altoids;",
            "use base qw(Class::Accessor Class::Fields);",
            "use fields qw(curiously strong mints);",
            "Altoids->mkaccessors( Altoids->showfields('Public') );",
            "sub new {",
            "my $proto = shift;",
            "my $class = ref $proto || $proto;",
            "return fields::new($class);",
            "my Altoids $tin = Altoids->new;",
            "$tin->curiously('Curiouser and curiouser');",
            "print $tin->{curiously};    # prints 'Curiouser and curiouser'",
            "# Subclassing works, too.",
            "package Mint::Snuff;",
            "use base qw(Altoids);",
            "my Mint::Snuff $pouch = Mint::Snuff->new;",
            "$pouch->strong('Blow your head off!');",
            "print $pouch->{strong};     # prints 'Blow your head off!'",
            "Here's a simple example of altering the behavior of your accessors.",
            "package Foo;",
            "use base qw(Class::Accessor);",
            "Foo->mkaccessors(qw(this that up down));",
            "sub get {",
            "my $self = shift;",
            "# Note every time someone gets some data.",
            "print STDERR \"Getting @\\n\";",
            "$self->SUPER::get(@);",
            "sub set {",
            "my ($self, $key) = splice(@, 0, 2);",
            "# Note every time someone sets some data.",
            "print STDERR \"Setting $key to @\\n\";",
            "$self->SUPER::set($key, @);"
        ],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 26,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 28,
                "subsections": [
                    {
                        "name": "mk_accessors",
                        "lines": 19
                    },
                    {
                        "name": "Moose-like",
                        "lines": 10
                    }
                ]
            },
            {
                "name": "CONSTRUCTOR",
                "lines": 22,
                "subsections": []
            },
            {
                "name": "MAKING ACCESSORS",
                "lines": 88,
                "subsections": []
            },
            {
                "name": "DETAILS",
                "lines": 17,
                "subsections": [
                    {
                        "name": "Modifying the behavior of the accessor",
                        "lines": 9
                    },
                    {
                        "name": "set",
                        "lines": 6
                    },
                    {
                        "name": "get",
                        "lines": 27
                    }
                ]
            },
            {
                "name": "EXCEPTIONS",
                "lines": 4,
                "subsections": []
            },
            {
                "name": "EFFICIENCY",
                "lines": 34,
                "subsections": []
            },
            {
                "name": "EXAMPLES",
                "lines": 52,
                "subsections": []
            },
            {
                "name": "CAVEATS AND TRICKS",
                "lines": 5,
                "subsections": [
                    {
                        "name": "Don't make a field called DESTROY",
                        "lines": 3
                    },
                    {
                        "name": "Overriding autogenerated accessors",
                        "lines": 51
                    }
                ]
            },
            {
                "name": "AUTHORS",
                "lines": 15,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 8,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "Class::Accessor - Automated accessor generation\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "package Foo;\nuse base qw(Class::Accessor);\nFoo->followbestpractice;\nFoo->mkaccessors(qw(name role salary));\n\n# or if you prefer a Moose-like interface...\n\npackage Foo;\nuse Class::Accessor \"antlers\";\nhas name => ( is => \"rw\", isa => \"Str\" );\nhas role => ( is => \"rw\", isa => \"Str\" );\nhas salary => ( is => \"rw\", isa => \"Num\" );\n\n# Meanwhile, in a nearby piece of code!\n# Class::Accessor provides new().\nmy $mp = Foo->new({ name => \"Marty\", role => \"JAPH\" });\n\nmy $job = $mp->role;  # gets $mp->{role}\n$mp->salary(400000);  # sets $mp->{salary} = 400000 # I wish\n\n# like my @info = @{$mp}{qw(name role)}\nmy @info = $mp->get(qw(name role));\n\n# $mp->{salary} = 400000\n$mp->set('salary', 400000);\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "This module automagically generates accessors/mutators for your class.\n\nMost of the time, writing accessors is an exercise in cutting and pasting. You usually wind up\nwith a series of methods like this:\n\nsub name {\nmy $self = shift;\nif(@) {\n$self->{name} = $[0];\n}\nreturn $self->{name};\n}\n\nsub salary {\nmy $self = shift;\nif(@) {\n$self->{salary} = $[0];\n}\nreturn $self->{salary};\n}\n\n# etc...\n\nOne for each piece of data in your object. While some will be unique, doing value checks and\nspecial storage tricks, most will simply be exercises in repetition. Not only is it Bad Style to\nhave a bunch of repetitious code, but it's also simply not lazy, which is the real tragedy.\n\nIf you make your module a subclass of Class::Accessor and declare your accessor fields with",
                "subsections": [
                    {
                        "name": "mk_accessors",
                        "content": "can even be customized!\n\nThe basic set up is very simple:\n\npackage Foo;\nuse base qw(Class::Accessor);\nFoo->mkaccessors( qw(far bar car) );\n\nDone. Foo now has simple far(), bar() and car() accessors defined.\n\nAlternatively, if you want to follow Damian's *best practice* guidelines you can use:\n\npackage Foo;\nuse base qw(Class::Accessor);\nFoo->followbestpractice;\nFoo->mkaccessors( qw(far bar car) );\n\nNote: you must call \"followbestpractice\" before calling \"mkaccessors\".\n"
                    },
                    {
                        "name": "Moose-like",
                        "content": "By popular demand we now have a simple Moose-like interface. You can now do:\n\npackage Foo;\nuse Class::Accessor \"antlers\";\nhas far => ( is => \"rw\" );\nhas bar => ( is => \"rw\" );\nhas car => ( is => \"rw\" );\n\nCurrently only the \"is\" attribute is supported.\n"
                    }
                ]
            },
            "CONSTRUCTOR": {
                "content": "Class::Accessor provides a basic constructor, \"new\". It generates a hash-based object and can be\ncalled as either a class method or an object method.\n\nnew\nmy $obj = Foo->new;\nmy $obj = $otherobj->new;\n\nmy $obj = Foo->new(\\%fields);\nmy $obj = $otherobj->new(\\%fields);\n\nIt takes an optional %fields hash which is used to initialize the object (handy if you use\nread-only accessors). The fields of the hash correspond to the names of your accessors, so...\n\npackage Foo;\nuse base qw(Class::Accessor);\nFoo->mkaccessors('foo');\n\nmy $obj = Foo->new({ foo => 42 });\nprint $obj->foo;    # 42\n\nhowever %fields can contain anything, new() will shove them all into your object.\n",
                "subsections": []
            },
            "MAKING ACCESSORS": {
                "content": "followbestpractice\nIn Damian's Perl Best Practices book he recommends separate get and set methods with the prefix\nset and get to make it explicit what you intend to do. If you want to create those accessor\nmethods instead of the default ones, call:\n\nPACKAGE->followbestpractice\n\nbefore you call any of the accessor-making methods.\n\naccessornamefor / mutatornamefor\nYou may have your own crazy ideas for the names of the accessors, so you can make those happen\nby overriding \"accessornamefor\" and \"mutatornamefor\" in your subclass. (I copied that idea\nfrom Class::DBI.)\n\nmkaccessors\nPACKAGE->mkaccessors(@fields);\n\nThis creates accessor/mutator methods for each named field given in @fields. Foreach field in\n@fields it will generate two accessors. One called \"field()\" and the other called\n\"fieldaccessor()\". For example:\n\n# Generates foo(), fooaccessor(), bar() and baraccessor().\nPACKAGE->mkaccessors(qw(foo bar));\n\nSee \"Overriding autogenerated accessors\" in CAVEATS AND TRICKS for details.\n\nmkroaccessors\nPACKAGE->mkroaccessors(@readonlyfields);\n\nSame as mkaccessors() except it will generate read-only accessors (ie. true accessors). If you\nattempt to set a value with these accessors it will throw an exception. It only uses get() and\nnot set().\n\npackage Foo;\nuse base qw(Class::Accessor);\nFoo->mkroaccessors(qw(foo bar));\n\n# Let's assume we have an object $foo of class Foo...\nprint $foo->foo;  # ok, prints whatever the value of $foo->{foo} is\n$foo->foo(42);    # BOOM!  Naughty you.\n\nmkwoaccessors\nPACKAGE->mkwoaccessors(@writeonlyfields);\n\nSame as mkaccessors() except it will generate write-only accessors (ie. mutators). If you\nattempt to read a value with these accessors it will throw an exception. It only uses set() and\nnot get().\n\nNOTE I'm not entirely sure why this is useful, but I'm sure someone will need it. If you've\nfound a use, let me know. Right now it's here for orthogonality and because it's easy to\nimplement.\n\npackage Foo;\nuse base qw(Class::Accessor);\nFoo->mkwoaccessors(qw(foo bar));\n\n# Let's assume we have an object $foo of class Foo...\n$foo->foo(42);      # OK.  Sets $self->{foo} = 42\nprint $foo->foo;    # BOOM!  Can't read from this accessor.\n\nMoose!\nIf you prefer a Moose-like interface to create accessors, you can use \"has\" by importing this\nmodule like this:\n\nuse Class::Accessor \"antlers\";\n\nor\n\nuse Class::Accessor \"moose-like\";\n\nThen you can declare accessors like this:\n\nhas alpha => ( is => \"rw\", isa => \"Str\" );\nhas beta  => ( is => \"ro\", isa => \"Str\" );\nhas gamma => ( is => \"wo\", isa => \"Str\" );\n\nCurrently only the \"is\" attribute is supported. And our \"is\" also supports the \"wo\" value to\nmake a write-only accessor.\n\nIf you are using the Moose-like interface then you should use the \"extends\" rather than tweaking\nyour @ISA directly. Basically, replace\n\n@ISA = qw/Foo Bar/;\n\nwith\n\nextends(qw/Foo Bar/);\n",
                "subsections": []
            },
            "DETAILS": {
                "content": "An accessor generated by Class::Accessor looks something like this:\n\n# Your foo may vary.\nsub foo {\nmy($self) = shift;\nif(@) {    # set\nreturn $self->set('foo', @);\n}\nelse {\nreturn $self->get('foo');\n}\n}\n\nVery simple. All it does is determine if you're wanting to set a value or get a value and calls\nthe appropriate method. Class::Accessor provides default get() and set() methods which your\nclass can override. They're detailed later.\n",
                "subsections": [
                    {
                        "name": "Modifying the behavior of the accessor",
                        "content": "Rather than actually modifying the accessor itself, it is much more sensible to simply override\nthe two key methods which the accessor calls. Namely set() and get().\n\nIf you -really- want to, you can override makeaccessor().\n\nset\n$obj->set($key, $value);\n$obj->set($key, @values);\n"
                    },
                    {
                        "name": "set",
                        "content": "override this method to change how data is stored by your accessors.\n\nget\n$value  = $obj->get($key);\n@values = $obj->get(@keys);\n"
                    },
                    {
                        "name": "get",
                        "content": "override this method to change how it is retrieved.\n\nmakeaccessor\n$accessor = PACKAGE->makeaccessor($field);\n\nGenerates a subroutine reference which acts as an accessor for the given $field. It calls get()\nand set().\n\nIf you wish to change the behavior of your accessors, try overriding get() and set() before you\nstart mucking with makeaccessor().\n\nmakeroaccessor\n$readonlyaccessor = PACKAGE->makeroaccessor($field);\n\nGenerates a subroutine reference which acts as a read-only accessor for the given $field. It\nonly calls get().\n\nOverride get() to change the behavior of your accessors.\n\nmakewoaccessor\n$writeonlyaccessor = PACKAGE->makewoaccessor($field);\n\nGenerates a subroutine reference which acts as a write-only accessor (mutator) for the given\n$field. It only calls set().\n\nOverride set() to change the behavior of your accessors.\n"
                    }
                ]
            },
            "EXCEPTIONS": {
                "content": "If something goes wrong Class::Accessor will warn or die by calling Carp::carp or Carp::croak.\nIf you don't like this you can override carp() and croak() in your subclass and do whatever\nelse you want.\n",
                "subsections": []
            },
            "EFFICIENCY": {
                "content": "Class::Accessor does not employ an autoloader, thus it is much faster than you'd think. Its\ngenerated methods incur no special penalty over ones you'd write yourself.\n\naccessors:\nRate  Basic   Fast Faster Direct\nBasic   367589/s     --   -51%   -55%   -89%\nFast    747964/s   103%     --    -9%   -77%\nFaster  819199/s   123%    10%     --   -75%\nDirect 3245887/s   783%   334%   296%     --\n\nmutators:\nRate    Acc   Fast Faster Direct\nAcc     265564/s     --   -54%   -63%   -91%\nFast    573439/s   116%     --   -21%   -80%\nFaster  724710/s   173%    26%     --   -75%\nDirect 2860979/s   977%   399%   295%     --\n\nClass::Accessor::Fast is faster than methods written by an average programmer (where \"average\"\nis based on Schwern's example code).\n\nClass::Accessor is slower than average, but more flexible.\n\nClass::Accessor::Faster is even faster than Class::Accessor::Fast. It uses an array internally,\nnot a hash. This could be a good or bad feature depending on your point of view.\n\nDirect hash access is, of course, much faster than all of these, but it provides no\nencapsulation.\n\nOf course, it's not as simple as saying \"Class::Accessor is slower than average\". These are\nbenchmarks for a simple accessor. If your accessors do any sort of complicated work (such as\ntalking to a database or writing to a file) the time spent doing that work will quickly swamp\nthe time spend just calling the accessor. In that case, Class::Accessor and the ones you write\nwill be roughly the same speed.\n",
                "subsections": []
            },
            "EXAMPLES": {
                "content": "Here's an example of generating an accessor for every public field of your class.\n\npackage Altoids;\n\nuse base qw(Class::Accessor Class::Fields);\nuse fields qw(curiously strong mints);\nAltoids->mkaccessors( Altoids->showfields('Public') );\n\nsub new {\nmy $proto = shift;\nmy $class = ref $proto || $proto;\nreturn fields::new($class);\n}\n\nmy Altoids $tin = Altoids->new;\n\n$tin->curiously('Curiouser and curiouser');\nprint $tin->{curiously};    # prints 'Curiouser and curiouser'\n\n\n# Subclassing works, too.\npackage Mint::Snuff;\nuse base qw(Altoids);\n\nmy Mint::Snuff $pouch = Mint::Snuff->new;\n$pouch->strong('Blow your head off!');\nprint $pouch->{strong};     # prints 'Blow your head off!'\n\nHere's a simple example of altering the behavior of your accessors.\n\npackage Foo;\nuse base qw(Class::Accessor);\nFoo->mkaccessors(qw(this that up down));\n\nsub get {\nmy $self = shift;\n\n# Note every time someone gets some data.\nprint STDERR \"Getting @\\n\";\n\n$self->SUPER::get(@);\n}\n\nsub set {\nmy ($self, $key) = splice(@, 0, 2);\n\n# Note every time someone sets some data.\nprint STDERR \"Setting $key to @\\n\";\n\n$self->SUPER::set($key, @);\n}\n",
                "subsections": []
            },
            "CAVEATS AND TRICKS": {
                "content": "Class::Accessor has to do some internal wackiness to get its job done quickly and efficiently.\nBecause of this, there's a few tricks and traps one must know about.\n\nHey, nothing's perfect.\n",
                "subsections": [
                    {
                        "name": "Don't make a field called DESTROY",
                        "content": "This is bad. Since DESTROY is a magical method it would be bad for us to define an accessor\nusing that name. Class::Accessor will carp if you try to use it with a field named \"DESTROY\".\n"
                    },
                    {
                        "name": "Overriding autogenerated accessors",
                        "content": "You may want to override the autogenerated accessor with your own, yet have your custom accessor\ncall the default one. For instance, maybe you want to have an accessor which checks its input.\nNormally, one would expect this to work:\n\npackage Foo;\nuse base qw(Class::Accessor);\nFoo->mkaccessors(qw(email this that whatever));\n\n# Only accept addresses which look valid.\nsub email {\nmy($self) = shift;\nmy($email) = @;\n\nif( @ ) {  # Setting\nrequire Email::Valid;\nunless( Email::Valid->address($email) ) {\ncarp(\"$email doesn't look like a valid address.\");\nreturn;\n}\n}\n\nreturn $self->SUPER::email(@);\n}\n\nThere's a subtle problem in the last example, and it's in this line:\n\nreturn $self->SUPER::email(@);\n\nIf we look at how Foo was defined, it called mkaccessors() which stuck email() right into Foo's\nnamespace. There *is* no SUPER::email() to delegate to! Two ways around this... first is to make\na \"pure\" base class for Foo. This pure class will generate the accessors and provide the\nnecessary super class for Foo to use:\n\npackage Pure::Organic::Foo;\nuse base qw(Class::Accessor);\nPure::Organic::Foo->mkaccessors(qw(email this that whatever));\n\npackage Foo;\nuse base qw(Pure::Organic::Foo);\n\nAnd now Foo::email() can override the generated Pure::Organic::Foo::email() and use it as\nSUPER::email().\n\nThis is probably the most obvious solution to everyone but me. Instead, what first made sense to\nme was for mkaccessors() to define an alias of email(), emailaccessor(). Using this solution,\nFoo::email() would be written with:\n\nreturn $self->emailaccessor(@);\n\ninstead of the expected SUPER::email().\n"
                    }
                ]
            },
            "AUTHORS": {
                "content": "Copyright 2017 Marty Pauley <marty+perl@martian.org>\n\nThis program is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself. That means either (a) the GNU General Public License or (b) the Artistic License.\n\nORIGINAL AUTHOR\nMichael G Schwern <schwern@pobox.com>\n\nTHANKS\nLiz and RUZ for performance tweaks.\n\nTels, for his big feature request/bug report.\n\nVarious presenters at YAPC::Asia 2009 for criticising the non-Moose interface.\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "See Class::Accessor::Fast and Class::Accessor::Faster if speed is more important than\nflexibility.\n\nThese are some modules which do similar things in different ways Class::Struct,\nClass::Methodmaker, Class::Generate, Class::Class, Class::Contract, Moose, Mouse\n\nSee Class::DBI for an example of this module in use.\n",
                "subsections": []
            }
        }
    }
}