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

Hi,

I encountered a problem backtick and would like to get some help.
The environment is Windows XP, cygwin, and ActivePerl 5.8.8. Here is the Perl code (try.pl):
`echo test124!`; system('java', 'test');

Here is the java code:
public class test { public static void main(String[] arg) { System.out.println("Hello, world!"); } }
And here is the makefile:
all: perl try.pl

I ran the makefile, but I couldn't see the "Hello, world!" output. If I removed the " `echo test124!`; " line from try.pl, then I could get the result.
It seems to me the backtick interferes with "system" and screwed up the STDOUT.

Can someone help me on this problem?

Thanks!

Replies are listed 'Best First'.
Re: Problem with backtick?
by ikegami (Patriarch) on Nov 21, 2007 at 20:42 UTC
    Not likely. I'd look elsewhere. Such as your make tool. After all, the whole purpose of make is to avoid running programs. What happens if you run perl try.pl without your make tool?
Re: Problem with backtick?
by tuxz0r (Pilgrim) on Nov 21, 2007 at 22:54 UTC
    I think ikegami points you correctly to your make program. If I run your example, it works just fine using GNU make on my OS X:
    $ make perl try.pl Hello, world!
    You might want to check that you have literal 'tabs' in your Makefile on the line after your 'all:' target (spaces won't do). And, did you try to run perl try.pl without the Makefile? What did it do for you?

    ---
    echo S 1 [ Y V U | perl -ane 'print reverse map { $_ = chr(ord($_)-1) } @F;'
    Warning: Any code posted by tuxz0r is untested, unless otherwise stated, and is used at your own risk.

      You might want to check that you have literal 'tabs' in your Makefile on the line after your 'all:' target (spaces won't do). And, did you try to run perl try.pl without the Makefile? What did it do for you?
      It is indeed tab in the makefile. I ran perl try.pl without Makefile and it worked fine. It doesn't work only with cygwin make.
Re: Problem with backtick?
by okram (Monk) on Nov 21, 2007 at 21:08 UTC

    Hi

    Sorry I misunderstood your question on my previous post.

    From perldoc system:

    This is not what you want to use to capture the output from a command, for that you should use merely backticks or qx//, as described in "`STRING`" in perlop.

    To read both a command's STDOUT and its STDERR separately, it's easiest to redirect them separately to files, and then read from those files when the program is done:

    system("program args 1>program.stdout 2>program.stderr");

    Original submission:

    Hi

    The following code doesn't print anything on the screen:

    `echo test`;
    The following, instead, does:
    print `echo test`;

    The backticks (man perlop) just spawn a new process, execute the stuff, and capture the output on the left-hand-side variable.

    Example:

    my $output = `echo 123!`;
    print $output

    Basically, what has happened to your code is that the string got echoed on _another_ process, whose output you never bothered printing.

    Hope this helps

Re: Problem with backtick?
by iPerl (Initiate) on Nov 22, 2007 at 15:26 UTC
    The example works on Mac OS X and Linux. But it doesn't work on cygwin with ActivePerl 5.8.8.

    What is interesting is that by removing the line " `echo test124!`; " from try.pl then it begins to work.

    My feeling is that backtick somewhat changes the behavior of the pipe? Is it possible that this is a ActivePerl bug or cygwin bug?

    Thanks!