Perl cfg file processing
This is some pretty awesome code I wrote a while ago. I had to share. I removed the code that reads the config file into an array, @cfgList, which is trivial.
#-- Load cfg file into a hash --#
# How the regex's work:
# Ignore/blank out any comment lines.
# Remove any beginning space.
# Remove any trailing space.
# Capture the first word as the hash key, look for space = space, then
# capture the words in between the single quotes as the hash value.
my %cfgHash = map {
s/#.*//;
s/^\s+//;
s/\s+$//;
m/(.*?)\s*=\s*'(.*)'/;
} @cfgList;
This will load a config file into a hash. Here's a sample cfg file:
# -----------------------------------------------------------
# Read input from here:
inputDir = '/some/dir'
# Processing dir
localDir = '/other/dir'
#-- Load cfg file into a hash --#
# How the regex's work:
# Ignore/blank out any comment lines.
# Remove any beginning space.
# Remove any trailing space.
# Capture the first word as the hash key, look for space = space, then
# capture the words in between the single quotes as the hash value.
my %cfgHash = map {
s/#.*//;
s/^\s+//;
s/\s+$//;
m/(.*?)\s*=\s*'(.*)'/;
} @cfgList;
This will load a config file into a hash. Here's a sample cfg file:
# -----------------------------------------------------------
# Read input from here:
inputDir = '/some/dir'
# Processing dir
localDir = '/other/dir'
Comments
Post a Comment