=head1 VERSION
-Version 0.49
+Version 0.56
=cut
our $VERSION;
BEGIN {
- $VERSION = '0.49';
+ $VERSION = '0.56';
}
=head1 SYNOPSIS
=item *
+Magic is multivalued.
+
+You can safely apply different kinds of magics to the same variable, and each of them will be invoked successively.
+
+=item *
+
Magic is type-agnostic.
The same magic can be applied on scalars, arrays, hashes, subs or globs.
-But the same hook (see below for a list) may trigger differently depending on the the type of the variable.
+But the same hook (see below for a list) may trigger differently depending on the type of the variable.
=item *
-Magic is invisible at the Perl level.
+Magic is invisible at Perl level.
Magical and non-magical variables cannot be distinguished with C<ref>, C<tied> or another trick.
=item *
-C<get>
+I<get>
This magic is invoked when the variable is evaluated.
It is never called for arrays and hashes.
=item *
-C<set>
+I<set>
This magic is called each time the value of the variable changes.
It is called for array subscripts and slices, but never for hashes.
=item *
-C<len>
+I<len>
-This magic only applies to scalars and arrays, and is triggered when the 'size' or the 'length' of the variable has to be known by Perl.
+This magic only applies to arrays (though it used to also apply to scalars), and is triggered when the 'size' or the 'length' of the variable has to be known by Perl.
This is typically the magic involved when an array is evaluated in scalar context, but also on array assignment and loops (C<for>, C<map> or C<grep>).
The length is returned from the callback as an integer.
+Starting from perl 5.12, this magic is no longer called by the C<length> keyword, and starting from perl 5.17.4 it is also no longer called for scalars in any situation, making this magic only meaningful on arrays.
+You can use the constants L</VMG_COMPAT_SCALAR_LENGTH_NOLEN> and L</VMG_COMPAT_SCALAR_NOLEN> to see if this magic is available for scalars or not.
+
=item *
-C<clear>
+I<clear>
This magic is invoked when the variable is reset, such as when an array is emptied.
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 L<history|/PERL MAGIC HISTORY>).
=item *
-C<free>
+I<free>
-This magic is called when an object is destroyed as the result of the variable going out of scope (but not when the variable is undefined).
+This magic is called when a variable is destroyed as the result of going out of scope (but not when it is undefined).
+It behaves roughly like Perl object destructors (i.e. C<DESTROY> methods), except that exceptions thrown from inside a I<free> callback will always be propagated to the surrounding code.
=item *
-C<copy>
+I<copy>
+
+When applied to tied arrays and hashes, this magic fires when you try to access or change their elements.
-This magic only applies to tied arrays and hashes, and fires when you try to access or change their elements.
+Starting from perl 5.17.0, it can also be applied to closure prototypes, in which case the magic will be called when the prototype is cloned.
+The L</VMG_COMPAT_CODE_COPY_CLONE> constant is true when your perl support this feature.
=item *
-C<dup>
+I<dup>
This magic is invoked when the variable is cloned across threads.
It is currently not available.
=item *
-C<local>
+I<local>
When this magic is set on a variable, all subsequent localizations of the variable will trigger the callback.
It is available on your perl if and only if C<MGf_LOCAL> is true.
=back
The following actions only apply to hashes and are available if and only if L</VMG_UVAR> is true.
-They are referred to as C<uvar> magics.
+They are referred to as I<uvar> magics.
=over 4
=item *
-C<fetch>
+I<fetch>
This magic is invoked each time an element is fetched from the hash.
=item *
-C<store>
+I<store>
This one is called when an element is stored into the hash.
=item *
-C<exists>
+I<exists>
This magic fires when a key is tested for existence in the hash.
=item *
-C<delete>
+I<delete>
This magic is triggered when a key is deleted in the hash, regardless of whether the key actually exists in it.
=item *
-C<len>
+I<len>
-C<$_[2]> contains the natural, non-magical length of the variable (which can only be a scalar or an array as len magic is only relevant for these types).
+C<$_[2]> contains the natural, non-magical length of the variable (which can only be a scalar or an array as I<len> magic is only relevant for these types).
The callback is expected to return the new scalar or array length to use, or C<undef> to default to the normal length.
=item *
-C<copy>
+I<copy>
-C<$_[2]> is a either an alias or a copy of the current key, and C<$_[3]> is an alias to the current element (i.e. the value).
-Because C<$_[2]> might be a copy, it is useless to try to change it or cast magic on it.
+When the variable for which the magic is invoked is an array or an hash, C<$_[2]> is a either an alias or a copy of the current key, and C<$_[3]> is an alias to the current element (i.e. the value).
+Since C<$_[2]> might be a copy, it is useless to try to change it or cast magic on it.
+
+Starting from perl 5.17.0, this magic can also be called for code references.
+In this case, C<$_[2]> is always C<undef> and C<$_[3]> is a reference to the cloned anonymous subroutine.
=item *
-C<fetch>, C<store>, C<exists> and C<delete>
+I<fetch>, I<store>, I<exists> and I<delete>
C<$_[2]> is an alias to the current key.
Note that C<$_[2]> may rightfully be readonly if the key comes from a bareword, and as such it is unsafe to assign to it.
Both result in a small performance hit, but just getting the name is lighter than getting the op object.
These callbacks are executed in scalar context and are expected to return an integer, which is then passed straight to the perl magic API.
-However, only the return value of the C<len> callback currently holds a meaning.
+However, only the return value of the I<len> magic callback currently holds a meaning.
=back
=item *
a reference to C<undef>, in which case a no-op magic callback is installed instead of the default one.
-This may especially be helpful for 'local' magic, where an empty callback prevents magic from being copied during localization.
+This may especially be helpful for I<local> magic, where an empty callback prevents magic from being copied during localization.
=back
-Note that C<free> callbacks are I<never> called during global destruction, as there is no way to ensure that the wizard object and the C<free> callback were not destroyed before the variable.
+Note that I<free> magic is never called during global destruction, as there is no way to ensure that the wizard object and the callback were not destroyed before the variable.
Here is a simple usage example :
use POSIX;
cast $ENV{TZ}, wizard set => sub { POSIX::tzset(); () };
-If you want to handle the possible deletion of the C<'TZ'> entry, you must also specify C<store> uvar magic.
+If you want to handle the possible deletion of the C<'TZ'> entry, you must also specify I<store> magic.
=head2 C<getdata>
=head2 C<MGf_COPY>
-Evaluates to true if and only if the 'copy' magic is available.
+Evaluates to true if and only if the I<copy> magic is available.
This is the case for perl 5.7.3 and greater, which is ensured by the requirements of this module.
=head2 C<MGf_DUP>
-Evaluates to true if and only if the 'dup' magic is available.
+Evaluates to true if and only if the I<dup> magic is available.
This is the case for perl 5.7.3 and greater, which is ensured by the requirements of this module.
=head2 C<MGf_LOCAL>
-Evaluates to true if and only if the 'local' magic is available.
+Evaluates to true if and only if the I<local> magic is available.
This is the case for perl 5.9.3 and greater.
=head2 C<VMG_UVAR>
-When this constant is true, you can use the C<fetch,store,exists,delete> callbacks on hashes.
+When this constant is true, you can use the I<fetch>, I<store>, I<exists> and I<delete> magics on hashes.
Initial L</VMG_UVAR> capability was introduced in perl 5.9.5, with a fully functional implementation shipped with perl 5.10.0.
=head2 C<VMG_COMPAT_SCALAR_LENGTH_NOLEN>
-True for perls that don't call 'len' magic when taking the C<length> of a magical scalar.
+True for perls that don't call I<len> magic when taking the C<length> of a magical scalar.
+
+=head2 C<VMG_COMPAT_SCALAR_NOLEN>
+
+True for perls that don't call I<len> magic on scalars.
+Implies L</VMG_COMPAT_SCALAR_LENGTH_NOLEN>.
=head2 C<VMG_COMPAT_ARRAY_PUSH_NOLEN>
-True for perls that don't call 'len' magic when you push an element in a magical array.
+True for perls that don't call I<len> magic when you push an element in a magical array.
Starting from perl 5.11.0, this only refers to pushes in non-void context and hence is false.
=head2 C<VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID>
-True for perls that don't call 'len' magic when you push in void context an element in a magical array.
+True for perls that don't call I<len> magic when you push in void context an element in a magical array.
=head2 C<VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID>
-True for perls that don't call 'len' magic when you unshift in void context an element in a magical array.
+True for perls that don't call I<len> magic when you unshift in void context an element in a magical array.
=head2 C<VMG_COMPAT_ARRAY_UNDEF_CLEAR>
-True for perls that call 'clear' magic when undefining magical arrays.
+True for perls that call I<clear> magic when undefining magical arrays.
=head2 C<VMG_COMPAT_HASH_DELETE_NOUVAR_VOID>
-True for perls that don't call 'delete' uvar magic when you delete an element from a hash in void context.
+True for perls that don't call I<delete> magic when you delete an element from a hash in void context.
+
+=head2 C<VMG_COMPAT_CODE_COPY_CLONE>
+
+True for perls that call I<copy> magic when a magical closure prototype is cloned.
=head2 C<VMG_COMPAT_GLOB_GET>
-True for perls that call 'get' magic for operations on globs.
+True for perls that call I<get> magic for operations on globs.
=head2 C<VMG_PERL_PATCHLEVEL>
B<5.6.x>
-I<p14416> : 'copy' and 'dup' magic.
+I<p14416> : I<copy> and I<dup> magic.
=item *
B<5.9.3>
-I<p25854> : 'len' magic is no longer called when pushing an element into a magic array.
+I<p25854> : I<len> magic is no longer called when pushing an element into a magic array.
-I<p26569> : 'local' magic.
+I<p26569> : I<local> magic.
=item *
B<5.9.5>
-I<p31064> : Meaningful 'uvar' magic.
+I<p31064> : Meaningful I<uvar> magic.
-I<p31473> : 'clear' magic was not invoked when undefining an array.
+I<p31473> : I<clear> magic was not invoked when undefining an array.
The bug is fixed as of this version.
=item *
B<5.10.0>
-Since C<PERL_MAGIC_uvar> is uppercased, C<hv_magic_check()> triggers 'copy' magic on hash stores for (non-tied) hashes that also have 'uvar' magic.
+Since C<PERL_MAGIC_uvar> is uppercased, C<hv_magic_check()> triggers I<copy> magic on hash stores for (non-tied) hashes that also have I<uvar> magic.
=item *
B<5.11.x>
-I<p32969> : 'len' magic is no longer invoked when calling C<length> with a magical scalar.
+I<p32969> : I<len> magic is no longer invoked when calling C<length> with a magical scalar.
-I<p34908> : 'len' magic is no longer called when pushing / unshifting an element into a magical array in void context.
+I<p34908> : I<len> magic is no longer called when pushing / unshifting an element into a magical array in void context.
The C<push> part was already covered by I<p25854>.
-I<g9cdcb38b> : 'len' magic is called again when pushing into a magical array in non-void context.
+I<g9cdcb38b> : I<len> magic is called again when pushing into a magical array in non-void context.
=back
'consts' => [ qw<
MGf_COPY MGf_DUP MGf_LOCAL VMG_UVAR
VMG_COMPAT_SCALAR_LENGTH_NOLEN
+ VMG_COMPAT_SCALAR_NOLEN
VMG_COMPAT_ARRAY_PUSH_NOLEN VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID
VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID
VMG_COMPAT_ARRAY_UNDEF_CLEAR
VMG_COMPAT_HASH_DELETE_NOUVAR_VOID
+ VMG_COMPAT_CODE_COPY_CLONE
VMG_COMPAT_GLOB_GET
VMG_PERL_PATCHLEVEL
VMG_THREADSAFE VMG_FORKSAFE
If you want to store a magic object in the private data slot, you will not be able to recover the magic with L</getdata>, since magic is not copied by assignment.
You can work around this gotcha by storing a reference to the magic object instead.
-If you define a wizard with a C<free> callback and cast it on itself, it results in a memory cycle, so this destructor will not be called when the wizard is freed.
+If you define a wizard with I<free> magic and cast it on itself, it results in a memory cycle, so this destructor will not be called when the wizard is freed.
=head1 DEPENDENCIES
A C compiler.
This module may happen to build with a C++ compiler as well, but don't rely on it, as no guarantee is made in this regard.
-L<Carp> (core since perl 5), L<XSLoader> (since 5.006).
-
-Copy tests need L<Tie::Array> (core since perl 5.005) and L<Tie::Hash> (since 5.002).
-Some uvar tests need L<Hash::Util::FieldHash> (since 5.009004).
-Glob tests need L<Symbol> (since 5.002).
-Threads tests need L<threads> and L<threads::shared> (both since 5.007003).
+L<Carp> (core since perl 5), L<XSLoader> (since 5.6.0).
=head1 SEE ALSO
=head1 COPYRIGHT & LICENSE
-Copyright 2007,2008,2009,2010,2011,2012 Vincent Pit, all rights reserved.
+Copyright 2007,2008,2009,2010,2011,2012,2013,2014,2015 Vincent Pit, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.