ASPN ActiveState Programmer Network
  ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups | Web Services
SEARCH

Reference
ActiveTcl 8.5
ActiveTcl 8.5.4.0 User Guide
ActiveTcl 8.5.4.0 Documentation Index
Tutorial
Adding & Deleting members of a list
Adding new commands to Tcl - proc
Assigning values to variables
Associative Arrays
Building reusable libraries - packages and namespaces
Changing Working Directory - cd, pwd
Channel I/O: socket, fileevent, vwait
Child interpreters
Command line arguments and environment strings
Creating Commands - eval
Debugging & Errors - errorInfo errorCode catch error return
Evaluation & Substitutions 1: Grouping arguments with ""
Evaluation & Substitutions 2: Grouping arguments with {}
Evaluation & Substitutions 3: Grouping arguments with []
File Access 101
Information about Files - file, glob
Information about procs - info
Introduction
Invoking Subprocesses from Tcl - exec, open
Learning the existence of commands and variables ? - info
Leftovers - time, unset
Looping 101 - While loop
Looping 102 - For and incr
Modifying Strings - tolower, toupper, trim, format
Modularization - source
More channel I/O - fblocked & fconfigure
More command construction - format, list
More Debugging - trace
More Examples Of Regular Expressions
More list commands - lsearch, lsort, lrange
More On Arrays - Iterating and use in procedures
More Quoting Hell - Regular Expressions 102
Numeric Comparisons 101 - if
Regular Expressions 101
Results of a command - Math 101
Simple pattern matching - "globbing"
Simple Text Output
State of the interpreter - info
String comparisons - compare match first last wordend
String Subcommands - length index range
Substitution without evaluation - format, subst
Tcl Data Structures 101 - The list
Textual Comparison - switch
Time and Date - clock
Variable scope - global and upvar
Variations in proc arguments and return values

MyASPN >> Reference >> ActiveTcl 8.5 >> ActiveTcl 8.5.4.0 User Guide >> ActiveTcl 8.5.4.0 Documentation Index >> Tutorial
ActiveTcl 8.5 documentation

Modularization - source

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 in the next lesson on packages.

This command can be used to:

  • separate a program into multiple files.
  • make a library file that contains all the procs for a particular set of functions.
  • configure programs.
  • load data files.
source fileName
Reads the script in fileName and executes it. If the script executes successfully, source returns the value of the last statement in the script.
If there is an error in the script, source will return that error.
If there is a return (other than within a proc definition) then source will return immediately, without executing the remainder of the script.
If fileName starts with a tilde (~) then $env(HOME) will substituted for the tilde, as is done in the file command.

Example

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"

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState 2004 All rights reserved