gyrson has asked for the wisdom of the Perl Monks concerning the following question:

Dear Brothers, I am looking for some experienced monks who are willing to help a humble novice. Since one month I am learning PERL with different Books and each day I like it more and more. I am trying now to solve some Problems with PERL. And here my Problem:

Somebody wrote an awfull Java-Program that I need to interact with. This Program asks me allways the same questions, but he doesn't allow to give it as attribute or to read (like <<<EOF). It runs in a machine which doesn't allow me to compile, so no CPAN help. He has the usual suspects modules like IPC::Open3, which I used to interact with the Program. And when I thought I had it, I saw that this Program calls another Java-Program and open for it a new child. With my Solution I cannot access to the Grandchild to give him the input. Here my Code:

use IPC::Open3; local(*START_IN, *START_OUT, *START_ERR); my $childpid = open3(*START_IN, *START_OUT, *START_ERR, 'external.sh') +; while (<START_OUT>) { print; if (/Question1/) { print START_IN "Answer1"; } elsif (/Question2/) { print START_IN "Answer2"; } }

When I run this "Interact.pl", he prints out some input and stops when he calls the Java-Program. And that's all. My Solaris ptree looks like that:

8278 zsched 9312 -ksh 590 bash 28268 /usr/perl5/5.8.4/bin/perl ./Interact.pl 28269 /usr/bin/ksh /tools/external.sh 28270 java ExternalPG

Replies are listed 'Best First'.
Re: Interact external Program's child.
by keszler (Priest) on Nov 06, 2011 at 12:21 UTC

    external.sh, from the name, appears to be a shell script. What does it contain?

    My guess is that in addition to asking questions it sets up the environment for the java program, then runs it. If so, then the answer may be to do all those things in Interact.pl, including calling open3 with a command like 'java ExternalPG --something Answer1 --otherthing Answer2'

      Hello Keszler, thank you for the quick answer.

      I have also thought something like that. But sadly there are 2 things which stands on my way. 1st is that I have no policy to edit the external.sh. the 2nd one was that I wanted to make my own version of it, and when I looked at it, it appeard that:

      more /tools/external.sh ^?ELF^A^B^A^B^B^A^Aj^X4^C-È4 ^F(^X^V^F4^A4ÀÀ^E^Cô^Q^D^A^A^C^Qü^C^Qü^E^ +A^A^C ^E ^KØ"¬^G^A^B^C&h^E&hð^Goÿÿû^F/usr/lib/ld.so.1 ^Aÿÿ^Aÿÿ^Aÿÿ^Aÿÿ^Aÿÿ^Aÿÿ^Aÿÿ^Aÿÿ^Aÿÿ^A^D^A^D^ ... etc

        Well that makes it more difficult.

        If you're running Solaris 8 or 9, or 10 if you have root, the command /usr/ucb/ps -aeguxwwwww <pid of java ExternalPG> should show you the complete command-line and environment variables. With that you could try calling open3 directly on java ExternalPG ... to bypass whatever IO redirection external.sh is doing.

        update - missed the 'e' in the ps command

        first of all let's see if is really a bash script

        file external.sh

        I have no policy to edit the external.sh

        Can you read-only open it?

        If you can, then let's see if zless can open it or not

        zless external.sh
Re: Interact external Program's child.
by JavaFan (Canon) on Nov 06, 2011 at 12:40 UTC
    Can you talk to said "Somebody"? Or do you work in an environment where communication between coworkers is forbidden?
      Sadly the situation is quitly political more then tecnical. You know how sometimes logic goes lost in political decicions. I am sadly not able to change external.sh nor ask for an update. I hoped actually to solve that in my PERL-Program. Like greping the child-process and send STDIN there or something like that. And "Somebody" left the company and (lucky guy) went to Australia 2 years ago. He was a nice guy who would have bypassed the political decicion in order to get things done.
        1. Don't try to use technical solutions to fix political problems.
        2. Talk to the next "Somebody". Or do you work for a company where code is considered sealed and unmodifiable when the original author left the company?