Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
This is now an archive web site.
Some is still relevant as at December 2025 but some is purely of historic interest.


Lisa and I (Graham) are now fully retired from IT training.We have made many friends over 30 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Apache, Linux and Solaris/SunOS too. Our training notes are out of date, but with upward compatability some examples remain operational and relevant. You are welcome to make use of them "as seen", at your own risk. We now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We remain active, enjoying the times that we are retired but still healthy enough in mind and body to do things!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Loading incr-tcl into tclsh

If you're going to be using the Object Oriented extension to Tcl (Itcl), you may we working with a Tcl or tcl-based executable file that has it built in, or you may wish to load it in as a separate package. Many of the examples that you'll see in our training notes (and elsewhere) are designed for use with executables with [incr-Tcl] built in. So how can you use [incr-Tcl] otherwise?

[INCR-TCL]

What if you don't have an "itcl" executable, or you want to use [incr-Tcl] in association with a specific build of wish, expect or expectk that doesn't include the extra facilities?

earth-wind-and-fire:~/apr05 grahamellis$ tclsh
% source animal.itcl
invalid command name "class"
%

These days, [incr-Tcl] is supplied as a package with most distributions of Tcl and you can package require it to cause it to be loaded as necessary into the application. You'll then want to import the names from the namespace. Thus:

earth-wind-and-fire:~/apr05 grahamellis$ tclsh
% package require Itcl
3.3
% namespace import itcl::*
% source animal.itcl
% animal bill toucan 1.5
bill
% animal ben dog 6.2
ben
% ben getweight g
6200.0
% bill getweight lbs
0.681818181818
%

As an alternative, you could encapsulate the requirement for [incr-Tcl] within the sourced file so that the user's none the wiser that Itcl is being loaded. That is probably a better approach where you have a wide range of scripts being distributed to many machines and run through a variety of different executables.

FILE ANIMAL2.ITCL:

package require Itcl

itcl::class animal {
  variable breed
  variable weight
 constructor {b w} {
             set breed $b
            set weight $w
           }
       method getbreed {} {
            return $breed
           }
       method getweight {{units kgs}} {
                set w $weight
           if {$units == "g"} {set w [expr $w * 1000.0]}
           if {$units == "lbs"} {set w [expr $w / 2.2]}
            return $w
               }
       }

THE APPLICATION THAT CALLS THE ABOVE CLASS

#!/usr/bin/tclsh

# Example that loads Itcl into tclsh

puts "Hello - this is an Incr Tcl Demo"

source animal2.itcl

animal charlie cat 3.0
animal muppet dog 5.8

puts -nonewline "Charlie's weight (in pounds): "
puts [charlie getweight lbs]

puts -nonewline "Muppet's weight (in pounds): "
puts [muppet getweight lbs]

In operation:

earth-wind-and-fire:~/apr05 grahamellis$ ./petal
Hello - this is an Incr Tcl Demo
Charlie's weight (in pounds): 1.36363636364
Muppet's weight (in pounds): 2.63636363636
earth-wind-and-fire:~/apr05 grahamellis$


See also [incr-Tcl] training

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

Tcl/Tk - [incr-Tcl]
  [290] - ()
  [656] - ()
  [1528] - ()
  [1819] - ()
  [1925] - ()
  [3142] - ()
  [4456] - ()
  [4460] - ()

resource index - TclTk
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2025: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/tcl-load ... tclsh.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard