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

I am opening a pipe from $cmd like so:
my $cmd="nmake NODEBUG=1"; open(RS,"$cmd 2>&1 |"); close(RS);
However it fails to pass the quoted argument if $cmd looks like this:
$cmd=qq|nmake NODEBUG=1 "ADDED_CFLAGS=/DCOST_EN_US /DPAYMENT_CREDIT_CA +RD"|;
Can someone enlighten me on what I can do to resolve this? Thanks.

-------------------------------
Need a good Perl friendly Host Provider?
http://www.dreamhost.com

Replies are listed 'Best First'.
Re: How to open a pipe from CMD if CMD has quotes?
by tuxz0r (Pilgrim) on Dec 05, 2007 at 01:02 UTC
    I'm not getting any errors just using the snippets you posted above. Can you give us the exact message it gives you when you fail? And, maybe post the second version of $cmd in context showing us how it's used? Specifically, I don't have nmake, so I wrote a little program to show the arguments it receives instead. I get this output using your snippet above:
    use strict; use warnings; my $cmd=qq|showargs NODEBUG=1 "ADDED_CFLAGS=/DCOST_EN_US /DPAYMENT_CRE +DIT_CARD"|; print "$cmd\n"; open(RS,"$cmd 2>&1 |") or die "Can't open pipe: $!\n"; while (<RS>) { print; } close(RS);
    Here's the output, showing the complete quoted second argument you had:
    showargs NODEBUG=1 "ADDED_CFLAGS=/DCOST_EN_US /DPAYMENT_CREDIT_CARD" Arguments were: 0: NODEBUG=1 1: ADDED_CFLAGS=/DCOST_EN_US /DPAYMENT_CREDIT_CARD

    ---
    s;;:<).>|\;\;_>?\\^0<|=!]=,|{\$/.'>|<?.|/"&?=#!>%\$|#/\$%{};;y;,'} -/:-@[-`{-};,'}`-{/" -;;s;;$_;see;
    Warning: Any code posted by tuxz0r is untested, unless otherwise stated, and is used at your own risk.

      I wonder if this behavior is specific to nmake? I will do some more debugging on this tonight and post what I find. Steve

      -------------------------------
      Need a good Perl friendly Host Provider?
      http://www.dreamhost.com

        Without knowing what your error was in it's specific context, I can't really point you anywhere. However, passing of command line arguments is handled by the shell (except in DOS, where I think you have to parse them out yourself), so if you are using a unix shell, your quoting of the command line is correct if you indeed intended 2 arguments. Let us know what you find with further testing.

        ---
        s;;:<).>|\;\;_>?\\^0<|=!]=,|{\$/.'>|<?.|/"&?=#!>%\$|#/\$%{};;y;,'} -/:-@[-`{-};,'}`-{/" -;;s;;$_;see;
        Warning: Any code posted by tuxz0r is untested, unless otherwise stated, and is used at your own risk.

Re: How to open a pipe from CMD if CMD has quotes?
by ikegami (Patriarch) on Dec 05, 2007 at 04:38 UTC

    What makes you think it "fails to pass"? Works fine for me.

    Makefile:

    all: echo NODEBUG=$(NODEBUG) echo ADDED_CFLAGS=$(ADDED_CFLAGS)

    655027.pl:

    my $cmd = q|nmake /s NODEBUG=1 "ADDED_CFLAGS=/DCOST_EN_US /DPAYMENT_CR +EDIT_CARD"|; open(my $fr_nmake, '-|', "$cmd 2>&1") or die; print <$fr_nmake>; close($fr_nmake);

    Output:

    >perl 655027.pl Microsoft (R) Program Maintenance Utility Version 6.00.8168.0 Copyright (C) Microsoft Corp 1988-1998. All rights reserved. NODEBUG=1 ADDED_CFLAGS=/DCOST_EN_US /DPAYMENT_CREDIT_CARD

    On a non-Windows system, you'd do:

    my @cmd = ('nmake', '/s', 'NODEBUG=1', 'ADDED_CFLAGS=/DCOST_EN_US /DPA +YMENT_CREDIT_CARD'); my $cmd = join ' ', map quotemeta, @cmd; open(my $fr_nmake, '-|', "$cmd 2>&1") or die; print <$fr_nmake>; close($fr_nmake);