]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - README
Jumbo POD overhaul
[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.49
6
7 SYNOPSIS
8         use Variable::Magic qw<wizard cast VMG_OP_INFO_NAME>;
9
10         { # A variable tracer
11          my $wiz = wizard(
12           set  => sub { print "now set to ${$_[0]}!\n" },
13           free => sub { print "destroyed!\n" },
14          );
15
16          my $a = 1;
17          cast $a, $wiz;
18          $a = 2;        # "now set to 2!"
19         }               # "destroyed!"
20
21         { # A hash with a default value
22          my $wiz = wizard(
23           data     => sub { $_[1] },
24           fetch    => sub { $_[2] = $_[1] unless exists $_[0]->{$_[2]}; () },
25           store    => sub { print "key $_[2] stored in $_[-1]\n" },
26           copy_key => 1,
27           op_info  => VMG_OP_INFO_NAME,
28          );
29
30          my %h = (_default => 0, apple => 2);
31          cast %h, $wiz, '_default';
32          print $h{banana}, "\n"; # "0" (there is no 'banana' key in %h)
33          $h{pear} = 1;           # "key pear stored in helem"
34         }
35
36 DESCRIPTION
37     Magic is Perl's way of enhancing variables. This mechanism lets the user
38     add extra data to any variable and hook syntactical operations (such as
39     access, assignment or destruction) that can be applied to it. With this
40     module, you can add your own magic to any variable without having to
41     write a single line of XS.
42
43     You'll realize that these magic variables look a lot like tied
44     variables. It's not surprising, as tied variables are implemented as a
45     special kind of magic, just like any 'irregular' Perl variable : scalars
46     like $!, $( or $^W, the %ENV and %SIG hashes, the @ISA array, "vec()"
47     and "substr()" lvalues, threads::shared variables... They all share the
48     same underlying C API, and this module gives you direct access to it.
49
50     Still, the magic made available by this module differs from tieing and
51     overloading in several ways :
52
53     *   It isn't copied on assignment.
54
55         You attach it to variables, not values (as for blessed references).
56
57     *   It doesn't replace the original semantics.
58
59         Magic callbacks usually get triggered before the original action
60         takes place, and can't prevent it from happening. This also makes
61         catching individual events easier than with "tie", where you have to
62         provide fallbacks methods for all actions by usually inheriting from
63         the correct "Tie::Std*" class and overriding individual methods in
64         your own class.
65
66     *   It's type-agnostic.
67
68         The same magic can be applied on scalars, arrays, hashes, subs or
69         globs. But the same hook (see below for a list) may trigger
70         differently depending on the the type of the variable.
71
72     *   It's mostly invisible at the Perl level.
73
74         Magical and non-magical variables cannot be distinguished with
75         "ref", "tied" or another trick.
76
77     *   It's notably faster.
78
79         Mainly because perl's way of handling magic is lighter by nature,
80         and because there's no need for any method resolution. Also, since
81         you don't have to reimplement all the variable semantics, you only
82         pay for what you actually use.
83
84     The operations that can be overloaded are :
85
86     *   "get"
87
88         This magic is invoked when the variable is evaluated. It is never
89         called for arrays and hashes.
90
91     *   "set"
92
93         This one is triggered each time the value of the variable changes.
94         It is called for array subscripts and slices, but never for hashes.
95
96     *   "len"
97
98         This magic is a little special : it is called when the 'size' or the
99         'length' of the variable has to be known by Perl. Typically, it's
100         the magic involved when an array is evaluated in scalar context, but
101         also on array assignment and loops ("for", "map" or "grep"). The
102         callback has then to return the length as an integer.
103
104     *   "clear"
105
106         This magic is invoked when the variable is reset, such as when an
107         array is emptied. Please note that this is different from undefining
108         the variable, even though the magic is called when the clearing is a
109         result of the undefine (e.g. for an array, but actually a bug
110         prevent it to work before perl 5.9.5 - see the history).
111
112     *   "free"
113
114         This one can be considered as an object destructor. It happens when
115         the variable goes out of scope, but not when it is undefined.
116
117     *   "copy"
118
119         This magic only applies to tied arrays and hashes. It fires when you
120         try to access or change their elements. It is available on your perl
121         iff "MGf_COPY" is true.
122
123     *   "dup"
124
125         Invoked when the variable is cloned across threads. Currently not
126         available.
127
128     *   "local"
129
130         When this magic is set on a variable, all subsequent localizations
131         of the variable will trigger the callback. It is available on your
132         perl iff "MGf_LOCAL" is true.
133
134     The following actions only apply to hashes and are available iff
135     "VMG_UVAR" is true. They are referred to as "uvar" magics.
136
137     *   "fetch"
138
139         This magic happens each time an element is fetched from the hash.
140
141     *   "store"
142
143         This one is called when an element is stored into the hash.
144
145     *   "exists"
146
147         This magic fires when a key is tested for existence in the hash.
148
149     *   "delete"
150
151         This last one triggers when a key is deleted in the hash, regardless
152         of whether the key actually exists in it.
153
154     You can refer to the tests to have more insight of where the different
155     magics are invoked.
156
157 FUNCTIONS
158   "wizard"
159         wizard(
160          data     => sub { ... },
161          get      => sub { my ($ref, $data [, $op]) = @_; ... },
162          set      => sub { my ($ref, $data [, $op]) = @_; ... },
163          len      => sub {
164           my ($ref, $data, $len [, $op]) = @_; ... ; return $newlen
165          },
166          clear    => sub { my ($ref, $data [, $op]) = @_; ... },
167          free     => sub { my ($ref, $data [, $op]) = @_, ... },
168          copy     => sub { my ($ref, $data, $key, $elt [, $op]) = @_; ... },
169          local    => sub { my ($ref, $data [, $op]) = @_; ... },
170          fetch    => sub { my ($ref, $data, $key [, $op]) = @_; ... },
171          store    => sub { my ($ref, $data, $key [, $op]) = @_; ... },
172          exists   => sub { my ($ref, $data, $key [, $op]) = @_; ... },
173          delete   => sub { my ($ref, $data, $key [, $op]) = @_; ... },
174          copy_key => $bool,
175          op_info  => [ 0 | VMG_OP_INFO_NAME | VMG_OP_INFO_OBJECT ],
176         )
177
178     This function creates a 'wizard', an opaque type that holds the magic
179     information. It takes a list of keys / values as argument, whose keys
180     can be :
181
182     *   "data"
183
184         A code (or string) reference to a private data constructor. It is
185         called each time this magic is cast on a variable, and the scalar
186         returned is used as private data storage for it. $_[0] is a
187         reference to the magic object and @_[1 .. @_-1] are all extra
188         arguments that were passed to "cast".
189
190     *   "get", "set", "len", "clear", "free", "copy", "local", "fetch",
191         "store", "exists" and "delete"
192
193         Code (or string) references to the corresponding magic callbacks.
194         You don't have to specify all of them : the magic associated with
195         undefined entries simply won't be hooked. In those callbacks, $_[0]
196         is always a reference to the magic object and $_[1] is always the
197         private data (or "undef" when no private data constructor was
198         supplied).
199
200         Moreover, when you pass "op_info => $num" to "wizard", the last
201         element of @_ will be the current op name if "$num ==
202         VMG_OP_INFO_NAME" and a "B::OP" object representing the current op
203         if "$num == VMG_OP_INFO_OBJECT". Both have a performance hit, but
204         just getting the name is lighter than getting the op object.
205
206         Other arguments are specific to the magic hooked :
207
208         *       "len"
209
210                 When the variable is an array or a scalar, $_[2] contains
211                 the non-magical length. The callback can return the new
212                 scalar or array length to use, or "undef" to default to the
213                 normal length.
214
215         *       "copy"
216
217                 $_[2] is a either a copy or an alias of the current key,
218                 which means that it is useless to try to change or cast
219                 magic on it. $_[3] is an alias to the current element (i.e.
220                 the value).
221
222         *       "fetch", "store", "exists" and "delete"
223
224                 $_[2] is an alias to the current key. Nothing prevents you
225                 from changing it, but be aware that there lurk dangerous
226                 side effects. For example, it may rightfully be readonly if
227                 the key was a bareword. You can get a copy instead by
228                 passing "copy_key => 1" to "wizard", which allows you to
229                 safely assign to $_[2] in order to e.g. redirect the action
230                 to another key. This however has a little performance
231                 drawback because of the copy.
232
233         All the callbacks are expected to return an integer, which is passed
234         straight to the perl magic API. However, only the return value of
235         the "len" callback currently holds a meaning.
236
237     Each callback can be specified as :
238
239     *   a code reference, which will be called as a subroutine.
240
241     *   a string reference, where the string denotes which subroutine is to
242         be called when magic is triggered. If the subroutine name is not
243         fully qualified, then the current package at the time the magic is
244         invoked will be used instead.
245
246     *   a reference to "undef", in which case a no-op magic callback is
247         installed instead of the default one. This may especially be helpful
248         for 'local' magic, where an empty callback prevents magic from being
249         copied during localization.
250
251     Note that "free" callbacks are *never* called during global destruction,
252     as there's no way to ensure that the wizard and the "free" callback
253     weren't destroyed before the variable.
254
255     Here's a simple usage example :
256
257         # A simple scalar tracer
258         my $wiz = wizard(
259          get  => sub { print STDERR "got ${$_[0]}\n" },
260          set  => sub { print STDERR "set to ${$_[0]}\n" },
261          free => sub { print STDERR "${$_[0]} was deleted\n" },
262         );
263
264   "cast"
265         cast [$@%&*]var, $wiz, ...
266
267     This function associates $wiz magic to the variable supplied, without
268     overwriting any other kind of magic. It returns true on success or when
269     $wiz magic is already present, and croaks on error. All extra arguments
270     specified after $wiz are passed to the private data constructor in @_[1
271     .. @_-1]. If the variable isn't a hash, any "uvar" callback of the
272     wizard is safely ignored.
273
274         # Casts $wiz onto $x, and pass '1' to the data constructor.
275         my $x;
276         cast $x, $wiz, 1;
277
278     The "var" argument can be an array or hash value. Magic for those
279     behaves like for any other scalar, except that it is dispelled when the
280     entry is deleted from the container. For example, if you want to call
281     "POSIX::tzset" each time the 'TZ' environment variable is changed in
282     %ENV, you can use :
283
284         use POSIX;
285         cast $ENV{TZ}, wizard set => sub { POSIX::tzset(); () };
286
287     If you want to overcome the possible deletion of the 'TZ' entry, you
288     have no choice but to rely on "store" uvar magic.
289
290   "getdata"
291         getdata [$@%&*]var, $wiz
292
293     This accessor fetches the private data associated with the magic $wiz in
294     the variable. It croaks when $wiz do not represent a valid magic object,
295     and returns an empty list if no such magic is attached to the variable
296     or when the wizard has no data constructor.
297
298         # Get the attached data, or undef if the wizard does not attach any.
299         my $data = getdata $x, $wiz;
300
301   "dispell"
302         dispell [$@%&*]variable, $wiz
303
304     The exact opposite of "cast" : it dissociates $wiz magic from the
305     variable. This function returns true on success, 0 when no magic
306     represented by $wiz could be found in the variable, and croaks if the
307     supplied wizard is invalid.
308
309         # Dispell now.
310         die 'no such magic in $x' unless dispell $x, $wiz;
311
312 CONSTANTS
313   "MGf_COPY"
314     Evaluates to true iff the 'copy' magic is available.
315
316   "MGf_DUP"
317     Evaluates to true iff the 'dup' magic is available.
318
319   "MGf_LOCAL"
320     Evaluates to true iff the 'local' magic is available.
321
322   "VMG_UVAR"
323     When this constant is true, you can use the "fetch,store,exists,delete"
324     callbacks on hashes. Initial VMG_UVAR capability was introduced in perl
325     5.9.5, with a fully functional implementation shipped with perl 5.10.0.
326
327   "VMG_COMPAT_SCALAR_LENGTH_NOLEN"
328     True for perls that don't call 'len' magic when taking the "length" of a
329     magical scalar.
330
331   "VMG_COMPAT_ARRAY_PUSH_NOLEN"
332     True for perls that don't call 'len' magic when you push an element in a
333     magical array. Starting from perl 5.11.0, this only refers to pushes in
334     non-void context and hence is false.
335
336   "VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID"
337     True for perls that don't call 'len' magic when you push in void context
338     an element in a magical array.
339
340   "VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID"
341     True for perls that don't call 'len' magic when you unshift in void
342     context an element in a magical array.
343
344   "VMG_COMPAT_ARRAY_UNDEF_CLEAR"
345     True for perls that call 'clear' magic when undefining magical arrays.
346
347   "VMG_COMPAT_HASH_DELETE_NOUVAR_VOID"
348     True for perls that don't call 'delete' uvar magic when you delete an
349     element from a hash in void context.
350
351   "VMG_COMPAT_GLOB_GET"
352     True for perls that call 'get' magic for operations on globs.
353
354   "VMG_PERL_PATCHLEVEL"
355     The perl patchlevel this module was built with, or 0 for non-debugging
356     perls.
357
358   "VMG_THREADSAFE"
359     True iff this module could have been built with thread-safety features
360     enabled.
361
362   "VMG_FORKSAFE"
363     True iff this module could have been built with fork-safety features
364     enabled. This will always be true except on Windows where it's false for
365     perl 5.10.0 and below .
366
367   "VMG_OP_INFO_NAME"
368     Value to pass with "op_info" to get the current op name in the magic
369     callbacks.
370
371   "VMG_OP_INFO_OBJECT"
372     Value to pass with "op_info" to get a "B::OP" object representing the
373     current op in the magic callbacks.
374
375 COOKBOOK
376   Associate an object to any perl variable
377     This technique can be useful for passing user data through limited APIs.
378     It is similar to using inside-out objects, but without the drawback of
379     having to implement a complex destructor.
380
381         {
382          package Magical::UserData;
383
384          use Variable::Magic qw<wizard cast getdata>;
385
386          my $wiz = wizard data => sub { \$_[1] };
387
388          sub ud (\[$@%*&]) : lvalue {
389           my ($var) = @_;
390           my $data = &getdata($var, $wiz);
391           unless (defined $data) {
392            $data = \(my $slot);
393            &cast($var, $wiz, $slot)
394                      or die "Couldn't cast UserData magic onto the variable";
395           }
396           $$data;
397          }
398         }
399
400         {
401          BEGIN { *ud = \&Magical::UserData::ud }
402
403          my $cb;
404          $cb = sub { print 'Hello, ', ud(&$cb), "!\n" };
405
406          ud(&$cb) = 'world';
407          $cb->(); # Hello, world!
408         }
409
410   Recursively cast magic on datastructures
411     "cast" can be called from any magical callback, and in particular from
412     "data". This allows you to recursively cast magic on datastructures :
413
414         my $wiz;
415         $wiz = wizard data => sub {
416          my ($var, $depth) = @_;
417          $depth ||= 0;
418          my $r = ref $var;
419          if ($r eq 'ARRAY') {
420           &cast((ref() ? $_ : \$_), $wiz, $depth + 1) for @$var;
421          } elsif ($r eq 'HASH') {
422           &cast((ref() ? $_ : \$_), $wiz, $depth + 1) for values %$var;
423          }
424          return $depth;
425         },
426         free => sub {
427          my ($var, $depth) = @_;
428          my $r = ref $var;
429          print "free $r at depth $depth\n";
430          ();
431         };
432
433         {
434          my %h = (
435           a => [ 1, 2 ],
436           b => { c => 3 }
437          );
438          cast %h, $wiz;
439         }
440
441     When %h goes out of scope, this will print something among the lines of
442     :
443
444         free HASH at depth 0
445         free HASH at depth 1
446         free SCALAR at depth 2
447         free ARRAY at depth 1
448         free SCALAR at depth 3
449         free SCALAR at depth 3
450
451     Of course, this example does nothing with the values that are added
452     after the "cast".
453
454 PERL MAGIC HISTORY
455     The places where magic is invoked have changed a bit through perl
456     history. Here's a little list of the most recent ones.
457
458     *   5.6.x
459
460         *p14416* : 'copy' and 'dup' magic.
461
462     *   5.8.9
463
464         *p28160* : Integration of *p25854* (see below).
465
466         *p32542* : Integration of *p31473* (see below).
467
468     *   5.9.3
469
470         *p25854* : 'len' magic is no longer called when pushing an element
471         into a magic array.
472
473         *p26569* : 'local' magic.
474
475     *   5.9.5
476
477         *p31064* : Meaningful 'uvar' magic.
478
479         *p31473* : 'clear' magic wasn't invoked when undefining an array.
480         The bug is fixed as of this version.
481
482     *   5.10.0
483
484         Since "PERL_MAGIC_uvar" is uppercased, "hv_magic_check()" triggers
485         'copy' magic on hash stores for (non-tied) hashes that also have
486         'uvar' magic.
487
488     *   5.11.x
489
490         *p32969* : 'len' magic is no longer invoked when calling "length"
491         with a magical scalar.
492
493         *p34908* : 'len' magic is no longer called when pushing / unshifting
494         an element into a magical array in void context. The "push" part was
495         already covered by *p25854*.
496
497         *g9cdcb38b* : 'len' magic is called again when pushing into a
498         magical array in non-void context.
499
500 EXPORT
501     The functions "wizard", "cast", "getdata" and "dispell" are only
502     exported on request. All of them are exported by the tags ':funcs' and
503     ':all'.
504
505     All the constants are also only exported on request, either individually
506     or by the tags ':consts' and ':all'.
507
508 CAVEATS
509     If you store a magic object in the private data slot, the magic won't be
510     accessible by "getdata" since it's not copied by assignment. The only
511     way to address this would be to return a reference.
512
513     If you define a wizard with a "free" callback and cast it on itself,
514     this destructor won't be called because the wizard will be destroyed
515     first.
516
517     In order to define magic on hash members, you need at least perl 5.10.0
518     (see "VMG_UVAR")
519
520 DEPENDENCIES
521     perl 5.8.
522
523     A C compiler. This module may happen to build with a C++ compiler as
524     well, but don't rely on it, as no guarantee is made in this regard.
525
526     Carp (standard since perl 5), XSLoader (standard since perl 5.006).
527
528     Copy tests need Tie::Array (standard since perl 5.005) and Tie::Hash
529     (since 5.002).
530
531     Some uvar tests need Hash::Util::FieldHash (standard since perl
532     5.009004).
533
534     Glob tests need Symbol (standard since perl 5.002).
535
536     Threads tests need threads and threads::shared.
537
538 SEE ALSO
539     perlguts and perlapi for internal information about magic.
540
541     perltie and overload for other ways of enhancing objects.
542
543 AUTHOR
544     Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
545
546     You can contact me by mail or on "irc.perl.org" (vincent).
547
548 BUGS
549     Please report any bugs or feature requests to "bug-variable-magic at
550     rt.cpan.org", or through the web interface at
551     <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Variable-Magic>. I will
552     be notified, and then you'll automatically be notified of progress on
553     your bug as I make changes.
554
555 SUPPORT
556     You can find documentation for this module with the perldoc command.
557
558         perldoc Variable::Magic
559
560     Tests code coverage report is available at
561     <http://www.profvince.com/perl/cover/Variable-Magic>.
562
563 COPYRIGHT & LICENSE
564     Copyright 2007,2008,2009,2010,2011,2012 Vincent Pit, all rights
565     reserved.
566
567     This program is free software; you can redistribute it and/or modify it
568     under the same terms as Perl itself.
569