1/09/2022

Source and Package in TCL


Source Command in TCL

  • source "fileName" : Reads the TCL Script contained in "fileName" and executes it. 
  • This allows a single Bigger TCL Script broken into multiple smaller TCL Scripts. Each part corresponding to a specific functionality.
  • This allows the code reuse with localisation & limitation.
  • You cannot use the reusable code Treated As LIBRARY.

File Structure : Source Example

We will understand the usage of the command source through code-example. The entire code is split into two parts.  The first one (sourceEx1.tcl) contains the code to be sourced . The second part (sourceEx2.tcl) contains the code where the "sourceEx1.tcl" has to be used.  The below info-graphics explains the split of the codes :


The "sourceEx1.tcl" is a simple code that defines three mathematical procedures as  "mul" , "sub" and "div" for multiplication , subtraction and division respectively :

The "sourceEx2.tcl" is a simple code that first sources "sourceEx1.tcl" and then uses the theree mathematical procedures as shown below :

The example is simple enough to understand and gives the below output upon execution :



You may extend the concept in VLSI with proper procedures and APIs that can interact with a live design . Hence the actual utility will come into the picture .


Package in TCL :

  • Packages are used for creating reusable TCL Code Library.
  • Namespace is used to encapsulate proc & variables for the defined package.
  • The package can be a collection of Tcl scripts, binary library, or a combination of both.

File Structure :: Package Example

We will understand the usage of the command source through code-example. The entire code is split into three parts. 


TCL Code :: A Package

In the first split (package1.tcl) we have defined the TCL package to be used. 

The definition of the package is done using the command : package provide <package-name> <version number>

If we want to include any other package , we use : package require <package-name>  <version-number>

The rest of the code is similar to the code we have used above. The only difference is that we have encapsulated the three procedures inside a namespace with same name as the package

Packaging The Package: 

In the second spilt (package2.tcl) the necessary TCL codes have been written to build this package in to a library.  We use  pkg_mkIndex to  Build an index for automatic loading of packages as shown in the below code. Except for the last filed (i.e. the wildcard matching of the TCL file names ) , you may not have to touch the code at all at your end. In case you are using the same names of the file then the entire code remain untouched. 

Upon execution of this code gives the below output :


Auto Generated File :: pkgIndex.tcl

The file (pkgIndex.tcl) gets auto-generated during the build process. You don't have to touch or edit it at all !

Use of the Package :: Code Reuse

In the third split (package3.tcl) we have used the build library. Here we use 

lappend auto_path $thisDirectory

to append the present working directory to TCL search path "auto_path" . The we use "package require" syntax to use the already built library. Then we go on as usual to use the procedures written in the first split of the code. Here is the third split of code for your reference :

Upon execution of this code , we get the below output in the screen :


The entire article is well narrated in the below video :

Courtesy : www.pngegg.com