X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=lib%2FVariable%2FMagic.pm;h=1be2bf17906b14f84bc7515c8150a27098ba5a04;hb=2d569c9187148522afa2ec8ec9c8c805cc39af8a;hp=db71e5e0e625a457d48f967f7b917acb6e1258dd;hpb=2cb1dced4bb9a9898138c0ca3d6be8e103fa36c1;p=perl%2Fmodules%2FVariable-Magic.git
diff --git a/lib/Variable/Magic.pm b/lib/Variable/Magic.pm
index db71e5e..1be2bf1 100644
--- a/lib/Variable/Magic.pm
+++ b/lib/Variable/Magic.pm
@@ -5,30 +5,30 @@ 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.33
+Version 0.54
=cut
our $VERSION;
BEGIN {
- $VERSION = '0.33';
+ $VERSION = '0.54';
}
=head1 SYNOPSIS
- use Variable::Magic qw/wizard cast VMG_OP_INFO_NAME/;
+ use Variable::Magic qw;
{ # A variable tracer
- my $wiz = wizard set => sub { print "now set to ${$_[0]}!\n" },
- free => sub { print "destroyed!\n" };
+ my $wiz = wizard(
+ set => sub { print "now set to ${$_[0]}!\n" },
+ free => sub { print "destroyed!\n" },
+ );
my $a = 1;
cast $a, $wiz;
@@ -36,26 +36,28 @@ BEGIN {
} # "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 $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", because the 'banana' key doesn't exist in %h
+ 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 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.
-It's 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...
+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.
Still, the magic made available by this module differs from tieing and overloading in several ways :
@@ -64,35 +66,41 @@ Still, the magic made available by this module differs from tieing and overloadi
=item *
-It isn't copied on assignment.
+Magic is not copied on assignment.
You attach it to variables, not values (as for blessed references).
=item *
-It doesn't replace the original semantics.
+Magic does not 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 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 *
-It's type-agnostic.
+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 *
-It's mostly invisible at the Perl level.
+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.
+Magic is notably faster.
-Mainly because perl's way of handling magic is lighter by nature, and because there's no need for any method resolution.
+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
@@ -103,98 +111,101 @@ 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 is a little special : it is called when the 'size' or the 'length' of the variable has to be known by Perl.
-Typically, it's 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'>.
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 VMG_COMPAT_SCALAR_LENGTH_NOLEN/,
- qw/VMG_PERL_PATCHLEVEL/,
- qw/VMG_THREADSAFE/,
- qw/VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT/
- ]
+ '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 assignment.
-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 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.
-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 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.
=head1 DEPENDENCIES
L 5.8.
-L (standard since perl 5), L (standard since perl 5.006).
-
-Copy tests need L (standard since perl 5.005) and L (since 5.002).
-
-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
@@ -594,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
@@ -606,7 +716,7 @@ Tests code coverage report is available at L