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:

$VAR1 = bless({ text => bless ([ bless ([ bless ([ '[', 'value1', ']' ], 'Config::Yapc::Deparse::header'), [ bless ([ bless ([ 'key1', '=', 'value1' ], 'Config::Yapc::Deparse::pair') ], 'Config::Yapc::Deparse::assign'), bless ([ bless ([ 'key2', '=', 'value2' ], 'Config::Yapc::Deparse::pair'), bless ([ ';', 'comment2' ], 'Config::Yapc::Deparse::comment'), ], 'Config::Yapc::Deparse::assign'), bless ([ bless ([ 'key3', '=', 'value3' ], 'Config::Yapc::Deparse::pair') ], 'Config::Yapc::Deparse::assign') ] ], 'Config::Yapc::Deparse::section'), bless ([ bless ([ '[', 'value1', ']' ], 'Config::Yapc::Deparse::header'), [ bless ([ bless ([ 'key4', '=', 'value4' ], 'Config::Yapc::Deparse::pair') ], 'Config::Yapc::Deparse::assign'), bless ([ bless ([ 'key5', '=', 'value5' ], 'Config::Yapc::Deparse::pair') ], 'Config::Yapc::Deparse::assign'), bless ([ bless ([ 'key6', '=', 'value6' ], 'Config::Yapc::Deparse::pair'), bless ([ ';', 'comment6' ], 'Config::Yapc::Deparse::comment'), ], 'Config::Yapc::Deparse::assign') ] ], 'Config::Yapc::Deparse::section') ], 'Config::Yapc::Deparse::file'), name => 'text.ini' }, 'Config::Yacp' );

Or something similar, I just generated it by hand.

Then, you can define a bunch of similarly-named methods that correspond to your named rules, such as:

sub Config::Yacp::Deparse::file::deparse { ... } sub Config::Yacp::Deparse::section::deparse { ... } etc...

These methods would know how to deparse an object of its own type, calling the deparse methods of inner objects if needed. With this technique, it should be very simple to generate the type of structure that you want.

Parsing just doesn't get any easier than that. (-:


In reply to Re: P::RD action sequence by jryan
in thread P::RD action sequence by TStanley

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.