Good day to the respected habrasoobschestvu! There is little information here about the very pleasant and easy to use TCL programming language, and especially useful for those who are just starting to get acquainted with it. I will try to make a contribution to correcting such an annoying misunderstanding.
Preamble
I had a task at the institute: as a laboratory work, write a small application that operates with a certain amount of structured data. The application should run under Windows, without requiring installation of any interpreters, databases, frameworks and any additional software. The data structure is such that it is convenient to store them in a relational database. With a more free choice of PL and no requirement for an easy launch on any computer, I would by habit write everything in PHP + MySQL, but here I had to look for other ways.
Having asked his comrades who solved this problem, he found that they write mostly in Delphi / Visual Basic using csv-like files as storage. I did not want to use bulky systems and refuse SQL. And the insight came: I use TCL almost every day at work! After a brief search for information, it was found that it is perfect for solving our problem.
Turn TCL code into windows executable
TCL is an interpreted language. But there is a tool that allows you to create an executable file from the tcl-code:
TkWrap . It packs the interpreter and our source code into one file, thus providing us with the necessary portability we need.
Working with him is very simple:
- Download and unpack TkWrap
- In the resulting archive there are three files, we are interested in fullwrap.exe, because only it supports working with SQLite. Run it:
fullwrap.exe hello.tcl -o hello.exe
hello.tcl is the source file prepared in advance, hello.exe is the name of the executable file that we will get at the output. - Run the created hello.exe and admire the results of the work done.
Of course, to have something to admire, it is necessary to write something first. Now about the TCL code.
We work with SQLite from TCL
SQLite is a lightweight embedded relational database (quoted from Wikipedia). This means that we can easily use all the charms of SQL without having to run any database servers. Exactly what is needed.
A detailed description of working with SQLite from TCL can be found
on the official SQLite website . I will give a free translation of the main points:
The
sqlite3 dbcmd database-name
command connects us to the database-name database (if it does not exist, it will be created automatically). dbcmd is the connection identifier we made up. After executing this command, the dbcmd command is available to us with various methods (there are a total of 22). For example, consider only the most popular, eval:
dbcmd eval sql ?array-name ? ?script?
The command executes sql-code
sql and, if the last parameters are omitted, returns the result of the execution. You can use something like this to go through the lines (when sampling):
db1 eval {SELECT * FROM t1 ORDER BY a} values {
parray values
puts ""
}
For each of the rows in table t1 (of course, it must exist for this), an associative array of values will be created, containing the values of this string, and displayed.
')
We write our application
So, an example: a small Tcl / Tk application that allows you to execute an arbitrary sql query to ourdb database.
proc do_query {} {
upvar query_res query_res
set ret [query [.querytext get 0.0 end]]
set query_res [lindex $ret 1]
if {[lindex $ret 0] !=0} {
.res configure -fg red ;# ,
} {
.res configure -fg black
}
}
proc query {text} { ;# sql- , (0 ) :
upvar ourdb ourdb
set errcode [catch {ourdb eval $text} ret]
return [list $errcode $ret]
}
sqlite3 ourdb ourdb;#
wm title . " SQLite"
label .query -text " ourdb:" -compound center
text .querytext -width 100 -height 6
button .execute -text " " -command {do_query}
label .restitle -text ":" -compound center
label .res -textvariable query_res -wraplength 200
button .exit -text -command exit
. configure -padx 10 -pady 10 ;#
pack .query .querytext .execute .restitle .res .exit -expand yes ;#
Having saved this code and set TkWrapper described above on it, we’ll get our application:

Links
Read more about TCL here:
http://tmml.sourceforge.net/doc/tcl/About creating a graphical interface using Tk:
http://linux.yaroslavl.ru/docs/prog/tcltk/tk.html