Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: External Program Arguments Problem

by Stevie-O (Friar)
on Dec 22, 2004 at 03:09 UTC ( #416685=note: print w/ replies, xml ) Need Help??


in reply to External Program Arguments Problem

I believe that Perl uses cmd.exe under NT kernels for its shell.

Unfortunately, '\"' is not the correct way to escape a quote symbol in the command line. '^"' is.

The world of Win32 gets a little weird when you start to deal with running other processes.

--Stevie-O
$"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc


Comment on Re: External Program Arguments Problem
Download Code
Re^2: External Program Arguments Problem
by ZlR (Chaplain) on Dec 22, 2004 at 13:55 UTC
    One can also use Win32::Process::Create .
    Perl 5.8 has Win32::Job
    It rocks since you can specify filehandles for redirecting the output of the spawned process.

      Ahh, thanks Stevie-O. This does seem to be a problem with cmd.exe - I get the same results when I use it directly. Unfortunately, ^" doesn't seem to work correctly, nor does "". Very frustrating. Is there any way to compile Perl on my Windows environment to use the Cygwin bash shell instead?

      That looks interesting, ZlR. I'll check out those modules and see if I can easily change my program to use them. I wasn't really looking to redirect the output to a filehandle, though -- just capture it in a variable. I'm calling an external program to set the music tags on MP4 files that I'm processing, so I wanted to capture its error messages in case the program failed. This problem arose for some song names and artists that contained a " (usually 12" mix or something similar).

Re^2: External Program Arguments Problem
by Anonymous Monk on Dec 23, 2004 at 02:31 UTC

    Okay, Google'd and tested out a few things. It turns out I can get it to work in cmd.exe by replacing " with "^"" (all four characters). This works both from the cmd.exe shell and calling it from Perl:

    #### arg_test.pl #### my $output = `print_args.pl "Test" "Hi (12"^"" mix)" "test.txt" 2>&1`; print "$output";
    C:\scripts>print_args.pl "Test" "Hi (12"^"" mix)" "test.txt" 2>&1
    Arg 0 - Test
    Arg 1 - Hi (12" mix)
    Arg 2 - test.txt
    
    c:\scripts>arg_test.pl
    Arg 0 - Test
    Arg 1 - Hi (12" mix)
    Arg 2 - test.txt
    

    However, this isn't consistent for other external programs. I wrote a simple C program to do the same thing as print_args.pl. Replacing " with "^"" in parameters still works from the command line, but now it fails when running it from Perl.

    // PrintArgs.exe #include <stdio.h> int main(int argc, char **argv) { for (int i=0; i<argc; i++) { fprintf(stderr, "Arg %d: %s\n", i, argv[i]); } return 0; }
    #### arg_test.pl #### my $output = `PrintArgs.exe "Test" "Hi (12"^"" mix)" "test.txt" 2>&1`; print "$output";

    Here's the output I get running it directly in the shell and then from Perl:

    C:\scripts\arg_problem>PrintArgs.exe "Test" "Hi (12"^"" mix)" "test.txt" 2>&1
    Arg 0: PrintArgs.exe
    Arg 1: Test
    Arg 2: Hi (12" mix)
    Arg 3: test.txt
    
    C:\scripts\arg_problem>perl_test.pl
    Arg 0: PrintArgs.exe
    Arg 1: Test
    Arg 2: Hi (12^
    Arg 3: mix) test.txt 2>&1
    

    This is killing me. How is the Perl-shell interaction fine in the first case but not the second? Is Perl using the exact shell on my system (c:/windows/system32/cmd.exe) or its own version? Could it be processing the parameters in some way before passing them off to the shell?

    I'd really appreciate any ideas on what's going on.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://416685]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (13)
As of 2014-10-22 07:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (114 votes), past polls