|
By Andrew J. Wozniewicz
Milwaukee, August 6, 2008
"HelloWorld" is the traditional first program in just about any programming
language, so we will continue the tradition here.
The idea of a HelloWorld program is to write a single line of text to the output device (console), as follows:
Hello world!
To output (or "write") a line of text to the console in WANTScript, you can use the
built-in System.IO.Console.WriteLn function. A possible HelloWord program
looks like this in WANTScript:
System.IO.Console.WriteLn("Hello world!")
That's all there is to it - a HelloWorld WANTScript project is a single line
of code. You can save this one line into a text file hello.want and execute
it. WANT automatically creates a project named DEFAULT if you don't
explicitly specify a project name. The above single line of code is thus
exactly equivalent to the following:
project DEFAULT
System.IO.Console.WriteLn("Hello world!")
end
Now, if you want to output more than a single line of text, or if you are a
Pascal programmer (or both), you would probably object to writing the
fully-qualified name System.IO.Console.WriteLn every time you want to output
a line. Good news is that, instead of always using fully qualified names,
you could just import the entire System.IO.Console module, which - besides
WriteLn - contains some other useful functions and then use these functions
directly, without qualification. This is analogous to the uses-clause in
Pascal. You can import another module like this:
project HelloWorld
import System.IO.Console
WriteLn("Hello world!")
end
When you use the import directive, it makes all the publicly visible symbols
from the imported module available in the importing module. That way, you
can use the WriteLn function pretty much the same way you would use it in
Pascal, without any qualification, like the above example illustrates.
Now, even better news is that you don't need to bother importing
System.IO.Console explicitly at all, since it is automatically and
implicitly imported for you by the WANT Runtime. So, you could have written
the HelloWorld project simply as follows:
project HelloWorld
WriteLn("Hello world!")
end
That's not all, though. Since you already know that you can omit the project
header - if you are happy with the default project name of DEFAULT - the
entire HelloWorld program in WANTScript boils down to this single line of
code:
WriteLn("Hello world!")
Not bad, but it gets even better! In WANTScript, you can use a literal
value, like the "Hello world!" above, which is a string literal, in place of
a statement. It is automatically understood by the interpreter to mean a
call to the WriteLn procedure, with the literal as its argument. So, the
above line is exactly equivalent to
"Hello world!"
Now, even this can be simplified. WANTScript does not require that you
terminate your string literals. It will automatically terminate them for you
at the end-of-line. So, you can rewrite the above to
"Hello world!
Finally, since you can use either single or double quotes, you could equally
well have written
'Hello world!
which arguably holds the world's record for the shortest HelloWorld program
ever.
So now we've come full circle. The HelloWorld program in WANTScript could be
a very simple single line of code, if you wanted, but having seen these many
different variations on that theme should have given you a better feel for
some of WANTScript syntax.
-Andrew
|