Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: System call in Windows

by NateTut (Deacon)
on Oct 24, 2008 at 19:28 UTC ( [id://719411]=note: print w/replies, xml ) Need Help??


in reply to System call in Windows

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

Replies are listed 'Best First'.
Re^2: System call in Windows
by csarid (Sexton) on Oct 24, 2008 at 19:51 UTC
    Hello NateTut,
    Thanks that worked great! Can you tell or point me to somewhere where it explains the weirdness. Thanks again!
      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!
      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.
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://719411]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 21:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found