http://www.perlmonks.org?node_id=719412


in reply to System call in Windows

Try the following:
#!/usr/bin/perl while (<>){ s/CAT/mouse/g; print; }

Then run the code like so:
prompt> perl -w myscript.pl 'D:\tmp\file.txt' > D:\tmp\file.txt

Update: Thanks for the reference to the -pi flags. I understand now. The while loop is superfluous with the -p and the -i will edit the file given in place. Why does he need a system() function call around the whole thing? Won't that spawn another shell?

Replies are listed 'Best First'.
Re^2: System call in Windows
by runrig (Abbot) on Oct 24, 2008 at 19:37 UTC
    If the OP fixed the quoting, then the file would be opened, read, and written to. Look at the -p and -i options in perlrun.

    Update:

    Won't that spawn another shell?

    Yes, and you could do it without spawning another shell, with a bit more code. But the -p and -i flags make the job convenient, and if the OP doesn't care about the extra process, then neither do I :-)

Re^2: System call in Windows
by csarid (Sexton) on Oct 24, 2008 at 19:58 UTC
    Hello krusty,
    Thanks for the suggestion. It's a good work around and I'll definitely use it in some other case. But what part of what I'm trying to do is also provide variables in the replacement so instead of CAT and mouse I would have "$var1" and "$var2".
    Thanks again!
Re^2: System call in Windows
by NateTut (Deacon) on Oct 24, 2008 at 19:54 UTC
    I myself wouldn't have wanted the overhead of a system call to start another perl interpreter, but it sounded like the OP was porting over something that had worked in UNIX to WinDoze, so I answered his basic question about that.