Perl’s hash type is a good choice for storing a configuration database.  The keys can be names of configuration variables, and the values associated with the keys can be whatever value makes sense for that variable.  To implement a configuration file, a file with a simple “name = value” scheme can be used.  Such a file can be parsed in only a few lines of Perl code, and the result can be stored in a hash variable for later use in the script.  A system like this might be useful for a large Perl script that would otherwise require a lot of command line variables.  The following example demonstrates this:



# Default configuration

my %config = (

    size => “default”,

    place => “default”,

    color => “default”,

    subject => “default”,

    action => “default”



# Read in a config file and build the config hash

if (open my $cfg_file, “<config.cfg”) {

    while (my $line = <$cfg_file>) {

        if ($line =~ /(\w+)[ \t]*=[ \t]*(.*)/) {

            $config{$1} = $2;



    close $cfg_file;



# The script can use values from the config file

print “I went to the $config{place}.\n”;

print “I saw a $config{size} $config{subject}.\n”;

print “It was $config{color}.\n”;

print “It was $config{action}.\n”;

The first section defines a configuration hash with default values.  The next section tries to open the configuration file and reads it line-by-line.  It defines a regex that will match a ‘key = value’ string and matches it to the line.  For every match, it adds the key and value to the configuration hash.  The last part shows how to the configuration hash can be used in a program.