]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - README
This is 0.21_01
[perl/modules/Variable-Magic.git] / README
1 NAME
2     Variable::Magic - Associate user-defined magic to variables from Perl.
3
4 VERSION
5     Version 0.21_01
6
7 SYNOPSIS
8         use Variable::Magic qw/wizard cast dispell/;
9
10         my $wiz = wizard set => sub { print STDERR "now set to ${$_[0]}!\n" };
11         my $a = 1;
12         cast $a, $wiz;
13         $a = 2;          # "now set to 2!"
14         dispell $a, $wiz;
15         $a = 3           # (nothing)
16
17 DESCRIPTION
18     Magic is Perl way of enhancing objects. This mechanism let the user add
19     extra data to any variable and overload syntaxical operations (such as
20     access, assignation or destruction) that can be applied to it. With this
21     module, you can add your own magic to any variable without the pain of
22     the C API.
23
24     The operations that can be overloaded are :
25
26     *   "get"
27
28         This magic is invoked when the variable is evaluated (does not
29         include array/hash subscripts and slices).
30
31     *   "set"
32
33         This one is triggered each time the value of the variable changes
34         (includes array/hash subscripts and slices).
35
36     *   "len"
37
38         This magic is a little special : it is called when the 'size' or the
39         'length' of the variable has to be known by Perl. Typically, it's
40         the magic involved when an array is evaluated in scalar context, but
41         also on array assignation and loops ("for", "map" or "grep"). The
42         callback has then to return the length as an integer.
43
44     *   "clear"
45
46         This magic is invoked when the variable is reset, such as when an
47         array is emptied. Please note that this is different from undefining
48         the variable, even though the magic is called when the clearing is a
49         result of the undefine (e.g. for an array, but actually a bug
50         prevent it to work before perl 5.9.5 - see the history).
51
52     *   "free"
53
54         This one can be considered as an object destructor. It happens when
55         the variable goes out of scope (with the exception of global scope),
56         but not when it is undefined.
57
58     *   "copy"
59
60         This magic only applies to tied arrays and hashes. It fires when you
61         try to access or change their elements. It is available on your perl
62         iff "MGf_COPY" is true.
63
64     *   "dup"
65
66         Invoked when the variable is cloned across threads. Currently not
67         available.
68
69     *   "local"
70
71         When this magic is set on a variable, all subsequent localizations
72         of the variable will trigger the callback. It is available on your
73         perl iff "MGf_LOCAL" is true.
74
75     The following actions only apply to hashes and are available iff
76     "VMG_UVAR" is true. They are referred to as "uvar" magics.
77
78     *   "fetch"
79
80         This magic happens each time an element is fetched from the hash.
81
82     *   "store"
83
84         This one is called when an element is stored into the hash.
85
86     *   "exists"
87
88         This magic fires when a key is tested for existence in the hash.
89
90     *   "delete"
91
92         This last one triggers when a key is deleted in the hash, regardless
93         of whether the key actually exists in it.
94
95     You can refer to the tests to have more insight of where the different
96     magics are invoked.
97
98     To prevent any clash between different magics defined with this module,
99     an unique numerical signature is attached to each kind of magic (i.e.
100     each set of callbacks for magic operations).
101
102 PERL MAGIC HISTORY
103     The places where magic is invoked have changed a bit through perl
104     history. Here's a little list of the most recent ones.
105
106     *   5.6.x
107
108         *p14416* : 'copy' and 'dup' magic.
109
110     *   5.9.3
111
112         *p25854* : 'len' magic is no longer called when pushing an element
113         into a magic array.
114
115         *p26569* : 'local' magic.
116
117     *   5.9.5
118
119         *p31064* : Meaningful 'uvar' magic.
120
121         *p31473* : 'clear' magic wasn't invoked when undefining an array.
122         The bug is fixed as of this version.
123
124     *   5.10.0
125
126         Since "PERL_MAGIC_uvar" is uppercased, "hv_magic_check()" triggers
127         'copy' magic on hash stores for (non-tied) hashes that also have
128         'uvar' magic.
129
130     *   5.11.x
131
132         *p32969* : 'len' magic is no longer invoked when calling "length"
133         with a magical scalar.
134
135 CONSTANTS
136   "SIG_MIN"
137     The minimum integer used as a signature for user-defined magic.
138
139   "SIG_MAX"
140     The maximum integer used as a signature for user-defined magic.
141
142   "SIG_NBR"
143         SIG_NBR = SIG_MAX - SIG_MIN + 1
144
145   "MGf_COPY"
146     Evaluates to true iff the 'copy' magic is available.
147
148   "MGf_DUP"
149     Evaluates to true iff the 'dup' magic is available.
150
151   "MGf_LOCAL"
152     Evaluates to true iff the 'local' magic is available.
153
154   "VMG_UVAR"
155     When this constant is true, you can use the "fetch,store,exists,delete"
156     callbacks on hashes.
157
158   "VMG_COMPAT_ARRAY_PUSH_NOLEN"
159     True for perls that don't call 'len' magic when you push an element in a
160     magical array.
161
162   "VMG_COMPAT_ARRAY_UNDEF_CLEAR"
163     True for perls that call 'clear' magic when undefining magical arrays.
164
165   "VMG_COMPAT_SCALAR_LENGTH_NOLEN"
166     True for perls that don't call 'len' magic when taking the "length" of a
167     magical scalar.
168
169   "VMG_PERL_PATCHLEVEL"
170     The perl patchlevel this module was built with, or 0 for non-debugging
171     perls.
172
173   "VMG_THREADSAFE"
174     True iff this module could have been built with thread-safety features
175     enabled.
176
177 FUNCTIONS
178   "wizard"
179         wizard sig    => ...,
180                data   => sub { ... },
181                get    => sub { my ($ref, $data) = @_; ... },
182                set    => sub { my ($ref, $data) = @_; ... },
183                len    => sub { my ($ref, $data, $len) = @_; ... ; return $newlen; },
184                clear  => sub { my ($ref, $data) = @_; ... },
185                free   => sub { my ($ref, $data) = @_, ... },
186                copy   => sub { my ($ref, $data, $key, $elt) = @_; ... },
187                local  => sub { my ($ref, $data) = @_; ... },
188                fetch  => sub { my ($ref, $data, $key) = @_; ... },
189                store  => sub { my ($ref, $data, $key) = @_; ... },
190                exists => sub { my ($ref, $data, $key) = @_; ... },
191                delete => sub { my ($ref, $data, $key) = @_; ... }
192
193     This function creates a 'wizard', an opaque type that holds the magic
194     information. It takes a list of keys / values as argument, whose keys
195     can be :
196
197     *   "sig"
198
199         The numerical signature. If not specified or undefined, a random
200         signature is generated. If the signature matches an already defined
201         magic, then the existant magic object is returned.
202
203     *   "data"
204
205         A code reference to a private data constructor. It is called each
206         time this magic is cast on a variable, and the scalar returned is
207         used as private data storage for it. $_[0] is a reference to the
208         magic object and @_[1 .. @_-1] are all extra arguments that were
209         passed to "cast".
210
211     *   "get", "set", "len", "clear", "free", "copy", "local", "fetch",
212         "store", "exists" and "delete"
213
214         Code references to corresponding magic callbacks. You don't have to
215         specify all of them : the magic associated with undefined entries
216         simply won't be hooked. In those callbacks, $_[0] is always a
217         reference to the magic object and $_[1] is always the private data
218         (or "undef" when no private data constructor was supplied). In the
219         special case of "len" magic and when the variable is an array, $_[2]
220         contains its normal length. $_[2] is the current key in "copy",
221         "fetch", "store", "exists" and "delete" callbacks, although for
222         "copy" it may just be a copy of the actual key so it's useless to
223         (for example) cast magic on it. "copy" magic also receives the
224         current element (i.e. the value) in $_[3].
225
226         # A simple scalar tracer
227         my $wiz = wizard get  => sub { print STDERR "got ${$_[0]}\n" },
228                          set  => sub { print STDERR "set to ${$_[0]}\n" },
229                          free => sub { print STDERR "${$_[0]} was deleted\n" }
230
231   "gensig"
232     With this tool, you can manually generate random magic signature between
233     SIG_MIN and SIG_MAX inclusive. That's the way "wizard" creates them when
234     no signature is supplied.
235
236         # Generate a signature
237         my $sig = gensig;
238
239   "getsig"
240         getsig $wiz
241
242     This accessor returns the magic signature of this wizard.
243
244         # Get $wiz signature
245         my $sig = getsig $wiz;
246
247   "cast"
248         cast [$@%&*]var, [$wiz|$sig], ...
249
250     This function associates $wiz magic to the variable supplied, without
251     overwriting any other kind of magic. You can also supply the numeric
252     signature $sig instead of $wiz. It returns true on success or when $wiz
253     magic is already present, 0 on error, and "undef" when no magic
254     corresponds to the given signature (in case $sig was supplied). All
255     extra arguments specified after $wiz are passed to the private data
256     constructor. If the variable isn't a hash, any "uvar" callback of the
257     wizard is safely ignored.
258
259         # Casts $wiz onto $x. If $wiz isn't a signature, undef can't be returned.
260         my $x;
261         die 'error' unless cast $x, $wiz;
262
263   "getdata"
264         getdata [$@%&*]var, [$wiz|$sig]
265
266     This accessor fetches the private data associated with the magic $wiz
267     (or the signature $sig) in the variable. "undef" is returned when no
268     such magic or data is found, or when $sig does not represent a current
269     valid magic object.
270
271         # Get the attached data.
272         my $data = getdata $x, $wiz or die 'no such magic or magic has no data';
273
274   "dispell"
275         dispell [$@%&*]variable, [$wiz|$sig]
276
277     The exact opposite of "cast" : it dissociates $wiz magic from the
278     variable. You can also pass the magic signature $sig as the second
279     argument. True is returned on success, 0 on error or when no magic
280     represented by $wiz could be found in the variable, and "undef" when no
281     magic corresponds to the given signature (in case $sig was supplied).
282
283         # Dispell now. If $wiz isn't a signature, undef can't be returned.
284         die 'no such magic or error' unless dispell $x, $wiz;
285
286 EXPORT
287     The functions "wizard", "gensig", "getsig", "cast", "getdata" and
288     "dispell" are only exported on request. All of them are exported by the
289     tags ':funcs' and ':all'.
290
291     The constants "SIG_MIN", "SIG_MAX", "SIG_NBR", "MGf_COPY", "MGf_DUP",
292     "MGf_LOCAL" and "VMG_UVAR" are also only exported on request. They are
293     all exported by the tags ':consts' and ':all'.
294
295 CAVEATS
296     If you store a magic object in the private data slot, the magic won't be
297     accessible by "getdata" since it's not copied by assignation. The only
298     way to address this would be to return a reference.
299
300     If you define a wizard with a "free" callback and cast it on itself,
301     this destructor won't be called because the wizard will be destroyed
302     first.
303
304 DEPENDENCIES
305     perl 5.7.3.
306
307     Carp (standard since perl 5), XSLoader (standard since perl 5.006).
308
309     Copy tests need Tie::Array (standard since perl 5.005) and Tie::Hash
310     (since 5.002).
311
312     Some uvar tests need Hash::Util::FieldHash (standard since perl
313     5.009004).
314
315     Glob tests need Symbol (standard since perl 5.002).
316
317     Threads tests need threads and threads::shared.
318
319 SEE ALSO
320     perlguts and perlapi for internal information about magic.
321
322     perltie and overload for other ways of enhancing objects.
323
324 AUTHOR
325     Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
326
327     You can contact me by mail or on #perl @ FreeNode (vincent or
328     Prof_Vince).
329
330 BUGS
331     Please report any bugs or feature requests to "bug-variable-magic at
332     rt.cpan.org", or through the web interface at
333     <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Variable-Magic>. I will
334     be notified, and then you'll automatically be notified of progress on
335     your bug as I make changes.
336
337 SUPPORT
338     You can find documentation for this module with the perldoc command.
339
340         perldoc Variable::Magic
341
342     Tests code coverage report is available at
343     <http://www.profvince.com/perl/cover/Variable-Magic>.
344
345 COPYRIGHT & LICENSE
346     Copyright 2007-2008 Vincent Pit, all rights reserved.
347
348     This program is free software; you can redistribute it and/or modify it
349     under the same terms as Perl itself.
350