use strict; use warnings; my $fileStr = < $fin}; $obj->parseStructure($fin); $obj->parseBlock('foo'); $obj->parseBlock('wibble'); $obj->parseBlock('bar'); sub parseStructure { my ($self) = @_; my $currBlock; while (1) { my $start = tell $fin; my $line = <$fin>; next if defined $line && $line !~ /^=(\w+)/; $self->{blocks}{$currBlock}{length} = $start - $self->{blocks}{$currBlock}{start} if defined $currBlock; $currBlock = $1; $self->{blocks}{$currBlock}{start} = $start if defined $line; last if eof $fin; } } sub parseBlock { my ($self, $blockType) = @_; if (!exists $self->{blocks}{$blockType}) { print "No '$blockType' blocks in file\n"; return; } seek $self->{fin}, $self->{blocks}{$blockType}{start}, 0; read $self->{fin}, my $block, $self->{blocks}{$blockType}{length}; print "$blockType:\n$block\n"; } #### foo: =foo the foo section wibble: =wibble The wibble section No 'bar' blocks in file