]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - README
Importing Variable-Magic-0.09.tar.gz
[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.09
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     'len' magic is no longer called when pushing an element into a magic
99     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 CONSTANTS
108   "SIG_MIN"
109     The minimum integer used as a signature for user-defined magic.
110
111   "SIG_MAX"
112     The maximum integer used as a signature for user-defined magic.
113
114   "SIG_NBR"
115         SIG_NBR = SIG_MAX - SIG_MIN + 1
116
117   "MGf_COPY"
118     Evaluates to true iff the 'copy' magic is available.
119
120   "MGf_DUP"
121     Evaluates to true iff the 'dup' magic is available.
122
123   "MGf_LOCAL"
124     Evaluates to true iff the 'local' magic is available.
125
126   "VMG_UVAR"
127     When this constant is true, you can use the "fetch,store,exists,delete"
128     callbacks on hashes.
129
130 FUNCTIONS
131   "wizard"
132         wizard sig    => ...,
133                data   => sub { ... },
134                get    => sub { my ($ref, $data) = @_; ... },
135                set    => sub { my ($ref, $data) = @_; ... },
136                len    => sub { my ($ref, $data, $len) = @_; ... ; return $newlen; },
137                clear  => sub { my ($ref, $data) = @_; ... },
138                free   => sub { my ($ref, $data) = @_, ... },
139                copy   => sub { my ($ref, $data, $elt) = @_; ... },
140                local  => sub { my ($ref, $data) = @_; ... },
141                fetch  => sub { my ($ref, $data, $key) = @_; ... },
142                store  => sub { my ($ref, $data, $key) = @_; ... },
143                exists => sub { my ($ref, $data, $key) = @_; ... },
144                delete => sub { my ($ref, $data, $key) = @_; ... }
145
146     This function creates a 'wizard', an opaque type that holds the magic
147     information. It takes a list of keys / values as argument, whose keys
148     can be :
149
150     "sig"
151         The numerical signature. If not specified or undefined, a random
152         signature is generated. If the signature matches an already defined
153         magic, then the existant magic object is returned.
154
155     "data"
156         A code reference to a private data constructor. It is called each
157         time this magic is cast on a variable, and the scalar returned is
158         used as private data storage for it. $_[0] is a reference to the
159         magic object and @_[1 .. @_-1] are all extra arguments that were
160         passed to "cast".
161
162     "get", "set", "len", "clear", "free", "copy", "local", "fetch", "store",
163     "exists" and "delete"
164         Code references to corresponding magic callbacks. You don't have to
165         specify all of them : the magic associated with undefined entries
166         simply won't be hooked. In those callbacks, $_[0] is always a
167         reference to the magic object and $_[1] is always the private data
168         (or "undef" when no private data constructor was supplied). In the
169         special case of "len" magic and when the variable is an array, $_[2]
170         contains its normal length. "copy" magic receives the current
171         element (i.e. the value) in $_[2]. $_[2] is also the current key in
172         "fetch", "store", "exists" and "delete" callbacks.
173
174         # A simple scalar tracer
175         my $wiz = wizard get  => sub { print STDERR "got ${$_[0]}\n" },
176                          set  => sub { print STDERR "set to ${$_[0]}\n" },
177                          free => sub { print STDERR "${$_[0]} was deleted\n" }
178
179   "gensig"
180     With this tool, you can manually generate random magic signature between
181     SIG_MIN and SIG_MAX inclusive. That's the way "wizard" creates them when
182     no signature is supplied.
183
184         # Generate a signature
185         my $sig = gensig;
186
187   "getsig"
188         getsig $wiz
189
190     This accessor returns the magic signature of this wizard.
191
192         # Get $wiz signature
193         my $sig = getsig $wiz;
194
195   "cast"
196         cast [$@%&*]var, [$wiz|$sig], ...
197
198     This function associates $wiz magic to the variable supplied, without
199     overwriting any other kind of magic. You can also supply the numeric
200     signature $sig instead of $wiz. It returns true on success or when $wiz
201     magic is already present, 0 on error, and "undef" when no magic
202     corresponds to the given signature (in case $sig was supplied). All
203     extra arguments specified after $wiz are passed to the private data
204     constructor. If the variable isn't a hash, any "uvar" callback of the
205     wizard is safely ignored.
206
207         # Casts $wiz onto $x. If $wiz isn't a signature, undef can't be returned.
208         my $x;
209         die 'error' unless cast $x, $wiz;
210
211   "getdata"
212         getdata [$@%&*]var, [$wiz|$sig]
213
214     This accessor fetches the private data associated with the magic $wiz
215     (or the signature $sig) in the variable. "undef" is returned when no
216     such magic or data is found, or when $sig does not represent a current
217     valid magic object.
218
219         # Get the attached data.
220         my $data = getdata $x, $wiz or die 'no such magic or magic has no data';
221
222   "dispell"
223         dispell [$@%&*]variable, [$wiz|$sig]
224
225     The exact opposite of "cast" : it dissociates $wiz magic from the
226     variable. You can also pass the magic signature $sig as the second
227     argument. True is returned on success, 0 on error or when no magic
228     represented by $wiz could be found in the variable, and "undef" when no
229     magic corresponds to the given signature (in case $sig was supplied).
230
231         # Dispell now. If $wiz isn't a signature, undef can't be returned.
232         die 'no such magic or error' unless dispell $x, $wiz;
233
234 EXPORT
235     The functions "wizard", "gensig", "getsig", "cast", "getdata" and
236     "dispell" are only exported on request. All of them are exported by the
237     tags ':funcs' and ':all'.
238
239     The constants "SIG_MIN", "SIG_MAX", "SIG_NBR", "MGf_COPY", "MGf_DUP",
240     "MGf_LOCAL" and "VMG_UVAR" are also only exported on request. They are
241     all exported by the tags ':consts' and ':all'.
242
243 CAVEATS
244     If you store a magic object in the private data slot, the magic won't be
245     accessible by "getdata" since it's not copied by assignation. The only
246     way to address this would be to return a reference.
247
248     If you define a wizard with a "free" callback and cast it on itself,
249     this destructor won't be called because the wizard will be destroyed
250     first.
251
252 DEPENDENCIES
253     perl 5.7.3.
254
255     Carp (standard since perl 5), XSLoader (standard since perl 5.006).
256
257     Copy tests need Tie::Array (standard since perl 5.005) and Tie::Hash
258     (since 5.002).
259
260     Some uvar tests need Hash::Util::FieldHash (standard since perl
261     5.009004).
262
263     Glob tests need Symbol (standard since perl 5.002).
264
265 SEE ALSO
266     perlguts and perlapi for internal information about magic.
267
268     perltie and overload for other ways of enhancing objects.
269
270 AUTHOR
271     Vincent Pit, "<perl at profvince.com>"
272
273     You can contact me by mail or on #perl @ FreeNode (Prof_Vince).
274
275 BUGS
276     Please report any bugs or feature requests to "bug-variable-magic at
277     rt.cpan.org", or through the web interface at
278     <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Variable-Magic>. I will
279     be notified, and then you'll automatically be notified of progress on
280     your bug as I make changes.
281
282 SUPPORT
283     You can find documentation for this module with the perldoc command.
284
285         perldoc Variable::Magic
286
287 COPYRIGHT & LICENSE
288     Copyright 2007-2008 Vincent Pit, all rights reserved.
289
290     This program is free software; you can redistribute it and/or modify it
291     under the same terms as Perl itself.
292