Version 5 of What's My Line?

Updated 2013-09-30 21:48:59 by pooryorick

What's My Line? is a game-style exercise where the goal is to guess the output of the Tcl script

Description

The following Tcl scripts are arranged in ascending order roughly according to the level of challenge the pose. The task is simple: read a script, and then guess what the output will be. Actually writing down your answer before looking at the answer is highly recommended. When you get something wrong, spend some time writing similar scripts in order to explore the subject in more detail.

Size your browser window so that the answer is not visible while you form your own answer.

Scripts

No Soup For You

proc proc {args} {
    puts {No soup for you!}
}

proc hello {} {
    puts {Hi there!}
}

catch {hello} res
puts $res

















































































Answer:

No soup for you!
invalid command name "hello"

The built-in [proc] is placed by a user-defined procedure that, instead of creating new procedures, prints No soup for you!. proc hello... then doesn't create a hello procedure, so when an attempt is made to invoke hello, no such command is found, and an error is produced instead.

Source in a [proc] in a [namespace]

Prior to running this script create a file named data that contains:

#this file should be called "data"
set localvar 99
namespace eval ns1 {
    proc proc1 {} {
        upvar 0 [namespace current]::var1 localvar
        source data
        set var2 Hello
    }
    proc1
}

catch {set ns1::var1} eres einfo
puts $eres
catch {set ns1::var2} eres einfo
puts $eres
catch {set $var2} eres einfo
puts $eres

















































































answer:

99
can't read "ns1::var2": no such variable
can't read "var2": no such variable

[source] causes a script to be evaluated in the current scope.