wayloud has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a config file for an application. In the config file is something like this:
<SITE> SERVERNAME1:PATH_TO_LOGFILE SERVERNAME2:PATH_TO_LOGFILE </SITE>
that then causes the application to go get the logfiles off of SERVERNAME1 and 2. What I then want is those files deposited into a directory

/data/SITE

What is killing me is how to quickly and simply extract the < and > from the opening of the config section. Any suggestions?

Replies are listed 'Best First'.
Re: Extracting data from Apache style config files
by VSarkiss (Monsignor) on Aug 13, 2004 at 00:06 UTC
Re: Extracting data from Apache style config files
by Joost (Canon) on Aug 13, 2004 at 00:04 UTC
    Your data format looks like XML without enough markup. You can probably use XML::Simple on it, but I'd fix the markup first:

    <site> <server name="servername1" path="/path/to/logfile"> <server name="servername2" path="/path/to/logfile2"> </site>
    or something like it.

    Alternatively, use some other data format, like YAML:

    --- #YAML:1.0 - Logfile: '/path/to/logfile' Server: Servername1 - Logfile: '/path/to/logfile2' Server: Servername2

    Which was generated from

    use YAML; DumpFile("config.yaml",[ { Server => 'Servername1', Logfile => '/path/to/logfile'}, { Server => 'Servername2', Logfile => '/path/to/logfile2'} ]);
    YAML::LoadFile will read that back.

    There are countless alternatives in CPAN.

    update: I missed the title of your post. Yes there is a module on cpan for parsing apache style config files as VSarkiss noted.

    I would not recommend you go that route, though. The apache config file syntax is one of its few weaknesses IMO. Please consider a format that is easier to parse and understand.

Re: Extracting data from Apache style config files
by dragonchild (Archbishop) on Aug 13, 2004 at 01:24 UTC
    All of my config files are in that format and I use Config::ApacheFormat.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: Extracting data from Apache style config files
by johnnywang (Priest) on Aug 13, 2004 at 00:09 UTC
    I assume you just want the name/path pairs:
    use strict; my $config = "path_to_your_conf_file"; open(CONFIG, "<$config") or die "Can't open $config\n"; while(<CONFIG>){ if(/(\w+):(\w+)/){ # now do your copy, # there is some work, if these are in different servers. } } close(CONFIG);