Im trying to modify a variable using upvar (in an upward stack), but the value of the variable is passed to the procedure and not the variable name.
I cannot change what is passed, since it is already implemented widely on the program.
Is there a way to modify the file name in some way ?
proc check_file_exists {name} {
upvar $name newName
check_exists $name #Do all checks that file is there
set newName $name_1
}
check_file_exists $name
puts $name
this code will print the old name of the file and not the new one.
What I think you should do is bite the bullet and change the calls. It's a fairly simple search-and-replace, after all. The code will be more sane than if you use any of the other solutions.
Or, you could add another parameter to the parameter list and use that to pass the name, making the first argument a dummy argument.
Or, if you're not using the return value, you could return the new value and assign it back:
Or, you could assign the new value to a global variable (e.g.
theValue) inside the procedure, and assign that back:Or, you could assign the name to a global variable (e.g.
theName) and access that inside the procedure: the procedure will be able to updatenamedirectly.(There are some variations on this f.i. using
upvar.)None of the alternatives are pretty, and all of them still require you to make a change at the call (except the last, if you only ever use one variable for this value). If you're adamant about not doing that, there's always Donal's
info framesolution, which only requires the procedure itself to be changed.Let me know if you want help with the procedure code for any of these alternatives.