New User FAQ
Mini-Tutorial #3A more in-depth look at Tcl
In this "mini-tutorial", we're going to take a closer look at Tcl in general. With all of the commands available to the NeoWebScript web developer, it is possible to create very powerful applications both easily and quickly. As with anything, the more you know about how commands and procedures work, the more you'll be able to do. NeoWebScript is based on a scripting language called Tcl, developed around 1990, that is very robust and capable when it comes to handling data. We'll be able to tap into that power and ability, but only if we know some of the basic rules Tcl uses in handling data. That's what this tutorial is about.
Before we begin, let's take a little look at how Tcl works in general, and see how our scripts are handled. Tcl is an interpreted language, rather than a compiled one. This means that there is a program that reads in your Tcl script and evaluates all of the commands. The commands we use in our Tcl scripts are not magical in any way--they are just important to the Tcl interpreter running on the computer. The interpreter takes our scripts and turns them into code that the computer can understand and execute. This is where NeoWebScript comes in. We took the Apache webserver, which is the most popular webserver in the world, and embedded a Tcl interpreter in it. What does this mean to you? Now you can put Tcl code directly inside of your web pages and have the webserver software evaluate it, using the built-in Tcl interpreter. This is where the power of NeoWebScript lies.
Unlike some formal languages like "C" and "Java", Tcl only has one data type: the string. A data type describes a piece of information. For example, the term "integer" describes a whole number, while "decimal" describes a fractional number. The string data type describes a string of characters, in other words: letters, sentences, words and paragraphs. To repeat: data types describe a piece of information. They tell the computer what type of data it is. We use data types in our everyday lives whenever we ask a question. We check to see if the answer matches what we were expecting. Take the following converstation, for example:
Obviously, Hector's answers were not what Achilles was expecting. Achilles wanted an integer (whole number), but Hector gave him a string. In much the same way Tcl commands expect a certain type of data, and if you give them the wrong type, they will get confused, and complain to you. Here's an example of the type of message NeoWebScript could give you, if you don't provide the command with what it was looking for:
Let's take a tour through the different ways that Tcl handles data.
Only one data type: the String
In earlier tutorials, we learned about how to use variables, and how to store information in them. All of this happens with the set command. Let's take a look at a simple example:
NeoWebScript provides a lot of information about the current request. If you look back in tutorial 1, you'll see that everything from the time the file was last modified to the server the page resides on can be accessed and given to the user. This information is kept in a group of variables. This group of variables is called an array. An array is nothing more than a collection of variables that are all related. Arrays allow the NeoWebScript to organize and keep track of all the information concerning the current page. You too can organize and keep track of your information in the form of an array. Let's see how arrays are setup:
There is a special type of string in Tcl called a list. A list is a collection of "things", including text, strings, integers, etc. These things are joined together and act as one unit, which can be put into a variable, sent to a command, or passed around. Although the list acts as one unit, you can access each individual member. The list is used most often when you have a group of similar things. For example, I could create a list of people going to my birthday party, and still have access to each name. It their simplest form, lists are just strings with spaces separating each element:
To group together a list of items, you can use the list command. The list command takes any number of items and returns a list generated by them. In the above example, I used the "" (double-quotes) to signify that some of the elements had spaces in them. Let's see the list command in action:
Notice how elements that have spaces in them are grouped together using the curly braces. You can also create a list that contains sub-lists! Take a look:
What are some of the things you can do with a list? Here are some of the commands that act on lists, with descriptions of what they do listed as well:
In the above example, the list "the_colors" was unsorted to begin with. The lsort command sorted them into a more orderly list. Since we surrounded the command with the square brackets [...], the Tcl interpreter took the results of that command and replaced the command with the result. It was just like we typed in the the sorted list directly into the html command.
The lindex command pulls out a particular element from the list. Notice that although I specified the third element "3", I got the fourth element from the left. This is because Tcl starts counting from Zero. So in the list given above, Red is the "Zeroith" element of the list, Blue is the "First", Yellow is the "Second", and so forth. Many commands act like this in Tcl and other languages.
There are some other commands that handle lists, and some particulars that allow you to further manipulate data, but for most simple NeoWebScript work, these are enough. Knowing a little about what commands expect and how to get your information into the form they need is important, and with arrays and lists, you can do that. For example, the procedures that handle storing information into a database expect you to give them an array of data to store, while commands like random_pick_html expect a list of data.
For more information on the particulars of certain commands, take a look at the main NeoWebScript site, at http://www.neosoft.com/neowebscript
|Copyright © 1995-1999 NeoSoft Inc., 1770 St. James Place, Suite 500, Houston, TX 77056 USA. All Rights Reserved|