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

Hi monks,

I am calling external jar file using perl script and that jar file return the some value. How to get the return's value to the perl scalar variable.

Example

$return=`java -jar xyz.jar`;

In the above example it returns the some value say 10. How to get 10 into the $return variable.

Replies are listed 'Best First'.
Re: get return value from jar file
by CountZero (Bishop) on Dec 05, 2011 at 13:10 UTC
    Did you try the code in your post? What was the result?

    The return value of the command run inside the backtics is the STDOUT of the command.

    qx/java -jar xyz.jar/ is equivalent to backticks. You can find more documentation about it in perldoc perlop.From the docs:

    qx/STRING/
    `STRING`
    A string which is (possibly) interpolated and then executed as a system command with "/bin/sh" or its equivalent. Shell wildcards, pipes, and redirections will be honored. The collected standard output of the command is returned; standard error is unaffected. In scalar context, it comes back as a single (potentially multi-line) string, or undef if the command failed. In list context, returns a list of lines (however you've defined lines with $/ or $INPUT_RECORD_SEPARATOR), or an empty list if the command failed.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: get return value from jar file
by vinian (Beadle) on Dec 05, 2011 at 12:44 UTC

    check perlvar and you will get this

    $CHILD_ERROR $? The status returned by the last pipe close, backtick (``) command, successful call to "wait()" or "waitpid()", or fr +om the "system()" operator. This is just the 16-bit status word returned by the traditional Unix "wait()" system call (or +else is made up to look like it). Thus, the exit value of the subprocess is really ("$? >> 8"), and "$? & 127" gives whi +ch signal, if any, the process died from, and "$? & 128" repo +rts whether there was a core dump.

    update:

    sorry, misunderstanding the question

    you have got the return value of the backtick command. just as CountZero posted.

      And how does your answer enlighten the OP? Did he ask for the error status of the command?

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        thanks for your reminding, i misunderstanded the question, and have update the answer.