2 Variable::Magic - Associate user-defined magic to variables from Perl.
8 use Variable::Magic qw/wizard cast dispell/;
10 my $wiz = wizard set => sub { print STDERR "now set to ${$_[0]}!\n" };
13 $a = 2; # "now set to 2!"
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
24 The operations that can be overloaded are :
27 This magic is invoked when the variable is evaluated (does not
28 include array/hash subscripts and slices).
31 This one is triggered each time the value of the variable changes
32 (includes array/hash subscripts and slices).
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.
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).
49 This last one can be considered as an object destructor. It happens
50 when the variable goes out of scope (with the exception of global
51 scope), but not when it is undefined.
53 To prevent any clash between different magics defined with this module,
54 an unique numerical signature is attached to each kind of magic (i.e.
55 each set of callbacks for magic operations).
58 The places where magic is invoked have changed a bit through perl
59 history. Here's a little list of the most recent ones.
62 'len' magic is no longer called when pushing an element into a magic
66 'clear' magic wasn't invoked when undefining an array. The bug is fixed
71 The minimum integer used as a signature for user-defined magic.
74 The maximum integer used as a signature for user-defined magic.
77 SIG_NBR = SIG_MAX - SIG_MIN + 1
80 True iff the 'copy' magic is available.
83 True iff the 'dup' magic is available.
86 True iff the 'local' magic is available.
90 wizard sig => .., data => ..., get => .., set => .., len => .., clear => .., free => ..
92 This function creates a 'wizard', an opaque type that holds the magic
93 information. It takes a list of keys / values as argument, whose keys
97 The numerical signature. If not specified or undefined, a random
98 signature is generated. If the signature matches an already defined
99 magic, then the existant magic object is returned.
102 A code reference to a private data constructor. It is called each
103 time this magic is cast on a variable, and the scalar returned is
104 used as private data storage for it. $_[0] is a reference to the
105 magic object and @_[1 .. @_-1] are all extra arguments that were
108 'get', 'set', 'len', 'clear' and 'free'
109 Code references to corresponding magic callbacks. You don't have to
110 specify all of them : the magic associated with undefined entries
111 simply won't be hooked. In those callbacks, $_[0] is a reference to
112 the magic object and $_[1] is the private data (or "undef" when no
113 private data constructor was supplied). In the special case of "len"
114 magic and when the variable is an array, $_[2] contains its normal
117 # A simple scalar tracer
118 my $wiz = wizard get => sub { print STDERR "got ${$_[0]}\n" },
119 set => sub { print STDERR "set to ${$_[0]}\n" },
120 free => sub { print STDERR "${$_[0]} was deleted\n" }
123 With this tool, you can manually generate random magic signature between
124 SIG_MIN and SIG_MAX inclusive. That's the way "wizard" creates them when
125 no signature is supplied.
127 # Generate a signature
133 This accessor returns the magic signature of this wizard.
136 my $sig = getsig $wiz;
139 cast [$@%&*]var, [$wiz|$sig], ...
141 This function associates $wiz magic to the variable supplied, without
142 overwriting any other kind of magic. You can also supply the numeric
143 signature $sig instead of $wiz. It returns true on success or when $wiz
144 magic is already present, 0 on error, and "undef" when no magic
145 corresponds to the given signature (in case $sig was supplied). All
146 extra arguments specified after $wiz are passed to the private data
149 # Casts $wiz onto $x. If $wiz isn't a signature, undef can't be returned.
151 die 'error' unless cast $x, $wiz;
154 getdata [$@%&*]var, [$wiz|$sig]
156 This accessor fetches the private data associated with the magic $wiz
157 (or the signature $sig) in the variable. "undef" is returned when no
158 such magic or data is found, or when $sig does not represent a current
161 # Get the attached data.
162 my $data = getdata $x, $wiz or die 'no such magic or magic has no data';
165 dispell [$@%&*]variable, [$wiz|$sig]
167 The exact opposite of "cast" : it dissociates $wiz magic from the
168 variable. You can also pass the magic signature $sig as the second
169 argument. True is returned on success, 0 on error or when no magic
170 represented by $wiz could be found in the variable, and "undef" when no
171 magic corresponds to the given signature (in case $sig was supplied).
173 # Dispell now. If $wiz isn't a signature, undef can't be returned.
174 die 'no such magic or error' unless dispell $x, $wiz;
177 The functions "wizard", "gensig", "getsig", "cast", "getdata" and
178 "dispell" are only exported on request. All of them are exported by the
179 tags ':funcs' and ':all'.
181 The constants "SIG_MIN", "SIG_MAX" and "SIG_NBR" are also only exported
182 on request. They are all exported by the tags ':consts' and ':all'.
187 Carp (standard since perl 5), XSLoader (standard since perl 5.006).
189 Glob tests need Symbol (standard since perl 5.002).
192 perlguts and perlapi for internal information about magic.
195 Vincent Pit, "<perl at profvince.com>"
197 You can contact me by mail or on #perl @ FreeNode (Prof_Vince).
200 Please report any bugs or feature requests to "bug-variable-magic at
201 rt.cpan.org", or through the web interface at
202 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Variable-Magic>. I will
203 be notified, and then you'll automatically be notified of progress on
204 your bug as I make changes.
207 You can find documentation for this module with the perldoc command.
209 perldoc Variable::Magic
212 Copyright 2007 Vincent Pit, all rights reserved.
214 This program is free software; you can redistribute it and/or modify it
215 under the same terms as Perl itself.