X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=lib%2FVariable%2FMagic.pm;h=12af5b595c3cac25c8ae90716e5275a03eb60aa2;hb=810a63159661ab3e66c139d6f62fcd28f19ab7be;hp=7289d6e4e3c8f7c3d2a0ace1535c0bdfad1fe49e;hpb=14f66d40970bef63105be046a109c1a32859a8a0;p=perl%2Fmodules%2FVariable-Magic.git
diff --git a/lib/Variable/Magic.pm b/lib/Variable/Magic.pm
index 7289d6e..12af5b5 100644
--- a/lib/Variable/Magic.pm
+++ b/lib/Variable/Magic.pm
@@ -1,227 +1,705 @@
package Variable::Magic;
+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.04
+Version 0.57
=cut
-our $VERSION = '0.04';
+our $VERSION;
+BEGIN {
+ $VERSION = '0.57';
+}
=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 overload 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.
-The operations that can be overloaded are :
+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.
+
+Still, the magic made available by this module differs from tieing and overloading in several ways :
=over 4
-=item C
+=item *
-This magic is invoked when the variable is evaluated (does not include array/hash subscripts and slices).
+Magic is not copied on assignment.
-=item C
+You attach it to variables, not values (as for blessed references).
-This one is triggered each time the value of the variable changes (includes array/hash subscripts and slices).
+=item *
-=item C
+Magic does not replace the original semantics.
-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 assignation 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 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/ ]
+ '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;
+$EXPORT_TAGS{'all'} = [ @EXPORT_OK ];
+
+=head1 CAVEATS
+
+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 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 (standard since perl 5), L (standard since perl 5.006).
+L 5.8.
+
+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).
=head1 SEE ALSO
L and L for internal information about magic.
+L and L for other ways of enhancing objects.
+
=head1 AUTHOR
-Vincent Pit, C<< >>
+Vincent Pit, C<< >>, L.
-You can contact me by mail or on #perl @ FreeNode (Prof_Vince).
+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
@@ -229,9 +707,11 @@ 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 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.