Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
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?


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
% namespace import itcl::*
% source animal.itcl
% animal bill toucan 1.5
% animal ben dog 6.2
% ben getweight g
% bill getweight lbs

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.


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



# 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., 2022: 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