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

My perl script calls a Java program. It captures the output of that program(to handle the exception) and display the output to screen as well.
When run the Java program at DOS prompt, it generates:
RLU: Using PropertyResourceBundle... Record: 1 Result: surveyresponse.jsp Record: 2 Result: surveyresponse.jsp Record: 3 Result: surveyresponse.jsp Record: 4 Result: surveyresponse.jsp Hit return to exit.
The "Hit return to exit." causes the problem. When I run my perl script, it doesn't print the output to screen until I hit the return key. I guess Java program still hangs there because lack of "hit return". How to solve it? Hope I explain my problem clearly.
Thanks in advance.
-Hui
************ my script ************* #! /per/bin/perl -w use strict; my($program) = 'd:\ema\extension\win32\surveyimport d:\ema\extension\win32\Yahoo.cfg'; my(@res); open(RES, "$program |") || die "Unable to run $program: $!\n"; foreach (<RES>) { push @res, $_; #print "$_"; } print $?; print @res; close(RES); exit(0); ***************************************

Edit kudra, 2002-04-19 Changed title per ntc request

Replies are listed 'Best First'.
Re: output to screen by hitting return key
by tachyon (Chancellor) on Apr 18, 2002 at 23:39 UTC

    Add buffer autoflushing just after the use strict with $|++ see Suffering from Buffering

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      I don't see how the article could help. When the Java program runs, D:\dir\javac suveryimport
      it generates:
      RLU: Using PropertyResourceBundle... Record: 1 Result: surveyresponse.jsp Record: 2 Result: surveyresponse.jsp Record: 3 Result: surveyresponse.jsp Record: 4 Result: surveyresponse.jsp Hit return to exit.
      Without hitting the return, it won't return to the Dos prompt. Besides this, when the Java program throws no exception, with the help of hitting, I may get the right output order. If java program thorws exception(encounter error), the output is out of order which means one stream is out through terminal(bufferred), the other stream is out through STDERR (not bufferred). Any idea to walk around the problem?
      run directly from dos prompt: D:\Ema\extension\win32>surveyimport Kazoo.cfg Starting proxy manager. user name = sa RLU: Using PropertyResourceBundle... Record: 1 Result: surveyresponse.jsp java.lang.RuntimeException: Junk after quoted field at rubric.data.DataParser.hasMoreElements(DataParser.java:37) at rubric.data.ImportSurveyData.importFile(ImportSurveyData.java:186) at rubric.data.ImportSurveyData.main(ImportSurveyData.java:83) Hit return to exit. run from perl: D:\emaperl>perl emaperl.pl java.lang.RuntimeException: Junk after quoted field at rubric.data.DataParser.hasMoreElements(DataParser.java:37) at rubric.data.ImportSurveyData.importFile(ImportSurveyData.java:186) at rubric.data.ImportSurveyData.main(ImportSurveyData.java:83) Starting proxy manager. user name = sa RLU: Using PropertyResourceBundle... Record: 1 Result: surveyresponse.jsp Hit return to exit.
        Only a suggestion, but can you close STDIN before calling the Java program? Sometimes the Right Thing happens when reading from a file descriptor you expect to be open, when it isn't. (And sometimes spectacularly the wrong thing happens...)
Re: output to screen by hitting return key
by Albannach (Monsignor) on Apr 18, 2002 at 23:40 UTC
    If it's never going to need any more response than that, then this should do the trick - just change your open like so (of course I can't test this with your java app): open(RES, "echo \n | $program |") or die "Unable to run $program: $!\n"; Update: That's odd that it didn't work - I even tested it very simply using "dir /p", but perhaps it is a difference between Win95 and your platform.

    --
    I'd like to be able to assign to an luser

      I think you are close. Create a file with a newline in it. Then do this:
      open( RES, "$program < mynewline.file |" ) or die $!;
      Of course I think you should use IO::Pipe for that...

      Who was it, Knuth?, that said, "Beware the above code... I havn't tried it."

        thanks for the response. It rocks! Any idea about the java exception problem in my previous post?
      It didn't work. It seems the Java program never be called. the screen only display "ECHO is on." -Hui