Constant trap that won't add defines to the env array

Constant trap that won't add defines to the env array

Post by Andrew Fal » Sat, 08 Sep 2007 04:30:18


Hi,

The code for my "constant's" is below:

namespace eval tools {
# proc for making constants, constant.
proc _constant_trap { val name1 name2 ops } {
# read up on the trace protocol for the argument above
upvar $name1 var

if { $name2 == "" } {
# we're not dealing with an array
set var $val
} else {
set var($name2) $val
}
}
}

proc ::define { varName { value 1 } } {
upvar $varName locVar
set locVar $value

uplevel [list trace add variable $varName "read write" \
[list ::tools::_constant_trap $value]]
}

With this code, I can create constants with no trouble unless that
constant is supposed to be part of the env array. Here's the tclsh
session I had after copy/pasting from the script the above code.

% define test
% puts $test
1
% set test 342334
1
% puts $test
1
% define ary(test1)
% puts $ary(test1)
1
% set ary(test1) 2345242
1
% puts $ary(test1)
1
% define ary(test2)
% puts $ary(test2)
1
% info exists env(test)
0
% define env(test)
% puts $env(test)
can't read "env(test)": no such variable

So what is preventing me from doing this? The define procedure
creates the array element first and then assigns the trace callback to
it. What is throwing me for a loop is that if I type in:

set env(test) 1
trace add variable env(test) "read
write" [list ::tools::_constant_trap 1]

from the TCL shell, it all works just fine. What is the issue with
this? I'm really quite lost since I thought, perhaps, the problem
might be that I'm trying to add an element to an array that already
exists. So, I just created a new array with set, and then added a new
element using my define procedure and it worked! So, why is this
being so selective with the env array?

Thanks,
Andy
 
 
 

Constant trap that won't add defines to the env array

Post by Alexandre » Sat, 08 Sep 2007 05:49:20


Currently, ::env is special in that it has internal traces *** to
its sleeves, just to maintain it in perfect sync with the environment
of whatever child process it may (or may not) spawn someday...
This is questionable, and questioned in

http://www.yqcomputer.com/ +array+ferrieux&rnum=1#be31b1884a83d1df

Feel free to chime in, and encourage Joe for his (future) TIP ;-)

-Alex

 
 
 

Constant trap that won't add defines to the env array

Post by Andrew Fal » Sun, 09 Sep 2007 00:19:00

On Sep 6, 2:49 pm, Alexandre Ferrieux < XXXX@XXXXX.COM >


Thanks. I was basically assuming that this was the case to some
respect. I hadn't yet found anything in the TCL manual pages that
lead me to believe that with this one exception taken from the manual
page for tclvars, "If the entire env array is unset then Tcl will stop
monitoring env accesses and will not update environment variables."
Since TCL monitors accesses, etc., I was thinking that env was in some
way "special" to interpreter.

Thanks again and I'll try to read through that thread you posted.

Andy