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.


In reply to Re: Execute command with spaces in perl by haukex
in thread Execute command with spaces in perl by naveenp5

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.