malloc has asked for the wisdom of the Perl Monks concerning the following question:

I am having some trouble grasping the concept of perl -i. perhaps we could discuss the most efficient/elegant way to open a file and replace all occurences of the word foo with bar, using only one Open.. right now i have to do this:
open(FH,"myfile") or die "e tu brutus?\n"; # clobber it. while(<FH>) if ($_ =~ m/foo/){ $line = $_; $line =~ s/foo/bar/g; } else{ $line = $_; } $file = $file . $line; } open (FH,"> myfile") or die "e tu brutus?\n"; print FH $file
can we discuss how to do this with the -i modifier, if this is the right tool, or how other people handle this common task? Your wisdom is much appreciated. -malloc

Replies are listed 'Best First'.
Re: perl -i
by jepri (Parson) on May 24, 2001 at 19:40 UTC
    Strangely enough I posted an example using -i yesterday and merlyn showed a better way to do it in this thread.

    -i is very handy for quickly doing search and replaces on files. Use it with -pe's/something/something else/g'. I'm sure there are many more tricks though.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

Re: perl -i
by danger (Priest) on May 24, 2001 at 19:40 UTC

    The -i switch is usually used for one-liner in-place file edits, combined with the -p switch. So, to substitute 'foo' with 'bar' in a file, from the commandline:

    perl -pi -e's/foo/bar/g' filename # or, to leave a backup with a .bak extension: perl -pi.bak -e's/foo/bar/g' filename
Re: perl -i
by chipmunk (Parson) on May 24, 2001 at 21:59 UTC
    -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;
      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