Skip to Content
Exploring Expect
book

Exploring Expect

by Don Libes
December 1994
Intermediate to advanced
606 pages
16h 7m
English
O'Reilly Media, Inc.
Content preview from Exploring Expect

How Expect Writes Variables In Different Scopes

Although Expect commands look in two scopes when reading variables, only one scope is used when writing variables.

  • When writing a variable, the variable is written in the current scope unless a global command has declared the variable, in which case, the variable is written in the global scope.

This is the usual Tcl behavior, but since it differs from the previous rule, I will describe it in more detail.

In the previous definition of ftpcmd, the expect command looks for ftp to return "220*ftp>“. The expect command, as usual, writes what it finds into expect_out(buffer). However, expect writes the variable into the local scope. That means that the caller does not see the updated expect_out. In the following code, the caller assumes expect_out is not overwritten by ftpcmd.

expect $shellprompt
ftpcmd "get file"
send_user "found shell prompt: $expect_out(buffer)\n"

If you need a procedure to write into the global version of expect_out, then a global command must be used in the procedure. Here is a definition for ftpcmd which does that.

proc ftpcmd {cmd} {
    global expect_out

    send "cmd\r"
    expect "220*ftp> "
}

The rules just described for expect_out hold for spawn_id as well. You need a global command if you want to write the value of spawn_id outside the current procedure. Without a global command, the spawn command writes spawn_id into the local scope. As soon as the procedure returns, spawn_id reverts back to its old definition. In Chapter 4

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

AI Agents in Action

AI Agents in Action

Micheal Lanham
Learning Go

Learning Go

Jon Bodner

Publisher Resources

ISBN: 9781565920903Supplemental ContentErrata Page