Importing variables from Main Expect Body to Procedures

tcl

Importing variables from Main Expect Body to Procedures

Postby martin » Sun, 14 May 2006 11:56:51 GMT

After much Google searching and wasting time, I am still stuck on a
fairly simple problem.  I have written an expect script in which the
lindex function is used to receive a password from the outside but
then I got fancy and am trying to import it in to a procedure.  The
manual says that all variables are local unless you use the global
designation which I did with no effect at all.  Here is a
stripped-down example of the script.  The complaint from expect -d is
that the variable pw can't be read.

#!/usr/local/bin/expect -f
#Pick up first argument as password.
set pw [lindex $argv 0]
#This has the proper value anywhere in the main body.
#Feed it to proc login.
proc login { } {
send "$pw\r"
expect {
-exact "ord" {
send -- ""
expect -exact "\$"
send -- "exit\r"
expect eof
exit 1
} 
-exact "\$" {
return
}
}
}

	I did try 

global pw

and occupied a few more bytes of disk space, but nothing seems to
import that variable into proc login.

	The other lines in the script do work if I hard-code the
variable in to the procedure so this little problem is all that
separates me from victory.:-).  Any constructive ideas are much
appreciated.
-- 

Martin McCormick WB5AGZ  Stillwater, OK 
Information Technology Division Network Operations Group

Re: Importing variables from Main Expect Body to Procedures

Postby Jeff Godfrey » Sun, 14 May 2006 12:43:19 GMT







Martin,

I suspect that you didn't have your global declaration in the right 
spot.  First, the original assignment of "pw" is global, just due to 
the fact that it's done outside of a procedure.  Now, the problem 
comes when you try to access that variable within your "login" proc. 
Any reference to "pw" there is *local* to the procedure unless stated 
otherwise.  There general ways of doing that are:

1.  add a "global pw" *inside* the "login" proc - before referencing 
the variable
2.  just reference the variable with it's fully qualified name (::pw 
or $::pw depending on what you're doing).

Jeff 



Re: Importing variables from Main Expect Body to Procedures

Postby Bryan Oakley » Sun, 14 May 2006 13:30:55 GMT



In this stripped down version you don't declare pw as global. It should 
be something like this:

   proc login {} {
     global pw
     send "$pw\r"
     ...
   }

"global" only affects the current scope, BTW. You must declare pw as 
global in every proc in which it is used. You don't have to declare it 
as global if used in the global scope.

You can also just fully qualify the reference. Some people prefer this 
because it makes it clear you're accessing a global variable:

   proc login {} {
     send "$::pw\r"
     ...
   }


-- 
Bryan Oakley
 http://www.**--****.com/ 

Re: Importing variables from Main Expect Body to Procedures

Postby Donald Arseneau » Sun, 14 May 2006 13:52:40 GMT

 XXXX@XXXXX.COM  (Martin McCormick) writes:



global pw



It would be good to show what you tried.  My guess is you declared
it once, outside any proc, which is useless.


-- 
Donald Arseneau                           XXXX@XXXXX.COM 

Re: Importing variables from Main Expect Body to Procedures

Postby martin » Mon, 15 May 2006 11:39:04 GMT

In article < XXXX@XXXXX.COM >,



	That is exactly what I did and even did it on purpose because
I didn't know any better.  I am used to C where global variables get
their globalness by being put in the lines above main().  The only way
you get in trouble, there, is if you forget to use extern for that
variable in a module and then the compiler complains when you do a
make.

	My utmost thanks to all who answered and explained.  There is
always that funny feeling that one just asked the silliest question of
the week and will be told that, etc.
-- 

Martin McCormick WB5AGZ  Stillwater, OK 
Information Technology Division Network Operations Group

Similar Threads:

1.How to use a variable in expect body which embedded in a shell script

 XXXX@XXXXX.COM  < XXXX@XXXXX.COM > wrote:
>> > pawn telnet '"$hostname"'>>>> Actually, I'm sorry for having posted it at all, because it is maximally>>>> unsafe. ...
>
> Thanks a lot for your prompt response. Does expect -c take things>
> between ' and ' as the code of expect?
Yes, and expect doesn't see the single quotes themselves.
>
> The code outside ' and ' will be taken as shell,
The shell, which also interprets the single-quotes according
to it's syntax (namely: "take everything literally, up to the
next single-quote") will also parse the "$hostname" and finally
create a single string that has all the parts concatenated, and
this string is then passed to expect, which takes it as if it
it had read it from a script-file.
>
> so "$hostname" was actually a shell style variable.
yes.
>
> So you can just omit those two ", like>
> expect -c '>
> set timeout 15>
> spawn telnet '$hostname'

One principially can, but that has an effect on what the shell
does with the contents of the variable.  For sh-like shells, 
(unlike tclsh!), there *is* a difference between $var and "$var",
but this difference is only relevant for values of var that
contain certain shell-meta-characters such as e.g. whitespace.

2.How to use a variable in expect body which embedded in a shell script

Hi all,

My question is how to use expect in a shell script. For example, here
is a little program, it just telnet to some host, and login root
automatically.

#!/bin/bash

echo "This is to test how to call expect in a shell"

#hostname=10.0.0.1

expect -c '
set timeout 15
spawn telnet 10.0.0.1
expect "login:" { send "root\r" }
expect "assword:" { send "abcd1234\r" }
interact
'

Can I use variable in the body of expect? Such as the 10.0.0.1 is a
variation passed to the shell scripts, like the following, actually
this is wrong, expect can't handle $hostname

#!/bin/bash

echo "This is to test how to call expect in a shell"

hostname=10.0.0.1

expect -c '
set timeout 15
spawn telnet $hostname
expect "login:" { send "root\r" }
expect "assword:" { send "abcd1234\r" }
interact
'

it will issue the following error.
[tina@kgardenia test]$ ./test.expect
This is to test how to call expect in a shell
can't read "hostname": no such variable
    while executing
"spawn telnet $hostname"


Does anybody know how to use a variable in the body of the expect
code, which is embedded in a bash?

Many thanks to you all :D

3.import selected module variables within that module's procedures

I wish there were a way to import only a few module variables, derived
types, and procedures into a procedure of the module, for example

module foo_mod
integer :: i,j
contains
subroutine sub()
use foo_mod, only: i  ! NOT a legal Fortran 95 statement
end subroutine sub
end module foo_mod

but one cannot write "use foo_mod" within module foo_mod. Is there a
way to get this effect in Fortran 95? Fortran 2003? Could the
"submodule" of TR 19767 accomplish this?

4.expect procedure name in procedure call(newbie)

Hello.

I've got 'expect procedure name in procedure call' warning but
I think my code is good
Inside testclass.adb I have Create function and when I'm trying to call
it from
main.adb unit I receive that error.

Below are full codes of my Ada units.

And by the way - how can I dynamically allocate memory for e.g. 10
elements(array of Floats)?
How can I reallocate them to 20 elements or 4?
How can I free the memory?

thanks in advance
best regards R

Codes:

main.adb:
--------
with testclass;
procedure Main is
object : testclass.rec1_Access;
begin
testclass.Create(object, 10);
end Main;

testclass.ads:
--------------
package testclass is
type rec1 is tagged private;
type rec1_Access is access rec1;
function Create(this: rec1_Access; s: Integer) return Integer;
private
type rec1 is tagged record
field: Integer;
end record;
end testclass;

testclass.adb:
--------------
package body testclass is
function Create(this: rec1_Access; s: Integer) return Integer is
begin
this.field :=s;
		return this.field;
	end Create;
end testclass;

5.Reply: expect procedure name in procedure call(newbie)

You are calling a function as though it were a procedure.  This is
alright in C and C++, where the result is just silently thrown away,
but in Ada you have to do something with the result, such as assign it
or use it in a subsequent expression or routine call.

6. procedure inside package body and modelsim error

7. Clarion 5.5PE legacy application import procedure

8. Main procedure inside a package?



Return to tcl

 

Who is online

Users browsing this forum: No registered users and 54 guest