in reply to Execute command with spaces in perl

Your quoting does not match how the Windows shell (cmd.exe) processes quotes.

cmd.exe does not recognize a backslash before a space as the intention to quote that space. A working invocation of firefox.exe could be:

my $ff_exe = q{C:\\Program Files\\Mozilla Firefox\\firefox.exe}; my @cmd = ($ff_exe, '--version'); my $shell_command = join " ", # separate by spaces map { /\s/ ? qq{"$_"} : $_ } # enclose in double q +uotes if needed @cmd; print $shell_command;

Note that under Windows, the --version command line switch does not output anything.

Replies are listed 'Best First'.
Re^2: Execute command with spaces in perl
by pryrt (Abbot) on Aug 27, 2018 at 15:52 UTC

    Interestingly, it doesn't output anything normally (to the cmd.exe window), but it does output when the output is piped or redirected:

    C:\usr\local\share\PassThru\perl>"C:\Program Files\Mozilla Firefox\fir +efox.exe" --version C:\usr\local\share\PassThru\perl>"C:\Program Files\Mozilla Firefox\fir +efox.exe" --version | perl -e "print $_ for <>" Mozilla Firefox 61.0.2 C:\usr\local\share\PassThru\perl>"C:\Program Files\Mozilla Firefox\fir +efox.exe" --version > out.txt C:\usr\local\share\PassThru\perl>type out.txt Mozilla Firefox 61.0.2

    I actually first noticed it inside perl, using the piped open described elsewhere, while writing Re^5: Execute command with spaces in perl:

    C:\usr\local\share\PassThru\perl>perl -Mwarnings -Mstrict -e "my $cmd += q|C:\Program Files\Mozilla Firefox\firefox.exe|; open my $fh, '-|', + $cmd, '--version' or die $!; my $out = do { local $/; <$fh> }; close + $fh or die $! ? $! : qq(\$?=$?); print $out" Mozilla Firefox 61.0.2