Another cool perl module: Commands::Guarded
09 November 2005 19:21
Today while scouring CPAN for a Perl module that wrapped the 'chkconfig' command I bumped into the Commands::Guarded module. This module is based around the concept of idempotency. Idempotency is basically this: a function f(x) is idempotent if f(f(x)) = f(x). In other words, evaluating such a function more than once (and even many times) still yields the same result as evaluating it only once.
As a sysadmin, I very much like scripts and tools that are idempotent. That is, no matter how many times I run them, they always move the system towards the desired end state. Once the system is in that desired end state, running the script has no further effect. This is the main concept espoused by cfengine, in which each run of the agent brings the system towards (but no further) than the state defined by the configuration.
This module is particularly useful for sysadmins writing scripts which execute a sequence of actions, each of which depends upon the successful execution of the previous action. It makes for much cleaner and almost declarative code, in that I can define the state each step needs to effectuate, and the code required to bring that state into being. Thus if you run your script and it dies on step 3 of 20, you can run it again, and it will recognize that steps 1 and 2 have already happened, and that it needs to try step 3 again and go from there.
A good read through the module docs is highly recommended.