X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FVariable-Magic.git;a=blobdiff_plain;f=lib%2FVariable%2FMagic.pm;h=4d1ec64a004a0e42981ed885b85d87ffeaf77ca5;hp=e37984b04474d6c11d579d63198ac8f0bea8935b;hb=5c32c65e51fca0ff49c43b814e9653e01b1e56f1;hpb=1614df334a71910f04841705db4db7ec693fed16 diff --git a/lib/Variable/Magic.pm b/lib/Variable/Magic.pm index e37984b..4d1ec64 100644 --- a/lib/Variable/Magic.pm +++ b/lib/Variable/Magic.pm @@ -11,13 +11,13 @@ Variable::Magic - Associate user-defined magic to variables from Perl. =head1 VERSION -Version 0.53 +Version 0.58 =cut our $VERSION; BEGIN { - $VERSION = '0.53'; + $VERSION = '0.58'; } =head1 SYNOPSIS @@ -309,8 +309,12 @@ C<$_[-1]> is the C object for the current op. 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 I magic callback currently holds a meaning. +These callbacks are always executed in scalar context. +The returned value is coerced into a signed integer, which is then passed straight to the perl magic API. +However, note that perl currently only cares about the return value of the I magic callback and ignores all the others. +Starting with Variable::Magic 0.58, a reference returned from a non-I magic callback will not be destroyed immediately but will be allowed to survive until the end of the statement that triggered the magic. +This lets you use this return value as a token for triggering a destructor after the original magic action takes place. +You can see an example of this technique in the L. =back @@ -577,6 +581,44 @@ When C<%h> goes out of scope, this prints something among the lines of : Of course, this example does nothing with the values that are added after the C. +=head2 Delayed magic actions + +Starting with Variable::Magic 0.58, the return value of the magic callbacks can be used to delay the action until after the original action takes place : + + my $delayed; + my $delayed_aux = wizard( + data => sub { $_[1] }, + free => sub { + my ($target) = $_[1]; + my $target_data = &getdata($target, $delayed); + local $target_data->{guard} = 1; + if (ref $target eq 'SCALAR') { + my $orig = $$target; + $$target = $target_data->{mangler}->($orig); + } + return; + }, + ); + $delayed = wizard( + data => sub { + return +{ guard => 0, mangler => $_[1] }; + }, + set => sub { + return if $_[1]->{guard}; + my $token; + cast $token, $delayed_aux, $_[0]; + return \$token; + }, + ); + my $x = 1; + cast $x, $delayed => sub { $_[0] * 2 }; + $x = 2; + # $x is now 4 + # But note that the delayed action only takes place at the end of the + # current statement : + my @y = ($x = 5, $x); + # $x is now 10, but @y is (5, 5) + =head1 PERL MAGIC HISTORY The places where magic is invoked have changed a bit through perl history. @@ -684,11 +726,6 @@ This module may happen to build with a C++ compiler as well, but don't rely on i L (core since perl 5), L (since 5.6.0). -Copy tests need L (core since perl 5.005) and L (since 5.002). -Some uvar tests need L (since 5.9.4). -Glob tests need L (since 5.002). -Threads tests need L and L (both since 5.7.3). - =head1 SEE ALSO L and L for internal information about magic. @@ -712,11 +749,9 @@ You can find documentation for this module with the perldoc command. perldoc Variable::Magic -Tests code coverage report is available at L. - =head1 COPYRIGHT & LICENSE -Copyright 2007,2008,2009,2010,2011,2012,2013,2014 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.