:TITLE: Modularization - source ;# ;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl30.lsn,v 1.1 2004/11/04 16:01:14 davidw Exp $ ;# Copyright (c) 1995 Clif Flynt ;# 9300 Fleming Rd. ;# Dexter, MI 48130 ;# clif@cflynt.com ;# See file "NOTICE" for licensing terms. ;# ::CMD:: if {([info exists tcl_platform])} { switch $tcl_platform(platform) { unix { set Tutor(lsn.codeMod) { } } windows { set Tutor(lsn.codeMod) { {global env} {regsub -all {\\} $env(COMSPEC) {/} cspec} {regsub {cat} $line " $cspec /C type" line} {if {[glob -nocomplain C:/temp] != ""} { regsub "/tmp/" $line {C:\\\\temp\\\\} line } } {if {[glob -nocomplain C:/windows/temp] != ""} { regsub "/tmp/" $line {C:\\\\windows\\\\temp\\\\} line } } {if {[glob -nocomplain C:/winnt/temp] != ""} { regsub "/tmp/" $line {C:\\\\winnt\\\\temp\\\\} line } } } } mac { set Tutor(lsn.codeMod) { } } default { puts "I don't recognize the platform: $tcl_platform(platform)" puts "Can't set platform specific parameters" } } } :LESSON_TEXT_START_LEVEL 0: Like other modern languages, Tcl supports mechanisms for separating a program into multiple files. This function is implemented with the source command.
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 $HOME will substituted for the tilde, as is done in the file command. More details on this substitution is available on the TildeSubst man page.
The source command will load a file and execute it.
This command can be used to:
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 $HOME will substituted for the tilde, as is done in the file command. More details on this substitution is available on the TildeSubst man page.
The source command will load a file and execute it.
This command can be used to:
As you can see, there are many uses for the source command. The main use is that this command lets you separate a set of related procs into a single file. You can then source that file into different applications and have those procs available.
Now, you could get the same effect by pasting the code from a set of procs into each application you write that needs those procs. The reason you shouldn't do this is that the odds are good that eventually you will find a bug, or a nifty way to do something better, and if the code is copied in a dozen files, you'll have to modify all those files. If the code exists in one file that is sourced into the other files, then you'll just have to make the fix once.
Run the example, and examine the output and example code now.
The example code creates a file with a proc, a data definition, a return, and another data definition.
The first two lines create a unique temporary filename using the pid command, and then open the file for writing.
The next five lines are writing the contents of the file, and then closing the file. Closing the file forces it to be flushed. Without closing or flushing the file, the data might remain in an I/O buffer, and not be available when the script tries to source that file.
Next, the file is displayed using exec to cat the file to the standard output. Exec returns the output of the program it executes, which is then output by the puts command.
Just as a check, the visible variables are displayed next. After the temporary file has been sourced, the variables will be displayed again.
The call to info procs testproc will return an empty string,
since testproc hasn't been defined yet. The test will be a success,
and the temporary file will be sourced.
Once the temporary file has been sourced, testproc can be executed. It will display the name of the file it was sourced from as an example of using the info script command. If you delete the \ before the square bracket in the [info script] call, the call will be evaluated before being written, and it will display the name of the example script that is writing the temp file. (It will be something like /tmp/xmpl.PID.tcl)
Next, the visible variables are displayed again. Note that abc is in the list, but aaaa is not. This is because of the return command just before aaaa is defined.
Finally, the temporary file is deleted, to keep things neat and clean.
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 $HOME will substituted for the tilde, as is done in the file command. More details on this substitution is available on the TildeSubst man page.