Tcl Source Code

View Ticket
Login
Ticket UUID: 57e44de76e71ba13b896571404da2451fcc6c4f1
Title: Array Read Trace fires only if Array has been written to
Type: Bug Created on: 2015-07-05 20:07:16
Submitter: venkat Assigned to: nobody
Subsystem: 07. Variables Severity: Important
Priority: 5 Medium Last modified: 2015-07-06 17:05:02
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 8.6.4 296892a8fb4c0
Description:
=== start of a.tcl ===

proc ::stracer {junk var op} {
    if {$var == ""} return
    switch $op {
        r {
            puts "Read Fired"
            set ::a($var) 5
        }
        w {
            puts "Write Fired"
        }
    }
}

trace variable ::a rw ::stracer

# set a(test2) 5
puts "$a(test1)"

=== end of a.tcl ===

Running:

% tclsh8.6 a.tcl
can't read "a(test1)": no such variable
    while executing
"puts "$a(test1)""
    (file "a.tcl" line 17)


Now remove the commented line (Remove # in "# set a(test2) 5)

% tclsh8.6 a.tcl
Write Fired
Read Fired
5

----

Why does the write to the array change the behaviour of the read trace?
User Comments:
venkat added on 2015-07-05 23:33:15:
Instead of the write : "set a(test2) 5",  even a "array set a [list]" makes it work.     This might be a documentation issue rather than a code issue.

ferrieux added on 2015-07-06 17:05:02:
Yes: most likely the var must gain "array status" before the trace is set.