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

I was trying to pull some version information on java into an array and thought I could pull the string I wanted out of the array. But for some reason my script was not getting the data into the array like I thought it would.
Running:
#! /usr/local/bin/perl use strict; my @javaInfo = `java -version`; foreach my $line (@javaInfo) { print "- $line\n"; }
Gives me an output from java -version displayed while the script runs, but the array never gets populated for some reason.
Running:
#! /usr/local/bin/perl use strict; my @perlInfo = `perl -version`; foreach my $line (@perlInfo) { print "- $line\n"; }
Gets the perl information fine...so is this due to the way the data is returned? The format (like the quotes around the java version), maybe I am missing pipe for the data stream or something else?
Thanks

Replies are listed 'Best First'.
Re: Why isn't my array populating?
by Fletch (Bishop) on Mar 27, 2008 at 15:14 UTC

    That you got output on your screen is your clue that java was writing to STDERR, not STDOUT. Redirect and all will be fixed (adjust accordingly to your shell's syntax, of couse).

    my @javaInfo = `java -version 2>&1`;

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Ah yeah, I can see that now. It actually had not occurred to me that this might be the case.
      I still have the question why java would direct to STDERR and not STDOUT.
        I still have the question why java would direct to STDERR and not STDOUT.

        When a command-line utility (in this case "java") offers a "-v" (or "--version") option to print its version number, it's actually fairly common for the version string to be printed to STDERR rather than to STDOUT, especially if the app tends to write actual data to STDOUT. This is so that the version string won't get treated as actual data in a typical pipeline. (Your case is exceptional: you want the version string treated as data, so you have to explicitly make it do that.)

A reply falls below the community's threshold of quality. You may see it by logging in.