'''hello, world''' The canonical first program (according to Brian Kernighan and Dennis Ritchie, c reators of the C language) simply prints the message "hello, world". In Tcl, t his program is: puts "hello, world" To run the program, you have to put it in a file. Use your favorite text edito r to create a file named "hello.tcl" . (CL thinks there ought to be a graceful mention here of interactive interpretat ion, but has yet to figure out how to express it. The point is that there are even ways to use Tcl without a source file which holds a program). Then use the Tcl shell to interpret your program. Depending on your operating system, the paticular 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: 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 optio n - like on MacOS versions before 6-9] ]] Now let us examine your first program. All Tcl programs are made up of command s. 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 mig ht proceed to set greeting "hello" set addressee "world" puts "$greeting, $addressee" We have now introduced two variables, ''greeting'' and ''addressee''. A variabl e is created if you assign a value to it (the ''set'' commands). A variable's v alue is retrieved if you prefix its name with a dollar sign. The Tcl parser sub stitutes "hello" for "$greeting" and "world" for "$addressee" in the argument t o ''puts'' in the third line - even inside the quoted string that otherwise con tains a comma and a space. This flexibility in string handling distinguishes Tc l 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 th e variable name, and finishes that on any other character (the comma in that ca se). 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 y ou. At the moment, it's an overkill ;-) ---- If you are a Microsoft Windows user, running the simplest scripts is a little m ore complicated. Windows distinguishes between "console" applications, which r un 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. ''Another possibility on Windows: add the line'' catch {console show} ''to your script. This will open a console window which says "hello world", eve n if you just double-click the hello.tcl file. -- RS'' ---- '''hello, world''' with a GUI One of the great things about Tcl is that you also get to use Tk. The "hello w orld" program for Tk is almost as simple. We just have to define a command but ton which will perform the printing action, then use one of the geometry manage rs 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 res ult 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 Cl ass' hello sample with less than two dozen lines of Tcl/Tk code. (Including co mments 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 m enu accelerator, is implemented in something like 250 lines of Win32 C++ code a nd resource files. The advantage to Tcl/Tk commands is, of course, that it als o runs on UNIX (and maybe even on a Mac). ''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) --jcw'' ---- ''Running this script in wish on my RedHat 7.0 with tcl/tk8.3 says: "can't invo ke "message" command: application has been destroyed". What am I doing wrong? --dma'' That doesn't look like a tcl error message. To run this program enter the abov e text into a file hello.tcl and then invoke wish hello.tcl you should then see the gui displayed. ''Yes, it runs, including F1. -- DMA'' ---- [Beginning Tcl] - [Arts and Crafts of Tcl-Tk programming]