I am new to Perl programming and would like to get some ideas on what is the best way to structure a piece of code in the form of Perl module(s). It should provide a framework such that the ‘data’ can grow/shrink without impacting the code.
Essentially, the program needs to perform 3 different things (‘data’ portion):
- Gather text from multiple files(entire text) depending upon which RPM (RPM name = ‘A’, ‘B’, ‘C’ respectively, for argument’s sake) is installed on the host. The list of files will grow and should be stored in a simple externalized file which should be easily editable.
- Run OS commands on the operating system (Linux) and capture the output of the commands into a text file, depending upon which RPM (RPM name = ‘A’, ‘B’, ‘C’, for argument’s sake) is installed on the host.
- Run Database commands on a specific database (lets call it ‘DBA’, ‘DBB’, ‘DBC’, 3 databases corresponding to 3 hosts) and capture the output to a text file, depending upon which RPM (RPM name = ‘A’, ‘B’, ‘C’ respectively, for argument’s sake) is installed on the host.
I need to program the logic in such a way that:
- There has to be a “RPM to file/command/DB-command” mapping which is easily editable separately from the main Perl program
- The number of files, their names/location + OS + DB commands is expected to change and therefore should be easily editable separately from the main Perl program
- The RPM name and the mapping to the above data can change and therefore should be easily editable separately from the main Perl program
Ideas that I have researched so far:
- Hash of Hashes (in a Perl module)
- Array of Arrays (in a Perl module)
- external XML
- external Key-value pair file
Any guidance on whats the easiest and simplest way to go about doing this? Illustrative code would be greatly beneficial.
How you store your configuration depends on how complex it is, and how accessible it has to be to someone who didn’t program the system.
The benefit of storing configuration as Perl code is, that parsing is quite inexpensive. However, there may be security concerns, as the config file wouldn’t be loaded, but executed. Also, it is a horrible design.
XML is quite flexible, but is (a) horrible to edit manually, and is (b) more expensive to process than other solutions[citation needed].
If your configuration data can be very easily represented as key-value pairs, go for that. Else, take a look at JSON or YAML, which will give you all the power you will need (most of the time).
I would write the system in such a way, that a main object is loaded at the start of the program. The constructor takes the RPM type as argument, and processes the appropriate configuration files.
You would then create an interface to this object so that processing text files, running system commands and running DB queries would use the correct config values.
Just make sure to make your code nice and generic, so that the main logic of the programm doesn’t need to know what RPM it is using, except at initialization.
A Perl class usually is a module at the same time. Create helper modules as needed, when it is sensible.
Example constructor:
Example usage