Tracing Perl/Tk Variables
This is something of an oddball topic for this Anatomy lesson, but it introduces background information we’ll use later. Plus, it lets us do some neat things.
The Perl way to trace (or set
watchpoints upon) a variable is by using the built-in
tie
function or the CPAN module Tie::Watch. Tcl
has three commands associated with variable tracing:
trace variable
,
trace vdelete
, and
trace vinfo
. We’ll
examine sample code that uses three similar Perl subroutines, then
briefly illustrate how our new Trace module is implemented.
First we need to define three new
commands, the analogs of the Tcl/Tk Trace commands. They are
traceVariable
(start tracing a variable),
traceVinfo
(show trace information), and
traceVdelete
(stop tracing a variable). Using
these commands, we can write a program that animates an analog dial
via a Scale widget (see Figure 15-5).
Figure 15-5. Animating a meter
The dial is actually a fat Canvas line item with an arrow on one end.
The Scale goes from 0 to 100, with the dial pointing straight up when
it reads 50. The Scale’s value is updated in the variable
$v
.
my $c = $mw->Canvas(qw/-width 200 -height 110 -bd 2 -relief sunken/)->grid; $c->createLine(qw/ 100 100 10 100 -tag meter -arrow last -width 5/); my $s = $mw->Scale(qw/-orient h -from 0 -to 100 -variable/ => \my $v)->grid; $mw->Label(-text => 'Slide Me for > 5 Seconds')->grid;
The ...
Get Mastering Perl/Tk now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.