]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blobdiff - lib/Variable/Magic.pm
Importing Variable-Magic-0.04.tar.gz
[perl/modules/Variable-Magic.git] / lib / Variable / Magic.pm
index 9944a9102df012175232d687346082c53b88a9de..7289d6e4e3c8f7c3d2a0ace1535c0bdfad1fe49e 100644 (file)
@@ -11,17 +11,17 @@ Variable::Magic - Associate user-defined magic to variables from Perl.
 
 =head1 VERSION
 
-Version 0.01
+Version 0.04
 
 =cut
 
-our $VERSION = '0.01';
+our $VERSION = '0.04';
 
 =head1 SYNOPSIS
 
     use Variable::Magic qw/wizard cast dispell/;
 
-    my $wiz = wizard set => sub { print STDERR "now set to $_[0]!\n" };
+    my $wiz = wizard set => sub { print STDERR "now set to ${$_[0]}!\n" };
     my $a = 1;
     cast $a, $wiz;
     $a = 2;          # "now set to 2!"
@@ -50,7 +50,7 @@ This magic is a little special : it is called when the 'size' or the 'length' of
 
 =item C<clear>
 
-This magic is invoked when the variable is reset, such as when an array is emptied. Please note that this is different from undefining the variable, even though the magic is called when the reset is a result of the undefine (e.g. for an array).
+This magic is invoked when the variable is reset, such as when an array is emptied. Please note that this is different from undefining the variable, even though the magic is called when the clearing is a result of the undefine (e.g. for an array, but actually a bug prevent it to work before perl 5.9.5 - see the L<history|/PERL MAGIC HISTORY>).
 
 =item C<free>
 
@@ -60,41 +60,47 @@ This last one can be considered as an object destructor. It happens when the var
 
 To prevent any clash between different magics defined with this module, an unique numerical signature is attached to each kind of magic (i.e. each set of callbacks for magic operations).
 
+=head1 PERL MAGIC HISTORY
+
+The places where magic is invoked have changed a bit through perl history. Here's a little list of the most recent ones.
+
+=head2 B<5.9.3>
+
+=over 4
+
+=item 'len' magic is no longer called when pushing an element into a magic array.
+
+=back
+
+=head2 B<5.9.5>
+
+=over 4
+
+=item 'clear' magic wasn't invoked when undefining an array. The bug is fixed as of this version.
+
+=back
+
 =head1 CONSTANTS
 
 =head2 C<SIG_MIN>
 
 The minimum integer used as a signature for user-defined magic.
 
-=cut
-
-use constant SIG_MIN => 2 ** 8;
-
 =head2 C<SIG_MAX>
 
 The maximum integer used as a signature for user-defined magic.
 
-=cut
-
-use constant SIG_MAX => 2 ** 16 - 1;
-
 =head2 C<SIG_NBR>
 
     SIG_NBR = SIG_MAX - SIG_MIN + 1
 
-=cut
-
-use constant SIG_NBR => SIG_MAX - SIG_MIN + 1;
-
 =head1 FUNCTIONS
 
 =cut
 
-require XSLoader;
+use XSLoader;
 
-XSLoader::load(__PACKAGE__, $VERSION);
-
-my %wizz;
+XSLoader::load __PACKAGE__, $VERSION;
 
 =head2 C<wizard>
 
@@ -106,37 +112,29 @@ This function creates a 'wizard', an opaque type that holds the magic informatio
 
 =item C<'sig'>
 
-The numerical signature. If not specified or undefined, a random signature is generated.
+The numerical signature. If not specified or undefined, a random signature is generated. If the signature matches an already defined magic, then the existant magic object is returned.
 
 =item C<'data'>
 
-A code reference to a private data constructor. It will be called each time this magic is cast on a variable, and the scalar returned will be used as private data storage for it.
+A code 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</cast>.
 
 =item C<'get'>, C<'set'>, C<'len'>, C<'clear'> and C<'free'>
 
-Code references to corresponding magic callbacks. You don't have to specify all of them : the magic associated with undefined entries simply won't be hooked. When the magic variable is an array or a hash, C<$_[0]> is a reference to it, but directly references it otherwise. C<$_[1]> is the private data (or C<undef> when no private data constructor was supplied). In the special case of C<len> magic and when the variable is an array, C<$_[2]> contains its normal length.
+Code references to 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 a reference to the magic object and C<$_[1]> is the private data (or C<undef> when no private data constructor was supplied). In the special case of C<len> magic and when the variable is an array, C<$_[2]> contains its normal length.
 
 =back
 
     # 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" }
+    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" }
 
 =cut
 
 sub wizard {
  croak 'Wrong number of arguments for wizard()' if @_ % 2;
  my %opts = @_;
- my $sig;
- if (defined $opts{sig}) {
-  $sig = int $opts{sig};
-  $sig += SIG_MIN if $sig < SIG_MIN;
-  $sig %= SIG_MAX + 1 if $sig > SIG_MAX;
- } else {
-  $sig = gensig();
- }
- return _wizard($sig, map { $opts{$_} } qw/get set len clear free data/);
+ return _wizard(map { $opts{$_} } qw/sig get set len clear free data/);
 }
 
 =head2 C<gensig>
@@ -146,16 +144,6 @@ With this tool, you can manually generate random magic signature between SIG_MIN
     # Generate a signature
     my $sig = gensig;
 
-=cut
-
-sub gensig {
- my $sig;
- my $used = ~~keys %wizz;
- croak 'Too many magic signatures used' if $used == SIG_NBR;
- do { $sig = SIG_MIN + int(rand(SIG_NBR)) } while $wizz{$sig};
- return $sig;
-}
-
 =head2 C<getsig>
 
     getsig $wiz
@@ -167,28 +155,30 @@ This accessor returns the magic signature of this wizard.
 
 =head2 C<cast>
 
-    cast [$@%&*]var, $wiz
+    cast [$@%&*]var, [$wiz|$sig], ...
 
-This function associates C<$wiz> magic to the variable supplied, without overwriting any other kind of magic. It returns true on success or when C<$wiz> magic is already present, and false on error.
+This function associates C<$wiz> magic to the variable supplied, without overwriting any other kind of magic. You can also supply the numeric signature C<$sig> instead of C<$wiz>. It returns true on success or when C<$wiz> magic is already present, C<0> on error, and C<undef> when no magic corresponds to the given signature (in case C<$sig> was supplied). All extra arguments specified after C<$wiz> are passed to the private data constructor.
 
-    # Casts $wiz to $x
+    # Casts $wiz onto $x. If $wiz isn't a signature, undef can't be returned.
     my $x;
     die 'error' unless cast $x, $wiz;
 
 =head2 C<getdata>
 
-    getdata [$@%&*]var, $wiz
+    getdata [$@%&*]var, [$wiz|$sig]
+
+This accessor fetches the private data associated with the magic C<$wiz> (or the signature C<$sig>) in the variable. C<undef> is returned when no such magic or data is found, or when C<$sig> does not represent a current valid magic object.
 
-This accessor fetches the private data associated with the magic C<$wiz> in the variable. C<undef> is returned when no such magic or data is found.
+    # Get the attached data.
+    my $data = getdata $x, $wiz or die 'no such magic or magic has no data';
 
 =head2 C<dispell>
 
-    dispell [$@%&*]variable, $wiz
-    dispell [$@%&*]variable, $sig
+    dispell [$@%&*]variable, [$wiz|$sig]
 
-The exact opposite of L</cast> : it dissociates C<$wiz> magic from the variable. You can also pass the magic signature as the second argument. True is returned on success, and false on error or when no magic represented by C<$wiz> could be found in the variable.
+The exact opposite of L</cast> : it dissociates C<$wiz> magic from the variable. You can also pass the magic signature C<$sig> as the second argument. True is returned on success, C<0> on error or when no magic represented by C<$wiz> could be found in the variable, and C<undef> when no magic corresponds to the given signature (in case C<$sig> was supplied).
 
-    # Dispell now
+    # Dispell now. If $wiz isn't a signature, undef can't be returned.
     die 'no such magic or error' unless dispell $x, $wiz;
 
 =head1 EXPORT
@@ -213,7 +203,7 @@ $EXPORT_TAGS{'all'} = \@EXPORT_OK;
 
 L<Carp> (standard since perl 5), L<XSLoader> (standard since perl 5.006).
 
-Tests use L<Symbol> (standard since perl 5.002).
+Glob tests need L<Symbol> (standard since perl 5.002).
 
 =head1 SEE ALSO
 
@@ -223,6 +213,8 @@ L<perlguts> and L<perlapi> for internal information about magic.
 
 Vincent Pit, C<< <perl at profvince.com> >>
 
+You can contact me by mail or on #perl @ FreeNode (Prof_Vince).
+
 =head1 BUGS
 
 Please report any bugs or feature requests to