in reply to changing data

The easiest way IMO is to use the -i command like switch, provided this is all the script has to do, either on the command line
perl -i.bak file
script:
while(<>) { s/oldline/newline/g; print; }
or including the -p switch:
s/oldline/newline
where both the while loop and the print are implicit.

Or, alternatively, do it in the shebang line:

#! perl -i.bak -p s/oldline/newline/

Replies are listed 'Best First'.
Re: Re: changing data
by sauoq (Abbot) on Aug 30, 2002 at 20:31 UTC

    If this is really all you are doing though, you might consider putting it all on the command line.

    perl -i.bak -pe 's/pattern/replacement/'

    If, however, you have decided to save your script in a file rather than just using a -e switch, then you might prefer to use the $^I special variable over the -i switch:

    #!/usr/bin/perl $^I = ".bak"; while (<>) { s/pattern/replacement/; print; }

    That way you won't forget that you have to supply the -i and clobber a file the next time you use the script. It also allows you to make the script itself executable without losing functionality. Putting it on the shebang line does too but this approach is better in that it is more flexible. For instance, you might not want to clobber file.bak if it exists but instead write to file.bak.N (where N is a sequence number) instead. Setting $^I in the script enables you to these kind of things.

    -sauoq
    "My two cents aren't worth a dime.";