Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Updating Hash from a config file

by Viki@Stag (Sexton)
on Oct 12, 2007 at 13:07 UTC ( [id://644472]=perlquestion: print w/replies, xml ) Need Help??

Viki@Stag has asked for the wisdom of the Perl Monks concerning the following question:

I have a configuration file filled with some config vars & values, it looks more like this

[section_name] var=value var=value ... [section_name] var=val var=val ...

I have to parse this file & fill a hash var. I have a script to do this, but it does not work in some conditions when the new lines of the config file changes(ex: if i remove a blank line).
dont know y it happens, i need suggestion to improve my code so that it is robust...

Thanks in advance

it will update the hash in this fashion:
hash = (
section_name_0 => val,
section_name_1 => val,
...
);
The keys will have section_name followed by 0 1 2... After each section the count resets to 0(zero) & so on.

Here is the code
sub UpdateConfigHash { $config_file = $ENV{'CONFPATH'}; open( CONFFILE, $config_file ) || die "$!\n"; my @config_file_contents = <CFILE>; my $abs_path_section_flag = 0; my $section_flag = 0; my $section_name = ""; my $config_files_line = ""; my $config_files_line_ind = 0; while ( defined( $config_file_contents[$config_files_line_ind] ) ) + { $config_files_line = $config_file_contents[$config_files_line_ +ind]; trim_clean( $config_files_line ); if ( $config_files_line =~ /^(\s*)#/ ) { $config_files_line_ind++; next; } if ( $config_files_line =~ /^(\s*)$/ ) { $config_files_line_ind++; next; } if ( $config_files_line =~ /^(\d+)$/ ){ $global_settings{"comm_port"} = $&; $config_files_line_ind++; next; } if ( $config_files_line =~ /^\[abs_paths\]/ ) { $abs_path_section_flag = 1; $section_flag = 0; $config_files_line_ind++; next; } if ( $config_files_line =~ /^\[(\w+)\]/ ) { $section_name = $1; $abs_path_section_flag = 0; $section_flag = 1; $section_index = 0; $config_files_line_ind++; next; } if ( $abs_path_section_flag == 1 && $section_flag == 0 ) { # indicates abs_paths is on my @temp = split( /=/, $config_files_line ); trim_clean (\$temp[0]); trim_clean (\$temp[1]); $global_settings{ $temp[0] } = $temp[1]; $config_files_line_ind++; next; } if ( $abs_path_section_flag == 0 && $section_flag == 1 ) { # indicates a section ( not abs_paths ) my @temp = split( /=/, $config_files_line ); trim_clean (\$temp[0]); trim_clean (\$temp[1]); $global_settings{ $section_name . "_" . $section_index } = + $temp[1]; $section_index++; $config_files_line_ind++; next; } } }

Replies are listed 'Best First'.
Re: Updating Hash from a config file
by siva kumar (Pilgrim) on Oct 12, 2007 at 13:29 UTC
    Try this..
    use Config::Simple; $Config={}; $confFile = "path/to/conf/file.conf"; Config::Simple->import_from($confFile,$Config); print $Config->{"section_nameX.varX"}; print $Config->{"section_nameY.varY"};
Re: Updating Hash from a config file
by BerntB (Deacon) on Oct 12, 2007 at 13:22 UTC

    You are aware of Config::Simple and others that support that kind of format? (Init-files on Win, right?)

Re: Updating Hash from a config file
by toolic (Bishop) on Oct 12, 2007 at 13:36 UTC
    Regarding requests for suggestions to improve your code's robustness...

    My guess is that you are probably not using the strictures, since $config_file is not declared with my (at least, not in your sub):

    use warnings; use strict;

    When accessing an environment variable, it is a good practice to test if it is defined. If it is not defined, you could either die (as shown below), or assign it a default value:

    my $config_file = (defined $ENV{'CONFPATH'}) ? $ENV{'CONFPATH'} : die "Error: CONFPATH not defined.\n";

    Should <CFILE> really be <CONFFILE>?

    It is also a good practice to close a filehandle when you are done.

    These tips probably do not solve your specific problem, but hopefully they are small steps toward more robust code.

      Thanks for the suggestions...

      And also i Will try using Config::Simple, it appears to be compatible with the structure of my config file...

      Thank you Monks
Re: Updating Hash from a config file
by Sinistral (Monsignor) on Oct 12, 2007 at 13:28 UTC
    I haven't closely examined your code, but considering the ultimate task at hand (reading a configuration file), perhaps you would be very interested in Config::General or Config::Generic ?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://644472]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2025-03-20 02:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (60 votes). Check out past polls.