in reply to Saving application configuration to files
Here's one really simple way to read in a commonly used config format. This version requires the double quotes around all values. You could modify it to suit your needs (ie: no quotes needed, give warnings/errors if it comes across an invalid config line, etc) but as it is, it works just fine.
use strict; use warnings; my ($sec, %config); while (<DATA>) { $sec = $1 and next if /^\[([^\]]+)\]/; $config{$sec}{$1} = $2 if /^\s*([^=]+)\s*=\s*"([^"]+)"/; } =cut we now have ths following structure: %config = ( client => { host = 'localhost', port = '8080' }, server => { host => 'localhost', port => '8900', timeout => '60' } ); =cut __DATA__ [client] host="localhost" port="8080" [server] host="localhost" port="8900" timeout="60"
|
|---|