Language Introduction |
In introducing a new programming language, often the first example given is a program that prints the message
Hello World!
and exits. What would a BRL program look like that performs this function? Actually, you just saw it. In BRL, the words ``Hello World!'' are all that is necessary. You may not see this as revolutionary at first. However, writing a WWW application involves writing a lot of HTML. The great thing about BRL is that you can look at your source code using your favorite WWW browser and get a good idea of what the output is going to look like. This makes BRL a more suitable language than PL/SQL.
Suppose you had a WWW form that let users enter their names and e-mail addresses, and you wanted to present this information to the user to check accuracy before continuing. This requires dynamically generating a web page with that info. Suppose you wanted this format:
What would BRL code look like that performs this function? Actually, you just saw it. In BRL, an square braces mark portions of the web page that are dynamically generated. HTML form inputs and CGI environment variables are represented by symbols corresponding to their names.
Continuing with the previous example, suppose you had problems with people typing e-mail addresses with angle brackets, e.g. <santa@northpole.org>. Because angle brackets have special meaning in HTML, the e-mail address does not show up. The angle brackets need to be encoded as &lt; and &gt;. Here is how you would do it:
The parentheses in [(html address)] show that you're doing
something more sophisticated than simply inserting the value of an input
or CGI environment variable. Instead, you're passing this value to a
procedure called html
that encodes the value for display in
an HTML page.
In BRL, a table is a sequence of rows. A
row is a sequence of items, each of which has a label. This
definition bridges HTML tables and SQL tables. A procedure called
sql
returns a table from a SQL SELECT statement. (For
non-SELECT statements it returns the number of rows processed. More
about that later.) The result of the sql
procedure can
be passed to the html
procedure to produce an HTML table.
For example:
[(html (sql "select user, sysdate from dual"))]
might yield
USER | SYSDATE |
---|---|
BRLEWIS | 08-MAY-1997 16:33 |
Note that the most complicated part of this example is the SQL expression. You will find that in the development of WWW/database applications, the most difficult things to design will be the HTML (epecially forms) and the SQL. Writing the BRL portion of your application is relatively simple.
A more sophisticated way to deal with a table is to use the
html-repeat
procedure. It reads an HTML start tag, then
reads everything up to an end tag. It displays the start tag, then
interprets the BRL code in the middle repeatedly, once for each row.
Then it displays the end tag. For example, to repeat elements of an
HTML unordered list, <UL>, using the rows of a table with columns
labeled a, b and c, the following works:
Simple, isn't it? The values of a, b and c go where you see [a], [b] and [c]. We could have used [(html a)] instead of a. We could have done more sophisticated HTML, such as hyperlinks with HREF's that depend on values in the table. There's no limit to what you can do.
More complicated constructs such as conditionals are easy in BRL. For example, suppose you wanted to output SANTA if the name given was Santa Claus, otherwise just print the name.
[(if (equal? name "Santa Claus") "SANTA" name)]
The source of this programming power is the language Scheme. Scheme is a mature programming language that is used at hundreds of colleges, universities, and secondary schools. For example, hundreds of MIT students learn Scheme each semester in 6.001. Scheme has the advantage of powerful semantics in simple syntax. Thus Scheme is especially good at expressing complicated relationships in a few lines of code. What might take five pages of code in BASIC, Pascal, C, or PL/SQL can usually be done in one page of Scheme.
In practice, you are unlikely to use most of Scheme's advanced features, but they're there in case you need them.
Using BRL is like parasailing. HTML and other aspects of the WWW make up the highly-visible parachute. Scheme is the powerful motorboat that moves the parachute around, and BRL is the rope that holds the system together.