How do you manage configuration files in Perl?

2019-01-14 16:31发布

问题:

I have a program that has to read a configuration file from a PHP script, and a quick search has revealed there are dozens of ways to handle configuration files in Perl:

  • Config data in a separate file
  • AppConfig
  • Config::General
  • Config::IniFiles
  • Config::Scoped
  • Config::Simple
  • ConfigReader::Simple
  • And many others

Brian provides an overview here.

Config::Simple will work for me, since the existing file is INI-like. I am interested to know what style people regularly use in their code though?

回答1:

I quite like using YAML configuration files. I find them to be very readable and easily manipulated. They fit naturally into Perl as well as the primary constructs are Arrays and Hashes.



回答2:

One solution is to use Config::Any and let the user choose from many options. Personally, I prefer Config::General.



回答3:

One question you have to ask is what is the audience ? That is, are you happy having your target audience (config file users/modifiers) changing Perl code, or modifying a .INI-like file. That will certainly drive your decision to some degree.



回答4:

If the audience is technical (used to formatted data structures), then I like using JSON for configuration. In your program it'll work like if you used YAML, but to me it's easier to read (and we use lots of JSON anyway).

If you use JSON::XS there's a "relaxed" option that'll allow comments and trailing commas and such in the files.