Well, you've got several problems with that. I almost don't know where to start... what stood out to me immediately was that you are making your deparse function return different types of things at different times. Here it is supposed to return a key, there it is supposed to return a reference to your file-scoped %Config variable, and over here and here it is supposed to just return; (which is the same as returning undef or, in list context, an empty list.)

The core issue is that this probably isn't the right place to try to use recursion. Recursion is great where the the data structure itself is recursive. Yours isn't because different levels within the structure contain data that is used for different purposes. Your elements aren't homogenous.

Just use an iterative approach to parsing it. It is much more straight forward. Here's my quick try at intrepreting your intentions:

sub deparse { my $tree = shift; my $deparsed = {}; for my $aref (@$tree) { for my $section (@$aref) { my $hash = $deparsed->{$section->[0]} = { }; # $hash is a +shortcut for my $aref (@{$section->[1]}) { $hash->{$aref->[0]} = [ $aref->[1] ]; if (my $comment = $aref->[2]) { $comment =~ s/^\s+[;#]//; push @{$hash->{$aref->[0]}}, $comment; } } } } return $deparsed; }
Since some of your array refs seem to be useless containers, you might be able to get away with fewer levels of nested fors. If it were my project, I'd almost certainly try to make it a bit shallower...

-sauoq
"My two cents aren't worth a dime.";

In reply to Re: How do I set up the recursion? by sauoq
in thread How do I set up the recursion? 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.