TCL/tk again - a bit further on

24 views
Skip to first unread message

dr.mt...@gmail.com

unread,
May 14, 2024, 3:53:38 PMMay 14
to Shen
I got an answer to my question on Stackoverflow; but not quite what I wanted.  This program

proc url {String} {
    package require http
    package require tls
    ::http::register https 443 [list ::tls::socket -request 1 -ssl2 0 -ssl3 0 -tls1 1 -cafile VeriSignClass3SecureServerCA-G3.crt]

    set token [::http::geturl $String]
    upvar #0 $token state
    return [::http::data $token]
}


Given url "https://shenlanguage.org/TBoS/Programs/3.3.txt" returns the program you
see on that page.  But what this url function returns I have no idea because I cannot write its output to a file.  It's seemingly not a string, which is what I want.  Any ideas welcome.  The problem is still standing so any input here welcome.

Mark

Bruno Deferrari

unread,
May 14, 2024, 5:36:07 PMMay 14
to qil...@googlegroups.com
It says here https://www.tcl.tk/man/tcl8.4/TclCmd/http.htm#M13

"The ::http::geturl command returns a token value that can be used to get information about the transaction. See the STATE ARRAY and ERRORS section for details."

then on the "STATE ARRAY" section: https://www.tcl.tk/man/tcl8.4/TclCmd/http.htm#M45

what I see is that there is a "body" elements contains what you want (not sure if as a string or raw binary).

It also says there you have to manually cleanup the token.

--
You received this message because you are subscribed to the Google Groups "Shen" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qilang+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/qilang/a8024efb-2909-4f01-a4a6-81b3cb10608cn%40googlegroups.com.


--
BD

Bruno Deferrari

unread,
May 14, 2024, 5:37:43 PMMay 14
to qil...@googlegroups.com
And the example here uses ::http::data https://wiki.tcl-lang.org/page/http::geturl
--
BD

dr.mt...@gmail.com

unread,
May 14, 2024, 6:18:49 PMMay 14
to Shen
This state variable is used in the original code:

proc url {String} {
  package require http
  package require tls
  ::http::register https 443 [list ::tls::socket -request 1 -ssl2 0 -ssl3 0 -tls1 1 -cafile VeriSignClass3SecureServerCA-G3.crt]
  set token [::http::geturl $String]
  upvar #0 $token state
  return [$state(body)] }

then if you invoke url on "https://shenlanguage.org/TBoS/Programs/3.3.txt" you get

invalid command name "(define factorial
   X -> (factorialh X 1))

(define factorialh
   0 Accum -> Accum
   X Accum -> (factorialh (- X 1) (* X Accum)))

(define plus
   X 0 -> X  
   X Y -> (+ 1 (plus X (- Y 1))))

(define fib
   0 -> 0  
   1 -> 1  
   X -> (+ (fib (- X 1)) (fib (- X 2))))

(define even?
   1 -> false  
   X -> (odd? (- X 1)))

(define odd?  
   1 -> true  
   X -> (even? (- X 1)))


"

You can see the right string has been passed but for some reason TCL/tk tries to execute it.

Mark

Bruno Deferrari

unread,
May 14, 2024, 7:15:05 PMMay 14
to qil...@googlegroups.com
Sorry, missed that detail. Then my guess is that data gives you "bytes" which need to be decoded (unexpected, I don't know much about tcl, but I remember it being all about strings)


so, something like:

set url_data [::http::data $token]
return [encoding convertfrom utf-8 $url_data]



--
BD

Chris Double

unread,
May 14, 2024, 7:22:28 PMMay 14
to qil...@googlegroups.com
On Wed, May 15, 2024, at 10:18 AM, dr.mt...@gmail.com wrote:
> *proc url {String} {
> package require http
> package require tls
> ::http::register https 443 [list ::tls::socket -request 1 -ssl2 0
> -ssl3 0 -tls1 1 -cafile VeriSignClass3SecureServerCA-G3.crt]
> set token [::http::geturl $String]
> upvar #0 $token state
> return [$state(body)] }*
> [...]
> You can see the right string has been passed but for some reason TCL/tk
> tries to execute it.

It's the square brackets in the return. They indicate that the results are to be evaluated as a command. Try:

return $state(body)

--
https://bluishcoder.co.nz
Reply all
Reply to author
Forward
0 new messages