Sure!
local @ARGV = $file_name;
|
This sets up the file to be read in via perl's command-line magic. It has the same effect as if you had specified the file on the command line to perl. Any subsequent read from <> will come from this opened file.
|
local $^I = '';
|
This has the same effect as the -i command-line switch to perl. It means that when a file is opened for reading via the @ARGV magic, then a subsequent print will go to that same file. Hence "edit in place". If you'd like to make a backup of the original file, use some value other than the empty string shown here. The string you give will be used as the extension for the backup. E.g. '.bak' Tip o' the hat to sauoq.
|
local $/; # sluurp
|
This sets $/ to an undefined value, thus turning off automatic per-line input reading. Any subsequent read from <> (in scalar context) will read all the remaining available input, instead of just the next line.
|
local $_ = <>;
|
Here, we finally get around to reading the input. Because of the first and third lines above, $_ will get the entire contents of the named file.
|
while ( chomp ) { }
|
Remember that chomp() returns the number of characters chomped off. Since you want to remove any/all newlines at the end of the file, we keep chomping until chomp says it wasn't able to chomp any more.
|
print;
|
When all that is done, print the contents of $_ back out to the file, by virtue of $^I being defined.
|
Finally, we want to make sure that all those global variables we used aren't munged from the perspective of any other code. (For example, you might have good reason for @ARGV to keep its original values that you passed on the command line.) Therefore, we use local to localize all our changes to the block specified by the curly braces.
|
I hope that's clear. If you'd like any further clarification, just ask!
| [reply] [d/l] [select] |
Thats perfect!
Thank you very much!
| [reply] |
Does the .bak file need to be removed in the end or does it get removed automatically?
| [reply] |
The .bak file does not get removed automatically.
If you need it to be removed, then you have to arrange for that yourself.
jdporter The 6th Rule of Perl Club is -- There is no Rule #6.
| [reply] |