in reply to forgetting the simple stuff?

It is possible to open a file for both reading and writing, but truth be told, it is hardly ever what you want. Basically, you can only write as many bytes as you read. If you try and read "bb" and replace it with "cccccc" you are going to clobber the four bytes that come after "bb" in the file. The bytes don't shift over and make room for you.

The standard way of doing this is indeed to open a file with a different name, and then move it back over the old one when you have finished (assuming you consider that all has gone well). You can get perl to handle this automatically with the -i switch, otherwise, you can do it yourself.

Another simple thing you are forgetting to do is not checking whether your opens are working correctly, or failing silently. Check the results of the $! variable.


print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
  • Comment on Re: forgetting the simple stuff? (-i and $!)