in reply to MS XAct XAP Data file parsing

The 'structure' is just a typical object that embeds copies of itself. You typically use recursion to navigate such an object. Such a structure is often called a a tree. If you think about it the file system is a direct parallel with your structure. The properties are analogous to files and the subobjects are analogous to directories. Search CPAN for Tree to see lots of options.

The answer to your aside in literal form is no. If you store a literal value that is what you store. You could store a reference and possibly dig up the info. The question would be why?

The data structure you show could be represented in perl like this (there are other options). Perl can manipulate it. The advantage of embeded objects is that you can recursively call methods on the objects. Data::Dumper Storable FreezeThaw or YAML will all let you serialize it into a stream you can write to disk, read from disk and use to restore your object. I use Storable mostly.

package Object; sub new { bless { _props => {}, _objs => [] }, shift }; sub add_prop { my ( $self, $prop, $value ) = @_; $self->{_props}->{$prop} = $value; } sub add_subobject { my ( $self, @obj ) = @_; push @{$self->{_objs}}, @obj; } my $base = new Object; my $sub1 = new Object; my $sub2 = new Object; my $subsub = new Object; $sub1->add_prop( sub1 => 1 ); $sub2->add_prop( sub2 => 1 ); $subsub->add_prop( deep => 1 ); $sub1->add_subobject( $subsub ); $base->add_prop( foo => 1 ); $base->add_prop( bar => 2 ); $base->add_subobject( $sub1, $sub2 ); use Data::Dumper; print Dumper $base; __DATA__ $VAR1 = bless( { '_objs' => [ bless( { '_objs' => [ bless( { '_objs' = +> [], '_props' +=> { + 'deep' => 1 + } }, 'Object' + ) ], '_props' => { 'sub1' => 1 } }, 'Object' ), bless( { '_objs' => [], '_props' => { 'sub2' => 1 } }, 'Object' ) ], '_props' => { 'foo' => 1, 'bar' => 2 } }, 'Object' );

cheers

tachyon

Replies are listed 'Best First'.
Re^2: MS XAct XAP Data file parsing
by Anonymous Monk on Jan 15, 2020 at 19:50 UTC

    Here is a naive parse function that receives reference to trimmed file lines.

    sub ParseXAP { my $lines = shift; my @props = (); my @children = (); while( scalar( @{$lines} ) ) { my $line = shift @{$lines}; if( length($line) ) { last if $line eq '}'; if( $line =~ /(.*) = (.*);/ ) { push @props, { 'name' => $1, 'value' => $2 }; } else { my $type = $line; $line = shift @{$lines}; die unless $line eq '{'; my $child = ParseXAP( $lines ); $child->{type} = $type; push @children, $child; } } } return { 'props' => \@props, 'children' => \@children }; }