use strict; use Carp; my @objects = get_objects("data"); print join "\n\n", @objects; sub get_block { my $a_ref = shift; my $open = 0; my @read; while (my $line = shift @$a_ref) { push @read, $line; $open += () = ($line =~ /\{/g); $open -= () = ($line =~ /\}/g); if (0 == $open) { return join '', @read; } elsif (0 > $open) { my $not_block = join '', @read; warn("Too many closing parentheses in:\n$not_block\n\n"); unshift @$a_ref, @read; return; } } confess "Unclosed brace at end of file\n"; } # Takes a filename, returns the contents. sub get_file { my $file = shift; local *IN; open (IN, "< $file") or confess "Cannot read '$file': $!"; if (wantarray) { return ; } else { return join '', ; } } # Takes the name of a pil-file and returns an array of object blocks. # Not particularly robust. sub get_objects { my @objs; my @lines = get_file(shift); while (my $line = shift @lines) { if ($line =~ /object\s*\{/) { # Found a block? unshift @lines, $line; push @objs, get_block(\@lines); } } return @objs; }