Sunday 15 January 2012

perl - mapping hardcoded config files -


I searched the module for reading Config , Config :: config files Small is , config: simple . I am not very obscure about the use of those people, are there any modules for reading / reading DBI config and username / password? I have tried to do this myself, I want a config file in a hash data structure to import easy in my module. Is it an easy way to try that which I am trying / or a favorite module which can be suggested?

Example Config File:

  [database] db = newsdb host = example .com user = test pass = test [login] user = john pass = doe   

Coding:

  Use strict; Use warnings; Use the file: Slurp; Use data: Dumper; # Get the database information from my% conf = map {/ ^ \ [database /? (): $ _} Grep {/^\w+.*$/} Map {/? S? \ N? \ R? // g; (Partition / = /) [0,1]} read_file ('database.conf'); Print dumper \% Conf; $ VAR1 = {'pass' = & gt; 'Test', 'db' = & gt; 'Newsby', 'User' = & gt; 'Test', 'host' = & gt; 'Example.com'};    

Use the config module to not read configuration files Instead it gives detailed information on the configuration of your perl .

Here is an easy way to use config :: simple , and then

  config: simple-> import_from ("database. Conf "= & gt; \ my% config); Print dumper% config;   

Output:

  $ VAR1 = {'database.host' = & gt; 'Example.com', 'login.pass' = & gt; 'DO', 'Login. User '= & gt; 'John', 'Database.User ='> 'Test', 'database.db' = & gt; 'News', 'database.pass' = & gt; 'testing' }; Alternatively, to use only one block, we can do  
  my $ config = Config :: Simple-> New ("database.conf"). ) - & gt; Get_block ("database"); Print dumper $ config;   

which must be given

  $ VAR1 = {'pass' = & gt; 'Test', 'db' = & gt; 'Newsby', 'User' = & gt; 'Test', 'host' = & gt; 'Example.com'}; As output, read   

for more information.


It becomes simpler with Config :: Tiny :

  my $ config = Configure Tiny- & Gt; Read ("database.conf"); Print dumper $ config;   

give

  $ VAR1 = blessing ({'database' => {'pass' => 'test', 'db' = & Gt; 'newdib', 'user' => gtc: test = 'host' = & gt; example.com '},' login '=> {' pass '= & gt;' dow ',' Users' = & gt; 'John'}}, 'Config :: Tiny');   

Therefore the share of database

  print dumpor $ config- & gt; Can be selected with {database}  which will output  
  $ VAR1 = {'pass' => 'Test', 'db' = & gt; 'Newsby', 'User' = & gt; 'Test', 'host' = & gt; 'Example.com'};   

You can learn more in

No comments:

Post a Comment