1

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??

1
  • You'll probably want to use forward slashes (or double backslashes) for your paths. Backslashes have a special meaning in Tcl -- \f and \t in your code will be a formfeed and a tab character respecitvely Commented Apr 13, 2016 at 14:42

2 Answers 2

1

Just connect Tcl's stdio channels to perl's

exec perl test.pl <@stdin >@stdout 2>@stderr
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.