Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
source + spawn + proc + expect = got me

Posted by jb55 (jb55), 16 May 2007
Hi,

I narrowed down my problem to something pretty small that baffles me.  Two scripts, outer.tcl sources inner.tcl.  The problem seems to be either the source, the spawn, the proc, or the expect.

========
outer.tcl:
========
proc outerProc {} {
       source inner.tcl
}
puts "Starting outer.tcl"
outerProc


========
inner.tcl:
========
proc myWaitFor {} {
   set iTimeout 6
   set iStr "avg"
   set iSend "1\r"
   puts "Waiting $iTimeout seconds for string $iStr to send back $iSend..."
   expect {
       timeout {
           puts "ERROR: timed out, did not receive expected <$iStr>"
           return -2
       }
       -re "$iStr" {
           puts "Got string $iStr"
       }
       eof {
           puts "ERROR: EOF received while waiting for $iStr"
           return -1
       }
   }
puts "Done myWaitFor"
   return 0
}

puts "Starting inner.tcl"
set pingPid [spawn /usr/sbin/ping myhostname -n 3]
puts "spawned ping pid=$pingPid"
myWaitFor
puts "after wait avg"


=======================
I was expecting the ping command to ping myhostname with 3 packets, then tabulate the min/max/avg where my expect would have caught the "avg" before the 6 seconds timeout and display "Got String".  However, eof is caught instead:

bash-2.00$ expect outer.tcl
Starting outer.tcl
Starting inner.tcl
spawn /usr/sbin/ping myhostname -n 3
spawned ping pid=19367
...ting 6 seconds for string avg to send back 1
ERROR: timed out, did not receive expected <avg>
after wait avg



It works if:
1) I move the spawn inside myWaitFor
2) expect inner.tcl directly (instead of expect outer.tcl)

I thought I had some understanding of TCL but this proc + source + spawn + expect strangeness has me confused.

Thanks for any insight.


Posted by admin (Graham Ellis), 17 May 2007
I'm not sure, but I think it's going to relate to the scope of the spawn_id, and the spawn_id being neither global nor local to the function in which you're expecting.  It would be an interetsing experiment to globalise this variable in the top of inner.tcl which (when run within outer) is in effect a part of your outerProc

Posted by jb55 (jb55), 17 May 2007
on 05/17/07 at 03:42:51, Graham Ellis wrote:
I'm not sure, but I think it's going to relate to the scope of the spawn_id, and the spawn_id being neither global nor local to the function in which you're expecting.  It would be an interetsing experiment to globalise this variable in the top of inner.tcl which (when run within outer) is in effect a part of your outerProc



I think there is something about TCL scope with source command that I am missing or do not understand (yet).

In inner.tcl tried:

proc myWaitFor {} {
  global pingPid
....

and also tried adding to the main of inner.tcl:

global pingPid
puts "Starting inner.tcl"
set pingPid [spawn /usr/sbin/ping myhostname -n 3]
....





Posted by admin (Graham Ellis), 17 May 2007
Still looks like your problem is the spawn_id variable.  As I recall this needs to be global and it's the default variable used by send-s and expect-s. If you spawn inside a proc, (which in effct you are doing when you source) you may need to global or uplevel it in that proc.  I don't think your pingPid is relevant - you save it, report it, and do nothing else with it.

As pre previous suggestion ... add
global spawn_id
at the very top of inner.tcl

Posted by jb55 (jb55), 17 May 2007
on 05/17/07 at 20:43:43, Graham Ellis wrote:
As pre previous suggestion ... add
global spawn_id
at the very top of inner.tcl




aahh there is my missing piece.  spawn_id is a special variable to expect.  I knew I was missing something  

Thank you very much it works now.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho