X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;ds=inline;f=lib%2FVariable%2FMagic.pm;h=01b1c7069223a53b2597aaf91cd542f71623832e;hb=12570a1f39c0588d6ae68281b45ef1b7e6083958;hp=d33c37b4d47e2b1a8666bfe5bdb7a7378ca3a50f;hpb=017450c5da83c82c9149b5170c757ce03679b9a9;p=perl%2Fmodules%2FVariable-Magic.git
diff --git a/lib/Variable/Magic.pm b/lib/Variable/Magic.pm
index d33c37b..01b1c70 100644
--- a/lib/Variable/Magic.pm
+++ b/lib/Variable/Magic.pm
@@ -1,227 +1,646 @@
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.05
+Version 0.46
=cut
-our $VERSION = '0.05';
+our $VERSION;
+BEGIN {
+ $VERSION = '0.46';
+}
=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'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...
+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).
+It isn't 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
+It doesn't 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_ARRAY_PUSH_NOLEN VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID
+ VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID
+ VMG_COMPAT_ARRAY_UNDEF_CLEAR
+ VMG_COMPAT_SCALAR_LENGTH_NOLEN
+ 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
+
+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.
+
+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.
+
+In order to define magic on hash members, you need at least L 5.10.0 (see L)
=head1 DEPENDENCIES
+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.
+
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).
+
Glob tests need L (standard since perl 5.002).
+Threads tests need L and L.
+
=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 +648,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 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.