Re: What config file for a .pm perl module ?
On Thu, Dec 27, 2001 at 07:03:38PM +0100, Eric Van Buggenhaut wrote:
> I wanted to be able to use a config file similar to /etc/passwd like:
>
> #This is the list of users needed by DBIx::Password
> #(/usr/lib/perl5/DBIx/Password.pm)
> #
> #Syntax is:
> #host:username:password:port:database:attributes:connect:driver:host
> #
> #You can use simple quotes when field contains colon(s)
>
> acs:root:op.re,13::acs:{}:'DBI:mysql:database=acs:host=localhost':mysql:localhost
> personales:root:op.re,13::acs:{}:'DBI:mysql:database=PaginasPersonales:host=localhost':mysql:localhost
>
since you have : characters within fields, it's far simpler to use
another character (e.g. pipe) as the field separator, like so:
acs|root|op.re,13||acs|{}|DBI:mysql:database=acs:host=localhost|mysql|localhost
personales|root|op.re,13||acs|{}|DBI:mysql:database=PaginasPersonales:host=localhost|mysql|localhost
TAB characters would be another good choice for field separator...or
anything which isn't going to appear within the fields.
> while (<IN>) {
> next if (/^#/ || /^$/);
> @host = m/:?([^':]*)||:?'([^']*)'/g;
> foreach (@host) {print "$_ "};
> print "\n";
> }
try:
# set up an array containing the field names in order, so that
# we can use a for loop rather than a whole bunch of assignment
# statements.
@fields = qw(hostname username password port database attributes
connect driver dbhost);
my %virtual1 = {};
while (<>) {
chomp ;
s/#.*//; # strip comments
s/^\s*|\s*$//g; # strip leading & trailing spaces
next if (/^$/); # ignore blank lines (incl. comments)
my @line = split /\|/ ;
# do whatever you need with @line
# $line[0] = hostname
# $line[1] = username
# $line[2] = password
# ...
# $line[8] = dbhost
foreach(1..8) { # loop from $fields[1]..$fields[8]
$virtual1->{$line[0]}->{$fields[$_]} = $line[$_] ;
} ;
};
close(IN);
you can verify that this does what you want by using the Data::Dumper
module. e.g. by adding something like the following lines to the script:
use Data::Dumper ;
print $Dumper($virtual1);
alternatively, use the IniConf module (in package libiniconf-perl). it
uses ugly multi-line windows style .ini configurations, but the IniConf
module parses it automatically into a hash for you. it's almost ideal
for what you want to do, if you can handle the ugliness of .ini style
configurations.
craig
--
craig sanders <cas@taz.net.au>
Fabricati Diem, PVNC.
-- motto of the Ankh-Morpork City Watch
Reply to: