A beginner example to start with package on TCL : HelloWorld

Card Puncher Data Processing

tcl package

Creation of the package

To create a package with TCL, you need two files :

  • the package file which contain your code
  • the index package file to declare your package

The package file

A package use the notion of namespace to avoid name collision. This happens when two pieces of code try to define a procedure or variable with the same name. In Tcl when this occurs the old procedure or variable is simply overwritten. This is sometimes a useful feature, but more often it is the cause of bugs if the two definitions are not compatible.

Each namespace can contain :

  • local commands
  • local variables

When a command in a namespace is invoked it can see all the other commands and variables in its namespace, as well as those in the global namespace.

Items in a namespace can be accessed by creating a path to the item. This is done by joining the names of the items with ::. For instance, to access the variable bar in the namespace foo, you could use the path foo::bar.

The command “namespace export pattern pattern …” adds any procedure matching one of the patterns to the list of commands.

Example :

# Create the namespace
namespace eval ::HelloWorld {
    	
	# Export proc
    	namespace export Talk
	
	# Set up Variable
	set version 1.0
	set HelloWorldDescription "HelloWorld"

        # Variable for the path of the script
        variable home [file join [pwd] [file dirname [info script]]]

}

# Definition of the procedure talk
proc ::HelloWorld::Talk {} {
	puts $HelloWorld::HelloWorldDescription
}
# Definition of the procedure getpath
proc ::HelloWorld::GetPath {} {
	variable home
        return $home
}
package provide HelloWorld $HelloWorld::version
package require Tcl      8.0

The index package file

This file tells Tcl how to load your package. In essence the index file is simply a Tcl file which is loaded into the interpreter when Tcl searches for packages. It should use the package ifneeded command register a script which will load the package when it is required.

The index package must be :

  • named : pkgIndex.tcl
  • located in the directories define by the tcl_pkgPath or auto_path global variables list.
package ifneeded HelloWorld 1.0 [list source [file join $dir helloworld.tcl]]
package ifneeded OtherPackage 1.0 [list source [file join $dir otherpackage.tcl]]

Running the command package ifneeded is equivalent to load the file with the command source.

OMB+> package ifneeded Pk_ExternalTable 1.0
source C:/EclipseWorkspace/OMB/Package_ExternalTableProcedures.tcl

The main script

All the script above (helloworld.tcl and pkgIndex.tcl) are in the directory “C:/Dir/Dir With Space/test”.

Use the forward slash / character in the path of the directory (or two backslash \\)

# Declare the auto_path variable to allow tcl to find the files
lappend auto_path "C:/Dir/Dir With Space/test"

# Ask for the package
package require HelloWorld 1.0

# Call the procedure HelloWorld::Talk
puts [HelloWorld::Talk]

The result :

OMB*Plus: Release 10.2.0.3.33
Copyright (c) 2000, 2006, Oracle. All rights reserved.
OMB+> lappend auto_path "C:/Dir/Dir With Space/test"
resource:/tcl/lang/library {C:/Dir/Dir With Space/test}
OMB+> package require HelloWorld 1.0
1.0
OMB+> puts [HelloWorld::Talk]
HelloWorld

Package Management

You can manage the package (loads one or more binary or script files designed to extend the tcl functionality) with several command Package Command.

To reload a package, you can just load it with the command source

source C:/MyPackage.tcl

Support

If the version of package and/or the TCL version are not correct, this message is fired :

version conflict for package "HelloWorld": have 0.1, need 1.0
version conflict for package "Tcl": have 8.0, need 8.5

Reference





Discover More
Card Puncher Data Processing
OWB - OMB (Oracle Meta Base) Language

OMB is : the scripting language from . a set of java function embedded in the scripting langage tcl (with the help of tcljava). Thus, you must...
Card Puncher Data Processing
Tool Command Language (TCL)

Tcl (from “Tool Command Language”) is a scripting language created by John Ousterhout. intended to be embedded into applications ( Oracle Warehouse Builder use it as scripting language : Mixed with...



Share this page:
Follow us:
Task Runner