ChatGPT and Tcl

ChatGPT AI knowns Tcl!

I took the program from char2ent and asked.

What does the following program do? (input program from char2ent)

Response from ChatGPT:

This program appears to be written in Tcl (Tool Command Language), a high-level interpreted language that is commonly used for building a wide range of applications, including networked applications, web applications, and graphical user interface (GUI) programs.

The program appears to be a command-line utility that converts special characters in a given input file to either HTML or XML entities, and writes the result to an output file. The program takes three arguments: a command-line option ("-h" or "-x"), the name of an input file, and the name of an output file.

The program begins by checking the arguments that were passed to it. If the only argument is "--help", the program prints a help message and exits. If the first argument is neither "-h" nor "-x", or if there are not exactly three arguments in total, the program prints an error message and exits.

The program then checks whether the input file exists and is readable, and whether the output file is different from the input file. If any of these checks fail, the program prints an error message and exits.

Finally, the program attempts to open the input file and the output file. If either of these operations fails, the program prints an error message and exits. If both files are successfully opened, the program reads the input file and converts special characters to either HTML or XML entities, depending on the command-line option that was passed. The resulting output is written to the output file, and both files are closed.

I then asked:

How to make a HTTP request in tcl language?

Response from ChatGPT:

To make a HTTP request in Tcl, you can use the http::geturl command from the http package. Here is an example of how to use this command to make a GET request to a URL:

package require http

set url ""
set token [http::geturl $url]

# Check the status of the request
if {[http::status $token] != "ok"} {
    puts "Error: [http::status $token]"

# Read the response body
set response [http::data $token]
puts $response

# Close the connection
http::cleanup $token

This example makes a GET request to the specified URL and prints the response body. Note that you may need to adjust the code depending on your specific requirements and the type of request you want to make.

Can you write me a tcl/tk program that generates a logon dialog. This dialog must take a user's name and their password, and also have ok and cancel buttons.

Response from ChatGPT:

Certainly! Here is a Tcl/Tk program that generates a logon dialog with a text entry field for the user's name, a text entry field for the password, and "OK" and "Cancel" buttons:

Copy code

package require Tk

# Define the logon dialog window
proc logon {} {
    global username password

    # Create the main window for the logon dialog
    toplevel .logon

    # Create a label and a text entry field for the username
    label .logon.username -text "Username:"
    entry .logon.username.entry -textvariable username

    # Create a label and a text entry field for the password
    label .logon.password -text "Password:"
    entry .logon.password.entry -show "*" -textvariable password

    # Create "OK" and "Cancel" buttons
    button .logon.ok -text "OK" -command {ok}
    button .logon.cancel -text "Cancel" -command {cancel}

    # Use the "grid" geometry manager to arrange the widgets in the window
    grid .logon.username .logon.username.entry
    grid .logon.password .logon.password.entry
    grid .logon.ok .logon.cancel -sticky ew

# Define the "ok" function, which is called when the "OK" button is clicked
proc ok {} {
    global username password

    # Print the entered username and password to the console
    puts "Username: $username"
    puts "Password: $password"  ; # <<== Here stopped.  Not finishing the string and going on to the next proc.

proc cancel {} {
    # Destroy the logon dialog window
    destroy .logon

# Call the "logon" function to display the logon dialog

# Enter the Tk event loop
pack .logon
focus .logon.username.entry
focus -force .logon.username.entry
tkwait window .logon

This program creates a logon dialog with a text entry field for the username and a text entry field for the password, as well as "OK" and "Cancel" buttons. When the "OK" button is clicked, the program prints the entered username and password to the console. When the "Cancel" button is clicked, the program closes the logon dialog. The program uses the Tk "grid" geometry manager to arrange the widgets in the window, and enters the Tk event loop to process user input and display the logon dialog on the screen.

You can now write Tcl code without known Tcl! I've tried other things too. It pretty good. Maybe I will put more of my observations here.

JMN 2022-12-25

A couple of weeks ago ChatGPT confidently told me:

The open_port built-in command is included in all distributions and versions of TCL, starting from the earliest versions of TCL. It is a fundamental part of the TCL language and is used to provide low-level access to external applications and programs.

I eventually got it to back down and apologize:

To confirm, the open_port command is not a built-in command of the Tcl language. It is not included in the Tcl language by default, and it is not documented in the Tcl reference manual.

As a language model trained by OpenAI, my knowledge is based on the information that is publicly available on the internet as of my training date, which was in 2021. I do not have the ability to browse the internet or access new information that is published after my training date. In this case, it appears that the information in my previous response was outdated or incorrect.

I apologize for any confusion or inconvenience that my previous response may have caused. If you have any further questions, please feel free to ask, and I will do my best to provide a helpful and accurate response.

I'm not sure why it went wrong - but it has a way to go before I'll consider it useful. It's ability to keep chat context is sometimes impressive, but it goofs up and will sometimes choose the wrong concept when multiple are available, without asking the user to disambiguate. (e.g asking for code related to a 'port driver' in erlang/elixir it seems to talk about either serial ports or TCP ports without recognising that this is a specific concept in that context; yet if you ask it more specifically, it seems to know what a 'port driver' is)

I suspect in this case it got confused between programming languages as I mentioned 3 in the one chat session - but the apparent confidence with which it replies, even providing URLs to non-existent documentation is a worry.

I expect it'll be a useful tool one day though. For now, even for basic code, I'd rather read docs and work it out, or google for human-written examples.

===== Earl 2022-12-26

Yes it goofs up all the time. Nothing I've seen it produce is ready for "prime time". On the other hand, its knowledge is vast, it understands English so it can understand your questions even though you don't know any the specialized terminology for the subject (i.e. No more trying to guess the optimal Google keyword set for a topic). It can explain itself in English which is something rare in programming. Once you get an answer you can continue on with more and more detailed questions as your understanding expands. I think this has great potential. To see some of it's potential look at which shows how C++ and chatgpt can interact.

===== dox - 2022-01-13

It almost had me fooled when asking how to save a dict to file. It then decided to invent this fantasy procedure. The fabled dict dump command anyone?

    proc save_dict_to_file { dict file } {
    set dict_string [dict dump $dict]
    set file_handle [open $file w]
    puts $file_handle $dict_string
    close $file_handle

And so, after asking it nicely to create the procedure.. it did.

    proc dict_dump {data file_path} {
    set fd [open $file_path "w"]
    foreach {key value} [dict get $data] {
        puts $fd "$key $value"
    close $fd