File: Trace.pm

package info (click to toggle)
perl-tk 1%3A800.024-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 18,528 kB
  • ctags: 19,152
  • sloc: ansic: 206,767; perl: 40,255; makefile: 4,370; sh: 2,290; yacc: 762
file content (276 lines) | stat: -rw-r--r-- 8,917 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package Tk::Trace;

use vars qw($VERSION);
$VERSION = '3.002'; # $Id: //depot/Tk8/Tk/Trace.pm#2 $

use Exporter;
use base qw/Exporter/;
@EXPORT = qw/traceVariable traceVdelete traceVinfo/;
use Tie::Watch;
use strict;

my %trace;                      # watchpoints indexed by stringified ref
my %op = (                      # map Tcl op to tie function
    'r' => ['-fetch',   \&fetch],
    'w' => ['-store',   \&store],
    'u' => ['-destroy', \&destroy],
);

sub fetch {

    # fetch() wraps the user's callback with necessary tie() bookkeeping
    # and invokes the callback with the proper arguments. It expects:
    #
    # $_[0] = Tie::Watch object
    # $_[1] = undef for a scalar, an index/key for an array/hash
    #
    # The user's callback is passed these arguments:
    #
    #   $_[0]        = undef for a scalar, index/key for array/hash
    #   $_[1]        = current value
    #   $_[2]        = operation (r, w, or u)
    #   $_[3 .. $#_] = optional user callback arguments
    #
    # The user callback returns the final value to assign the variable.

    my $self = shift;		             # Tie::Watch object
    my $val = $self->Fetch(@_);	             # get variable's current value
    my $aref = $self->Args(-fetch);          # argument reference
    my $sub = shift @$aref;	             # user's callback
    unshift @_, undef if scalar @_ == 0;     # undef "index" for a scalar
    my @args = @_;                           # save for post-callback work
    $args[1] = &$sub(@_, $val, 'r', @$aref); # invoke user callback
    shift @args unless defined $args[0];     # drop scalar "index"
    $self->Store(@args);                     # update variable's value

} # end fetch

sub store {

    # store() wraps the user's callback with necessary tie() bookkeeping
    # and invokes the callback with the proper arguments. It expects:
    #
    # $_[0] = Tie::Watch object
    # $_[1] = new value for a scalar, index/key for an array/hash
    # $_[2] = undef for a scalar, new value for an array/hash
    #
    # The user's callback is passed these arguments:
    #
    #   $_[0]        = undef for a scalar, index/key for array/hash
    #   $_[1]        = new value
    #   $_[2]        = operation (r, w, or u)
    #   $_[3 .. $#_] = optional user callback arguments
    #
    # The user callback returns the final value to assign the variable.

    my $self = shift;		             # Tie::Watch object
    $self->Store(@_);                        # store variable's new value
    my $aref = $self->Args(-store);          # argument reference
    my $sub = shift @$aref;                  # user's callback
    unshift @_, undef if scalar @_ == 1;     # undef "index" for a scalar
    my @args = @_;                           # save for post-callback work
    $args[1] = &$sub(@_, 'w', @$aref);       # invoke user callback
    shift @args unless defined $args[0];     # drop scalar "index"
    $self->Store(@args);                     # update variable's value

} # end store

sub destroy {
    my $self = shift;
    my $aref = $self->Args(-destroy);        # argument reference
    my $sub = shift @$aref;	             # user's callback
    my $val = $self->Fetch(@_);              # get final value
    &$sub(undef, $val, 'u', @$aref);         # invoke user callback
    $self->Destroy(@_);                      # destroy variable
}

sub traceVariable {
    my($parent, $vref, $op, $callback) = @_;
    die "Illegal parent." unless ref $parent;
    die "Illegal variable." unless ref $vref;
    die "Illegal trace operation '$op'." unless $op;
    die "Illegal trace operation '$op'." if $op =~ /[^rwu]/;
    die "Illegal callback." unless $callback;

    # Need to add our internal callback to user's callback arg list
    # so we can call it first, followed by the user's callback and
    # any user arguments.

    my($fetch, $store, $destroy);
    if (ref $callback eq 'CODE') {
        $fetch   = [\&fetch,   $callback];
        $store   = [\&store,   $callback];
        $destroy = [\&destroy, $callback];
    } else {                    # assume [] form
        $fetch   = [\&fetch,   @$callback];
        $store   = [\&store,   @$callback];
        $destroy = [\&destroy, @$callback];
    }

    my @wargs;
    push @wargs, (-fetch   => $fetch)   if $op =~ /r/;
    push @wargs, (-store   => $store)   if $op =~ /w/;
    push @wargs, (-destroy => $destroy) if $op =~ /w/;
    my $watch = Tie::Watch->new(
        -variable => $vref,
        @wargs,
    );

    $trace{$vref} = $watch;

} # end traceVariable

sub traceVdelete {
    my($parent, $vref, $op_not_honored, $callabck_not_honored) = @_;
    if (defined $trace{$vref}) {
        $trace{$vref}->Unwatch;
        delete $trace{$vref};
    }
}

sub traceVinfo {
    my($parent, $vref) = @_;
    return (defined $trace{$vref}) ? $trace{$vref}->Info : undef;
}

=head1 NAME

Tk::Trace - emulate Tcl/Tk B<trace> functions.

=head1 SYNOPSIS

 use Tk::Trace

 $mw->traceVariable(\$v, 'wru' => [\&update_meter, $scale]);
 %vinfo = $mw->traceVinfo(\$v);
 print "Trace info  :\n  ", join("\n  ", @{$vinfo{-legible}}), "\n";
 $mw->traceVdelete(\$v);

=head1 DESCRIPTION

This class module emulates the Tcl/Tk B<trace> family of commands by
binding subroutines of your devising to Perl variables using simple
B<Tie::Watch> features.

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.

User callbacks are passed these arguments:

 $_[0]        = undef for a scalar, index/key for array/hash
 $_[1]        = variable's current (read), new (write), final (undef) value
 $_[2]        = operation (r, w, or u)
 $_[3 .. $#_] = optional user callback arguments

As a Trace user, you have an important responsibility when writing your
callback, since you control the final value assigned to the variable.
A typical callback might look like:

 sub callback {
    my($index, $value, $op, @args) = @_;
    return if $op eq 'u';
    # .... code which uses $value ...
    return $value;     # variable's final value
 }

Note that the callback's return value becomes the variable's final value,
for either read or write traces.

For write operations, the variable is updated with its new value before
the callback is invoked.

Only one callback can be attached to a variable, but read, write and undef
operations can be traced simultaneously.

=head1 METHODS

=over 4

=item $mw->traceVariable(varRef, op => callback);

B<varRef> is a reference to the scalar, array or hash variable you
wish to trace.  B<op> is the trace operation, and can be any combination
of B<r> for read, B<w> for write, and B<u> for undef.  B<callback> is a
standard Perl/Tk callback, and is invoked, depending upon the value of
B<op>, whenever the variable is read, written, or destroyed.

=item %vinfo = $mw->traceVinfo(varRef);

Returns a hash detailing the internals of the Trace object, with these
keys:

 %vinfo = (
     -variable =>  varRef
     -debug    =>  '0'
     -shadow   =>  '1'
     -value    =>  'HELLO SCALAR'
     -destroy  =>  callback
     -fetch    =>  callback
     -store    =>  callback
     -legible  =>  above data formatted as a list of string, for printing
 );

For array and hash Trace objects, the B<-value> key is replaced with a
B<-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.

=item $mw->traceVdelete(\$v);

Stop tracing the variable.

=back

=head1 EXAMPLE

 use Tk;

 # Trace a Scale's variable and move a meter in unison.

 $pi = 3.1415926;
 $mw = MainWindow->new;
 $c = $mw->Canvas(qw/-width 200 -height 110 -bd 2 -relief sunken/)->grid;
 $c->createLine(qw/100 100 10 100 -tag meter/);
 $s = $mw->Scale(qw/-orient h -from 0 -to 100 -variable/ => \$v)->grid;
 $mw->Label(-text => 'Slide Me for 5 Seconds')->grid;

 $mw->traceVariable(\$v, 'w' => [\&update_meter, $s]);

 $mw->after(5000 => sub {
     print "Untrace time ...\n";
     %vinfo = $s->traceVinfo(\$v);
     print "Watch info  :\n  ", join("\n  ", @{$vinfo{-legible}}), "\n";
     $c->traceVdelete(\$v);
 });

 MainLoop;

 sub update_meter {
     my($index, $value, $op, @args) = @_;
     return if $op eq 'u';
     $min = $s->cget(-from);
     $max = $s->cget(-to);
     $pos = $value / abs($max - $min);
     $x = 100.0 - 90.0 * (cos( $pos * $pi ));
     $y = 100.0 - 90.0 * (sin( $pos * $pi ));
     $c->coords(qw/meter 100 100/, $x, $y);
     return $value;
 }

=head1 HISTORY

 Stephen.O.Lidie@Lehigh.EDU, Lehigh University Computing Center, 2000/08/01
 . Version 1.0, for Tk800.022.

=head1 COPYRIGHT

Copyright (C) 2000 - 2000 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.

=cut

1;