On Wednesday, June 6, 2018 at 10:44:05 AM UTC+2, Cecil Westerhof wrote:
> proc getExternalIP {} {
> set token [http::geturl
http://myexternalip.com/raw]
> set externalIP [string trim [http::data ${token}]]
> ::http::cleanup ${token}
> return ${externalIP}
> }
It might be overkill in this case, but you seem to like learning features of Tcl:
proc insecureGetExternalIp {} {
try {
string trim [::http::data $token]
} finally {
::http::cleanup $token
}
}
Notes:
* Considering variable context only, [set token] might go inside [try]; but if [::http::geturl] failed, then token is not defined in finally. This would hide the original error in the key -during of the result dict.
* I removed the [return] to demonstrate how the return value of commands propagate in this case. However, iirc "it is always a good idea to use [return] explicitly" (reference or correction needed as I cannot recall the reason).