【转】Tcl/Tk 漫谈


〖摘要〗

Tcl/Tk   是一种简明,高效,可移植性好的编程语言。在信息产业领域具有广泛的应用。本文介绍其成长历史,特点,优势及应用范围。
 

〖关键词〗

Tcl/Tk,   组件,可移植性

Tcl/Tk   的发明人   John   Ousterhout   教授在八十年代初,是伯克利大学的教授。在其教学过程中,他发现在集成电路   CAD   设计中,很多时间是花在编程建立测试环境上。并且,环境一旦发生了变化,就要重新修改代码以适应。这种费力而又低效的方法,迫使   Ousterhout   教授力图寻找一种新的编程语言,它即要有好的代码可重用性,又要简单易学,这样就促成了   Tcl   (Tool   Command   Language)   语言的产生。

Tcl   最初的构想的是希望把编程按照基于组件的方法   (component   approach),即与其为单个的应用程序编写成百上千行的程序代码,不如寻找一个种方法将程序分割成一个个小的,   具备一定“完整”功能的,可重复使用的组件。这些小的组件小到可以基本满足一些独立的应用程序的需求,其它部分可由这些小的组件功能基础上生成。不同的组件有不同的功能,用于不同的目的。并可为其它的应用程序所利用。当然,   这种语言还要有良好的扩展性,   以便用户为其增添新的功能模块。最后,需要用一种强的,灵活的“胶水”把这些组件“粘”合在一起,   使各个组件之间可互相“通信”,协同工作。程序设计有如拼图游戏一样,这种设计思想与后来的   Java   不谋而合。终于在   1988   年的春天,   这种强大灵活的胶水   -   Tcl   语言被发明出来了。

按照   Ousterhout   教授的定义,   Tcl   是一种可嵌入的命令脚本化语言   (Command   Script   Language)。“可嵌入”是指把很多应用有效,无缝地集成在一起。“命令”是指每一条   Tcl   语句都可以理解成命令加参数的形式:

  命令   [参数   1]   [参数   2]   [参数   3]   [参数   4]   ......   [参数   N]

脚本化是指   Tcl   为特殊的,特定的任务所设计。但从现在角度看,可以说   Tcl   是一种集   C   语言灵活强大的功能与   BASIC   语言易学高效的风格于一身的通用程序设计语言。

Tk   (Tool   Kit)   是基于   Tcl   的图形程序开发工具箱,   是   Tcl   的重要扩展部分。Tk   隐含许多   C/C++   程序员需要了解的程序设计细节,   可快速地开发基于图形界面   Windows   的程序。据称,   用   Tcl/Tk   开发一个简单的   GUI   应用程序只需几个小时,   比用   C/C++   要提高效率十倍。需要指明的是这里所说的“窗口”是指   Tcl   定义的窗口,与   X-Windows   与   MS   Windows   的定义有所不同,但它可完美地运行在以上两个系统上。

下面看一下   Tcl   的总体结构图(注:图中的黑方块代表组件)

Tcl   语言的结构设计图

下面就   Tcl   语言的特点分别加以介绍。

一,“可信赖的”可移植性

Tcl   是一种高级程序设计语言。它将程序设计概念高度抽象,真正地把程序设计与操作系统底层结构隔开,因此不依赖于任何平台,具有良好的可移植性。

相反,虽然   C   语言也是一种可移植性好的语言,但由于其设计时,保留对系统底层的操作,程序就有了“依赖性”,再加上其“开放”的策略,各个厂家“各自为政”,争先制定“标准”,结果造成版本众多,互不兼容。而   Tcl   却非常的“单纯”,“标准”也只有一个。是否100%   的“纯”是程序设计语言可移植性的先决条件。

“可信赖”是我给加的。笔者曾有过这样的经验,将一个上万行的,在   SUN   上用   Tcl   开发的   GUI   程序移植到   NT   平台,   竟未改一行   Source   Code。相反,   笔者也曾将一个基于   Java   的几百行的一个搜索引擎程序移植到   NT   平台,   结果发现涉及文件系统,汉字处理的模块必须做出一个   NT   版本才能正确执行。

Tcl   是用   C   语言开发的。它现在可运行在   Unix,Windows   和   Macintosh   等各种平台上。

二,较高的执行效率

Tcl   常用的功能模块被编译生成   C   的库文件。因此,   Tcl   虽然是按解释方式执行的,   但多数执行代码调用的是编译成机器语言的   C   库文件,   因此其执行效率仍然很高。

笔者曾分别用   C++,Java   和   Tcl   编写了   grep   串匹配查找程序,对一个六万行,   长度为   15MB   的字典文件,用同一模板从文件首进行串查找,用时分别为   C++   (53s),Tcl   (69s),Java   (287s)。测试环境为:

硬件:SUN   SPARCWorkstaion   5
软件:SUN   C++   Compiler   4.2,JDK   1.15,Tcl/Tk   7.6

另外,由于   Tcl   是用解释方式执行的,其扩展和修改应用是在执行过程当中,   比   Microsoft   的   OLE   的静态的对象嵌入连接更具灵活性。例如,一个电子表格程序每次执行时可动态的更新表格中来自被数据库应用程序修改过的数据。
 
三,简单易学

Tcl   语言简单易学,与   C   语言的风格有相似的流程控制语句,支持过程化结构。但它也有其本身的风格特点。如隐含了数据类型,即没有了字符,   整数,   浮点,数组等的差别,全为统一的变量。变量间赋值仍有数据类型间的强制或缺省转换。取消了变量的定义,在程序体的任何部分,都可顺手“抓来”变量。

如果用户有   C   语言的基础,   注意一下   Tcl   的变量定义方法,花一天的时间,   即可基本掌握。没有   Windows   编程经验的的人(你完全可以没有   X-Windows   或   MS   Windows   的概念),   也可在几天之内,   参照一些范例程序编出跨平台的   Windows   窗口程序来。

正如每一种语言都以“Hello   World”做为其第一个程序,   让我们看看   Tcl/Tk   的“Hello   World”:

button   .b   -text   "Hello   World! "   -command   exit
pack   .b
 

程序的运行结果是:

这简简单单的两行命令,就已经实现了一个窗口程序所具有的全部功能,并且它还可以不加任何修改地运行在   X-Windows   和   MS   Windows   系统上。


四,与操作系统的集成

Tcl   还提供以   Shell   的方式与操作系统集成在一起。如同包裹在原来系统   Shell   外的又一层   Shell。这样,你即可运行所有的操作系统命令,同时又可运行   Tcl   的命令,从而克服了一般   Unix   Shell   script   的编程复杂,   可移植性差的缺点,以及在   MS   Windows   系统下没有脚本开发语言的缺憾,实现了“强强合作”。

下面就   Tcl   与经典的的程序设计语言   C++,及现在时髦的   Java   做一些比较。

C++,Tcl/Tk   和   Java   的性能比较表

    C++   Tcl/Tk   Java  
运行程序速度   快   与   C++   可比   慢    
调试难易程度
    复杂
每次修改完代码需重新编译   简单
修改完代码可直接运行   比较简单
修改完代码需重新编译成   ByteCode,   而且编译速度很慢  
程序代码复杂程度
以上面的   grep   为例   复杂
50   行程序代码   简明
10   行程序代码   比较简单
40   行程序代码  
系统资源占用情况   200MB   HD
32MB   Memory   3MB   HD
4MB   Memory   20MB   HD
4MB   Memory  
代码可维护性   好   一般   较好  
可移植性   较好   非常好   好  
学习难度   复杂,难掌握   简单易学   有一定难度,需要有面向对象程序设计的概念  

Ousterhout   教授被   SUN   于   1994   年召致麾下,加入   SUNLab,领导一个小组从事将   Tcl   移植到所有其它平台的工作,如   Windows   和   Macintosh。同时为   Tcl   增加了   Safe-Tcl   安全模块,   并为浏览器的开发了   Tcl   plug-in,以及支持   Java   bytecode   的编译器,大字符集,新的   IO   接口,与   Java   的计算平台相连等。Tcl   还有自己的浏览器。在面向对象程序设计占主导地位的今天,又开发了支持面向对象的   incr   Tcl。为了鼓励各厂商开发第三方的程序,Tcl   的源代码可免费下载。所有这些努力使   Tcl   成为一个适应当代信息产业潮流的,支持多平台的,优秀的的开发语言。

“小巧,易学,高效,跨平台执行”是   Tcl   语言特点的集中体现。实际上,Tcl   不仅仅在开发小的应用程序上有其快速,可维护强的优势,在大的应用系统方面,如操作系统及网络管理,测试系统,自控,仿真,可视化应用,计算机辅助设计等都有丰富的应用成果。在商业应用方面,如   SYBASE   公司花费上千万美金的数据库自适应测试系统,   Cygnus   公司著名的集软件工程管理,   源代码控制于一体的   Source   Navigator,以及   Cayenne   Soft   的协同工作组件   Object   Team   等等都是应用   Tcl/Tk   开发的。Tcl/Tk   还在美国的许多大学及实验室中得到了广泛的应用。

“script   once,   run   everywhere”是   Tcl   的口号,与   Java   的“Code   once,   run   everywhere”形成对照。也许是不甘   Tcl   寄篱于   Java   之下的缘故吧,   Ousterhout   教授于   1998   年初,离开了   SUN,自立   Scriptics   公司,继续   Tcl/Tk   的研究和开发工作。

最后演示一个产生国标码表的完整的   Tcl   程序,相信即使那些没有使用过   Tcl   语言的人,也感受一下其鲜明的风格特点吧!

#!/export/home/tcl/bin/tclsh

#   判断参数是否输入正确
if   {$argc   !=   1}   {

error   "Usage:   gen-gb   FileName "

}

#   打开写入文件
set   f   [open   [lindex   $argv   0]   w]

#   设置国标码区号
set   sect   0xa1

for   {set   i   1}   {$i   <=   94}   {incr   i}   {

#   设置国标码位号
set   bits   0xa1

#   写文件
puts   -nonewline   $f   [format   "%2d "   [expr   $sect-0xa0]]
puts   $f   "   区 "

for   {set   j   1}   {$j   <=   94}   {incr   j}   {

puts   -nonewline   $f   [format   "%c "   $sect]
puts   -nonewline   $f   [format   "%c "   $bits]

incr   bits

}

puts   $f   " "
puts   $f   " "

incr   sect

}

#   关闭文件
close   $f

相关信息网址:

http://sunscript.sun.com/  
http://www.scriptics.com/  
news://comp.lang.tcl/

参考资料:

John   K.   Ousterhout,   Tcl   and   the   Tk   toolkit
 
联系地址:
email:   [email protected]
homepage:   http://start.at/Linux-Online

“海纳百川有容乃大,壁立千仞无欲则刚。”-   林则徐

http://topic.csdn.net/t/20030210/13/1414011.html

如何编译tcl&tk

原文: http://www.tcl.tk/doc/howto/compile.html#unix

暂时没有时间翻译此文,贴至此处,供大家参考。


This page provides a "how to" guide for compiling Tcl from a source distribution. Tcl has been ported to a wide variety of platforms, and compilation has been made easier through GNU autoconf on UNIX.

Before You Start
Before trying to compile Tcl you should do the following things:

Try ActiveTcl. ActiveState provides easy-to-install binaries of Tcl for many platforms.
Make sure you have the most recent patch release (8.5.2). Look in the FTP directory from which you retrieved this distribution to see if it has been updated with patches. Patch releases fix bugs, so you should normally use the latest patch release for the version of Tcl that you want.
Source Code Organization
Each Tcl component has its source organized into the following structure, which is illustrated in the figure below:

doc Contains manual pages in a variety of formats.
generic Contains source code that is common for all platforms (.c and .h files)
library Contains a library of Tcl scripts used by the component.
macosx Contains Macintosh-specific files and XCode project files.
tests Contains a test suite.
tools Contains a collection of tools used when generating Tcl distributions. (Tcl only.)
unix Contains UNIX-specific source code and configure and Makefiles used for building on UNIX (including Mac OS X). You can create subdirectories of the unix directory if you want to build for multiple versions of UNIX.
win Contains Windows-specific source code and Makefiles used for compiling with VC++ or mingw (gcc).
 
Installed Software Organization
When you install Tcl, it ends up in a different organization that supports an installation for multiple operating systems and machine types. You can define architecture-specific subdirectories (e.g., solaris-sparc) that contain programs and binary object files for that platform. The directory structure also supports installation of various Tcl extensions so they can be automatically found by the Tcl shell programs. For example, any subdirectory of the install/lib directory is searched for Tcl script packages, and any shared libraries in the architecture-specific lib directory can be dynamically loaded into the Tcl shells.

After installation, Tcl files are kept in the following structure:

doc Contains manual pages in a variety of formats.
lib Contains subdirectories for each Tcl package that contain their Tcl script files.
lib/tcl8.5 The installed Tcl script library
linux-ix86 Contains compiled shells and libraries for Linux.
solaris-sparc Contains compiled shells and libraries for Solaris.
solaris-sparc/lib Contains runtime libraries (.so files) for Solaris.
solaris-sparc/bin Contains executable shells (tclsh8.5, wish8.5) for Solaris.
 
Configuring and Compiling on UNIX
Compiling Tcl on UNIX has two steps: configure and make. In the simplest case you type the following commands to your shell:

  cd tcl8.5.0/unix
  configure options
  make
  make test
  make installConfigure
The configure script tests your system for different compilation and linking options and generates a Makefile. The configure script is created by GNU autoconf. There are two commonly-specified configure options:

--prefix=directory
This flag specifies the installation directory. The default is /usr/local.
--exec-prefix=directory
This flag specifies the installation directory for architecture-specific (i.e., binary) files. The default is to install architecture-specific files into the location specified by --prefix. This is typically set to a subdirectory of the main prefix, (e.g., /usr/local/solaris-sparc) for sites that want to maintain binaries for multiple architectures. If you just have a single system type, then you can ignore this option.
Note: be sure to use only absolute path names (those starting with "/") in the --prefix and --exec-prefix options.

To install into the directories /home/user/tcl/bin and /home/user/tcl/lib, for example, you should configure with:

  configure --prefix=/home/user/tclTcl's configure supports several options in addition to the standard ones. Calling configure --help will list them all, but common options are:

--enable-threads
If this switch is set, Tcl will compile itself with multithreading support.
--enable-shared
If this switch is specified (the default), Tcl will compile itself as a shared library if it can figure out how to do that on this platform. Add =no will create a static build.
--enable-symbols
If this switch is specified, Tcl will build with debugging symbols.
If you wish to specify a particular compiler, set the CC environment variable before calling configure. You can also specify CFLAGS prior to configure and they will be used during compilation.

Configuring for multiple architectures
Suppose you are building for two platforms, Linux and Solaris, and your installation will be shared by both kinds of hosts. You'll want to use the --exec-prefix configure option to specify different binary installation directories.

There are two ways you can build for multiple platforms. The first way is to build both platforms in the unix subdirectory. First configure and build for one platform. After you install, type make distclean and then configure and build for the second platorm. Be sure that both the configure and build steps are run on the platform for which you are building. For example, don't configure under Solaris and then build under HP-UX.

You can also build the different platforms in different subdirectories of the unix directory. If you do this, then you must name the configure script with a full pathname. For example:

  cd /home/user/src/tcl8.5.0/unix
  mkdir solaris
  cd solaris
  /home/user/src/tcl8.5.0/unix/configure / 
 --prefix=/usr/local /
 --exec-prefix=/usr/local/solaris-sparcConfiguring Tk
When you configure Tk you may need to tell it where Tcl was built with the --with-tcl flag. This is true, for example, if you build Tcl in a subdirectory of unix as just described. To configure Tk:

  cd /home/user/src/tk8.5.0/unix
  mkdir solaris
  cd solaris
  /home/user/src/tk8.5.0/unix/configure /
 --with-tcl=/home/user/src/tcl8.5.0/unix/solaris /
 --prefix=/usr/local /
 --exec-prefix=/usr/local/solaris-sparcBuilding with Make
What is Make?

After you configure your Makefile, type "make". This will create a library archive called libtcl.a or libtcl.so and an interpreter application called tclsh that allows you to type Tcl commands interactively or execute script files. When you build Tk, you create libtk.a or libtk.so and the wish executable.

Type "make test" to run an exhaustive test suite. In most cases there should not be test failures. The Tk test suite, however, does have some tests that depend on font metrics and the window manger, which can vary across platforms. The README file in the tests subdirectory has documentation about the test suite.

Type "make install" to install Tcl binaries and script files in the directories you specified with --prefix or --exec-prefix during the configure step. You'll need write permission on the installation directories to do this.

If you have trouble compiling Tcl, check out the platforms list. This is an on-line database of porting information. We make no guarantees that this information is accurate, complete, or up-to-date, but you may find it useful. If you get Tcl running on a new configuration, we would be happy to receive new information to add to the database. We're also interested in hearing how to change the configuration setup so that Tcl compiles out of the box on more platforms.

Compiling on Windows
In order to compile Tcl for Windows, you need the following items:

Tcl 8.5.2 source distribution (or the latest patch release)
MS Visual C++ 6.x or greater.
In the win subdirectory of the source release, you will find makefile.vc. This is the makefile for the Visual C++ compiler. You should update the paths at the top of the file to reflect your system configuration. Now you can use make (or nmake for VC++) to build the tcl libraries and the tclsh executable.

In order to use the binaries generated by these makefiles, use

  make installto place the Tcl script library files someplace where Tcl can find them. Tcl looks in the following places for the library files:

The path specified in the environment variable TCL_LIBRARY.
Relative to the directory containing the current .exe. Tcl will look for a directory ../lib/tcl8.5 relative to the directory containing the currently running .exe.
Note that in order to run tclsh85.exe, you must ensure that tcl85.dll is on your path in the system directory, or in the directory containing tclsh85.exe.

Compiling Tk for windows follows a similar process. You must compile Tcl before you compile Tk.

If you are compiling a Tcl extension for windows, please see the TEA reference.

Compiling for Macintosh
Tcl 8.4 was the last version to support Mac Classic (OS <= 9). See the READMEs in the sources for Mac Classic build support. For Mac OS X, use the unix instructions above, with consideration for the following extra options:

--enable-framework
Builds the shared libraries as Mac OS X Frameworks.
--enable-aqua
Target the Aqua windowing system instead of X11 on Mac OS X.


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/carry1314lele/archive/2008/07/11/2639925.aspx

http://blog.csdn.net/carry1314lele/archive/2008/07/11/2639925.aspx

编译_编译TCL/TK


1. 下载[xia zai]tcl-8.5.2.x(假设解压后的路径[lu jing]为F:/tcl-8.5.2.x, 然后将文件[wen jian]夹tcl-8.5.2.x改名为tcl8.5.2, 方便后面编译[bian yi]tix.)

     SVN地址[di zhi]: http://svn.python.org/projects/external/tcl-8.5.2.x

2. 下载[xia zai]tk-8.5.2.x(假设解压后的路径[lu jing]为F:/tk-8.5.2.x, 然后将文件[wen jian]夹tk-8.5.2.x改名为tk8.5.2, 方便后面编译[bian yi]tix.)

     SVN地址[di zhi]: http://svn.python.org/projects/external/tk-8.5.2.x

3. 下载[xia zai]tix-8.4.3.x(假设解压后的路径[lu jing]为F:/tix-8.4.3.x, 为方便操作和统一风格改名为tix8.4.3)

     SVN地址[di zhi]: http://svn.python.org/projects/external/tix-8.4.3.x

4. 启动[qi dong]VS command prompt控制[kong zhi]台(开始/程序/Microsoft Visual Studio 2005/Visual Studio Tools/Visual Studio 2005 Command Prompt)

5.  编译[bian yi]tcl  (依次输入[shu ru]以下命令[ming ling])

     1.) cd F:/tcl8.5.2/win

     2.) nmake -f makefile.vc

     3.) nmake -f makefile.vc INSTALLDIR=../tcltk install

6. 编译[bian yi]tk  (依次输入[shu ru]以下命令[ming ling])

    1.) cd F:/tk8.5.2/win

    2.) nmake -f makefile.vc TCLDIR=../tcl8.5.2

    3.) nmake -f makefile.vc TCLDIR=../tcl8.5.2 INSTALLDIR=../tcltk install

7.编译[bian yi]tix (依次执行[zhi hang]以下操作)

    1.) cd F:/tix8.4.3/win

    2.) 将tix8.4.3/win/python.mak文件[wen jian]第14行!if  $(DEBUG) 修正为 !if "$(DEBUG)" == "1"

    3.) 
          1. 如果你使用的是自定义的Tcl/Tk文件[wen jian]夹名的话, 需要指定TCL_PATCH和TK_PATCH
         
              nmake -f python.mak TCL_PATCH=../tcl8.5.2 TK_PATCH=../tk8.5.2

          2. 如果是按照上面我说的做法, 只需改变tix8.4.3/win/makefile.vc第45行开始定义的版本号[ban ben hao].将8 4 7 改为 8 5 2.如下

               !IFNDEF TCL_MAJOR
               TCL_MAJOR = 8
               !ENDIF
               !IFNDEF TCL_MINOR
               TCL_MINOR = 5
               !ENDIF
               !IFNDEF TCL_PATCH
               TCL_PATCH = 2
               !ENDIF
  
    4.) 由于makefile.vc配置[pei zhi]的原因, 我们需要回头将tcl8.5.2/win和tk8.5.2/win 目录下生成的Release_VC8文件[wen jian]夹改名为Release_IX86_VC8, 当然你也可以修改[xiu gai]tix8.4.3/win/python.mak
          的第38行判断条件[tiao jian], 原文件[wen jian]代码如下:

         !if "$(MACHINE)" != "IX86"
         BUILDDIRTOP =$(BUILDDIRTOP)_$(MACHINE)
         !endif
         !if $(VCVER) > 6
         BUILDDIRTOP =$(BUILDDIRTOP)_VC$(VCVER)
         !endif

    5.)  nmake -f python.mak TCL_PATCH=../tcl8.5.2 TK_PATCH=../tk8.5.2 INSTALLDIR=../tcltk install 

         或者 nmake -f python.mak INSTALLDIR=../tcltk install (注: 必须将8 4 7 改为 8 5 2 才有效[you xiao])

posted

http://www.aowe.net/cyuyan/437.htm

 

你可能感兴趣的:(【转】Tcl/Tk 漫谈)