2

Inside a Tcl proc, I need to refer to variables both in the context of this proc as well as the level above because the file that is getting sourced uses variables there. So I tried writing something like:

proc source_file {file} {
    uplevel {source $file }
}

The problem with that is inside the 'uplevel' command, I can refer to variables at the top level, but now the local $file variable is not defined. I was hoping I can fix that by exposing the proc variable as file_up in the uplevel context, but that's not working either:

proc source_file {file} {
    upvar "file_up" $file
    uplevel {source $file_up }
}

source_file test.tcl can't read "file_up": no such variable

What am I doing wrong?

Giacomo1968
  • 58,727
Chris
  • 37

1 Answers1

1

You can use a different quoting mechanism so that the local variable is substituted and the value is passed back up to the caller:

proc source_file {file} {
    uplevel [list source $file]
}

upvar is not appropriate here: you're not passing a variable name to the proc.

glenn jackman
  • 27,524