I'm using the Config::Hosts to make updates to the /etc/hosts file. My script runs under my regular user account and so it is unable to write to the /etc/hosts which is owned by root.
Just a few extra notes:
- You don't want to write critical system files like /etc/hosts directly. Imagine your program being killed or crashing in the middle of writing the file. You will end up with an incomplete, or worse, an unparseable file. The sane way of updating critical files is to write to a temporary file in the same directory, and then rename that temp file to the destination file. Rename is atomar (at least on local filesystems), so it is done either completely or not at all.
- Updating /etc/hosts generally sounds wrong. /etc/hosts should be almost empty, hostnames should be resolved using DNS. Updating /etc/hosts was proven not to scale in the early 1980s.
- Config::Hosts is old and probably unmaintained code, written in an even older style, using bare word file handles (without local *HANDLE, to make it worse), two-argument open, non-working prototypes for methods, print STDERR instead of warn or die. And without digging into details, it seems to do a lot of unnecessary work and keeps data in RAM at least twice. Plus, it directly overwrites the hosts file instead of using a temp file and an atomar rename.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)