1
votes

I am trying to remove my program's dependency on reading information from files and then interpreting that information. I have created a library of Perl commands that generates the necessary information on-demand. The one hang-up I am experiencing is that Perl sometimes calls an expect script and the expect script is currently designed to take in a file path, open that file, read in the information and then use that information.

Instead I would like to pass in a Perl array to the expect script that contains all of the information. Herein lies my problem: I have been struggling to figure out how an expect script handles an array as a parameter and translating that parameter into something useful.

Perl example:

my $expectCmd = "expect.sh 300 $vmIp $vmPort $pass @configInfo";
system($expectCmd);

Expect Script:

set ip [lindex $argv 1]
set port [lindex $argv 2]
set pass [lindex $argv 3]

At this point I have no idea how to read in the @configInfo array and use it. I have tried various methods of upvar, array set, for-loops, foreach loops, begging, pleading and bribing. Nothing has worked so far.

The end goal will be to send the @configInfo array line-by-line to a file. Basically, I want each index of the array to be a line in the file - if it was Perl I would just:

foreach my $line (@configInfo) {
    print FILE $line;
}

If anyone has experience in passing arrays into an expect script and then unpacking their contents for use I would be very grateful for a response. Many thanks!

1
Have you tried printing your $expectCmd variable and confirming it's in the correct format. The items in the array may be getting appended to each other without spacing. - Joshua
Hi @Joshua - I am sure the expect command is being called and I am sure that all of the variables (ip, port and pass) are being passed properly. The final variable, the array, is my issue: I do not know how to properly pass it or, once I have passed it to the expect script, how to properly use an array parameter inside the expect script itself. - Captain Ryan

1 Answers

2
votes

You should use the multi-argument version of system:

system('expect.sh', 300, $vmIp, $vmPort, $pass, @configInfo);

And in expect, you'd write (with a recent expect)

set config_info [lassign $argv num ip port pass]

If your expect does not know about the lassign command, do this

set num  [lindex $argv 0]
set ip   [lindex $argv 1]
set port [lindex $argv 2]
set pass [lindex $argv 3]
set config_info [lrange $argv 4 end]

To satisfy your end goal

set fh [open some.file.name w]
puts $fh [join $config_info \n]
close $fh