Training, Open Source computer languages

This is page http://www.wellho.net/forum/The-Tcl- ... guage/source-spawn-proc-expect-got-me.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

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