|
|
| laziness, impatience, and hubris | |
| PerlMonks |
Re: modifying and overwriting a fileby bjelli (Pilgrim) |
| on May 17, 2001 at 12:16 UTC ( #81186=note: print w/ replies, xml ) | Need Help?? |
|
In the perl man-pages "modifying and overwriting a file" is called inplace-editing. Perl has special provisions for that. chipmonk explained the usage on the command line in his previous article. If you don't want to use the command line you can use some built-in variables. Quoting from perlvar: to inplace-edit several file, you push them onto @ARGV, set $^I if you want to have backups of the files, and then just read from <> and write to STDOUT like so:
This creates backups of the original files as a side effect (the backups are called 'firstfile.txt.bak' and 'secondfile.txt.bak') BEWARE! If you run the program again, you add another underscore, and loose the backup. Avoiding that is left as an exercise to the reader :-) -- Brigitte 'I never met a chocolate I didnt like' Jellinek http://www.horus.com/~bjelli/ http://perlwelt.horus.at
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||