X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=lib%2FVariable%2FMagic.pm;h=bdbb97e8acc365adb5144d31aa3a5e85009b36b9;hb=ace906d7e76ed647adbd1ab1243ac9bdbde9b1d5;hp=c600a15ffbbedb89e095710b544822a78626d480;hpb=8edd65482a48cda016b4677014dcb80b2b923cb1;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/lib/Variable/Magic.pm b/lib/Variable/Magic.pm index c600a15..bdbb97e 100644 --- a/lib/Variable/Magic.pm +++ b/lib/Variable/Magic.pm @@ -13,13 +13,13 @@ Variable::Magic - Associate user-defined magic to variables from Perl. =head1 VERSION -Version 0.38 +Version 0.40 =cut our $VERSION; BEGIN { - $VERSION = '0.38'; + $VERSION = '0.40'; } =head1 SYNOPSIS @@ -50,8 +50,8 @@ BEGIN { =head1 DESCRIPTION -Magic is Perl way of enhancing objects. -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. +Magic is Perl's way of enhancing variables. +This mechanism lets the user add extra data to any variable and hook syntactical operations (such as access, assignment or destruction) that can be applied to it. With this module, you can add your own magic to any variable without having to write a single line of XS. You'll realize that these magic variables look a lot like tied variables. @@ -72,7 +72,7 @@ You attach it to variables, not values (as for blessed references). It doesn't replace the original semantics. -Magic callbacks usually trigger before the original action take place, and can't prevent it to happen. +Magic callbacks usually get triggered before the original action takes place, and can't prevent it from happening. This also makes catching individual events easier than with C, where you have to provide fallbacks methods for all actions by usually inheriting from the correct C class and overriding individual methods in your own class. =item * @@ -232,7 +232,7 @@ It takes a list of keys / values as argument, whose keys can be : C -A code reference to a private data constructor. +A code (or string) reference to a private data constructor. It is called each time this magic is cast on a variable, and the scalar returned is used as private data storage for it. C<$_[0]> is a reference to the magic object and C<@_[1 .. @_-1]> are all extra arguments that were passed to L. @@ -240,7 +240,7 @@ C<$_[0]> is a reference to the magic object and C<@_[1 .. @_-1]> are all extra a C, C, C, C, C, C, C, C, C, C and C -Code references to the corresponding magic callbacks. +Code (or string) references to the corresponding magic callbacks. You don't have to specify all of them : the magic associated with undefined entries simply won't be hooked. In those callbacks, C<$_[0]> is always a reference to the magic object and C<$_[1]> is always the private data (or C when no private data constructor was supplied). @@ -282,13 +282,17 @@ However, only the return value of the C callback currently holds a meaning. =back +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. + +Note that C callbacks are I called during global destruction, as there's no way to ensure that the wizard and the C callback weren't destroyed before the variable. + +Here's a simple usage example : + # A simple scalar tracer my $wiz = wizard get => sub { print STDERR "got ${$_[0]}\n" }, set => sub { print STDERR "set to ${$_[0]}\n" }, free => sub { print STDERR "${$_[0]} was deleted\n" } -Note that C callbacks are I called during global destruction, as there's no way to ensure that the wizard and the C callback weren't destroyed before the variable. - =cut sub wizard { @@ -413,7 +417,8 @@ Value to pass with C to get a C object representing the current =head2 Associate an object to any perl variable -This can be useful for passing user data through limited APIs. +This technique can be useful for passing user data through limited APIs. +It is similar to using inside-out objects, but without the drawback of having to implement a complex destructor. { package Magical::UserData; @@ -426,9 +431,9 @@ This can be useful for passing user data through limited APIs. my ($var) = @_; my $data = &getdata($var, $wiz); unless (defined $data) { - &cast($var, $wiz); - $data = &getdata($var, $wiz); - die "Couldn't cast UserData magic onto the variable" unless defined $data; + $data = \(my $slot); + &cast($var, $wiz, $slot) + or die "Couldn't cast UserData magic onto the variable"; } $$data; } @@ -619,7 +624,7 @@ Tests code coverage report is available at L