Well, the 'culprit' in your initial code is that you are using lappend l1 [list $a1]
. You should be getting:
l1={{set a 1}}
When you use puts "l1=$l1"
and what the above means is that you have a list within another list, which is itself in yet another list! (you lose the outermost braces when you print a list)
Indeed, when you do [list $a1]
, you are appending a list containing the list/element {set a 1}
(i.e. {{set a 1}}
) to the list $l1
while if you did lappend l1 $a1
, you would be adding the string/list {set a 1}
to the list $l1
.
I think what you actually meant to use is something a bit more like this:
set a1 [list set a 1]
# Where you get a list containing the 3 elements "set" "a" and "1"
set l1 {}
lappend l1 $a1
foreach e $l1 {puts $e; eval $e}
Maybe another way to look at it:
set a1 [list set a 1]
puts $a1
# => set a 1
set a1 {set a 1}
puts $a1
# => set a 1
set a [list $a1]
puts $a
# => {set a 1} -- A list within a list
set l {}
lappend l $a1
puts $l
# => {set a 1}
lappend l $a
puts $l
# => {set a 1} {{set a 1}}
# list list in list -- and $l is the list containing those two.