** Summary ** A step-by-step guide to getting started, for those [Learning Tcl] ** Hello, World! ** The canonical first program (according to Brian Kernighan and Dennis Ritchie, creators of the C language) simply prints the message "hello, world". In Tcl, this program is: ====== puts "hello, world" ====== There are two ways to experiment with Tcl. One can either take [An Interactive Approach to Experimenting with Tcl], or one can write their Tcl code into text files and ask a [Tcl] [interpreter] to execute them. The rest of this page is going to discuss the second method. To run the program, you have to put this one line in a file. Use your favorite text editor to create a file named "hello.tcl" . Then use the Tcl shell to interpret your program. Depending on your operating system, the particular version installation, etc. your Tcl shell could be named 'tclsh', 'tclsh8.3', or some other numbered variant. For this example, we will assume 'tclsh'. From a command line, type: ======none tclsh hello.tcl ====== And you should see the message: ====== hello, world ====== [[Someone should point to an additional page that describes for developers what to do to get to a command line and what to do if a command line isn't an option - like on MacOS versions before [[6-9]] ]] Now let us examine your first program. All Tcl programs are made up of commands. Each command may take some number of arguments. We are using the '[puts]' command, which will print a string. In our case, the string "hello, world" is in quotes, so it is a single argument to puts. If we want to factor out data, to make this program better maintainable, we might proceed to ====== set greeting "hello" set addressee "world" puts "$greeting, $addressee" ====== We have now introduced two variables, ''greeting'' and ''addressee''. A variable is created if you assign a value to it (the ''set'' commands). A variable's value is retrieved if you prefix its name with a dollar sign. The Tcl parser substitutes "hello" for "$greeting" and "world" for "$addressee" in the argument to ''puts'' in the third line - even inside the quoted string that otherwise contains a comma and a space. This flexibility in string handling distinguishes Tcl from most other languages. Notice the end of the variable names is not delimited. This is o.k. as the Tcl parser after a dollar sign picks up all letters, digits, and underscores for the variable name, and finishes that on any other character (the comma in that case). If you want fancier variable names (like "->" below), brace them: ====== set -> "world" puts "$greeting,${->}" ====== But then again, maybe better not... If you had more of such greetings and addressees, this style would be fit for you. At the moment, it's an overkill ;-) ---- If you are a Microsoft Windows user, running the simplest scripts is a little more complicated. Windows distinguishes between "console" applications, which run in a terminal window environment, and "windows" applications, which have a snazzy graphical user interface (GUI). The default Tcl/Tk installation on Windows runs all ''.tcl'' files in the wish shell, as a graphical application. Just put the file on the desktop or find it in the explorer and click on it. But since our simple script doesn't create any windows, and it isn't clear where the words "hello, world" would go, this program won't appear to do too much. You can run this program by starting tclsh from the ''start'' menu, then typing the command "source c:/hello.tcl". Note that the traditional Windows backslash must be replaced with a forward slash. [RS] Another possibility on Windows: add the line ====== catch {console show} ====== to your script. This will open a console window which says "hello world", even if you just double-click the hello.tcl file. ---- ** Hello, World -- the GUI Version ** One of the greatest things about Tcl is that you also get to use Tk. The "hello world" program for Tk is almost as simple. We just have to define a command button which will perform the printing action, then use one of the geometry managers to position it on the screen. The program looks like this: ====== button .b -text "Push Me" -command {tk_messageBox -message "hello, world"} pack .b ====== And you run it using the 'wish' shell. (Windows users can just double click on the file.) The first line of your program creates a pushbutton, named '.b' with the label "Push Me". Instead of our favorite 'puts' command, we create a Tk messageBox (a modal dialog) with our message. The second line instructs the pack geometry manager to display your pushbutton. The button command returns the ''widget'' name it received, .b in this case. So we can rewrite the above into a one-liner, where we call ''pack'' with the result of the ''button'' command: pack [[button .b -text "Push Me" -command {tk_messageBox -message "hello, world"}]] ** Hello, Windows! ** If you're feeling really ambitious, you can emulate the Microsoft Foundation Class' hello sample with less than two dozen lines of Tcl/Tk code. (Including comments and white space!) ====== # Create the main message window message .m -text "Hello Tcl!" -background white pack .m -expand true -fill both -ipadx 100 -ipady 40 # Create the main menu bar with a Help-About entry menu .menubar menu .menubar.help -tearoff 0 .menubar add cascade -label "Help" -menu .menubar.help -underline 0 .menubar.help add command -label "About Hello ..." \ -accelerator "F1" -underline 0 \ -command showAbout # Define a procedure - an action for Help-About proc showAbout {} { tk_messageBox -message "Tcl/Tk\nHello Windows\nVersion 1.0" \ -title "About Hello" } # Configure the main window wm title . "Hello Foundation Application" . configure -menu .menubar -width 200 -height 150 bind . "" {showAbout} ====== Note that this industrial strength GUI sample, including a popup dialog and a menu accelerator, is implemented in something like 250 lines of Win32 C++ code and resource files. The advantage to Tcl/Tk commands is, of course, that it also runs on UNIX (and maybe even on a Mac). [jcw]: Yes, the Mac version works (but not F1, and the message box icon I see in the about box is weird - I'm using my own build of TclKit on Mac rlb: Using Mac OS X and wish, F1 works in combination with fn key for me. [pps]: This is obvously on a laptop(iBook). It works with F1 on my Mac G4 B&W. What Wish seems not to accept on MacOSX is a paste from a clipboard with CopyPaste installed (it just writes v after cmd-v). I have to type the commands (or quit CopyPaste). ---- DMA: Running this script in wish on my RedHat 7.0 with tcl/tk8.3 says: "can't invoke "message" command: application has been destroyed". What am I doing wrong? ---- That doesn't look like a tcl error message. To run this program enter the above text into a file named hello.tcl and then invoke ====== wish hello.tcl ====== you should then see the gui displayed. DMA: Yes, it runs, including F1 ---- [escargo] 2005-08-08: Note that if you want a script that runs like a program on [Windows] you should look at [DOS BAT magic] or you want it to run on [Linux] or [Unix] you should look at [exec magic]. [RS]: On Windows, installing [ActiveTcl] usually also associates the extension .tcl to running wish with it, so scripts can run by just double-clicking them. [escargo]: And using [ActiveTcl] is the ''suggested'' way.... ---- ** See also ** [The Hello World program as implemented in Tcl/Tk] <> Tutorial