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

Thanks to artist for the help to my earlier problem, but I am now stumped again. The grammar worked just fine, but now I need to figure out the actions to create the hash structure that gets passed back (actually a reference to a hash). My test script looks like:
#!/opt/perl5/bin/perl -w use strict; use Parse::RecDescent; use vars qw ($grammar); $::RD_HINT=1; use Data::Dumper; $grammar = q( file: section(s) section: header assign(s?) header: '[' /\w+/ ']' { $item[2] } assign: pair comment(?) pair: /\w+/ '=' /\w+/ { [$item[1],$item[3]] } comment: /[\#\;]/ /.*/ { $item[2] } ); my $parser= Parse::RecDescent->new($grammar); my $text; { $/=undef; $text=<DATA>; } my $tree=$parser->file($text); print Dumper($tree); __DATA__ [Section1] key1=value1 key2=value2 #Comment2 key3=value3 [Section2] key4=value4 key5=value5 key6=value6 ;Comment 6
The end result that I would like to achieve, when I dump it out with Data::Dumper, would be:
$VAR1 = bless( { 'Section2' => { 'key4' => 'value4', 'key5' => 'value5', 'key6' => [ 'value6', 'Comment 6' ] }, 'INI' => 'test.ini', 'Section1' => { 'key1' => 'value1', 'key2' => [ 'value2', 'Comment2' ], 'key3' => 'value3' } }, 'Config::Yacp' );
As you can see from my test script, I've got the values I need for the header, pair, and comments, but I'm missing the file and section. When I run the test script, it basically prints out an AoAoA, with only the comments inside. All I need is just a pointer in the general direction

TStanley
--------

Replies are listed 'Best First'.
Re: P::RD action sequence
by jryan (Vicar) on May 28, 2003 at 02:21 UTC

    If I may make a suggestion, as well as solve your problem as well, try to stick with using the @item array rather than the %item hash as the docs suggest, for speed reasons. I think it is easier to work with anyways. With that in mind, you can get rid of your trailing actions and do something like this:

    BEGIN { $::RD_AUTOACTION = q { bless [@item[1..$#item]], 'Config::Yacp::Deparse::'.$item[0] }; }

    And later:

    my $tree = Config::Yapc->new('test.ini', $parser->file($text)); my $deparsed = $tree->deparse;

    $tree will now look something like:

•Re: P::RD action sequence
by merlyn (Sage) on May 28, 2003 at 11:58 UTC
      You know, I had entirely forgotten that I have a column that does nearly exactly this.

      Yeah, and I found the copy of SysAdmin magazine that has it in my desk drawer. AAAAAGGHH!!!

      TStanley
      --------
•Re: P::RD action sequence
by merlyn (Sage) on May 27, 2003 at 20:42 UTC
Re: P::RD action sequence
by adamk (Chaplain) on May 28, 2003 at 11:31 UTC
    Are you sure hand customizing a derivative of Config::Tiny wouldn't get you where you need to go, rather than the large overhead of loading RecDescent? Config::Tiny is a minimalistic way of loading a .ini style config file, and it spits out a simple hash structure. It looks VERY close to what you are doing...

    I reckon you could do it with half a dozen lines of code...

    Of course, I could have a complete wrong grasp of the task...

Re: P::RD action sequence
by Daruma (Curate) on May 27, 2003 at 21:09 UTC
    Greetings, TStanley!

    I was able to get results similar to what you are looking for by playing around with <autotree> in the grammar rule.

    Daruma