The '-|", LIST pipe seems not working on Windows.
open my $fh2, "$cmd|" or die $!;
it again breaks splitting on the space

I tested my code before posting, and the '-|', LIST form should work on Windows (Update: at least on Perl 5.22 and up). If it's not working for you, please show the code you're trying and the exact error messages you're getting (How do I post a question effectively? and Short, Self-Contained, Correct Example). The code you posted here is not the '-|', LIST form, which it why it isn't working - why aren't you using the code I showed? With a single string argument, you need to do the quoting correctly; I explained the difference between the two in the node I linked to earlier.

If you need --version to be variable as well, you could do this:

use warnings; use strict; my @cmd = ('C:\Program Files\Mozilla Firefox\firefox.exe', '--version'); die "\@cmd must have more than one element" unless @cmd>1; open my $fh, '-|', @cmd or die $!; my $out = do { local $/; <$fh> }; # slurp close $fh or die $! ? $! : "\$?=$?"; chomp $out;

Note I've added the check for @cmd having more than one element to make sure the LIST form is being used.

If for whatever reason you really need the string form, then you'll have to use better quoting, like what is provided by Win32::ShellQuote, or the other modules I named.

My script should be able to execute seamlessly between Linux and Windows.

The code I showed should work fine on both OSes, except of course for the filename of the Firefox executable; if you've having trouble on Linux, again, please show exactly what that trouble is. For platform-independent filename handling, I recommend the core module File::Spec, or with a nicer interface Path::Class.


In reply to Re^3: 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.