home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 May
/
Chip_2000-05_cd1.bin
/
zkuste
/
Perl
/
ActivePerl-5.6.0.613.msi
/
䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥
/
_be3e57b1fa1c106df0318707fab2dc3e
< prev
next >
Wrap
Text File
|
2000-03-23
|
11KB
|
261 lines
<HTML>
<HEAD>
<TITLE>Tie::Watch - place watchpoints on Perl variables.</TITLE>
<LINK REL="stylesheet" HREF="../../../Active.css" TYPE="text/css">
<LINK REV="made" HREF="mailto:">
</HEAD>
<BODY>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> Tie::Watch - place watchpoints on Perl variables.</P></STRONG>
</TD></TR>
</TABLE>
<A NAME="__index__"></A>
<!-- INDEX BEGIN -->
<UL>
<LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
<LI><A HREF="#synopsis">SYNOPSIS</A></LI>
<LI><A HREF="#description">DESCRIPTION</A></LI>
<LI><A HREF="#methods">METHODS</A></LI>
<LI><A HREF="#efficiency considerations">EFFICIENCY CONSIDERATIONS</A></LI>
<LI><A HREF="#author">AUTHOR</A></LI>
<LI><A HREF="#history">HISTORY</A></LI>
<LI><A HREF="#copyright">COPYRIGHT</A></LI>
</UL>
<!-- INDEX END -->
<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<PRE>
Tie::Watch - place watchpoints on Perl variables.</PRE>
<P>
<HR>
<H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
<UL>
<LI>Linux</LI>
<LI>Solaris</LI>
<LI>Windows</LI>
</UL>
<HR>
<H1><A NAME="synopsis">SYNOPSIS</A></H1>
<PRE>
use Tie::Watch;</PRE>
<PRE>
$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;</PRE>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P>This class module binds one or more subroutines of your devising to a
Perl variable. All variables can have <STRONG>FETCH</STRONG>, <STRONG>STORE</STRONG> and
<STRONG>DESTROY</STRONG> callbacks. Additionally, arrays can define <STRONG>CLEAR</STRONG>, <STRONG>EXTEND</STRONG>,
<STRONG>FETCHSIZE</STRONG>, <STRONG>POP</STRONG>, <STRONG>PUSH</STRONG>, <STRONG>SHIFT</STRONG>, <STRONG>SPLICE</STRONG>, <STRONG>STORESIZE</STRONG> and
<STRONG>UNSHIFT</STRONG> callbacks, and hashes can define <STRONG>CLEAR</STRONG>, <STRONG>DELETE</STRONG>, <STRONG>EXISTS</STRONG>,
<STRONG>FIRSTKEY</STRONG> and <STRONG>NEXTKEY</STRONG> callbacks. If these term are unfamiliar to you,
I <EM>really</EM> suggest you read <A HREF="../../../lib/Pod/perltie.html">the perltie manpage</A>.</P>
<P>With Tie::Watch you can:</P>
<PRE>
. 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</PRE>
<P>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.)</P>
<P>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!</P>
<P>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.</P>
<P>Here are two callbacks for a scalar. The <STRONG>FETCH</STRONG> (read) callback does
nothing other than illustrate the fact that it returns the value to
assign the variable. The <STRONG>STORE</STRONG> (write) callback uppercases the
variable and returns it. In all cases the callback <EM>must</EM> return the
correct read or write value - typically, it does this by invoking the
underlying method.</P>
<PRE>
my $fetch_scalar = sub {
my($self) = @_;
$self->Fetch;
};</PRE>
<PRE>
my $store_scalar = sub {
my($self, $new_val) = @_;
$self->Store(uc $new_val);
};</PRE>
<P>Here are <STRONG>FETCH</STRONG> and <STRONG>STORE</STRONG> callbacks for either an array or hash.
They do essentially the same thing as the scalar callbacks, but
provide a little more information.</P>
<PRE>
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;
};</PRE>
<PRE>
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;
};</PRE>
<P>In all cases, the first parameter is a reference to the Watch object,
used to invoke the following class methods.</P>
<P>
<HR>
<H1><A NAME="methods">METHODS</A></H1>
<DL>
<DT><STRONG><A NAME="item_new">$watch = Tie::Watch->new(-options => values);</A></STRONG><BR>
<DD>
The watchpoint constructor method that accepts option/value pairs to
create and configure the Watch object. The only required option is
<STRONG>-variable</STRONG>.
<P><STRONG>-variable</STRONG> is a <EM>reference</EM> to a scalar, array or hash variable.</P>
<P><STRONG>-debug</STRONG> (default 0) is 1 to activate debug print statements internal
to Tie::Watch.</P>
<P><STRONG>-shadow</STRONG> (default 1) is 0 to disable array and hash shadowing. To
prevent infinite recursion 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 my default. If you disable shadowing any changes made
to an array or hash are lost when the watchpoint is deleted.</P>
<P>Specify any of the following relevant callback parameters, in the
format described above: <STRONG>-fetch</STRONG>, <STRONG>-store</STRONG>, <STRONG>-destroy</STRONG>.
Additionally for arrays: <STRONG>-clear</STRONG>, <STRONG>-extend</STRONG>, <STRONG>-fetchsize</STRONG>,
<STRONG>-pop</STRONG>, <STRONG>-push</STRONG>, <STRONG>-shift</STRONG>, <STRONG>-splice</STRONG>, <STRONG>-storesize</STRONG> and
<STRONG>-unshift</STRONG>. Additionally for hashes: <STRONG>-clear</STRONG>, <STRONG>-delete</STRONG>,
<STRONG>-exists</STRONG>, <STRONG>-firstkey</STRONG> and <STRONG>-nextkey</STRONG>.</P>
<P></P>
<DT><STRONG><A NAME="item_Args">$args = $watch->Args(-fetch);</A></STRONG><BR>
<DD>
Returns a reference to a list of arguments for the specified callback,
or undefined if none.
<P></P>
<DT><STRONG><A NAME="item_Fetch">$watch->Fetch(); $watch->Fetch($key);</A></STRONG><BR>
<DD>
Returns a variable's current value. $key is required for an array or
hash.
<P></P>
<DT><STRONG><A NAME="item_Info">%vinfo = $watch->Info();</A></STRONG><BR>
<DD>
Returns a hash detailing the internals of the Watch object, with these
keys:
<PRE>
%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
}</PRE>
<P>For array and hash Watch objects, the <STRONG>-value</STRONG> key is replaced with a
<STRONG>-ptr</STRONG> 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.</P>
<P></P>
<DT><STRONG><A NAME="item_Say">$watch->Say($val);</A></STRONG><BR>
<DD>
Used mainly for debugging, it returns $val in quotes if required, or
the string ``undefined'' for undefined values.
<P></P>
<DT><STRONG><A NAME="item_Store">$watch->Store($new_val); $watch->Store($key, $new_val);</A></STRONG><BR>
<DD>
Store a variable's new value. $key is required for an array or hash.
<P></P>
<DT><STRONG><A NAME="item_Unwatch">$watch->Unwatch();</A></STRONG><BR>
<DD>
Stop watching the variable.
<P></P></DL>
<P>
<HR>
<H1><A NAME="efficiency considerations">EFFICIENCY CONSIDERATIONS</A></H1>
<P>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.</P>
<P>
<HR>
<H1><A NAME="author">AUTHOR</A></H1>
<P><A HREF="mailto:Stephen.O.Lidie@Lehigh.EDU">Stephen.O.Lidie@Lehigh.EDU</A></P>
<P>
<HR>
<H1><A NAME="history">HISTORY</A></H1>
<PRE>
lusol@Lehigh.EDU, LUCC, 96/05/30
. Original version 0.92 release, based on the Trace module from Hans Mulder,
and ideas from Tim Bunce.</PRE>
<PRE>
lusol@Lehigh.EDU, LUCC, 96/12/25
. Version 0.96, release two inner references detected by Perl 5.004.</PRE>
<PRE>
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.</PRE>
<PRE>
Stephen.O.Lidie@Lehigh.EDU, Lehigh University Computing Center, 97/10/03
. Version 0.98, implement -shadow option for arrays and hashes.</PRE>
<PRE>
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().</PRE>
<PRE>
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).</PRE>
<P>
<HR>
<H1><A NAME="copyright">COPYRIGHT</A></H1>
<P>Copyright (C) 1996 - 1999 Stephen O. Lidie. All rights reserved.</P>
<P>This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.</P>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> Tie::Watch - place watchpoints on Perl variables.</P></STRONG>
</TD></TR>
</TABLE>
</BODY>
</HTML>