What in the world are you trying to do? If variable flag is constant then why not set it in file2? If flag is being manipulated and modified then file1 must execute!!!
It seems you may need to modularize file1. By that I mean, you need to create procedures that do very specific tasks in file1.
1. Issue source to read the file1 from file2.
2. Then call on specific procedure in file1 from file2 to get your flag. Code example might help.
file1.tcl: (modularized)
# global data
set flag 1
puts "This is global scope"
puts "Any code here will be executed via source command"
set flag 2
# Only p1 is maniuplating flag value
proc p1 {a1 a2} {
global flag
set flag [expr $flag + $a1 + $a2]
return $flag
}
proc p2 {} {
puts "This is p2"
}
proc p3 {msg} {
puts "msg: $msg"
}
puts "Global scope still!"
puts "source command from file2 will execute this code too!"
file2.tcl:
set argc [llength $argv]
if {$argc < 1} {
puts "Too few arguments"
exit
}
# Assuming flag is the first argument to this script
set flag [lindex $argv 0]
puts "flag = $flag"
# test code to read argv
for {set i 0} {$i<$argc} {incr i} {
puts "arg $i: [lindex $argv $i]"
}
# This is now using source command!
# Note: Only global scope is visible!
source file1.tcl
# calls ONLY procedure p1 w/ 2 arguments in file1!
p1 10 20
# calls ONLY procedure p2 w/ no arguments in file1!
p2
If file1.tcl is lots of work to modularize then you could use the code as I illustrated in previous post with conditions around the code, i.e,
file1.tcl:
if {$flag == 2} {
set output [exec tclsh /path/to/file2.tcl $flag]
puts stdout $output
# no need to continue execution of file1!
exit 0
} else {
# file1 execution continues
}
Hope this helps.
Feherke, how do you put your code into a window?

Thanks