Sumathi Gokul
Sumathi Gokul

Reputation: 111

Tcl script to run perl script that has multiple file handles and generate resultant output file?

I need to run perl script that has multiple file handles and user data through tcl script and generate resultant output file.

tcl script (test.tcl) has following line and expects resultant output file of perl script to be generated in folder1.

exec perl \...\folder1\test.pl

test.pl code is as follows.

print "Enter user data1: ";
my $data1 = <STDIN>;
print "Enter user data2: ";
my $data2 = <STDIN>;
my $sum = $data1 + $data2;

This is a portion of code and the varaible "sum" will be used in furtehr code. Finally, perl script will generate a output text file. How to use tcl interpreter to extract the user data for perl script and generate output file??

Upvotes: 1

Views: 94

Answers (2)

glenn jackman
glenn jackman

Reputation: 247210

Just connect Tcl's stdio channels to perl's

exec perl test.pl <@stdin >@stdout 2>@stderr

Upvotes: 1

Dinesh
Dinesh

Reputation: 16436

Since the sub-process (i.e. perl program) involves multiple user input from stdin, you have to feed them from a file.

% cat test.txt
10
20
% cat test.pl
print "Enter user data1: ";
my $data1 = <STDIN>;
print "Enter user data2: ";
my $data2 = <STDIN>;
my $sum = $data1 + $data2;
print "Result : $sum\n";
%
% exec perl test.pl < test.txt
Enter user data1: Enter user data2: Result : 30
%  

If you want it to be an actual interactive like session, then you should use Expect.

#!/usr/bin/expect
spawn perl test.pl
expect {
    "data1: " {send "10\r";exp_continue}
    "data2: " {send "20\r";exp_continue}
     eof {puts "completed"}
}

Output :

Enter user data1: 10
Enter user data2: 20
Result : 30
completed

Reference : exec

Upvotes: 0

Related Questions