in reply to Re^2: Execute command with spaces in perl
in thread Execute command with spaces in perl
The '-|", LIST pipe seems not working on Windows.it again breaks splitting on the spaceopen my $fh2, "$cmd|" or die $!;
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Execute command with spaces in perl
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 | |
by haukex (Archbishop) on Aug 28, 2018 at 10:27 UTC |