1Class::XSAccessor::ArraUys(e3r)Contributed Perl DocumentCaltaisosn::XSAccessor::Array(3)
2
3
4
6 Class::XSAccessor::Array - Generate fast XS accessors without runtime
7 compilation
8
10 package MyClassUsingArraysAsInternalStorage;
11 use Class::XSAccessor::Array
12 constructor => 'new',
13 getters => {
14 get_foo => 0, # 0 is the array index to access
15 get_bar => 1,
16 },
17 setters => {
18 set_foo => 0,
19 set_bar => 1,
20 },
21 accessors => { # a mutator
22 buz => 2,
23 },
24 predicates => { # test for definedness
25 has_buz => 2,
26 },
27 true => [ 'is_token', 'is_whitespace' ],
28 false => [ 'significant' ];
29
30 # The imported methods are implemented in fast XS.
31
32 # normal class code here.
33
34 As of version 1.05, some alternative syntax forms are available:
35
36 package MyClass;
37
38 # Options can be passed as a HASH reference if you prefer it,
39 # which can also help PerlTidy to flow the statement correctly.
40 use Class::XSAccessor {
41 getters => {
42 get_foo => 0,
43 get_bar => 1,
44 },
45 };
46
48 The module implements fast XS accessors both for getting at and setting
49 an object attribute. Additionally, the module supports mutators and
50 simple predicates ("has_foo()" like tests for definedness of an
51 attributes). The module works only with objects that are implemented
52 as arrays. Using it on hash-based objects is bound to make your life
53 miserable. Refer to Class::XSAccessor for an implementation that works
54 with hash-based objects.
55
56 A simple benchmark showed more than a factor of two performance
57 advantage over writing accessors in Perl.
58
59 Since version 0.10, the module can also generate simple constructors
60 (implemented in XS) for you. Simply supply the "constructor =>
61 'constructor_name'" option or the "constructors => ['new', 'create',
62 'spawn']" option. These constructors do the equivalent of the
63 following Perl code:
64
65 sub new {
66 my $class = shift;
67 return bless [], ref($class)||$class;
68 }
69
70 That means they can be called on objects and classes but will not clone
71 objects entirely. Note that any parameters to new() will be discarded!
72 If there is a better idiom for array-based objects, let me know.
73
74 While generally more obscure than hash-based objects, objects using
75 blessed arrays as internal representation are a bit faster as its
76 somewhat faster to access arrays than hashes. Accordingly, this module
77 is slightly faster (~10-15%) than Class::XSAccessor, which works on
78 hash-based objects.
79
80 The method names may be fully qualified. In the example of the
81 synopsis, you could have written "MyClass::get_foo" instead of
82 "get_foo". This way, you can install methods in classes other than the
83 current class. See also: The "class" option below.
84
85 Since version 1.01, you can generate extremely simple methods which
86 just return true or false (and always do so). If that seems like a
87 really superfluous thing to you, then think of a large class hierarchy
88 with interfaces such as PPI. This is implemented as the "true" and
89 "false" options, see synopsis.
90
92 In addition to specifying the types and names of accessors, you can add
93 options which modify behaviour. The options are specified as key/value
94 pairs just as the accessor declaration. Example:
95
96 use Class::XSAccessor::Array
97 getters => {
98 get_foo => 0,
99 },
100 replace => 1;
101
102 The list of available options is:
103
104 replace
105 Set this to a true value to prevent "Class::XSAccessor::Array" from
106 complaining about replacing existing subroutines.
107
108 chained
109 Set this to a true value to change the return value of setters and
110 mutators (when called with an argument). If "chained" is enabled, the
111 setters and accessors/mutators will return the object. Mutators called
112 without an argument still return the value of the associated attribute.
113
114 As with the other options, "chained" affects all methods generated in
115 the same "use Class::XSAccessor::Array ..." statement.
116
117 class
118 By default, the accessors are generated in the calling class. Using the
119 "class" option, you can explicitly specify where the methods are to be
120 generated.
121
123 Probably wouldn't work if your objects are tied. But that's a strange
124 thing to do anyway.
125
126 Scary code exploiting strange XS features.
127
128 If you think writing an accessor in XS should be a laughably simple
129 exercise, then please contemplate how you could instantiate a new XS
130 accessor for a new hash key or array index that's only known at run-
131 time. Note that compiling C code at run-time a la Inline::C is a no go.
132
133 Threading. With version 1.00, a memory leak has been fixed that would
134 leak a small amount of memory if you loaded "Class::XSAccessor"-based
135 classes in a subthread that hadn't been loaded in the "main" thread
136 before. If the subthread then terminated, a hash key and an int per
137 associated method used ot be lost. Note that this mattered only if
138 classes were only loaded in a sort of throw-away thread.
139
140 In the new implementation as of 1.00, the memory will not be released
141 again either in the above situation. But it will be recycled when the
142 same class or a similar class is loaded again in any thread.
143
145 Class::XSAccessor
146
147 AutoXS
148
150 Steffen Mueller <smueller@cpan.org>
151
153 Copyright (C) 2008-2009 by Steffen Mueller
154
155 This library is free software; you can redistribute it and/or modify it
156 under the same terms as Perl itself, either Perl version 5.8 or, at
157 your option, any later version of Perl 5 you may have available.
158
159
160
161perl v5.12.1 2010-08-15 Class::XSAccessor::Array(3)