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