-
Procedure
MCL may construct modules with procedure, namespace, and package. A procedure defines a new command with the combination of existed commands; namespace avoids the global naming confliction; and package loads an extension without concerning its location.
-
proc
The syntax of proc command is
The first argument 'name' of proc is the command name. 'argument' is a list of arguments of the command. An element of the 'argument' can be a string or a list pair. A string is the argument name without default value. A list pair consists of an arugment name and its default value. The 'body' specifies the command sequence that implements the function of a procedure. All the variables except the global variables in the body have a local scope. The return value of the last command will be the return value of the procedure except exist a return command.
proc distance {x, y, {a 0} {b 0}} {
set xa [expr $x-$a]
set yb [expr $y-$b]
return [expr sqrt($xa*$xa + $yb*$yb)]
}
to invoke the procedure
set d [distance $a1 $b1 $a2 $b2]
-
Namespace
Namespace specifies a new scope for global variables and procedures. It minimizes the naming conflict. Namespace is used to represent commands in structure. Following declaration specifies the namespace definition.
namespace declaration
namespace eval name {
variable var value ...
namespace export proc1 proc2 ...
}
procedure declaration
proc name::proc1 {args} {
variable var
commands ...
}
...
more procedure declaration
proc name::procn {args} {
commands ...
}
-
declaration
'namespace eval name' specifies the name of a namespace. In the namespace specification, keyword 'variable' declares the variable 'var' and its initial value 'value'. The 'namespace export' command declares the procedure names that will be available for invocation. Outside the 'namespace eval' declaration the proc specifies a procedure of the namespace. The procedure name consists a namespace prefix name linked by :: with a procedure name and its arguments. Local namespace variable must be declared by variable command.
It is possible to define a namespace with the full qualified name rather than relative qualified name. Global prefix :: must be added to the namespace name. We suggest namespace name and proc name starts with the capital letter, variable name starts in the lowercase letter. These naming convenition will make code readable.
-
usage
To invoke a namespace procedure, you can use 'name::proc args'.
Network::Protocol "TCP/IP"
::Network::Protocol "TCP/IP"
-
Package
Package
organizes
a
libray
of
programs.
Ladybug2000
uses
the
facility
of
Tcl
to
extend
its
functions
for
component
programming.
-
package
Package command provides a facility to group a set of commands. To setup a package each library must declare a 'package provide pkgname pkgver' in its file. The 'pkgname' is the package name. 'pkgver' is the version number of the package with the format 'major.minor'. Same major number expresses the interfaces of the packages are compatible. Different minor number may have different implementation. Usually a package should keep backward compatibility. That is the package with bigger major number will work for the package with smaller major number. A package may be distributed on several files by specifing the identical 'package provide' command.
# in the library file
package provide media 1.0
To use a package a 'package require' command must be declared in a program. The syntax is 'package require pkgname [pkgver]'. Without the pkgver argument the hightest version of the package is loaded. If there is no suitable version of package available, 'package require' command will raise an error .
# in the application file
package require media 1.0
How to create a package? At present we have no package manager program. You need to do manually package installation.
- You need to create a package file with namespace or procedures and add 'package provide' command in the file.
- You may put the package file to a subdirectory. Then you need add a command 'lappend ::auto_path subdirectory' in the beginning of your code. With this command, the package will automatically search the files in the auto_path and its subdirectories.
- In addition, you must execute a command 'pkg_makIndex sudirectory name1.tcl name2.dll' to generate a pkgIndex.tcl file. You may modify the pkgIndex.tcl file to add or delete a command.
<< Types
Media >>