Tcl7.6 User Commands Man Page -- scope (n)
Table of Contents


NAME

scope - capture the namespace context for a variable

SYNOPSIS

scope string

DESCRIPTION

Creates a scoped value for the specified string, which is usually a variable name. A scoped value is a list with three elements: the "@scope" keyword, a namespace context, and the value string. For example, the command

namespace foo {
scope "x"
}

produces the scoped value:

@scope ::foo x

Note that the scope command captures the current namespace context. If the string is itself a scoped value, then the current context is ignored and the string is returned directly.

Ordinary variable names refer to variables in the global namespace. A scoped value captures a variable name together with its namespace context in a way that allows it to be referenced properly later. It is needed, for example, to wrap up variable names when a Tk widget is used within a namespace:

namespace foo {
private variable mode 1

radiobutton .rb1 -text "Mode #1"
-variable [scope mode] -value 1 pack .rb1

radiobutton .rb2 -text "Mode #2"
-variable [scope mode] -value 2 pack .rb2 }

Radiobuttons .rb1 and .rb2 interact via the variable "mode" contained in the namespace "foo". The scope command guarantees this by creating the variable name "@scope ::foo mode".

Scoped variables can be used anywhere that an ordinary variable name is recognized. For example, the following statements are allowed:

set {@scope ::foo mode} 3
puts "value = ${@scope ::foo mode}"

Note that scoped variables by-pass the usual protection mechanisms; the name "@scope ::foo mode" can be used in any namespace context to access the "foo::mode" variable, even though it is private.

KEYWORDS

@scope, code, namespace, private, protected, public


Table of Contents