in reply to Execute command with spaces in perl
I would strongly recommend you use a module to do this for you, instead of trying to do it yourself. For example, there's capturex from IPC::System::Simple, or IPC::Run3, which uses Win32::ShellQuote under the hood on Windows:
use warnings; use strict; use IPC::Run3 'run3'; my $cmd = 'C:\Program Files\Mozilla Firefox\firefox.exe'; run3 [$cmd,'--version'], undef, \my $out or die "run3: $!"; die "\$?=$?" if $?; chomp $out;
If you want to stick with piped open, you could also use Win32::ShellQuote directly to do the quoting for you. I wrote about the above options, as well as the pitfalls with running external commands, at length here, with example code. In this case, since you're running a fixed external command with one or more arguments, you can also use the LIST form of open:
use warnings; use strict; my $cmd = 'C:\Program Files\Mozilla Firefox\firefox.exe'; open my $fh, '-|', $cmd, '--version' or die $!; my $out = do { local $/; <$fh> }; # slurp close $fh or die $! ? $! : "\$?=$?"; chomp $out;
Note how I am also checking close for errors, this is necessary for a piped open. I'm also slupring the entire output of the command into my variable $out; in your code you're only fetching the first line of output, despite that you've named your variable $aarray.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Execute command with spaces in perl
by Lotus1 (Vicar) on Aug 27, 2018 at 13:33 UTC | |
by haukex (Archbishop) on Aug 27, 2018 at 13:41 UTC | |
|
Re^2: Execute command with spaces in perl
by naveenp5 (Initiate) on Aug 27, 2018 at 12:06 UTC | |
by haukex (Archbishop) on Aug 27, 2018 at 12:52 UTC | |
by Lotus1 (Vicar) on Aug 27, 2018 at 15:01 UTC | |
by pryrt (Abbot) on Aug 27, 2018 at 15:50 UTC | |
by Corion (Patriarch) on Aug 27, 2018 at 16:07 UTC | |
by haukex (Archbishop) on Aug 27, 2018 at 15:47 UTC | |
by naveenp5 (Initiate) on Aug 28, 2018 at 06:34 UTC | |
|