Add built local::lib
[catagits/Gitalist.git] / local-lib5 / man / man3 / Variable::Magic.3pm
1 .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.3
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sh \" Subsection heading
6 .br
7 .if t .Sp
8 .ne 5
9 .PP
10 \fB\\$1\fR
11 .PP
12 ..
13 .de Sp \" Vertical space (when we can't use .PP)
14 .if t .sp .5v
15 .if n .sp
16 ..
17 .de Vb \" Begin verbatim text
18 .ft CW
19 .nf
20 .ne \\$1
21 ..
22 .de Ve \" End verbatim text
23 .ft R
24 .fi
25 ..
26 .\" Set up some character translations and predefined strings.  \*(-- will
27 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28 .\" double quote, and \*(R" will give a right double quote.  | will give a
29 .\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
30 .\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
31 .\" expand to `' in nroff, nothing in troff, for use with C<>.
32 .tr \(*W-|\(bv\*(Tr
33 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34 .ie n \{\
35 .    ds -- \(*W-
36 .    ds PI pi
37 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
39 .    ds L" ""
40 .    ds R" ""
41 .    ds C` ""
42 .    ds C' ""
43 'br\}
44 .el\{\
45 .    ds -- \|\(em\|
46 .    ds PI \(*p
47 .    ds L" ``
48 .    ds R" ''
49 'br\}
50 .\"
51 .\" If the F register is turned on, we'll generate index entries on stderr for
52 .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53 .\" entries marked with X<> in POD.  Of course, you'll have to process the
54 .\" output yourself in some meaningful fashion.
55 .if \nF \{\
56 .    de IX
57 .    tm Index:\\$1\t\\n%\t"\\$2"
58 ..
59 .    nr % 0
60 .    rr F
61 .\}
62 .\"
63 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
64 .\" way too many mistakes in technical documents.
65 .hy 0
66 .if n .na
67 .\"
68 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
70 .    \" fudge factors for nroff and troff
71 .if n \{\
72 .    ds #H 0
73 .    ds #V .8m
74 .    ds #F .3m
75 .    ds #[ \f1
76 .    ds #] \fP
77 .\}
78 .if t \{\
79 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 .    ds #V .6m
81 .    ds #F 0
82 .    ds #[ \&
83 .    ds #] \&
84 .\}
85 .    \" simple accents for nroff and troff
86 .if n \{\
87 .    ds ' \&
88 .    ds ` \&
89 .    ds ^ \&
90 .    ds , \&
91 .    ds ~ ~
92 .    ds /
93 .\}
94 .if t \{\
95 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101 .\}
102 .    \" troff and (daisy-wheel) nroff accents
103 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110 .ds ae a\h'-(\w'a'u*4/10)'e
111 .ds Ae A\h'-(\w'A'u*4/10)'E
112 .    \" corrections for vroff
113 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115 .    \" for low resolution devices (crt and lpr)
116 .if \n(.H>23 .if \n(.V>19 \
117 \{\
118 .    ds : e
119 .    ds 8 ss
120 .    ds o a
121 .    ds d- d\h'-1'\(ga
122 .    ds D- D\h'-1'\(hy
123 .    ds th \o'bp'
124 .    ds Th \o'LP'
125 .    ds ae ae
126 .    ds Ae AE
127 .\}
128 .rm #[ #] #H #V #F C
129 .\" ========================================================================
130 .\"
131 .IX Title "Variable::Magic 3"
132 .TH Variable::Magic 3 "2009-11-30" "perl v5.8.7" "User Contributed Perl Documentation"
133 .SH "NAME"
134 Variable::Magic \- Associate user\-defined magic to variables from Perl.
135 .SH "VERSION"
136 .IX Header "VERSION"
137 Version 0.39
138 .SH "SYNOPSIS"
139 .IX Header "SYNOPSIS"
140 .Vb 1
141 \&    use Variable::Magic qw/wizard cast VMG_OP_INFO_NAME/;
142 .Ve
143 .PP
144 .Vb 3
145 \&    { # A variable tracer
146 \&     my $wiz = wizard set  => sub { print "now set to ${$_[0]}!\en" },
147 \&                      free => sub { print "destroyed!\en" };
148 .Ve
149 .PP
150 .Vb 4
151 \&     my $a = 1;
152 \&     cast $a, $wiz;
153 \&     $a = 2;        # "now set to 2!"
154 \&    }               # "destroyed!"
155 .Ve
156 .PP
157 .Vb 6
158 \&    { # A hash with a default value
159 \&     my $wiz = wizard data     => sub { $_[1] },
160 \&                      fetch    => sub { $_[2] = $_[1] unless exists $_[0]\->{$_[2]}; () },
161 \&                      store    => sub { print "key $_[2] stored in $_[\-1]\en" },
162 \&                      copy_key => 1,
163 \&                      op_info  => VMG_OP_INFO_NAME;
164 .Ve
165 .PP
166 .Vb 5
167 \&     my %h = (_default => 0, apple => 2);
168 \&     cast %h, $wiz, '_default';
169 \&     print $h{banana}, "\en"; # "0", because the 'banana' key doesn't exist in %h
170 \&     $h{pear} = 1;           # "key pear stored in helem"
171 \&    }
172 .Ve
173 .SH "DESCRIPTION"
174 .IX Header "DESCRIPTION"
175 Magic is Perl way of enhancing objects.
176 This mechanism lets the user add extra data to any variable and hook syntaxical operations (such as access, assignment or destruction) that can be applied to it.
177 With this module, you can add your own magic to any variable without having to write a single line of \s-1XS\s0.
178 .PP
179 You'll realize that these magic variables look a lot like tied variables.
180 It's not surprising, as tied variables are implemented as a special kind of magic, just like any 'irregular' Perl variable : scalars like \f(CW$!\fR, \f(CW$(\fR or \f(CW$^W\fR, the \f(CW%ENV\fR and \f(CW%SIG\fR hashes, the \f(CW@ISA\fR array,  \f(CW\*(C`vec()\*(C'\fR and \f(CW\*(C`substr()\*(C'\fR lvalues, threads::shared variables...
181 They all share the same underlying C \s-1API\s0, and this module gives you direct access to it.
182 .PP
183 Still, the magic made available by this module differs from tieing and overloading in several ways :
184 .IP "\(bu" 4
185 It isn't copied on assignment.
186 .Sp
187 You attach it to variables, not values (as for blessed references).
188 .IP "\(bu" 4
189 It doesn't replace the original semantics.
190 .Sp
191 Magic callbacks usually trigger before the original action take place, and can't prevent it to happen.
192 This also makes catching individual events easier than with \f(CW\*(C`tie\*(C'\fR, where you have to provide fallbacks methods for all actions by usually inheriting from the correct \f(CW\*(C`Tie::Std*\*(C'\fR class and overriding individual methods in your own class.
193 .IP "\(bu" 4
194 It's type\-agnostic.
195 .Sp
196 The same magic can be applied on scalars, arrays, hashes, subs or globs.
197 But the same hook (see below for a list) may trigger differently depending on the the type of the variable.
198 .IP "\(bu" 4
199 It's mostly invisible at the Perl level.
200 .Sp
201 Magical and non-magical variables cannot be distinguished with \f(CW\*(C`ref\*(C'\fR, \f(CW\*(C`tied\*(C'\fR or another trick.
202 .IP "\(bu" 4
203 It's notably faster.
204 .Sp
205 Mainly because perl's way of handling magic is lighter by nature, and because there's no need for any method resolution.
206 Also, since you don't have to reimplement all the variable semantics, you only pay for what you actually use.
207 .PP
208 The operations that can be overloaded are :
209 .IP "\(bu" 4
210 \&\f(CW\*(C`get\*(C'\fR
211 .Sp
212 This magic is invoked when the variable is evaluated.
213 It is never called for arrays and hashes.
214 .IP "\(bu" 4
215 \&\f(CW\*(C`set\*(C'\fR
216 .Sp
217 This one is triggered each time the value of the variable changes.
218 It is called for array subscripts and slices, but never for hashes.
219 .IP "\(bu" 4
220 \&\f(CW\*(C`len\*(C'\fR
221 .Sp
222 This magic is a little special : it is called when the 'size' or the 'length' of the variable has to be known by Perl.
223 Typically, it's the magic involved when an array is evaluated in scalar context, but also on array assignment and loops (\f(CW\*(C`for\*(C'\fR, \f(CW\*(C`map\*(C'\fR or \f(CW\*(C`grep\*(C'\fR).
224 The callback has then to return the length as an integer.
225 .IP "\(bu" 4
226 \&\f(CW\*(C`clear\*(C'\fR
227 .Sp
228 This magic is invoked when the variable is reset, such as when an array is emptied.
229 Please note that this is different from undefining the variable, even though the magic is called when the clearing is a result of the undefine (e.g. for an array, but actually a bug prevent it to work before perl 5.9.5 \- see the history).
230 .IP "\(bu" 4
231 \&\f(CW\*(C`free\*(C'\fR
232 .Sp
233 This one can be considered as an object destructor.
234 It happens when the variable goes out of scope, but not when it is undefined.
235 .IP "\(bu" 4
236 \&\f(CW\*(C`copy\*(C'\fR
237 .Sp
238 This magic only applies to tied arrays and hashes.
239 It fires when you try to access or change their elements.
240 It is available on your perl iff \f(CW\*(C`MGf_COPY\*(C'\fR is true.
241 .IP "\(bu" 4
242 \&\f(CW\*(C`dup\*(C'\fR
243 .Sp
244 Invoked when the variable is cloned across threads.
245 Currently not available.
246 .IP "\(bu" 4
247 \&\f(CW\*(C`local\*(C'\fR
248 .Sp
249 When this magic is set on a variable, all subsequent localizations of the variable will trigger the callback.
250 It is available on your perl iff \f(CW\*(C`MGf_LOCAL\*(C'\fR is true.
251 .PP
252 The following actions only apply to hashes and are available iff \f(CW\*(C`VMG_UVAR\*(C'\fR is true.
253 They are referred to as \f(CW\*(C`uvar\*(C'\fR magics.
254 .IP "\(bu" 4
255 \&\f(CW\*(C`fetch\*(C'\fR
256 .Sp
257 This magic happens each time an element is fetched from the hash.
258 .IP "\(bu" 4
259 \&\f(CW\*(C`store\*(C'\fR
260 .Sp
261 This one is called when an element is stored into the hash.
262 .IP "\(bu" 4
263 \&\f(CW\*(C`exists\*(C'\fR
264 .Sp
265 This magic fires when a key is tested for existence in the hash.
266 .IP "\(bu" 4
267 \&\f(CW\*(C`delete\*(C'\fR
268 .Sp
269 This last one triggers when a key is deleted in the hash, regardless of whether the key actually exists in it.
270 .PP
271 You can refer to the tests to have more insight of where the different magics are invoked.
272 .PP
273 To prevent any clash between different magics defined with this module, an unique numerical signature is attached to each kind of magic (i.e. each set of callbacks for magic operations).
274 At the C level, magic tokens owned by magic created by this module have their \f(CW\*(C`mg\->mg_private\*(C'\fR field set to \f(CW0x3891\fR or \f(CW0x3892\fR, so please don't use these magic (sic) numbers in other extensions.
275 .SH "FUNCTIONS"
276 .IX Header "FUNCTIONS"
277 .ie n .Sh """wizard"""
278 .el .Sh "\f(CWwizard\fP"
279 .IX Subsection "wizard"
280 .Vb 14
281 \&    wizard data     => sub { ... },
282 \&           get      => sub { my ($ref, $data [, $op]) = @_; ... },
283 \&           set      => sub { my ($ref, $data [, $op]) = @_; ... },
284 \&           len      => sub { my ($ref, $data, $len [, $op]) = @_; ... ; return $newlen; },
285 \&           clear    => sub { my ($ref, $data [, $op]) = @_; ... },
286 \&           free     => sub { my ($ref, $data [, $op]) = @_, ... },
287 \&           copy     => sub { my ($ref, $data, $key, $elt [, $op]) = @_; ... },
288 \&           local    => sub { my ($ref, $data [, $op]) = @_; ... },
289 \&           fetch    => sub { my ($ref, $data, $key [, $op]) = @_; ... },
290 \&           store    => sub { my ($ref, $data, $key [, $op]) = @_; ... },
291 \&           exists   => sub { my ($ref, $data, $key [, $op]) = @_; ... },
292 \&           delete   => sub { my ($ref, $data, $key [, $op]) = @_; ... },
293 \&           copy_key => $bool,
294 \&           op_info  => [ 0 | VMG_OP_INFO_NAME | VMG_OP_INFO_OBJECT ]
295 .Ve
296 .PP
297 This function creates a 'wizard', an opaque type that holds the magic information.
298 It takes a list of keys / values as argument, whose keys can be :
299 .IP "\(bu" 4
300 \&\f(CW\*(C`data\*(C'\fR
301 .Sp
302 A code (or string) reference to a private data constructor.
303 It is called each time this magic is cast on a variable, and the scalar returned is used as private data storage for it.
304 \&\f(CW$_[0]\fR is a reference to the magic object and \f(CW@_[1 .. @_\-1]\fR are all extra arguments that were passed to \*(L"cast\*(R".
305 .IP "\(bu" 4
306 \&\f(CW\*(C`get\*(C'\fR, \f(CW\*(C`set\*(C'\fR, \f(CW\*(C`len\*(C'\fR, \f(CW\*(C`clear\*(C'\fR, \f(CW\*(C`free\*(C'\fR, \f(CW\*(C`copy\*(C'\fR, \f(CW\*(C`local\*(C'\fR, \f(CW\*(C`fetch\*(C'\fR, \f(CW\*(C`store\*(C'\fR, \f(CW\*(C`exists\*(C'\fR and \f(CW\*(C`delete\*(C'\fR
307 .Sp
308 Code (or string) references to the corresponding magic callbacks.
309 You don't have to specify all of them : the magic associated with undefined entries simply won't be hooked.
310 In those callbacks, \f(CW$_[0]\fR is always a reference to the magic object and \f(CW$_[1]\fR is always the private data (or \f(CW\*(C`undef\*(C'\fR when no private data constructor was supplied).
311 .Sp
312 Moreover, when you pass \f(CW\*(C`op_info => $num\*(C'\fR to \f(CW\*(C`wizard\*(C'\fR, the last element of \f(CW@_\fR will be the current op name if \f(CW\*(C`$num == VMG_OP_INFO_NAME\*(C'\fR and a \f(CW\*(C`B::OP\*(C'\fR object representing the current op if \f(CW\*(C`$num == VMG_OP_INFO_OBJECT\*(C'\fR.
313 Both have a performance hit, but just getting the name is lighter than getting the op object.
314 .Sp
315 Other arguments are specific to the magic hooked :
316 .RS 4
317 .IP "\(bu" 8
318 \&\f(CW\*(C`len\*(C'\fR
319 .Sp
320 When the variable is an array or a scalar, \f(CW$_[2]\fR contains the non-magical length.
321 The callback can return the new scalar or array length to use, or \f(CW\*(C`undef\*(C'\fR to default to the normal length.
322 .IP "\(bu" 8
323 \&\f(CW\*(C`copy\*(C'\fR
324 .Sp
325 \&\f(CW$_[2]\fR is a either a copy or an alias of the current key, which means that it is useless to try to change or cast magic on it.
326 \&\f(CW$_[3]\fR is an alias to the current element (i.e. the value).
327 .IP "\(bu" 8
328 \&\f(CW\*(C`fetch\*(C'\fR, \f(CW\*(C`store\*(C'\fR, \f(CW\*(C`exists\*(C'\fR and \f(CW\*(C`delete\*(C'\fR
329 .Sp
330 \&\f(CW$_[2]\fR is an alias to the current key.
331 Nothing prevents you from changing it, but be aware that there lurk dangerous side effects.
332 For example, it may rightfully be readonly if the key was a bareword.
333 You can get a copy instead by passing \f(CW\*(C`copy_key => 1\*(C'\fR to \*(L"wizard\*(R", which allows you to safely assign to \f(CW$_[2]\fR in order to e.g. redirect the action to another key.
334 This however has a little performance drawback because of the copy.
335 .RE
336 .RS 4
337 .Sp
338 All the callbacks are expected to return an integer, which is passed straight to the perl magic \s-1API\s0.
339 However, only the return value of the \f(CW\*(C`len\*(C'\fR callback currently holds a meaning.
340 .RE
341 .PP
342 Each callback can be specified as a code or a string reference, in which case the function denoted by the string will be used as the callback.
343 .PP
344 Note that \f(CW\*(C`free\*(C'\fR callbacks are \fInever\fR called during global destruction, as there's no way to ensure that the wizard and the \f(CW\*(C`free\*(C'\fR callback weren't destroyed before the variable.
345 .PP
346 Here's a simple usage example :
347 .PP
348 .Vb 4
349 \&    # A simple scalar tracer
350 \&    my $wiz = wizard get  => sub { print STDERR "got ${$_[0]}\en" },
351 \&                     set  => sub { print STDERR "set to ${$_[0]}\en" },
352 \&                     free => sub { print STDERR "${$_[0]} was deleted\en" }
353 .Ve
354 .ie n .Sh """cast"""
355 .el .Sh "\f(CWcast\fP"
356 .IX Subsection "cast"
357 .Vb 1
358 \&    cast [$@%&*]var, $wiz, ...
359 .Ve
360 .PP
361 This function associates \f(CW$wiz\fR magic to the variable supplied, without overwriting any other kind of magic.
362 It returns true on success or when \f(CW$wiz\fR magic is already present, and croaks on error.
363 All extra arguments specified after \f(CW$wiz\fR are passed to the private data constructor in \f(CW@_[1 .. @_\-1]\fR.
364 If the variable isn't a hash, any \f(CW\*(C`uvar\*(C'\fR callback of the wizard is safely ignored.
365 .PP
366 .Vb 3
367 \&    # Casts $wiz onto $x, and pass '1' to the data constructor.
368 \&    my $x;
369 \&    cast $x, $wiz, 1;
370 .Ve
371 .PP
372 The \f(CW\*(C`var\*(C'\fR argument can be an array or hash value.
373 Magic for those behaves like for any other scalar, except that it is dispelled when the entry is deleted from the container.
374 For example, if you want to call \f(CW\*(C`POSIX::tzset\*(C'\fR each time the \f(CW'TZ'\fR environment variable is changed in \f(CW%ENV\fR, you can use :
375 .PP
376 .Vb 2
377 \&    use POSIX;
378 \&    cast $ENV{TZ}, wizard set => sub { POSIX::tzset(); () };
379 .Ve
380 .PP
381 If you want to overcome the possible deletion of the \f(CW'TZ'\fR entry, you have no choice but to rely on \f(CW\*(C`store\*(C'\fR uvar magic.
382 .ie n .Sh """getdata"""
383 .el .Sh "\f(CWgetdata\fP"
384 .IX Subsection "getdata"
385 .Vb 1
386 \&    getdata [$@%&*]var, $wiz
387 .Ve
388 .PP
389 This accessor fetches the private data associated with the magic \f(CW$wiz\fR in the variable.
390 It croaks when \f(CW$wiz\fR do not represent a valid magic object, and returns an empty list if no such magic is attached to the variable or when the wizard has no data constructor.
391 .PP
392 .Vb 2
393 \&    # Get the attached data, or undef if the wizard does not attach any.
394 \&    my $data = getdata $x, $wiz;
395 .Ve
396 .ie n .Sh """dispell"""
397 .el .Sh "\f(CWdispell\fP"
398 .IX Subsection "dispell"
399 .Vb 1
400 \&    dispell [$@%&*]variable, $wiz
401 .Ve
402 .PP
403 The exact opposite of \*(L"cast\*(R" : it dissociates \f(CW$wiz\fR magic from the variable.
404 This function returns true on success, \f(CW0\fR when no magic represented by \f(CW$wiz\fR could be found in the variable, and croaks if the supplied wizard is invalid.
405 .PP
406 .Vb 2
407 \&    # Dispell now.
408 \&    die 'no such magic in $x' unless dispell $x, $wiz;
409 .Ve
410 .SH "CONSTANTS"
411 .IX Header "CONSTANTS"
412 .ie n .Sh """MGf_COPY"""
413 .el .Sh "\f(CWMGf_COPY\fP"
414 .IX Subsection "MGf_COPY"
415 Evaluates to true iff the 'copy' magic is available.
416 .ie n .Sh """MGf_DUP"""
417 .el .Sh "\f(CWMGf_DUP\fP"
418 .IX Subsection "MGf_DUP"
419 Evaluates to true iff the 'dup' magic is available.
420 .ie n .Sh """MGf_LOCAL"""
421 .el .Sh "\f(CWMGf_LOCAL\fP"
422 .IX Subsection "MGf_LOCAL"
423 Evaluates to true iff the 'local' magic is available.
424 .ie n .Sh """VMG_UVAR"""
425 .el .Sh "\f(CWVMG_UVAR\fP"
426 .IX Subsection "VMG_UVAR"
427 When this constant is true, you can use the \f(CW\*(C`fetch,store,exists,delete\*(C'\fR callbacks on hashes.
428 .ie n .Sh """VMG_COMPAT_ARRAY_PUSH_NOLEN"""
429 .el .Sh "\f(CWVMG_COMPAT_ARRAY_PUSH_NOLEN\fP"
430 .IX Subsection "VMG_COMPAT_ARRAY_PUSH_NOLEN"
431 True for perls that don't call 'len' magic when you push an element in a magical array.
432 Starting from perl 5.11.0, this only refers to pushes in non-void context and hence is false.
433 .ie n .Sh """VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID"""
434 .el .Sh "\f(CWVMG_COMPAT_ARRAY_PUSH_NOLEN_VOID\fP"
435 .IX Subsection "VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID"
436 True for perls that don't call 'len' magic when you push in void context an element in a magical array.
437 .ie n .Sh """VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID"""
438 .el .Sh "\f(CWVMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID\fP"
439 .IX Subsection "VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID"
440 True for perls that don't call 'len' magic when you unshift in void context an element in a magical array.
441 .ie n .Sh """VMG_COMPAT_ARRAY_UNDEF_CLEAR"""
442 .el .Sh "\f(CWVMG_COMPAT_ARRAY_UNDEF_CLEAR\fP"
443 .IX Subsection "VMG_COMPAT_ARRAY_UNDEF_CLEAR"
444 True for perls that call 'clear' magic when undefining magical arrays.
445 .ie n .Sh """VMG_COMPAT_SCALAR_LENGTH_NOLEN"""
446 .el .Sh "\f(CWVMG_COMPAT_SCALAR_LENGTH_NOLEN\fP"
447 .IX Subsection "VMG_COMPAT_SCALAR_LENGTH_NOLEN"
448 True for perls that don't call 'len' magic when taking the \f(CW\*(C`length\*(C'\fR of a magical scalar.
449 .ie n .Sh """VMG_PERL_PATCHLEVEL"""
450 .el .Sh "\f(CWVMG_PERL_PATCHLEVEL\fP"
451 .IX Subsection "VMG_PERL_PATCHLEVEL"
452 The perl patchlevel this module was built with, or \f(CW0\fR for non-debugging perls.
453 .ie n .Sh """VMG_THREADSAFE"""
454 .el .Sh "\f(CWVMG_THREADSAFE\fP"
455 .IX Subsection "VMG_THREADSAFE"
456 True iff this module could have been built with thread-safety features enabled.
457 .ie n .Sh """VMG_FORKSAFE"""
458 .el .Sh "\f(CWVMG_FORKSAFE\fP"
459 .IX Subsection "VMG_FORKSAFE"
460 True iff this module could have been built with fork-safety features enabled.
461 This will always be true except on Windows where it's false for perl 5.10.0 and below .
462 .ie n .Sh """VMG_OP_INFO_NAME"""
463 .el .Sh "\f(CWVMG_OP_INFO_NAME\fP"
464 .IX Subsection "VMG_OP_INFO_NAME"
465 Value to pass with \f(CW\*(C`op_info\*(C'\fR to get the current op name in the magic callbacks.
466 .ie n .Sh """VMG_OP_INFO_OBJECT"""
467 .el .Sh "\f(CWVMG_OP_INFO_OBJECT\fP"
468 .IX Subsection "VMG_OP_INFO_OBJECT"
469 Value to pass with \f(CW\*(C`op_info\*(C'\fR to get a \f(CW\*(C`B::OP\*(C'\fR object representing the current op in the magic callbacks.
470 .SH "COOKBOOK"
471 .IX Header "COOKBOOK"
472 .Sh "Associate an object to any perl variable"
473 .IX Subsection "Associate an object to any perl variable"
474 This can be useful for passing user data through limited APIs.
475 .PP
476 .Vb 2
477 \&    {
478 \&     package Magical::UserData;
479 .Ve
480 .PP
481 .Vb 1
482 \&     use Variable::Magic qw/wizard cast getdata/;
483 .Ve
484 .PP
485 .Vb 1
486 \&     my $wiz = wizard data => sub { \e$_[1] };
487 .Ve
488 .PP
489 .Vb 11
490 \&     sub ud (\e[$@%*&]) : lvalue {
491 \&      my ($var) = @_;
492 \&      my $data = &getdata($var, $wiz);
493 \&      unless (defined $data) {
494 \&       &cast($var, $wiz);
495 \&       $data = &getdata($var, $wiz);
496 \&       die "Couldn't cast UserData magic onto the variable" unless defined $data;
497 \&      }
498 \&      $$data;
499 \&     }
500 \&    }
501 .Ve
502 .PP
503 .Vb 2
504 \&    {
505 \&     BEGIN { *ud = \e&Magical::UserData::ud }
506 .Ve
507 .PP
508 .Vb 2
509 \&     my $cb;
510 \&     $cb = sub { print 'Hello, ', ud(&$cb), "!\en" };
511 .Ve
512 .PP
513 .Vb 3
514 \&     ud(&$cb) = 'world';
515 \&     $cb\->(); # Hello, world!
516 \&    }
517 .Ve
518 .Sh "Recursively cast magic on datastructures"
519 .IX Subsection "Recursively cast magic on datastructures"
520 \&\f(CW\*(C`cast\*(C'\fR can be called from any magical callback, and in particular from \f(CW\*(C`data\*(C'\fR.
521 This allows you to recursively cast magic on datastructures :
522 .PP
523 .Vb 18
524 \&    my $wiz;
525 \&    $wiz = wizard data => sub {
526 \&     my ($var, $depth) = @_;
527 \&     $depth ||= 0;
528 \&     my $r = ref $var;
529 \&     if ($r eq 'ARRAY') {
530 \&      &cast((ref() ? $_ : \e$_), $wiz, $depth + 1) for @$var;
531 \&     } elsif ($r eq 'HASH') {
532 \&      &cast((ref() ? $_ : \e$_), $wiz, $depth + 1) for values %$var;
533 \&     }
534 \&     return $depth;
535 \&    },
536 \&    free => sub {
537 \&     my ($var, $depth) = @_;
538 \&     my $r = ref $var;
539 \&     print "free $r at depth $depth\en";
540 \&     ();
541 \&    };
542 .Ve
543 .PP
544 .Vb 7
545 \&    {
546 \&     my %h = (
547 \&      a => [ 1, 2 ],
548 \&      b => { c => 3 }
549 \&     );
550 \&     cast %h, $wiz;
551 \&    }
552 .Ve
553 .PP
554 When \f(CW%h\fR goes out of scope, this will print something among the lines of :
555 .PP
556 .Vb 6
557 \&    free HASH at depth 0
558 \&    free HASH at depth 1
559 \&    free SCALAR at depth 2
560 \&    free ARRAY at depth 1
561 \&    free SCALAR at depth 3
562 \&    free SCALAR at depth 3
563 .Ve
564 .PP
565 Of course, this example does nothing with the values that are added after the \f(CW\*(C`cast\*(C'\fR.
566 .SH "PERL MAGIC HISTORY"
567 .IX Header "PERL MAGIC HISTORY"
568 The places where magic is invoked have changed a bit through perl history.
569 Here's a little list of the most recent ones.
570 .IP "\(bu" 4
571 \&\fB5.6.x\fR
572 .Sp
573 \&\fIp14416\fR : 'copy' and 'dup' magic.
574 .IP "\(bu" 4
575 \&\fB5.8.9\fR
576 .Sp
577 \&\fIp28160\fR : Integration of \fIp25854\fR (see below).
578 .Sp
579 \&\fIp32542\fR : Integration of \fIp31473\fR (see below).
580 .IP "\(bu" 4
581 \&\fB5.9.3\fR
582 .Sp
583 \&\fIp25854\fR : 'len' magic is no longer called when pushing an element into a magic array.
584 .Sp
585 \&\fIp26569\fR : 'local' magic.
586 .IP "\(bu" 4
587 \&\fB5.9.5\fR
588 .Sp
589 \&\fIp31064\fR : Meaningful 'uvar' magic.
590 .Sp
591 \&\fIp31473\fR : 'clear' magic wasn't invoked when undefining an array.
592 The bug is fixed as of this version.
593 .IP "\(bu" 4
594 \&\fB5.10.0\fR
595 .Sp
596 Since \f(CW\*(C`PERL_MAGIC_uvar\*(C'\fR is uppercased, \f(CW\*(C`hv_magic_check()\*(C'\fR triggers 'copy' magic on hash stores for (non\-tied) hashes that also have 'uvar' magic.
597 .IP "\(bu" 4
598 \&\fB5.11.x\fR
599 .Sp
600 \&\fIp32969\fR : 'len' magic is no longer invoked when calling \f(CW\*(C`length\*(C'\fR with a magical scalar.
601 .Sp
602 \&\fIp34908\fR : 'len' magic is no longer called when pushing / unshifting an element into a magical array in void context.
603 The \f(CW\*(C`push\*(C'\fR part was already covered by \fIp25854\fR.
604 .Sp
605 \&\fIg9cdcb38b\fR : 'len' magic is called again when pushing into a magical array in non-void context.
606 .SH "EXPORT"
607 .IX Header "EXPORT"
608 The functions \*(L"wizard\*(R", \*(L"cast\*(R", \*(L"getdata\*(R" and \*(L"dispell\*(R" are only exported on request.
609 All of them are exported by the tags \f(CW':funcs'\fR and \f(CW':all'\fR.
610 .PP
611 All the constants are also only exported on request, either individually or by the tags \f(CW':consts'\fR and \f(CW':all'\fR.
612 .SH "CAVEATS"
613 .IX Header "CAVEATS"
614 If you store a magic object in the private data slot, the magic won't be accessible by \*(L"getdata\*(R" since it's not copied by assignment.
615 The only way to address this would be to return a reference.
616 .PP
617 If you define a wizard with a \f(CW\*(C`free\*(C'\fR callback and cast it on itself, this destructor won't be called because the wizard will be destroyed first.
618 .SH "DEPENDENCIES"
619 .IX Header "DEPENDENCIES"
620 perl 5.8.
621 .PP
622 Carp (standard since perl 5), XSLoader (standard since perl 5.006).
623 .PP
624 Copy tests need Tie::Array (standard since perl 5.005) and Tie::Hash (since 5.002).
625 .PP
626 Some uvar tests need Hash::Util::FieldHash (standard since perl 5.009004).
627 .PP
628 Glob tests need Symbol (standard since perl 5.002).
629 .PP
630 Threads tests need threads and threads::shared.
631 .SH "SEE ALSO"
632 .IX Header "SEE ALSO"
633 perlguts and perlapi for internal information about magic.
634 .PP
635 perltie and overload for other ways of enhancing objects.
636 .SH "AUTHOR"
637 .IX Header "AUTHOR"
638 Vincent Pit, \f(CW\*(C`<perl at profvince.com>\*(C'\fR, <http://www.profvince.com>.
639 .PP
640 You can contact me by mail or on \f(CW\*(C`irc.perl.org\*(C'\fR (vincent).
641 .SH "BUGS"
642 .IX Header "BUGS"
643 Please report any bugs or feature requests to \f(CW\*(C`bug\-variable\-magic at rt.cpan.org\*(C'\fR, or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Variable\-Magic>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
644 .SH "SUPPORT"
645 .IX Header "SUPPORT"
646 You can find documentation for this module with the perldoc command.
647 .PP
648 .Vb 1
649 \&    perldoc Variable::Magic
650 .Ve
651 .PP
652 Tests code coverage report is available at <http://www.profvince.com/perl/cover/Variable\-Magic>.
653 .SH "COPYRIGHT & LICENSE"
654 .IX Header "COPYRIGHT & LICENSE"
655 Copyright 2007\-2009 Vincent Pit, all rights reserved.
656 .PP
657 This program is free software; you can redistribute it and/or modify it
658 under the same terms as Perl itself.