How to call expect script from Perl

I have a Perl script sub.pl, and i want to call another Expect script called sub.exp. The sub.exp will generate a text file called sub.txt, while the sub.pl called from a html form will display the content of sub.txt to the textarea on the html form. How do I call sub.exp from sub.pl???

<<sub.pl>>

#!/usr/bin/perl

system('sub.exp');

print "Content-type:text/html\n\n";
print "<html>\n";
print "<head><title></title></head>\n";
print "<body>\n";
print "<textarea name='sub' cols=30 rows=4>\n";
open(INFO, "sub.txt");
@array=<INFO>;
close (INFO);
chomp(@array);
 
foreach $line (@array){
#($last,$first)=split(/\|/,$line);
print "$line\n";
}
print "</textarea>\n";
print "</body>\n";
print "</html>\n";

---------- Post updated at 12:34 PM ---------- Previous update was at 11:41 AM ----------

Anybody help?

---------- Post updated at 12:34 PM ---------- Previous update was at 12:34 PM ----------

Anybody help?

First, make sure that the Expect script is executable. And if you call it, call it with the absolute path. Same goes for reading the text file.

Or, use Expect;, and skip using an external command.

Hi Pludi,

Thanks for your reply. I have put the absolute path of where the sub.exp is located, but it seems to me that the sub.exp is not called by the sub.pl script. If I run the sub.exp script, I can see a sub.txt is generated and it is what I want. Is the line in my perl script the correct way of calling a expect script?

If I run the sub.pl, I can see all other tags of html form gets printed, but cannot see the sub.txt content. My ultimate goal is to display the content of sub.txt to the TEXTAREA in the html form.

Thanks.

If you see a text file being generated, I'd say that's a pretty good indicator that your system() line is working. Now try using the absolute path for the text file when reading it.

Also, it would be good style to also include

use strict;
use warnings;

in your code and check for any errors.

Hi Pludi,

Sorry I didn't make it clear. I mean when I run sub.exp by "expect sub.exp", it can generate the sub.txt, so the expect script should be fine.

But when I run sub.pl with "perl sub.pl", it will generate,

 
-bash-3.00# perl sub.pl
Content-type:text/html
<html>
<head><title></title></head>
<body>
<textarea name='sub' cols=30 rows=4>
</textarea>
</body>
</html>
-bash-3.00# 
 

If I put the two lines that you indicated above, I will get this,

 
-bash-3.00# perl sub.pl
Global symbol "@array" requires explicit package name at sub.pl line 20.
Global symbol "@array" requires explicit package name at sub.pl line 22.
Global symbol "$line" requires explicit package name at sub.pl line 26.
Global symbol "@array" requires explicit package name at sub.pl line 26.
Global symbol "$line" requires explicit package name at sub.pl line 28.
Execution of sub.pl aborted due to compilation errors.
 

My current code is like this,

 
#!/usr/bin/perl
use strict;
use warnings;
 
system('/opt/xampp/htdocs/hsgw/sub.exp');
 
print "Content-type:text/html\n\n";
print "<html>\n";
print "<head><title></title></head>\n";
print "<body>\n";
print "<textarea name='sub' cols=30 rows=4>\n";
open(INFO, "/opt/xampp/htdocs/hsgw/sub.txt");
@array=<INFO>;
close (INFO);
chomp(@array);
 
foreach $line (@array){
print "$line\n";
}
print "</textarea>\n";
print "</body>\n";
print "</html>\n";
 
 

That's why I asked whether or not the file is executable. If it is (the 'x' bit is set), it can be called without calling expect explicitly. Otherwise, you'll have to call it the same way in the script as you do on the command line.