in reply to Nesting below emptiness and/or inverting a hash.

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