X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=lib%2FVariable%2FMagic.pm;h=1be2bf17906b14f84bc7515c8150a27098ba5a04;hb=2d569c9187148522afa2ec8ec9c8c805cc39af8a;hp=2700f7112ef08381f73ba5bbe9a785d141cc8258;hpb=9cad66dcbed4359e8d70b1bc1a26000b475a400c;p=perl%2Fmodules%2FVariable-Magic.git
diff --git a/lib/Variable/Magic.pm b/lib/Variable/Magic.pm
index 2700f71..1be2bf1 100644
--- a/lib/Variable/Magic.pm
+++ b/lib/Variable/Magic.pm
@@ -1,63 +1,107 @@
package Variable::Magic;
-use 5.007003;
+use 5.008;
use strict;
use warnings;
-use Carp qw/croak/;
-
=head1 NAME
Variable::Magic - Associate user-defined magic to variables from Perl.
=head1 VERSION
-Version 0.28
+Version 0.54
=cut
our $VERSION;
BEGIN {
- $VERSION = '0.28';
+ $VERSION = '0.54';
}
=head1 SYNOPSIS
- use Variable::Magic qw/wizard cast dispell/;
-
- my $wiz = wizard set => sub { print STDERR "now set to ${$_[0]}!\n" };
- my $a = 1;
- cast $a, $wiz;
- $a = 2; # "now set to 2!"
- dispell $a, $wiz;
- $a = 3 # (nothing)
+ use Variable::Magic qw;
+
+ { # A variable tracer
+ my $wiz = wizard(
+ set => sub { print "now set to ${$_[0]}!\n" },
+ free => sub { print "destroyed!\n" },
+ );
+
+ my $a = 1;
+ cast $a, $wiz;
+ $a = 2; # "now set to 2!"
+ } # "destroyed!"
+
+ { # A hash with a default value
+ my $wiz = wizard(
+ data => sub { $_[1] },
+ fetch => sub { $_[2] = $_[1] unless exists $_[0]->{$_[2]}; () },
+ store => sub { print "key $_[2] stored in $_[-1]\n" },
+ copy_key => 1,
+ op_info => VMG_OP_INFO_NAME,
+ );
+
+ my %h = (_default => 0, apple => 2);
+ cast %h, $wiz, '_default';
+ print $h{banana}, "\n"; # "0" (there is no 'banana' key in %h)
+ $h{pear} = 1; # "key pear stored in helem"
+ }
=head1 DESCRIPTION
-Magic is Perl way of enhancing objects.
-This mechanism let the user add extra data to any variable and hook syntaxical operations (such as access, assignation or destruction) that can be applied to it.
-With this module, you can add your own magic to any variable without the pain of the C API.
+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.
+It is not surprising, as tied variables are implemented as a special kind of magic, just like any 'irregular' Perl variable : scalars like C<$!>, C<$(> or C<$^W>, the C<%ENV> and C<%SIG> hashes, the C<@ISA> array, C and C lvalues, L variables...
+They all share the same underlying C API, and this module gives you direct access to it.
-Magic differs from tieing and overloading in several ways :
+Still, the magic made available by this module differs from tieing and overloading in several ways :
=over 4
=item *
-Magic isn't copied on assignation (as for blessed references) : you attach it to variables, not values.
+Magic is not copied on assignment.
+
+You attach it to variables, not values (as for blessed references).
+
+=item *
+
+Magic does not replace the original semantics.
+
+Magic callbacks usually get triggered before the original action takes place, and cannot 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 *
+
+Magic is multivalued.
+
+You can safely apply different kinds of magics to the same variable, and each of them will be invoked successively.
=item *
-It doesn't replace the original semantics : magic callbacks trigger before the original action take place, and can't prevent it to happen.
+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 type of the variable.
=item *
-It's mostly invisible at the Perl level : magical and non-magical variables cannot be distinguished with C[, C or another trick.
+Magic is invisible at Perl level.
+
+Magical and non-magical variables cannot be distinguished with C][, C or another trick.
=item *
-It's notably faster, since perl's way of handling magic is lighter by nature, and there's no need for any method resolution.
+Magic is notably faster.
+
+Mainly because perl's way of handling magic is lighter by nature, and because there is no need for any method resolution.
+Also, since you don't have to reimplement all the variable semantics, you only pay for what you actually use.
=back
@@ -67,197 +111,371 @@ The operations that can be overloaded are :
=item *
-C
+I
-This magic is invoked when the variable is evaluated (does not include array/hash subscripts and slices).
+This magic is invoked when the variable is evaluated.
+It is never called for arrays and hashes.
=item *
-C
+I
-This one is triggered each time the value of the variable changes (includes array/hash subscripts and slices).
+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
+I
+
+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, C], L, L and L are only exported on request.
All of them are exported by the tags C<':funcs'> and C<':all'>.
-The constants L, L, L, L, L, L and L are also only exported on request.
-They are all exported by the tags C<':consts'> and C<':all'>.
+All the constants are also only exported on request, either individually or by the tags C<':consts'> and C<':all'>.
=cut
-use base qw/Exporter/;
+use base qw;
our @EXPORT = ();
our %EXPORT_TAGS = (
- 'funcs' => [ qw/wizard gensig getsig cast getdata dispell/ ],
- 'consts' => [ qw/SIG_MIN SIG_MAX SIG_NBR MGf_COPY MGf_DUP MGf_LOCAL VMG_UVAR/,
- qw/VMG_COMPAT_ARRAY_PUSH_NOLEN VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID VMG_COMPAT_ARRAY_UNDEF_CLEAR/,
- qw/VMG_COMPAT_SCALAR_LENGTH_NOLEN/,
- qw/VMG_PERL_PATCHLEVEL/,
- qw/VMG_THREADSAFE/ ]
+ 'funcs' => [ qw ],
+ '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
+ VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT
+ > ],
);
our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS;
$EXPORT_TAGS{'all'} = [ @EXPORT_OK ];
=head1 CAVEATS
-If you store a magic object in the private data slot, the magic won't be accessible by L since it's not copied by assignation.
-The only way to address this would be to return a reference.
+In order to hook hash operations with magic, you need at least perl 5.10.0 (see L).
-If you define a wizard with a C callback and cast it on itself, this destructor won't be called because the wizard will be destroyed first.
+If you want to store a magic object in the private data slot, you will not be able to recover the magic with L, since magic is not copied by assignment.
+You can work around this gotcha by storing a reference to the magic object instead.
-=head1 DEPENDENCIES
+If you define a wizard with I magic and cast it on itself, it results in a memory cycle, so this destructor will not be called when the wizard is freed.
-L 5.7.3.
-
-L (standard since perl 5), L (standard since perl 5.006).
+=head1 DEPENDENCIES
-Copy tests need L (standard since perl 5.005) and L (since 5.002).
+L 5.8.
-Some uvar tests need L (standard since perl 5.009004).
+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.
-Glob tests need L (standard since perl 5.002).
+L (core since perl 5), L (since 5.6.0).
-Threads tests need L and L.
+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
@@ -490,7 +703,8 @@ You can contact me by mail or on C (vincent).
=head1 BUGS
-Please report any bugs or feature requests to C, or through the web interface at L. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
+Please report any bugs or feature requests to C, or through the web interface at L.
+I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
@@ -502,7 +716,7 @@ Tests code coverage report is available at L