'local' to trap undeclared variables 
Author Message
 'local' to trap undeclared variables

I'm fairly new to TCL so please excuse me if this is a non-question
or has been solved earlier.

If, as happens too often, a global variable is not declared before
use in a procedure, it is treated as a new local variable.  Many
languages, even Basic, allow you instead to trap these errors.

I have noted the suggestion to pass an array of globals, but that's
rather insecure since it allows any procedure to change any global.
So why not introduce 'local' as well as 'global'?  Then undeclared
variables could (optionally) be trapped by the interpreter.

--



Tue, 06 Jun 2000 03:00:00 GMT  
 'local' to trap undeclared variables

: I'm fairly new to TCL so please excuse me if this is a non-question
: or has been solved earlier.

: If, as happens too often, a global variable is not declared before
: use in a procedure, it is treated as a new local variable.  Many
: languages, even Basic, allow you instead to trap these errors.

: I have noted the suggestion to pass an array of globals, but that's
: rather insecure since it allows any procedure to change any global.
: So why not introduce 'local' as well as 'global'?  Then undeclared
: variables could (optionally) be trapped by the interpreter.
Yes. something like "use strict" in perl (probably package require Strict)

which would make tcl complain about each variable, which wasn't declared
either as global or as local (variable is also allowed) in current scope.

Preferable in compile-time.

note, that if you'll provide no-op local procedure in compat directory
of your distribution, your application would be compatible with previous
versions, which don't have this feature.

For pre 8.0 version this package could be implemented as little hack to
Tcl_GetVar /Tcl_SetVar which would check for existence of name in some
local variable, which should be created automatically upon entry in each
procedure.

package Strict would look like:

proc local args {
  # we cannot call local here to avoid infinite recursion
  set currentScopeVarList(var) 1
  foreach var $args {
    uplevel 1 set currentScopeVarList($var) 1
  }

Quote:
}

rename global tcl_global

proc global args {
  uplevel 1 local $args
  uplevel 1 tcl_global $args

Quote:
}

set EnableVarListChecking 1

package provide Strict 1.0

Now, you have only to hack Tcl_(Set|Get)Var2?
definition to make them check for particular value in local currentScopeVarList
array and implementation routine of Tcl procedures to create this array,
filled with args of procedure.

If course, all-C implementation of local and C variable as flag would be
more effective.

: --

--
--------------------------------------------------

Institute for Commerce          Office:7-(095)-333-2022
Engineering                     Home: 7-(095)-135-46-61
Programmer                      http://www.fe.msk.ru/~vitus



Tue, 06 Jun 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. FEATURE REQUEST: 'my' local variables

2. FEATURE REQUEST: 'my' local variables

3. local variables don't cause side effects

4. Global, local variables in SubVI's

5. Q: `loop' and local variables

6. un-'upvar'ing/un-'global'ing variables

7. Change request: return value from 'trap'

8. What is an 'IOT Trap'?

9. local variable and local variable in block behave differently

10. Icons missing in 'Local' mode

11. ``vars'' declarations local to a procedure

12. 'variable subclass', OrderedCollectionNoDuplicates, SortedCollectionNoDuplicates

 

 
Powered by phpBB® Forum Software