APM: One liner to get hash from an environment list
David Slimp
rock808 at davidslimp.com
Thu Jan 22 11:23:06 CST 2004
try a regex like this (works for me):
/^\s*([^#].*?)\s*=\s*['"]?(.*)['"]?/
david
On Thu, Jan 22, 2004 at 10:45:41AM -0600, Sam Foster wrote:
> >%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
>
> _______________________________________________
> Austin mailing list
> Austin at mail.pm.org
> http://mail.pm.org/mailman/listinfo/austin
--
David Slimp
rock808 at DavidSlimp.com Do you use Linux?!
http://www.DavidSlimp.com Get Counted! http://counter.li.org/
Jabber IM: rock808 at jabber.org fax: 801-858-4102
"He who desires the fruit, waters the tree." -- Nguyen Trai
More information about the Austin
mailing list