in reply to Config file parser

The purpose is for a configuration file that references earlier keys. For example:
sub=salami mayo=yes tuna=fish topping=$sub mustard=no

In this example, $config{'topping'} is set to "salami", not "$sub". The only advice I have for the script is to move the chomp until after the "next"s as there is no need to chomp lines you are bypassing. Other than that, a very nice little script.

Replies are listed 'Best First'.
RE: RE: Config file parser
by stephen (Priest) on Apr 17, 2000 at 04:40 UTC
    Neat and powerful.

    One quibble-- if you had a non-word character in there, like so:

    foo=hello fruit=apple foo.bar=tunafish color=red baz=$foo.bar
    Then it would turn it into
    baz=hello.bar
    instead of
    baz=tunafish

    Still, it's a powerful technique, and I like it. It gives config-files a Makelike capability. You could say:

    SCRIPT_DIR=/usr/local/scripts MAIN_SCRIPT=$SCRIPT_DIR/main_script.pl
    and then get:
    MAIN_SCRIPT=/usr/local/bin/scripts/main_script.pl

    You could probably answer the above objection by putting bracket support into it, like so (untested):

    $value =~ s/\$ ( {\.+?} | \w+)/$config{$2}/gx;
    That way, we could handle my previous example like so:
    foo=hello fruit=apple foo.bar=tunafish color=red baz=${foo.bar}