-1

Where's the telnet output?

thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ tclsh main.tcl 
spawn telnet rainmaker.wunderground.com
getting weather for nyc
^C
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 

main:

lappend auto_path /home/thufir/NetBeansProjects/spawnTelnet/telnet/api

package require weather 1.0


tutstack::connect "nyc"

code:

package provide weather  1.0
package require Tcl      8.5
package require Expect

namespace eval ::tutstack {
}

proc ::tutstack::parse {city} {
puts "getting weather for $city"
expect -nocase "Press Return to continue:"
#interact \004 return
interact \004 return
}

proc ::tutstack::connect {city} {
spawn telnet rainmaker.wunderground.com
set telnet $spawn_id
#interact
parse $city
}

this works:

thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ tclsh chainedProcs.tcl 
hello Alice from first
hello Alice from second
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ cat chainedProcs.tcl 
lappend auto_path /home/thufir/NetBeansProjects/spawnTelnet/telnet/chained

package require chained 1.0

example::first "Alice"
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ cat chained/chained.tcl 
package provide chained  1.0

namespace eval ::example {
}

proc ::example::first {foo} {
puts "hello $foo from first"
second $foo
}

proc ::example::second {bar} {
puts "hello $bar from second"
}
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 

but...not using telnet there. I'm looking to "chain" (?) a sequence but with telnet, expect, interact, etc.

Thufir
  • 8,216
  • 28
  • 125
  • 273
  • It's not clear what you are asking about. Could you please provide a [mcve]? –  May 09 '20 at 04:05

1 Answers1

2

Whenever you are using the Expect package's commands in a procedure, you need to take some care because of the way it accesses variables. In particular, you probably need to say at least:

global spawn_id

in each of those procedures. Perhaps like this:

proc ::tutstack::parse {city} {
    global spawn_id
    puts "getting weather for $city"
    expect -nocase "Press Return to continue:"
    # You *might* need inter_return instead of return; the documentation isn't clear
    interact "\004" return
}

proc ::tutstack::connect {city} {
    global spawn_id
    spawn telnet rainmaker.wunderground.com
    set telnet $spawn_id
    parse $city
}

However, you're probably better off keeping the spawn ID (i.e., the result of calling spawn) in a namespace variable and passing it explicitly into the relevant commands via the -i flag, like this:

proc ::tutstack::connect {city} {
    variable telnet [spawn telnet rainmaker.wunderground.com]
    parse $city
}

proc ::tutstack::parse {city} {
    variable telnet
    puts "getting weather for $city"
    expect -i $telnet -nocase "Press Return to continue:"
    # You *might* need inter_return instead of return; the documentation isn't clear
    interact -i $telnet "\004" return
}
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • 1
    FWIW, I find the expect manual to be quite remarkably difficult to read as reference material. There's a lot of potentially interesting bits and pieces hidden in there, and it is so easy to miss them unless you read extremely carefully! – Donal Fellows May 09 '20 at 08:15
  • I'm looking into the `-i` switch and the `spawn_id`; couldn't quite get those parts working. – Thufir May 10 '20 at 08:51