NAME

Data::Tie::Watch - place watchpoints on Perl variables.

SYNOPSIS

use Data::Tie::Watch;

$watch = Data::Tie::Watch->new(
    -variable => \$frog,
    -shadow   => 0,			  
    -fetch    => [\&fetch, 'arg1', 'arg2', ..., 'argn'],
    -store    => \&store,
    -destroy  => sub {print "Final value=$frog.\n"},
}
$val   = $watch->Fetch;
$watch->Store('Hello');
$watch->Unwatch;

DESCRIPTION

Note: This is a copy of Tk's Tie::Watch. Copied to avoid the Tk depedency.

This class module binds one or more subroutines of your devising to a Perl variable. All variables can have FETCH, STORE and DESTROY callbacks. Additionally, arrays can define CLEAR, DELETE, EXISTS, EXTEND, FETCHSIZE, POP, PUSH, SHIFT, SPLICE, STORESIZE and UNSHIFT callbacks, and hashes can define CLEAR, DELETE, EXISTS, FIRSTKEY and NEXTKEY callbacks. If these term are unfamiliar to you, I really suggest you read perltie.

With Data::Tie::Watch you can:

. alter a variable's value
. prevent a variable's value from being changed
. invoke a Perl/Tk callback when a variable changes
. trace references to a variable

Callback format is patterned after the Perl/Tk scheme: supply either a code reference, or, supply an array reference and pass the callback code reference in the first element of the array, followed by callback arguments. (See examples in the Synopsis, above.)

Tie::Watch provides default callbacks for any that you fail to specify. Other than negatively impacting performance, they perform the standard action that you'd expect, so the variable behaves "normally". Once you override a default callback, perhaps to insert debug code like print statements, your callback normally finishes by calling the underlying (overridden) method. But you don't have to!

To map a tied method name to a default callback name simply lowercase the tied method name and uppercase its first character. So FETCH becomes Fetch, NEXTKEY becomes Nextkey, etcetera.

SUBROUTINES/METHODS

$watch->Fetch();  $watch->Fetch($key);

Returns a variable's current value. $key is required for an array or hash.

$watch->Store($new_val);  $watch->Store($key, $new_val);

Store a variable's new value. $key is required for an array or hash.

new

Watch constructor.

The *real* constructor is Data::Tie::Watch->base_watch(), invoked by methods in other Watch packages, depending upon the variable's type. Here we supply defaulted parameter values and then verify them, normalize all callbacks and bind the variable to the appropriate package.

The watchpoint constructor method that accepts option/value pairs to create and configure the Watch object. The only required option is -variable.

-variable is a reference to a scalar, array or hash variable.

-shadow (default 1) is 0 to disable array and hash shadowing. To prevent infinite recursion Data::Tie::Watch maintains parallel variables for arrays and hashes. When the watchpoint is created the parallel shadow variable is initialized with the watched variable's contents, and when the watchpoint is deleted the shadow variable is copied to the original variable. Thus, changes made during the watch process are not lost. Shadowing is on by default. If you disable shadowing any changes made to an array or hash are lost when the watchpoint is deleted.

Specify any of the following relevant callback parameters, in the format described above: -fetch, -store, -destroy. Additionally for arrays: -clear, -extend, -fetchsize, -pop, -push, -shift, -splice, -storesize and -unshift. Additionally for hashes: -clear, -delete, -exists, -firstkey and -nextkey.

DESTROY

Clean up global cache.

Note: Originally the 'Unwatch()' method call was placed at just before the return of 'callback()' which appeared to be the logical place for it. However it would occasionally provoke a segmentation fault (possibly indirectly).

Unwatch

Stop watching a variable by releasing the last reference and untieing it.

Updates the original variable with its shadow, if appropriate.

base_watch

Watch base class constructor invoked by other Watch modules.

callback

Execute a Watch callback, either the default or user specified.
Note that the arguments are those supplied by the tied method,
not those (if any) specified by the user when the watch object
was instantiated.  This is for performance reasons.

$_[0] = self
$_[1] = callback type
$_[2] through $#_ = tied arguments

EFFICIENCY CONSIDERATIONS

If you can live with using the class methods provided, please do so. You can meddle with the object hash directly and improve watch performance, at the risk of your code breaking in the future.

AUTHOR

Originally: Stephen O. Lidie

Currently: Tim Potapov, <tim.potapov at gmail.com>

COPYRIGHT

Copyright (C) 1996 - 2005 Stephen O. Lidie. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)