{
    "mode": "perldoc",
    "parameter": "Class::XSAccessor",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Class%3A%3AXSAccessor/json",
    "generated": "2026-06-12T06:43:30Z",
    "synopsis": "package MyClass;\nuse Class::XSAccessor\nreplace     => 1,   # Replace existing methods (if any)\nconstructor => 'new',\ngetters     => {\ngetfoo => 'foo', # 'foo' is the hash key to access\ngetbar => 'bar',\n},\nsetters => {\nsetfoo => 'foo',\nsetbar => 'bar',\n},\naccessors => {\nfoo => 'foo',\nbar => 'bar',\n},\n# \"predicates\" is an alias for \"definedpredicates\"\ndefinedpredicates => {\ndefinedfoo => 'foo',\ndefinedbar => 'bar',\n},\nexistspredicates => {\nhasfoo => 'foo',\nhasbar => 'bar',\n},\nlvalueaccessors => { # see below\nbaz => 'baz', # ...\n},\ntrue  => [ 'istoken', 'iswhitespace' ],\nfalse => [ 'significant' ];\n# The imported methods are implemented in fast XS.\n# normal class code here.\nAs of version 1.05, some alternative syntax forms are available:\npackage MyClass;\n# Options can be passed as a HASH reference, if preferred,\n# which can also help Perl::Tidy to format the statement correctly.\nuse Class::XSAccessor {\n# If the name => key values are always identical,\n# the following shorthand can be used.\naccessors => [ 'foo', 'bar' ],\n};",
    "sections": {
        "NAME": {
            "content": "Class::XSAccessor - Generate fast XS accessors without runtime compilation\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package MyClass;\nuse Class::XSAccessor\nreplace     => 1,   # Replace existing methods (if any)\nconstructor => 'new',\ngetters     => {\ngetfoo => 'foo', # 'foo' is the hash key to access\ngetbar => 'bar',\n},\nsetters => {\nsetfoo => 'foo',\nsetbar => 'bar',\n},\naccessors => {\nfoo => 'foo',\nbar => 'bar',\n},\n# \"predicates\" is an alias for \"definedpredicates\"\ndefinedpredicates => {\ndefinedfoo => 'foo',\ndefinedbar => 'bar',\n},\nexistspredicates => {\nhasfoo => 'foo',\nhasbar => 'bar',\n},\nlvalueaccessors => { # see below\nbaz => 'baz', # ...\n},\ntrue  => [ 'istoken', 'iswhitespace' ],\nfalse => [ 'significant' ];\n\n# The imported methods are implemented in fast XS.\n\n# normal class code here.\n\nAs of version 1.05, some alternative syntax forms are available:\n\npackage MyClass;\n\n# Options can be passed as a HASH reference, if preferred,\n# which can also help Perl::Tidy to format the statement correctly.\nuse Class::XSAccessor {\n# If the name => key values are always identical,\n# the following shorthand can be used.\naccessors => [ 'foo', 'bar' ],\n};\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Class::XSAccessor implements fast read, write and read/write accessors in XS. Additionally, it\ncan provide predicates such as \"hasfoo()\" for testing whether the attribute \"foo\" exists in the\nobject (which is different from \"is defined within the object\"). It only works with objects that\nare implemented as ordinary hashes. Class::XSAccessor::Array implements the same interface for\nobjects that use arrays for their internal representation.\n\nSince version 0.10, the module can also generate simple constructors (implemented in XS). Simply\nsupply the \"constructor => 'constructorname'\" option or the \"constructors => ['new', 'create',\n'spawn']\" option. These constructors do the equivalent of the following Perl code:\n\nsub new {\nmy $class = shift;\nreturn bless { @ }, ref($class)||$class;\n}\n\nThat means they can be called on objects and classes but will not clone objects entirely.\nParameters to \"new()\" are added to the object.\n\nThe XS accessor methods are between 3 and 4 times faster than typical pure-Perl accessors in\nsome simple benchmarking. The lower factor applies to the potentially slightly obscure \"sub\nsetfoopp {$[0]->{foo} = $[1]}\", so if you usually write clear code, a factor of 3.5 speed-up\nis a good estimate. If in doubt, do your own benchmarking!\n\nThe method names may be fully qualified. The example in the synopsis could have been written as\n\"MyClass::getfoo\" instead of \"getfoo\". This way, methods can be installed in classes other\nthan the current class. See also: the \"class\" option below.\n\nBy default, the setters return the new value that was set, and the accessors (mutators) do the\nsame. This behaviour can be changed with the \"chained\" option - see below. The predicates return\na boolean.\n\nSince version 1.01, \"Class::XSAccessor\" can generate extremely simple methods which just return\ntrue or false (and always do so). If that seems like a really superfluous thing to you, then\nconsider a large class hierarchy with interfaces such as PPI. These methods are provided by the\n\"true\" and \"false\" options - see the synopsis.\n\n\"definedpredicates\" check whether a given object attribute is defined. \"predicates\" is an alias\nfor \"definedpredicates\" for compatibility with older versions of \"Class::XSAccessor\".\n\"existspredicates\" checks whether the given attribute exists in the object using \"exists\".\n",
            "subsections": []
        },
        "OPTIONS": {
            "content": "In addition to specifying the types and names of accessors, additional options can be supplied\nwhich modify behaviour. The options are specified as key/value pairs in the same manner as the\naccessor declaration. For example:\n\nuse Class::XSAccessor\ngetters => {\ngetfoo => 'foo',\n},\nreplace => 1;\n\nThe list of available options is:\n\nreplace\nSet this to a true value to prevent \"Class::XSAccessor\" from complaining about replacing\nexisting subroutines.\n\nchained\nSet this to a true value to change the return value of setters and mutators (when called with an\nargument). If \"chained\" is enabled, the setters and accessors/mutators will return the object.\nMutators called without an argument still return the value of the associated attribute.\n\nAs with the other options, \"chained\" affects all methods generated in the same \"use\nClass::XSAccessor ...\" statement.\n\nclass\nBy default, the accessors are generated in the calling class. The the \"class\" option allows the\ntarget class to be specified.\n",
            "subsections": []
        },
        "LVALUES": {
            "content": "Support for lvalue accessors via the keyword \"lvalueaccessors\" was added in version 1.08. At\nthis point, THEY ARE CONSIDERED HIGHLY EXPERIMENTAL. Furthermore, their performance hasn't been\nbenchmarked yet.\n\nThe following example demonstrates an lvalue accessor:\n\npackage Address;\nuse Class::XSAccessor\nconstructor => 'new',\nlvalueaccessors => { zipcode => 'zip' };\n\npackage main;\nmy $address = Address->new(zip => 2);\nprint $address->zipcode, \"\\n\"; # prints 2\n$address->zipcode = 76135; # <--- This is it!\nprint $address->zipcode, \"\\n\"; # prints 76135\n",
            "subsections": []
        },
        "CAVEATS": {
            "content": "Probably won't work for objects based on *tied* hashes. But that's a strange thing to do anyway.\n\nScary code exploiting strange XS features.\n\nIf you think writing an accessor in XS should be a laughably simple exercise, then please\ncontemplate how you could instantiate a new XS accessor for a new hash key that's only known at\nrun-time. Note that compiling C code at run-time a la Inline::C is a no go.\n\nThreading. With version 1.00, a memory leak has been fixed. Previously, a small amount of memory\nwould leak if \"Class::XSAccessor\"-based classes were loaded in a subthread without having been\nloaded in the \"main\" thread. If the subthread then terminated, a hash key and an int per\nassociated method used to be lost. Note that this mattered only if classes were only loaded in a\nsort of throw-away thread.\n\nIn the new implementation, as of 1.00, the memory will still not be released, in the same\nsituation, but it will be recycled when the same class, or a similar class, is loaded again in\nany thread.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "*   Class::XSAccessor::Array\n\n*   AutoXS\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Steffen Mueller <smueller@cpan.org>\n\nchocolateboy <chocolate@cpan.org>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 by Steffen Mueller\n\nThis library is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself, either Perl version 5.8 or, at your option, any later version of Perl 5 you may\nhave available.\n",
            "subsections": []
        }
    },
    "summary": "Class::XSAccessor - Generate fast XS accessors without runtime compilation",
    "flags": [],
    "examples": [],
    "see_also": []
}