From: Vincent Pit Date: Sat, 23 Jun 2012 20:02:16 +0000 (+0200) Subject: Jumbo POD overhaul X-Git-Tag: rt77991~9 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=8dcb0d71518205c9b31607040494731eed689de6;p=perl%2Fmodules%2FVariable-Magic.git Jumbo POD overhaul Lots of rewordings and clarifications. --- diff --git a/lib/Variable/Magic.pm b/lib/Variable/Magic.pm index b22fee6..1aa0080 100644 --- a/lib/Variable/Magic.pm +++ b/lib/Variable/Magic.pm @@ -57,7 +57,7 @@ This mechanism lets the user add extra data to any variable and hook syntactical 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 : @@ -66,35 +66,35 @@ 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 get triggered before the original action takes place, and can't prevent it from happening. +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 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. =item * -It's mostly invisible at the Perl level. +Magic is invisible at the 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 @@ -114,16 +114,16 @@ It is never called for arrays and hashes. C -This one is triggered each time the value of the variable changes. +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 -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 or C). -The callback has then to return the length as an integer. +This magic only applies to scalars and arrays, 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 or C). +The length is returned from the callback as an integer. =item * @@ -136,34 +136,32 @@ Please note that this is different from undefining the variable, even though the C -This one can be considered as an object destructor. -It happens when the variable goes out of scope, but not when it is undefined. +This magic is called when an object is destroyed as the result of the variable going out of scope (but not when the variable is undefined). =item * C -This magic only applies to tied arrays and hashes. -It fires when you try to access or change their elements. -It is available on your perl iff C is true. +This magic only applies to tied arrays and hashes, and fires when you try to access or change their elements. +It is available on your perl if and only if C is true. =item * C -Invoked when the variable is cloned across threads. -Currently not available. +This magic is invoked when the variable is cloned across threads. +It is currently not available. =item * C When this magic is set on a variable, all subsequent localizations of the variable will trigger the callback. -It is available on your perl iff C is true. +It is available on your perl if and only if C is true. =back -The following actions only apply to hashes and are available iff L is true. +The following actions only apply to hashes and are available if and only if L is true. They are referred to as C magics. =over 4 @@ -172,7 +170,7 @@ They are referred to as C magics. C -This magic happens each time an element is fetched from the hash. +This magic is invoked each time an element is fetched from the hash. =item * @@ -190,7 +188,7 @@ This magic fires when a key is tested for existence in the hash. C -This last one triggers when a key is deleted in the hash, regardless of whether the key actually exists in it. +This magic is triggered when a key is deleted in the hash, regardless of whether the key actually exists in it. =back @@ -226,7 +224,7 @@ BEGIN { op_info => [ 0 | VMG_OP_INFO_NAME | VMG_OP_INFO_OBJECT ], ) -This function creates a 'wizard', an opaque type that holds the magic information. +This function creates a 'wizard', an opaque object that holds the magic information. It takes a list of keys / values as argument, whose keys can be : =over 4 @@ -236,21 +234,18 @@ It takes a list of keys / values as argument, whose keys can be : C A code (or string) 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. +It is called in scalar context each time the magic is cast onto a variable, with C<$_[0]> being a reference to this variable and C<@_[1 .. @_-1]> being all extra arguments that were passed to L. +The scalar returned from this call is then attached to the variable and can be retrieved later with L. =item * C, C, C, C, C, C, C, C, C, C and C -Code (or string) references to the 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 always a reference to the magic object and C<$_[1]> is always the private data (or C when no private data constructor was supplied). +Code (or string) references to the respective magic callbacks. +You don't have to specify all of them : the magic corresponding to undefined entries will simply not be hooked. -Moreover, when you pass C<< op_info => $num >> to C, the last element of C<@_> will be the current op name if C<$num == VMG_OP_INFO_NAME> and a C object representing the current op if C<$num == VMG_OP_INFO_OBJECT>. -Both have a performance hit, but just getting the name is lighter than getting the op object. - -Other arguments are specific to the magic hooked : +When those callbacks are executed, C<$_[0]> is a reference to the magic variable and C<$_[1]> is the associated private data (or C when no private data constructor is supplied with the wizard). +Other arguments depend on which kind of magic is involved : =over 8 @@ -258,29 +253,48 @@ Other arguments are specific to the magic hooked : C -When the variable is an array or a scalar, C<$_[2]> contains the non-magical length. -The callback can return the new scalar or array length to use, or C to default to the normal length. +C<$_[2]> contains the natural, non-magical length of the variable (which can only be a scalar or an array as len magic is only relevant for these types). +The callback is expected to return the new scalar or array length to use, or C to default to the normal length. =item * C -C<$_[2]> is a either a copy or an alias of the current key, which means that it is useless to try to change or cast magic on it. -C<$_[3]> is an alias to the current element (i.e. the value). +C<$_[2]> is a either an alias or a copy of the current key, and C<$_[3]> is an alias to the current element (i.e. the value). +Because C<$_[2]> might be a copy, it is useless to try to change it or cast magic on it. =item * C, C, C and C C<$_[2]> is an alias to the current key. -Nothing prevents you from changing it, but be aware that there lurk dangerous side effects. -For example, it may rightfully be readonly if the key was a bareword. -You can get a copy instead by passing C<< copy_key => 1 >> to L, which allows you to safely assign to C<$_[2]> in order to e.g. redirect the action to another key. -This however has a little performance drawback because of the copy. +Note that C<$_[2]> may rightfully be readonly if the key comes from a bareword, and as such it is unsafe to assign to it. +You can ask for a copy instead by passing C<< copy_key => 1 >> to L which, at the price of a small performance hit, allows you to safely assign to C<$_[2]> in order to e.g. redirect the action to another key. + +=back + +Finally, if C<< op_info => $num >> is also passed to C, then one extra element is appended to C<@_>. +Its nature depends on the value of C<$num> : + +=over 8 + +=item * + +C + +C<$_[-1]> is the current op name. + +=item * + +C + +C<$_[-1]> is the C object for the current op. =back -All the callbacks are expected to return an integer, which is passed straight to the perl magic API. +Both result in a small performance hit, but just getting the name is lighter than getting the op object. + +These callbacks are executed in scalar context and are expected to return an integer, which is then passed straight to the perl magic API. However, only the return value of the C callback currently holds a meaning. =back @@ -305,9 +319,9 @@ This may especially be helpful for 'local' magic, where an empty callback preven =back -Note that C callbacks are I called during global destruction, as there's no way to ensure that the wizard and the C callback weren't destroyed before the variable. +Note that C callbacks are I called during global destruction, as there is no way to ensure that the wizard object and the C callback were not destroyed before the variable. -Here's a simple usage example : +Here is a simple usage example : # A simple scalar tracer my $wiz = wizard( @@ -347,34 +361,35 @@ sub wizard { =head2 C - cast [$@%&*]var, $wiz, ... + cast [$@%&*]var, $wiz, @args -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 croaks on error. -All extra arguments specified after C<$wiz> are passed to the private data constructor in C<@_[1 .. @_-1]>. -If the variable isn't a hash, any C callback of the wizard is safely ignored. +This function associates C<$wiz> magic to the supplied variable, without overwriting any other kind of magic. +It returns true on success or when C<$wiz> magic is already attached, and croaks on error. +When C<$wiz> provides a data constructor, it is called just before magic is cast onto the variable, and it receives a reference to the target variable in C<$_[0]> and the content of C<@args> in C<@_[1 .. @args]>. +Otherwise, C<@args> is ignored. - # Casts $wiz onto $x, and pass '1' to the data constructor. + # Casts $wiz onto $x, passing (\$x, '1') to the data constructor. my $x; cast $x, $wiz, 1; The C argument can be an array or hash value. -Magic for those behaves like for any other scalar, except that it is dispelled when the entry is deleted from the container. +Magic for these scalars behaves like for any other, except that it is dispelled when the entry is deleted from the container. For example, if you want to call C each time the C<'TZ'> environment variable is changed in C<%ENV>, you can use : use POSIX; cast $ENV{TZ}, wizard set => sub { POSIX::tzset(); () }; -If you want to overcome the possible deletion of the C<'TZ'> entry, you have no choice but to rely on C uvar magic. +If you want to handle the possible deletion of the C<'TZ'> entry, you must also specify C uvar magic. =head2 C getdata [$@%&*]var, $wiz This accessor fetches the private data associated with the magic C<$wiz> in the variable. -It croaks when C<$wiz> do not represent a valid magic object, and returns an empty list if no such magic is attached to the variable or when the wizard has no data constructor. +It croaks when C<$wiz> does not represent a valid magic object, and returns an empty list if no such magic is attached to the variable or when the wizard has no data constructor. - # Get the attached data, or undef if the wizard does not attach any. + # Get the data attached to $wiz in $x, or undef if $wiz + # did not attach any. my $data = getdata $x, $wiz; =head2 C @@ -391,21 +406,20 @@ This function returns true on success, C<0> when no magic represented by C<$wiz> =head2 C -Evaluates to true iff the 'copy' magic is available. +Evaluates to true if and only if the 'copy' magic is available. =head2 C -Evaluates to true iff the 'dup' magic is available. +Evaluates to true if and only if the 'dup' magic is available. =head2 C -Evaluates to true iff the 'local' magic is available. +Evaluates to true if and only if the 'local' magic is available. =head2 C When this constant is true, you can use the C callbacks on hashes. -Initial VMG_UVAR capability was introduced in perl 5.9.5, with a fully functional implementation -shipped with perl 5.10.0. +Initial VMG_UVAR capability was introduced in perl 5.9.5, with a fully functional implementation shipped with perl 5.10.0. =head2 C @@ -442,12 +456,12 @@ The perl patchlevel this module was built with, or C<0> for non-debugging perls. =head2 C -True iff this module could have been built with thread-safety features enabled. +True if and only if this module could have been built with thread-safety features enabled. =head2 C -True iff this module could have been built with fork-safety features enabled. -This will always be true except on Windows where it's false for perl 5.10.0 and below . +True if and only if this module could have been built with fork-safety features enabled. +This is always true except on Windows where it is false for perl 5.10.0 and below. =head2 C @@ -525,7 +539,7 @@ This allows you to recursively cast magic on datastructures : cast %h, $wiz; } -When C<%h> goes out of scope, this will print something among the lines of : +When C<%h> goes out of scope, this prints something among the lines of : free HASH at depth 0 free HASH at depth 1 @@ -539,7 +553,7 @@ Of course, this example does nothing with the values that are added after the C< =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. +Here is a little list of the most recent ones. =over 4 @@ -571,7 +585,7 @@ B<5.9.5> I : Meaningful 'uvar' magic. -I : 'clear' magic wasn't invoked when undefining an array. +I : 'clear' magic was not invoked when undefining an array. The bug is fixed as of this version. =item * @@ -625,12 +639,12 @@ $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 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. -In order to define magic on hash members, you need at least L 5.10.0 (see L) +If you define a wizard with a C callback 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 @@ -639,15 +653,12 @@ 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). +L (core since perl 5), L (since 5.006). -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.009004). +Glob tests need L (since 5.002). +Threads tests need L and L (both since 5.007003). =head1 SEE ALSO @@ -663,7 +674,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