Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Testing Java code

by drawde83 (Acolyte)
on Sep 30, 2005 at 00:34 UTC ( [id://496298]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I'm a bit of a perl newbie but heres my problem. for an assignment in java I have to test a program to see how well it runs. I have to run it with 10000, 20000, 30000 and 40000 random numbers. I have to do each of those setups five times and then graph the medians. I know I can use bash to store the results, I could use bash to find the median but I probably know perl a little better and then I'll graph it in gnuplot. What I want to know is there a way to call the java program from perl itself since it would be easier than reading it in from a file. also any tips to make this easier would be appreciated :-)

Replies are listed 'Best First'.
Re: Testing Java code
by herveus (Prior) on Sep 30, 2005 at 00:50 UTC
    Howdy!

    You can run an external command using system (if you don't need to capture the output) or backticks (``) or qx// if you do need to capture the output. It looks like you probably want to use backticks or qx//:

    my $output = qx/java .../;

    Update: If you capture the output in an array, you get a list of lines instead of one big string...

    yours,
    Michael
      And don't forget that you can also use open to start external programs:
      open($javain, "java ... |") or die "...."; while (<$javain>) { #receive the output } close($javain); # # or # open($javaout, "| java ...") or die "...."; foreach (@inputline) { print $javaout $_; #send some input } close($$javaout);
      Key thing is the pipe "|".

      $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print

        ok I've tried out those methods but when I run the line

         my $output = qx/java MyProgram 50000/

        it outputs the time for that the process runs to the screen but I can't retrieve it

Re: Testing Java code
by mpeters (Chaplain) on Sep 30, 2005 at 15:25 UTC
    Just to offer a completely different solution... You can invoke Java code (classes) directly from perl using Inline::Java.

    -- More people are killed every year by pigs than by sharks, which shows you how good we are at evaluating risk. -- Bruce Schneier
Re: Testing Java code
by Knom (Beadle) on Oct 03, 2005 at 03:32 UTC
    Try appending a ' 2>&1' to your command. This tells the shell to redirect the output of `time` (STDERR) to STDOUT instead. Good luck!
Re: Testing Java code
by rvosa (Curate) on Oct 01, 2005 at 01:08 UTC
    The CPAN module Java is neat. You start up a JavaServer, and if your Java program is in your CLASSPATH you can instantiate objects from its classes and make method calls, so conceivably you could do the operation you want to do, while timing it using benchmark.

    2005-10-02 Retitled by planetscape, as per Monastery guidelines
    Original title: 'Re:'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://496298]
Approved by herveus
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2025-03-20 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (61 votes). Check out past polls.