0

TCL has some built-in command.

For example $env(some_env_var), I'll take some environment variable.

I use $env(some_env_var) in proc

proc pc {} {
  return $env(NUMBER_OF_PROCESSORS)
}

puts [pc]

then I get error:

$ tclsh ./t.tcl
can't read "env(NUMBER_OF_PROCESSORS)": no such variable
    while executing
"return $env(NUMBER_OF_PROCESSORS)"
    (procedure "pc" line 2)
    invoked from within
"pc"
    invoked from within
"puts [pc]"
    (file "./t.tcl" line 5)

$env seems invalid in scope of proc,

How should I modify $env to make it valid in the scope of proc?

mrcalvin
  • 3,291
  • 12
  • 18
curlywei
  • 682
  • 10
  • 18

1 Answers1

2

You will have to bring the env array into scope in your pc proc:

proc pc {} {
  global env
  return $env(NUMBER_OF_PROCESSORS)
}

Alternatively, use the full namespace path to env:

proc pc {} {
  return $::env(NUMBER_OF_PROCESSORS)
}
Schelte Bron
  • 4,113
  • 1
  • 7
  • 13