8 Reads in a configuration file in a certain syntax and stores this configuration for access by scripts. Takes item names and lists of values for that item. Puts values as arrayrefs into a hash with the item names as a keys.
16 <name2> <value2> #another comment
18 #this example will put multiple values into name3
19 <name3> <value3.0> <value3.1> <value3.2>
25 Creates a new configuration object.
28 use CXGN::Configuration;
29 my $config=CXGN::Configuration->new($config_file_location);
33 Gives you back the FIRST value for the variable you ask for, using the information that was retrieved from the configuration file. If there are many values for this variable, and you want them, you must use get_conf_arrayref().
35 #example which gets the locally located location of your web checkout
36 my $basepath=$config->get_conf('basepath');
38 =head2 get_conf_arrayref
40 Gives you back an arrayref of the values for the variable you ask for, even if there is one value or no values.
42 =head2 get_conf_hashref_single_values
44 Gives you back a hash with all your items, and the first values in their lists.
48 For internal use. Gets a variable name from the configuration file and assigns a value or values to it.
52 john binns - John Binns <zombieite@gmail.com>
56 package CXGN
::Configuration
;
61 my $self=bless({},$class);
62 $self->{conf_files
}=\
@_;
65 for my $file_location(@
{$self->{conf_files
}})#these will be done in order, so later ones will override earlier ones
67 if(-f
($file_location))
70 if(open($FILE,$file_location))
72 # warn "parsing config $file_location\n";
73 while(my $line=<$FILE>)
76 my($name,$values)=$self->parse_line($line);
79 $self->{config
}->{$name}=$values;#set this element, and override any previously existing configuration setting for this element
83 else{warn"CXGN::Configuration: Cannot open file '$file_location: $!'.\n";return;}
85 #else{warn"CXGN::Configuration: File '$file_location' does not exist.\n";}
87 unless($files_found){warn"CXGN::Configuration: None of the requested configuration files were found.\n";return;}
94 $line =~ s/#.+//; #remove any comments from consideration
95 return unless $line =~ /\S/; #ignore empty lines
96 my($name,@values)=split(/[\t ]+/,$line);
97 unless(defined($values[0])){$values[0]='';}#let's make "no value" be stored as an empty string instead
98 return($name,\
@values);
100 sub get_conf
#returns a single scalar value even if this item contains more than one value. use get_conf_arrayref to get all values for this item.
103 my($requested_conf)=@_;
104 if($requested_conf and defined($self->{config
}->{$requested_conf}))
106 return $self->{config
}->{$requested_conf}->[0];
113 sub get_conf_arrayref
#returns an arrayref, even if a single value is stored
116 my($requested_conf)=@_;
117 return $self->{config
}->{$requested_conf};
119 sub get_conf_hashref_single_values
#returns a hashref with single values for all items, even if multiple values are available
123 for my $key(keys(%{$self->{config
}}))
125 $hash{$key}=$self->{config
}->{$key}->[0];