Ok, I'm rusty as hell and am stumped as to the best way to accomplish this. I need to parse a fairly complex config file a create a hash based on the contents.

The only argument to this script should be the path to the libnames.parm file. This script will then parse that file and validate the paths that have been defined within it.

The goal is to create a hash array of every library in the libnames.parm file. We have to account for every option.

The structure will, hopefully, end up looking like:

'domain1' => [ { 'libname' => 'foo', 'pathname' => '/path/to/metadata', 'owner' => 'someuser', 'libaclinherit' => 'yes|no', 'dynlock' => 'yes|no', 'options|roptions' => [ { 'datapath' => [ '/data/path01', '/data/path02', '/data/path03', ... ], 'indexpath' => [ '/indx/path1', '/indx/path2', '/indx/path3', ... ], 'workpath' => [ '/work/path1', '/work/path2', '/work/path3', ... ], 'metapath' => [ '/meta/path1', '/meta/path2', '/meta/path3', ... ] } ] } ], 'domain2' => [ { 'libname' => 'bar', 'pathname' => '/path/to/metadata', 'owner' => 'someuser', 'libaclinherit' => 'yes|no', 'dynlock' => 'yes|no' 'options|roptions' => [ { 'datapath' => [ '/data/path01', '/data/path02', '/data/path03', ... ], 'indexpath' => [ '/indx/path1', '/indx/path2', '/indx/path3', ... ], 'workpath' => [ '/work/path1', '/work/path2', '/work/path3', ... ], 'metapath' => [ '/meta/path1', '/meta/path2', '/meta/path3', ... ] } ] } ]
this would have to be generated from the config file with the format:
libname=foo pathname=/path/to/metadata/foo owner=someuser libaclinherit=no dynlock=no
   roptions="
       datapath=('/data/path1'
                 '/data/path2'
                 '/data/path3'
                 ...)
       indexpath=('/indx/path1'
                  '/indx/path2'
                  '/indx/path3'
                  ...)
       workpath=('/work/path1'
                 '/work/path2'
                 '/work/path3'
                 ...)
       metapath=('/meta/path1'
                 '/meta/path2'
                 '/meta/path3'
                 ...)";

libname=bar pathname=/path/to/metadata/bar owner=someuser libaclinherit=no dynlock=no
   roptions="
       datapath=('/data/path1'
                 '/data/path2'
                 '/data/path3'
                 ...)
       indexpath=('/indx/path1'
                  '/indx/path2'
                  '/indx/path3'
                  ...)
       workpath=('/work/path1'
                 '/work/path2'
                 '/work/path3'
                 ...)
       metapath=('/meta/path1'
                 '/meta/path2'
                 '/meta/path3'
                 ...)";
This parsing has to be able to handle the fact that everything after 'pathname' is optional. The simplest entry being:

libname=foobar pathname=/path/to/metadata;

The most complex is the examples above. It should also be flexible enough to handle new options without the need to recode the parser (dynamic hash creation).

Each block of the config begins with libname and ends with ';'. I've managed to parse it into blocks and dump the entire block into an array (libname=foo...;, libname=bar...;).

I can handle the simple case well enough...it's a simple split on =...it's the roptions part that has me stumped. I'm hoping for something elegant (I can brute force it I know know perl can do this more nicely)...when first looking at the config file I thought, this will be easy...how many late nights have begun with that statement?

In reply to Parsing a complex config file by solitaryrpr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.