By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,834 Members | 1,559 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,834 IT Pros & Developers. It's quick & easy.

Control one script from the other.

P: 3
I have two scripts:
per4.pl
Expand|Select|Wrap|Line Numbers
  1. @food = ("pear", "plum", "egg", "apple");
  2. $TableLength = $#food;
  3. print $TableLength;
  4. for($i = 0; $i < $TableLength; ++$i){
  5.     print "$food[$i]\n";
  6. }
  7. do{
  8.     print "Password?";
  9.     $a = <STDIN>;
  10.     chop $a;
  11. }
  12. while ($a ne "fred");
The other perl5.pl

Expand|Select|Wrap|Line Numbers
  1. $FileName = "perl4.pl";
  2. system $FileName;
  3.  
  4. while($Line ne "Password?"){
  5.     $Line = <STDIN>;
  6.     if($Line eq "Password?"){
  7.          print STDOUT "fred"
  8.        }
  9.        chop $Line;
  10. }
My intention is that when I got question about password from perl4 perl5 should set that and both should finish. Can anybody help me with this task??

Regards
krieger
Feb 29 '08 #1
Share this Question
Share on Google+
2 Replies


nithinpes
Expert 100+
P: 410
If you want to perl4.pl to take password from perl5.pl, then you should call perl5.pl within perl4.pl rather than calling perl4.pl from perl5.pl.
You have tried to capture line containing "Password" and pass "fred" to STDOUT, though it is incorrect. The actual flow is that system command will be executed first(perl4.pl will execute first) before getting to while loop.
I would suggest you to try the following script if your objective is just to set password from perl5.pl

perl4.pl
Expand|Select|Wrap|Line Numbers
  1. $FileName = "perl5.pl";
  2.  
  3. @food = ("pear", "plum", "egg", "apple");
  4. $TableLength = $#food;
  5. print $TableLength;
  6. for($i = 0; $i < $TableLength; ++$i){
  7.     print "$food[$i]\n";
  8. }
  9. do{
  10.     print "Password?\n";
  11.     $a = `perl $FileName`;  # get the output from perl5.pl
  12.     $a eq "fred" or $a=<STDIN>; ## take STDIN if $a is not "fred"
  13.     chomp $a;
  14.  
  15. }
  16. while ($a ne "fred");
  17.  

perl5.pl
Expand|Select|Wrap|Line Numbers
  1. print "fred";  ##pass "fred" or write your code
  2.  
I am using reverse quote instead of system command because, system() will return 0 if the command executed or null if it fails.
Mar 4 '08 #2

P: 3
If you want to perl4.pl to take password from perl5.pl, then you should call perl5.pl within perl4.pl rather than calling perl4.pl from perl5.pl.
You have tried to capture line containing "Password" and pass "fred" to STDOUT, though it is incorrect. The actual flow is that system command will be executed first(perl4.pl will execute first) before getting to while loop.
I would suggest you to try the following script if your objective is just to set password from perl5.pl

perl4.pl
Expand|Select|Wrap|Line Numbers
  1. $FileName = "perl5.pl";
  2.  
  3. @food = ("pear", "plum", "egg", "apple");
  4. $TableLength = $#food;
  5. print $TableLength;
  6. for($i = 0; $i < $TableLength; ++$i){
  7.     print "$food[$i]\n";
  8. }
  9. do{
  10.     print "Password?\n";
  11.     $a = `perl $FileName`;  # get the output from perl5.pl
  12.     $a eq "fred" or $a=<STDIN>; ## take STDIN if $a is not "fred"
  13.     chomp $a;
  14.  
  15. }
  16. while ($a ne "fred");
  17.  

perl5.pl
Expand|Select|Wrap|Line Numbers
  1. print "fred";  ##pass "fred" or write your code
  2.  
I am using reverse quote instead of system command because, system() will return 0 if the command executed or null if it fails.
Hi
The problem is that I will have more interaction between perl5 and perl4, I gave those files only as example. I need to execute perl4 from perl5. In real scripts I cannot change perl4, it is built in a System.
Mar 19 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.