]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - README
This is 0.50
[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.50
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 is 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     *   Magic is not copied on assignment.
54
55         You attach it to variables, not values (as for blessed references).
56
57     *   Magic does not replace the original semantics.
58
59         Magic callbacks usually get triggered before the original action
60         takes place, and cannot 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     *   Magic is 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     *   Magic is invisible at the Perl level.
73
74         Magical and non-magical variables cannot be distinguished with
75         "ref", "tied" or another trick.
76
77     *   Magic is notably faster.
78
79         Mainly because perl's way of handling magic is lighter by nature,
80         and because there is 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 magic is called each time the value of the variable changes. It
94         is called for array subscripts and slices, but never for hashes.
95
96     *   *len*
97
98         This magic only applies to scalars and arrays, and is triggered when
99         the 'size' or the 'length' of the variable has to be known by Perl.
100         This is typically the magic involved when an array is evaluated in
101         scalar context, but also on array assignment and loops ("for", "map"
102         or "grep"). The length is returned from the callback 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 magic is called when an object is destroyed as the result of
115         the variable going out of scope (but not when the variable is
116         undefined).
117
118     *   *copy*
119
120         This magic only applies to tied arrays and hashes, and fires when
121         you try to access or change their elements.
122
123     *   *dup*
124
125         This magic is invoked when the variable is cloned across threads. It
126         is currently not 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 if and only if "MGf_LOCAL" is true.
133
134     The following actions only apply to hashes and are available if and only
135     if "VMG_UVAR" is true. They are referred to as *uvar* magics.
136
137     *   *fetch*
138
139         This magic is invoked 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 magic is triggered when a key is deleted in the hash,
152         regardless 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 object 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 in scalar context each time the magic is cast onto a
186         variable, with $_[0] being a reference to this variable and @_[1 ..
187         @_-1] being all extra arguments that were passed to "cast". The
188         scalar returned from this call is then attached to the variable and
189         can be retrieved later with "getdata".
190
191     *   "get", "set", "len", "clear", "free", "copy", "local", "fetch",
192         "store", "exists" and "delete"
193
194         Code (or string) references to the respective magic callbacks. You
195         don't have to specify all of them : the magic corresponding to
196         undefined entries will simply not be hooked.
197
198         When those callbacks are executed, $_[0] is a reference to the magic
199         variable and $_[1] is the associated private data (or "undef" when
200         no private data constructor is supplied with the wizard). Other
201         arguments depend on which kind of magic is involved :
202
203         *       *len*
204
205                 $_[2] contains the natural, non-magical length of the
206                 variable (which can only be a scalar or an array as *len*
207                 magic is only relevant for these types). The callback is
208                 expected to return the new scalar or array length to use, or
209                 "undef" to default to the normal length.
210
211         *       *copy*
212
213                 $_[2] is a either an alias or a copy of the current key, and
214                 $_[3] is an alias to the current element (i.e. the value).
215                 Because $_[2] might be a copy, it is useless to try to
216                 change it or cast magic on it.
217
218         *       *fetch*, *store*, *exists* and *delete*
219
220                 $_[2] is an alias to the current key. Note that $_[2] may
221                 rightfully be readonly if the key comes from a bareword, and
222                 as such it is unsafe to assign to it. You can ask for a copy
223                 instead by passing "copy_key => 1" to "wizard" which, at the
224                 price of a small performance hit, allows you to safely
225                 assign to $_[2] in order to e.g. redirect the action to
226                 another key.
227
228         Finally, if "op_info => $num" is also passed to "wizard", then one
229         extra element is appended to @_. Its nature depends on the value of
230         $num :
231
232         *       "VMG_OP_INFO_NAME"
233
234                 $_[-1] is the current op name.
235
236         *       "VMG_OP_INFO_OBJECT"
237
238                 $_[-1] is the "B::OP" object for the current op.
239
240         Both result in a small performance hit, but just getting the name is
241         lighter than getting the op object.
242
243         These callbacks are executed in scalar context and are expected to
244         return an integer, which is then passed straight to the perl magic
245         API. However, only the return value of the *len* magic callback
246         currently holds a meaning.
247
248     Each callback can be specified as :
249
250     *   a code reference, which will be called as a subroutine.
251
252     *   a string reference, where the string denotes which subroutine is to
253         be called when magic is triggered. If the subroutine name is not
254         fully qualified, then the current package at the time the magic is
255         invoked will be used instead.
256
257     *   a reference to "undef", in which case a no-op magic callback is
258         installed instead of the default one. This may especially be helpful
259         for *local* magic, where an empty callback prevents magic from being
260         copied during localization.
261
262     Note that *free* magic is never called during global destruction, as
263     there is no way to ensure that the wizard object and the callback were
264     not destroyed before the variable.
265
266     Here is a simple usage example :
267
268         # A simple scalar tracer
269         my $wiz = wizard(
270          get  => sub { print STDERR "got ${$_[0]}\n" },
271          set  => sub { print STDERR "set to ${$_[0]}\n" },
272          free => sub { print STDERR "${$_[0]} was deleted\n" },
273         );
274
275   "cast"
276         cast [$@%&*]var, $wiz, @args
277
278     This function associates $wiz magic to the supplied variable, without
279     overwriting any other kind of magic. It returns true on success or when
280     $wiz magic is already attached, and croaks on error. When $wiz provides
281     a data constructor, it is called just before magic is cast onto the
282     variable, and it receives a reference to the target variable in $_[0]
283     and the content of @args in @_[1 .. @args]. Otherwise, @args is ignored.
284
285         # Casts $wiz onto $x, passing (\$x, '1') to the data constructor.
286         my $x;
287         cast $x, $wiz, 1;
288
289     The "var" argument can be an array or hash value. Magic for these
290     scalars behaves like for any other, except that it is dispelled when the
291     entry is deleted from the container. For example, if you want to call
292     "POSIX::tzset" each time the 'TZ' environment variable is changed in
293     %ENV, you can use :
294
295         use POSIX;
296         cast $ENV{TZ}, wizard set => sub { POSIX::tzset(); () };
297
298     If you want to handle the possible deletion of the 'TZ' entry, you must
299     also specify *store* magic.
300
301   "getdata"
302         getdata [$@%&*]var, $wiz
303
304     This accessor fetches the private data associated with the magic $wiz in
305     the variable. It croaks when $wiz does not represent a valid magic
306     object, and returns an empty list if no such magic is attached to the
307     variable or when the wizard has no data constructor.
308
309         # Get the data attached to $wiz in $x, or undef if $wiz
310         # did not attach any.
311         my $data = getdata $x, $wiz;
312
313   "dispell"
314         dispell [$@%&*]variable, $wiz
315
316     The exact opposite of "cast" : it dissociates $wiz magic from the
317     variable. This function returns true on success, 0 when no magic
318     represented by $wiz could be found in the variable, and croaks if the
319     supplied wizard is invalid.
320
321         # Dispell now.
322         die 'no such magic in $x' unless dispell $x, $wiz;
323
324 CONSTANTS
325   "MGf_COPY"
326     Evaluates to true if and only if the *copy* magic is available. This is
327     the case for perl 5.7.3 and greater, which is ensured by the
328     requirements of this module.
329
330   "MGf_DUP"
331     Evaluates to true if and only if the *dup* magic is available. This is
332     the case for perl 5.7.3 and greater, which is ensured by the
333     requirements of this module.
334
335   "MGf_LOCAL"
336     Evaluates to true if and only if the *local* magic is available. This is
337     the case for perl 5.9.3 and greater.
338
339   "VMG_UVAR"
340     When this constant is true, you can use the *fetch*, *store*, *exists*
341     and *delete* magics on hashes. Initial "VMG_UVAR" capability was
342     introduced in perl 5.9.5, with a fully functional implementation shipped
343     with perl 5.10.0.
344
345   "VMG_COMPAT_SCALAR_LENGTH_NOLEN"
346     True for perls that don't call *len* magic when taking the "length" of a
347     magical scalar.
348
349   "VMG_COMPAT_ARRAY_PUSH_NOLEN"
350     True for perls that don't call *len* magic when you push an element in a
351     magical array. Starting from perl 5.11.0, this only refers to pushes in
352     non-void context and hence is false.
353
354   "VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID"
355     True for perls that don't call *len* magic when you push in void context
356     an element in a magical array.
357
358   "VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID"
359     True for perls that don't call *len* magic when you unshift in void
360     context an element in a magical array.
361
362   "VMG_COMPAT_ARRAY_UNDEF_CLEAR"
363     True for perls that call *clear* magic when undefining magical arrays.
364
365   "VMG_COMPAT_HASH_DELETE_NOUVAR_VOID"
366     True for perls that don't call *delete* magic when you delete an element
367     from a hash in void context.
368
369   "VMG_COMPAT_GLOB_GET"
370     True for perls that call *get* magic for operations on globs.
371
372   "VMG_PERL_PATCHLEVEL"
373     The perl patchlevel this module was built with, or 0 for non-debugging
374     perls.
375
376   "VMG_THREADSAFE"
377     True if and only if this module could have been built with thread-safety
378     features enabled.
379
380   "VMG_FORKSAFE"
381     True if and only if this module could have been built with fork-safety
382     features enabled. This is always true except on Windows where it is
383     false for perl 5.10.0 and below.
384
385   "VMG_OP_INFO_NAME"
386     Value to pass with "op_info" to get the current op name in the magic
387     callbacks.
388
389   "VMG_OP_INFO_OBJECT"
390     Value to pass with "op_info" to get a "B::OP" object representing the
391     current op in the magic callbacks.
392
393 COOKBOOK
394   Associate an object to any perl variable
395     This technique can be useful for passing user data through limited APIs.
396     It is similar to using inside-out objects, but without the drawback of
397     having to implement a complex destructor.
398
399         {
400          package Magical::UserData;
401
402          use Variable::Magic qw<wizard cast getdata>;
403
404          my $wiz = wizard data => sub { \$_[1] };
405
406          sub ud (\[$@%*&]) : lvalue {
407           my ($var) = @_;
408           my $data = &getdata($var, $wiz);
409           unless (defined $data) {
410            $data = \(my $slot);
411            &cast($var, $wiz, $slot)
412                      or die "Couldn't cast UserData magic onto the variable";
413           }
414           $$data;
415          }
416         }
417
418         {
419          BEGIN { *ud = \&Magical::UserData::ud }
420
421          my $cb;
422          $cb = sub { print 'Hello, ', ud(&$cb), "!\n" };
423
424          ud(&$cb) = 'world';
425          $cb->(); # Hello, world!
426         }
427
428   Recursively cast magic on datastructures
429     "cast" can be called from any magical callback, and in particular from
430     "data". This allows you to recursively cast magic on datastructures :
431
432         my $wiz;
433         $wiz = wizard data => sub {
434          my ($var, $depth) = @_;
435          $depth ||= 0;
436          my $r = ref $var;
437          if ($r eq 'ARRAY') {
438           &cast((ref() ? $_ : \$_), $wiz, $depth + 1) for @$var;
439          } elsif ($r eq 'HASH') {
440           &cast((ref() ? $_ : \$_), $wiz, $depth + 1) for values %$var;
441          }
442          return $depth;
443         },
444         free => sub {
445          my ($var, $depth) = @_;
446          my $r = ref $var;
447          print "free $r at depth $depth\n";
448          ();
449         };
450
451         {
452          my %h = (
453           a => [ 1, 2 ],
454           b => { c => 3 }
455          );
456          cast %h, $wiz;
457         }
458
459     When %h goes out of scope, this prints something among the lines of :
460
461         free HASH at depth 0
462         free HASH at depth 1
463         free SCALAR at depth 2
464         free ARRAY at depth 1
465         free SCALAR at depth 3
466         free SCALAR at depth 3
467
468     Of course, this example does nothing with the values that are added
469     after the "cast".
470
471 PERL MAGIC HISTORY
472     The places where magic is invoked have changed a bit through perl
473     history. Here is a little list of the most recent ones.
474
475     *   5.6.x
476
477         *p14416* : *copy* and *dup* magic.
478
479     *   5.8.9
480
481         *p28160* : Integration of *p25854* (see below).
482
483         *p32542* : Integration of *p31473* (see below).
484
485     *   5.9.3
486
487         *p25854* : *len* magic is no longer called when pushing an element
488         into a magic array.
489
490         *p26569* : *local* magic.
491
492     *   5.9.5
493
494         *p31064* : Meaningful *uvar* magic.
495
496         *p31473* : *clear* magic was not invoked when undefining an array.
497         The bug is fixed as of this version.
498
499     *   5.10.0
500
501         Since "PERL_MAGIC_uvar" is uppercased, "hv_magic_check()" triggers
502         *copy* magic on hash stores for (non-tied) hashes that also have
503         *uvar* magic.
504
505     *   5.11.x
506
507         *p32969* : *len* magic is no longer invoked when calling "length"
508         with a magical scalar.
509
510         *p34908* : *len* magic is no longer called when pushing / unshifting
511         an element into a magical array in void context. The "push" part was
512         already covered by *p25854*.
513
514         *g9cdcb38b* : *len* magic is called again when pushing into a
515         magical array in non-void context.
516
517 EXPORT
518     The functions "wizard", "cast", "getdata" and "dispell" are only
519     exported on request. All of them are exported by the tags ':funcs' and
520     ':all'.
521
522     All the constants are also only exported on request, either individually
523     or by the tags ':consts' and ':all'.
524
525 CAVEATS
526     In order to hook hash operations with magic, you need at least perl
527     5.10.0 (see "VMG_UVAR").
528
529     If you want to store a magic object in the private data slot, you will
530     not be able to recover the magic with "getdata", since magic is not
531     copied by assignment. You can work around this gotcha by storing a
532     reference to the magic object instead.
533
534     If you define a wizard with *free* magic and cast it on itself, it
535     results in a memory cycle, so this destructor will not be called when
536     the wizard is freed.
537
538 DEPENDENCIES
539     perl 5.8.
540
541     A C compiler. This module may happen to build with a C++ compiler as
542     well, but don't rely on it, as no guarantee is made in this regard.
543
544     Carp (core since perl 5), XSLoader (since 5.006).
545
546     Copy tests need Tie::Array (core since perl 5.005) and Tie::Hash (since
547     5.002). Some uvar tests need Hash::Util::FieldHash (since 5.009004).
548     Glob tests need Symbol (since 5.002). Threads tests need threads and
549     threads::shared (both since 5.007003).
550
551 SEE ALSO
552     perlguts and perlapi for internal information about magic.
553
554     perltie and overload for other ways of enhancing objects.
555
556 AUTHOR
557     Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
558
559     You can contact me by mail or on "irc.perl.org" (vincent).
560
561 BUGS
562     Please report any bugs or feature requests to "bug-variable-magic at
563     rt.cpan.org", or through the web interface at
564     <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Variable-Magic>. I will
565     be notified, and then you'll automatically be notified of progress on
566     your bug as I make changes.
567
568 SUPPORT
569     You can find documentation for this module with the perldoc command.
570
571         perldoc Variable::Magic
572
573     Tests code coverage report is available at
574     <http://www.profvince.com/perl/cover/Variable-Magic>.
575
576 COPYRIGHT & LICENSE
577     Copyright 2007,2008,2009,2010,2011,2012 Vincent Pit, all rights
578     reserved.
579
580     This program is free software; you can redistribute it and/or modify it
581     under the same terms as Perl itself.
582