tags:

views:

48

answers:

2

Is it possible to call an Expect script from Perl by passing an array as a parameter? If so, how to retrieve that array in Expect like we retrieve non-array data as:

set value [lindex $argv 0]
+2  A: 

I assume you're talking about a Perl array, which is known as a list in Tcl. From Perl:

system 'myscript.exp', @ary;

Then your Expect script will have:

set list_of_arguments $argv
glenn jackman
+1  A: 

Glenn, I wonder whether Saranyya will understand your answer. I'll offer a bit of background that might help him.

First, Saranyya, be aware that you can choose a variety of different combinations of Perl and Expect. If an existing Expect script does something you want to enhance, you might do well simply to code the functionality in Expect, rather than Perl; Expect is a fully-general-purpose language, and can do essentially everything Perl does.

Conversely, if you have a Perl application which needs Expect functionality, you can almost surely write everything you need in Perl, with Expect.pm; there's no need to construct a separate Expect process.

Finally, it's also reasonable to do what you seem to describe, and what Glenn believes you're after: invocation of a stand-alone Expect process from Perl. If you do so, and if, as Glenn sketches, your Perl program includes

system 'myscript.exp', @ary;

then your Expect script accesses the parameters passed to it through the $::argv value, which is a list. I'll elaborate the example slightly: suppose you pass through system a list of hostnames; then in Expect you might usefully have

set list_of_hostnames $::argv
foreach hostname $list_of_hostnames {
    do_something_valuable $hostname
}

The interface from one process to another communicates values, rather than variables. By the time the information reaches Expect, it "doesn't know" that it was an array (for example) in Perl. You use the same techniques to dereference $::argv, however you packed things on the Perl side.

Cameron Laird
I changed my mind. After further reflection, I decided that Glenn's answer probably matches Saranyya's needs than mine does. Perhaps the latter is useful as a complement to the former, though.
Cameron Laird