| |||||||||||
| |||||||||||
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] 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, 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. | |||||||||||
PH: 0800 043 8225 or 01225 708225 • FAX: 0845 8382 405 or 01225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho | |||||||||||