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

This statement works: system("./TVT.exe Test/W001#00100/TestReport_W001#00100_110327_235516.pdf >test.txt"); When I use a string reperesenting the name like this: system("./TVT.exe $a >test.txt"); It prints the data to the console and makes a 0kbyte test.txt file, an empty file. I made double sure the file name in my $a matches by doing a print, and I can see the program is running on the correct file, but it seems that the expression is being evaluated differently, or in a different order. Any suggestions on how to get it to print to the text file while using the $a string?

Replies are listed 'Best First'.
Re: redirecting output from a system command to a text file
by cdarke (Prior) on Apr 19, 2011 at 19:59 UTC
    $a is a bad choice of variable names (as would $b), since it is a magic variable used in custom sorts, however I very much doubt if that is an issue here.

    The rediection should work, and the creation of the file shows that the shell (probably cmd.exe) is creating the output file. A few things spring to mind that can create issues with cmd.exe:
    Does the command-line (including $a) contain any quotes (other than the outer " used for Perl interpolation)?
    Does the parameter in $a contain any imbedded whitespace?

    You might like to replace the ./TVT.exe with echo to eliminate these issues. If that gets redirected correctly then you might need to look at the program. For example, is it writing to STDERR?

    Update: I have done some experimenting, and cmd.exe does not seem particularly happy with the ./ prefix to the exe. I know you say it worked previously, but try removing it (it is not needed in Windows because the current directory is searched, whether it is in the %path% or not).
      Actually, I got it to work by putting ticks around the $a: system("./TVT.exe '$a' >test.txt"); I am not quite sure why that worked, but it does.
        I just noticed that you are running under Cygwin in one of your replies, so the stuff about the current directory and %path% does not apply.

        There must be a char in the filename that the shell finds interesting.
      In this instance the entire name is: Test/W001#00100/TestReport_W001#00100_110327_235516.pdf When I removed the "./" it says it can't find the command.
Re: redirecting output from a system command to a text file
by believer (Sexton) on Apr 19, 2011 at 18:25 UTC
    It should not make a difference. Are you sure that there are no "bad" characters in $a? What is the output of
    print "./TVT.exe $a >test.txt";
      when I do print"./TVT.exe $a >test.txt"; It prints exactly the path and the name of the file I want, plus it has TVT.exe in front of it. I agree, it shouldn't make any difference.
        But what is the name of the file? I'm guessing that your shell doesn't like "#", maybe treating it as the start of a comment.
Re: redirecting output from a system command to a text file
by mellon85 (Monk) on Apr 19, 2011 at 18:21 UTC
    You are on Win and using bash redirection? I'm not familiar at all with the Win environment. As a stupid workaround I would just do
    open my $input "./TVT.exe Tes...._235516.pdf|"; open my $output "test.txt" foreach (<$output>) { print $output,$_; }
      Hmm, the problem with that is i need to use the $a variable like this: open my $input "./TVT.exe $a|"; "string found where operator expected" there. I am runniing this on an xp machine using a cygwin console.
        Missing comma,
        open my $input,"TVT.exe Tes...._235516.pdf|" or die "$!";
        there might be an issue with ./
Re: redirecting output from a system command to a text file
by mark4444az (Sexton) on Apr 19, 2011 at 22:03 UTC
    OK, I got it. I had a newline at the end of my $a. I used chomp to get rid of it. Works great now. Thanks for all the feedback.