Overview
This document is provided as a quick guide to what programming is all about. Since UTL is effectively a light-weight programming language, you will need to learn a number of basic concepts in order to be productive with the language. This document is provided for individuals that have never programmed templates using a scripting language.
What is a Program?
A program is a set of instructions which tell a computer system what to do. A traditional computer program is one that provides a set of statements that are converted into 1's and 0's that are interpreted by the computer and then executed upon. This compiled form is often refered to as machine code. The conversion from human-readable statements to machine code is called compilation.
Scripting langauges like UTL work a little bit differently internally than traditional programs, but the basic principal is the same. UTL is considered an interpretted language because statements are executed as they are read in. Additionally, UTL statements appear intermingled with HTML code.
What is a Statement?
In the discussion about what programs are, several references to the word statement were made. A computer statement is simple a single set of instructions that perform some task in a program. For example, to add two numbers together in a UTL template you could provide the following statement:
[% print 2 + 2 %]
The UTL code above will output the sum of two plus two. The square brakets and percent signs are called delimiters. A delimiter can be is simply tells the computer when a set of instructions begins and when they end. Since code is intermingled with HTML, delimiters are used to tell the template system when to execute code and when to output the HTML.
Program Flow
Most computer languages are similar in terms of how they operate. The following are ways in which programs execute statements:
Sequences
A sequence of instructions will execute in the order they are provided. This is the standard behavior a program will perform unless you perform one of the other flow control operations listed.
Branches
A branch is a set of paths a computer program may take based on certain conditions in the program. For example, if you wanted a program to say "Good morning" or "Good night" your program would have to take one of two paths of execution based on the current time of day. Oftentimes, branches are conveyed as if ... then ... else statements in programming languages - this is also how UTL provides them.
Looping
A loop is a way to repeat a set of instructions. For example, if you have a list of 10 animals that you would like to output, one on each line, to the screen - you could make a program that is 10 lines long with an instruction to output each animal in the list - one at a time. In this example, the effort to output 10 animals in 10 lines may not seem like a big deal. Buf let's suppose you had more data than 10 animals - how about 5,000 animals? This is where looping is needed - a way to iterate through the list of animals and execute the same instruction, just with a different item for each iteration. In programming languages, this is conveyed multiple ways - traditionally, in the form of a for or while statement. UTL supports both types of looping.
Functions
A function is a set of instructions that has a name. You can execute the set of instruction by using the name in your program. In UTL, these are called macros. Functions are useful if you have a bunch of instructions that you would like to execute in multiple places. Obviously, you could cut and paste the code in each place, but what if you want to make a change? You will need to change every spot you have copied the code. This can get tedious very quickly, espeically if you are using the code in separate files. If you put the code in a function, you can reference in the other spots by calling it's name. If you need to make a change, you can change the function's code and all the spots that reference the name will update automatically!
What are Variables?
A variable is a place holder in the computer's memory for some input or output of data. For example, if you had a variable called "x" in a program, you could assign it a value as follows:
[% x = 1 %]
Here, the code above is performing an action called assignment. The variable "x" is assigned the value of the integer number "1". Later, in the program, you can reference the value of "x" by using it in another statement. For example,
[% print x + 1 %]
The code above will output the value "2". This is because the value of "x" is used during the execution of the program.