The source command will load a file and execute it. This allows a program to be broken up into multiple files, with each file defining procedures and variables for a particular area of functionality. For instance, you might have a file called database.tcl that contains all the procedures for dealing with a database, or a file called gui.tcl that handles creating a graphical user interface with Tk. The main script can then simply include each file using the source command. More powerful techniques for program modularization are discussed with Building reusable libraries
This command can be used to:
sourcedata.tcl:
# Example data file to be sourced set scr [info script] proc testproc {} { global scr puts "testproc source file: $scr" } set abc 1 return set aaaa 1
sourcemain.tcl:
set filename "sourcedata.tcl" puts "Global variables visible before sourcing $filename:" puts "[lsort [info globals]]\n" if {[info procs testproc] eq ""} { puts "testproc does not exist. sourcing $filename" source $filename } puts "\nNow executing testproc" testproc puts "Global variables visible after sourcing $filename:" puts "[lsort [info globals]]\n"
(slightly edited to make the long list fit)
Global variables visible before sourcing sourcedata.tcl: argc argv argv0 auto_path env errorCode errorInfo filename tcl_interactive tcl_library tcl_patchLevel tcl_platform tcl_rcFileName tcl_version testproc does not exist. sourcing sourcedata.tcl Now executing testproc testproc source file: sourcedata.tcl Global variables visible after sourcing sourcedata.tcl: abc argc argv argv0 auto_path env errorCode errorInfo filename scr tcl_interactive tcl_library tcl_patchLevel tcl_platform tcl_rcFileName tcl_version