in reply to Re: System call in Windows
in thread System call in Windows

Hello NateTut,
Thanks that worked great! Can you tell or point me to somewhere where it explains the weirdness. Thanks again!

Replies are listed 'Best First'.
Re^3: System call in Windows
by NateTut (Deacon) on Oct 24, 2008 at 20:00 UTC
    Hmm, I don't know of a good reference on this particular issue, maybe someone else does, it is more of an operating system issue than a perl issue though. Windoze/DOS likes double quotes and *nix prefers single. I've had that particular issue bite me in the behind enough to recognize it when I see it. I would recommend though when faced with an odd problem such as this create a little test script that duplicates the problem and then just try different things. A lot of times the errors you get doing this along with simplifying it down to it's essence is enough to figure it out on your own. You get to learn something along the way as a bonus!
Re^3: System call in Windows
by runrig (Abbot) on Oct 24, 2008 at 20:12 UTC
    If the code in your original post is accurate, then you have several unquoted arguments, and I get a syntax error, even on Unix, so I'm surprised if it really does work. Fixing it comes down to passing system() a string, which means you have to escape each argument correctly for the shell (and deal with OS quoting), or passing system() an array, which means you don't have to escape any arguments. It's usually better to pass an array, when possible.
Re^3: System call in Windows
by blazar (Canon) on Oct 25, 2008 at 13:21 UTC
    Thanks that worked great! Can you tell or point me to somewhere where it explains the weirdness. Thanks again!

    I personally believe that there's no weirdness at all: system either wants a single string as an arguments list, or a several ones. In the former case, the string is parsed for shell metacharachters and if any, it is processed by the system's shell, otherwise, it is split up in "words" and executed directly by the suitable system call. The same happens in the latter case, directly. Now, your code as you posted in the root node is

    system(perl -pib -e "s/CAT/mouse/g" 'D:\tmp\file.txt');

    which would seem to fall in the second category, but most importantly contains many barewords: these should be disallowed at all if you're running under strict and you know you should run under strict except perhaps with the simplest oneliners. There should be no difference between operating systems wrt this issue. Moreover, the barewords are not separated by commas which also would make for a Perl syntax error irrespective of the OS, and strict too. All in all, you seem to think that whatever is an argument to system() doesn't need any quoting, as if perl itself were a shell, but that's not the case. You either want:

    system qq|perl -pib -e "s/CAT/mouse/g" 'D:\tmp\file.txt'|;

    (in which I used alternate delimiters on the outer string not to have to quote double quotes inside it) or

    system 'perl', '-pib', '-e', '"s/CAT/mouse/g"', q|'D:\tmp\file.txt'|;

    Incidentally, since no argument contains spaces, you may rewrite the latter like

    system qw|perl -pib -e "s/CAT/mouse/g" 'D:\tmp\file.txt'|;

    but pay attention in the general case! Whatever, in both cases you may have an OS quoting problem: you're giving perl a cmd line argument of 'D:\tmp\file.txt' which it will interpret literally as a filename, much different from D:\tmp\file.txt as you can see for yourself:

    C:\temp>perl -lpe "" foo.txt foo bar C:\temp>perl -lpe "" 'foo.txt' Can't open 'foo.txt': No such file or directory.
    --
    If you can't understand the incipit, then please check the IPB Campaign.