in reply to perl -i

-i is certainly useful in one-liners, as already mentioned, but it works fine in a full script too. The tricky bit is that -i only works on files that are processed using the magical <>. You might use it like this in your script:
#!perl -i # or: # $^I = ''; @ARGV = ('myfile'); while (<>) { s/foo/bar/; print; }
Although that could be reduced to a one-liner, if you wanted: perl -pi -e 's/bar/foo/g' myfile

 

By the way, note that these two code snippets have the same result, but one is cleaner:
if (/foo/) { s/foo/bar/g; } s/foo/bar/g;

Replies are listed 'Best First'.
Re: Re: perl -i
by malloc (Pilgrim) on May 24, 2001 at 22:57 UTC
    hmmm, the 'magical' <> operator is perplexing me, why won't it work on a file handle? and..ahh, okay, so basically all tests are done with $_, but you can change this to $anything to symbolize a line, then print $line back out in the loop and the whole line gets clobbered? Thank you soo much, this code example really cleared things up, but i still wish i could use file handles.. Just what is going on with that @ARGV ? Thanks again everyone. -malloc