Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Process an inline config file into a hash
#####################################################################################
# This code takes a config file that contains a single line of data that has all of
# the needed values separated by a pipe (|) character and parses each line
# into an array. Then, each element of the array is assigned to its respective key
# in the hash.
#
# Here is an example of what the config file should look like:
#
# /export/home/partd001/|subconnector|rck6701ftps02.bcbsmamd.net|sbsbprod|ty5jk3h
#
#####################################################################################
use strict;
use warnings;
# Open the config file into a file handle
open(CFGFILE, "<test2.cfg");
# Initialize the variables you will be using in the code
my $key;
my $value;
my @cfgvalues;
my %config;
# Read line from the config file into an array, splitting the line on the
# pipe character.
while(<CFGFILE>)
{
chomp($_);
@cfgvalues = split(/\|/, $_);
}
# Assign key/value pairs to the hash using the elements of the array created
# above.
$config{'ROOTDIR'} = $cfgvalues[0];
$config{'CLIENT'} = $cfgvalues[1];
$config{'TWSERVER'} = $cfgvalues[2];
$config{'TWUSER'} = $cfgvalues[3];
$config{'TWPWD'} = $cfgvalues[4];
# This just prints out the entire has in key/value paired lines
for $key ( keys %config) {
$value = $config{$key};
print("$key => $value \n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment