|
NAMETie::Watch - place watchpoints on Perl variables. SYNOPSISuse Tie::Watch; $watch = Tie::Watch->new( -variable => \$frog, -debug => 1, -shadow => 0, -fetch => [\&fetch, 'arg1', 'arg2', ..., 'argn'], -store => \&store, -destroy => sub {print "Final value=$frog.\n"}, } %vinfo = $watch->Info; $args = $watch->Args(-fetch); $val = $watch->Fetch; print "val=", $watch->Say($val), ".\n"; $watch->Store('Hello'); $watch->Unwatch; DESCRIPTIONThis 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 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. Here are two callbacks for a scalar. The FETCH (read) callback does nothing other than illustrate the fact that it returns the value to assign the variable. The STORE (write) callback uppercases the variable and returns it. In all cases the callback must return the correct read or write value - typically, it does this by invoking the underlying method. my $fetch_scalar = sub { my($self) = @_; $self->Fetch; }; my $store_scalar = sub { my($self, $new_val) = @_; $self->Store(uc $new_val); }; Here are FETCH and STORE callbacks for either an array or hash. They do essentially the same thing as the scalar callbacks, but provide a little more information. my $fetch = sub { my($self, $key) = @_; my $val = $self->Fetch($key); print "In fetch callback, key=$key, val=", $self->Say($val); my $args = $self->Args(-fetch); print ", args=('", join("', '", @$args), "')" if $args; print ".\n"; $val; }; my $store = sub { my($self, $key, $new_val) = @_; my $val = $self->Fetch($key); $new_val = uc $new_val; $self->Store($key, $new_val); print "In store callback, key=$key, val=", $self->Say($val), ", new_val=", $self->Say($new_val); my $args = $self->Args(-store); print ", args=('", join("', '", @$args), "')" if $args; print ".\n"; $new_val; }; In all cases, the first parameter is a reference to the Watch object, used to invoke the following class methods. METHODS
EFFICIENCY CONSIDERATIONSIf you can live using the class methods provided, please do so. You can meddle with the object hash directly and improved watch performance, at the risk of your code breaking in the future.AUTHORStephen O. LidieHISTORYlusol@Lehigh.EDU, LUCC, 96/05/30 . Original version 0.92 release, based on the Trace module from Hans Mulder, and ideas from Tim Bunce. lusol@Lehigh.EDU, LUCC, 96/12/25 . Version 0.96, release two inner references detected by Perl 5.004. lusol@Lehigh.EDU, LUCC, 97/01/11 . Version 0.97, fix Makefile.PL and MANIFEST (thanks Andreas Koenig). Make sure test.pl doesn't fail if Tk isn't installed. Stephen.O.Lidie@Lehigh.EDU, Lehigh University Computing Center, 97/10/03 . Version 0.98, implement -shadow option for arrays and hashes. Stephen.O.Lidie@Lehigh.EDU, Lehigh University Computing Center, 98/02/11 . Version 0.99, finally, with Perl 5.004_57, we can completely watch arrays. With tied array support this module is essentially complete, so its been optimized for speed at the expense of clarity - sorry about that. The Delete() method has been renamed Unwatch() because it conflicts with the builtin delete(). Stephen.O.Lidie@Lehigh.EDU, Lehigh University Computing Center, 99/04/04 . Version 1.0, for Perl 5.005_03, update Makefile.PL for ActiveState, and add two examples (one for Perl/Tk). sol0@lehigh.edu, Lehigh University Computing Center, 2003/06/07 . Version 1.1, for Perl 5.8, can trace a reference now, patch from Slaven Rezic. sol0@lehigh.edu, Lehigh University Computing Center, 2005/05/17 . Version 1.2, for Perl 5.8, per Rob Seegel's suggestion, support array DELETE and EXISTS. COPYRIGHTCopyright (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.
Visit the GSP FreeBSD Man Page Interface. |