my_question
my_question

Reputation: 3235

How come a proc local variable interferes with global variable

I am using TCL 8.6.

Here is the code:

proc unpack_list {list1  list2} {
    set i 0
    foreach e $list2 {
        puts "globals=[info globals $e]"
        global $e
        set $e [lindex $list1 $i] 
        incr i
    }
}

set l1 [list 10 20 30 40]
set l2 [list a b c e]
unpack_list $l1 $l2
puts $a
puts $b
puts $c
puts $e
puts [info globals ]

Running the code fails:

globals=
globals=
globals=
globals=
variable "e" already exists
    while executing
"global $e"
    (procedure "unpack_list" line 5)
    invoked from within
"unpack_list $l1 $l2"
    (file "tmp/1.tcl" line 13)

The problem lies at this line:

set l2 [list a b c e]

If I change "e" to "ee", the script runs fine:

globals=
globals=
globals=
globals=
10
20
30
40
tcl_rcFileName tcl_version argv0 argv tcl_interactive a ee b c auto_path env tcl_pkgPath tcl_patchLevel l1 argc l2 tcl_library tcl_platform

My question is: The variable "e" inside the proc does not exist in global namespace, how come it interferes with the global variable "e"?

Upvotes: 0

Views: 1354

Answers (2)

Schelte Bron
Schelte Bron

Reputation: 4813

To accomplish what you seem to want, you can use the upvar command. Try this version of the unpack_list command:

proc unpack_list {list1 list2} {
    foreach v $list1 e $list2 {
        upvar #0 $e var
        set var $v
    }
}

This will never cause a clash between the global and local variable names.

Upvotes: 1

andy mango
andy mango

Reputation: 1551

If you read the description of the global command it states that the command creates a local variable that is linked to a global variable. The error message tells the story. The local variable already exists and global refuses to change its reference. To do otherwise would make local variables change values magically.

Upvotes: 1

Related Questions