APM: One liner to get hash from an environment list

Sam Foster austin.pm at sam-i-am.com
Thu Jan 22 10:45:41 CST 2004


> %config = map { /(.*?)=['"]?(.*)['"]?/ } qx(cat /etc/defaults/some_prog_env.sh);
> 
> This should parse any valid bourne shell syntax key=value config file
> into a hash.

but if you want to allow #comments, blank lines etc, you might end up 
with something like this?

my %config;
foreach ( qx(cat /your/input/file) ) {
	next if /^\s*#/;	# skip commented lines

	# match for name=value pairs, allowing for indentation and
	# options whitespace around the '='.
	# We should really use a backreference to pair up the quotes?
	next unless ( /\s*(.*?)\s*=\s*['"]?(.*)['"]?/ );
	$config{$1} = $2;
}

Can you combine the regexps to get this back on one line? (for those 
who take joy from this sort of thing?)

Sam




More information about the Austin mailing list