Although the original placeholder approach used a bit less code, it seemed nevertheless apt to create future maintainance problems and philcrow was very nearly right with his stack idea. The stack for this situation has to be wound all the way to the top and then exhaustively popped during the recursive reconstruction of the hash. It musn't contain any subtag info or this could cause the whole file to be represented in the envelope - instant self-defeat - so not all the hash slice should be represented in the stack - the stack itself has to imply that part of the structure.

Meanwhile the gettag function also acquired new functionality to pick up comments and xml version specifiers between normal tags and store them in the hash for the following tag, later to be reproduced if necessary by puttag. Because xml version is always in the envelope (if configured) the envelope stack and build functionality has to support that too:

sub PutEnvelope { my $self = shift; # obj or hashref my $tref = shift; # tag hashref to be consigned to envelope my $eref = ( $self -> { ESTACK } ||= [] ); # envelope stack my $tag = each %$tref; push @$eref, { $tag => { ASSMNTS => $tref -> { ASSMNTS }, COMMENT => $tref -> { COMMENT }, DEPTH => $self -> { THIS }{ DEPTH } } }; # note SUBTAGS are not stacked up if ( $self -> { THIS }{ DEPTH } == 1 ) { # stack now at top. Unwind it:- MakeEnvelope( $self, $self -> { ENVELOPE } ||= {} ); } } sub MakeEnvelope { # recursively build envelope hash my ( $self, $eref ) = @_; # eref = where to build it $self -> { THIS }{ ENVDEPTH } ||= 0; $self -> { THIS }{ ENVDEPTH }++; my $frame = pop @{ $self -> { ESTACK }}; my $tag = each %$frame; unless( $frame ) { $self -> { THIS }{ EBVDEPTH }--; return; } while( $frame -> { $tag }{ DEPTH } < $self -> { THIS }{ ENVDEPTH } ) { XMLerror( $self, "multiple tags found at same " ."level of envelope (i.e. tags " ."above DEPTH) - ignoring tag $tag", 'WARNING' ); $frame = pop @{ $self -> { ESTACK } }; unless( $frame ) { $self -> { THIS }{ EBVDEPTH }--; return; } } delete ( $frame -> { $tag => { DEPTH } } ); # done its job $eref -> { $tag } = $frame -> { $tag }; if ( @{ $self -> { ESTACK } } ) { MakeEnvelope( $self, $eref -> { $tag => { SUBTAGS }} [0] = {} + ); # make sub-envelope and put back SUBTAGS } $self -> { THIS }{ ENVDEPTH }--; return; }

-M

Free your mind


In reply to Re: Nesting below emptiness and/or inverting a hash. by Moron
in thread Nesting below emptiness and/or inverting a hash. by Moron

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.