Tie::Watch(3)
π Tie::Watch(3) β place watchpoints on Perl variables π
okay so this little icon comes from the perl/Tk Documentation family, and it's here to let you put watchpoints on Perl variables. bestie, it's giving "I want to know when my variable breathes." let's go β¨
π¦ NAME π¦
Tie::Watch β place watchpoints on Perl variables.
π SYNOPSIS π
use 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;
πΈ DESCRIPTION πΈ
so this class module binds one or more subroutines of your own devising to a Perl variable. literally all variables can have FETCH, STORE and DESTROY callbacks. plus, 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 those terms are unfamiliar to you, the original really suggests you read perltie β and honestly, same.
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, bestie π)
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 β maybe to insert debug code like print statements β your callback normally finishes by calling the underlying (overridden) method. but you don't have to! main character choice tbh.
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. periodt.
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. love that for us π«Ά
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. slay.
π METHODS π
$watch = Tie::Watch->new(-options => values);β 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.-debug(default0) β is1to activate debug print statements internal toTie::Watch.-shadow(default1) β is0to disable array and hash shadowing. to prevent infinite recursionTie::Watchmaintains 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. β οΈ so maybe don't do that casually.
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.
$args = $watch->Args(-fetch);β returns a reference to a list of arguments for the specified callback, or undefined if none.$watch->Fetch();$watch->Fetch($key);β returns a variable's current value.$keyis required for an array or hash.%vinfo = $watch->Info();β returns a hash detailing the internals of the Watch object, with these keys:
%vinfo = {
-variable => SCALAR(0x200737f8)
-debug => '0'
-shadow => '1'
-value => 'HELLO SCALAR'
-destroy => ARRAY(0x200f86cc)
-fetch => ARRAY(0x200f8558)
-store => ARRAY(0x200f85a0)
-legible => above data formatted as a list of string, for printing
}
for array and hash Watch objects, the -value key is replaced with a -ptr key which is a reference to the parallel array or hash. additionally, for an array or hash, there are key/value pairs for all the variable specific callbacks. iykyk.
$watch->Say($val);β used mainly for debugging, it returns$valin quotes if required, or the string"undefined"for undefined values.$watch->Store($new_val);$watch->Store($key, $new_val);β store a variable's new value.$keyis required for an array or hash.$watch->Unwatch();β stop watching the variable. bye bestie π
π₯ EFFICIENCY CONSIDERATIONS π₯
if 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. highkey tempting, lowkey risky β your call.
β AUTHOR β
Stephen O. Lidie
π§ HISTORY π§
lusol@Lehigh.EDU, LUCC, 96/05/30β original version 0.92 release, based on theTracemodule 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, fixMakefile.PLandMANIFEST(thanks Andreas Koenig). make suretest.pldoesn't fail if Tk isn't installed.Stephen.O.Lidie@Lehigh.EDU, Lehigh University Computing Center, 97/10/03β version 0.98, implement-shadowoption 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. theDelete()method has been renamedUnwatch()because it conflicts with the builtindelete().Stephen.O.Lidie@Lehigh.EDU, Lehigh University Computing Center, 99/04/04β version 1.0, for Perl 5.005_03, updateMakefile.PLfor 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 arrayDELETEandEXISTS.
π 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.
that's the whole entire page, bestie β source/version Tk804.036, dated 2025-07-31. now go tie some variables and be the main character of your own watchpoint story πβ¨
π¦ where this page comes from
- alpine 3.23: perl-tk-doc 804.036-r8 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- alpine 3.24: perl-tk-doc 804.036-r8 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms