in reply to using sub routines and solving an issue i have with passing variables
Sounds like the sort of place I'd use really light weight objects:
use strict; use warnings; my $fileStr = <<FILE; =wibble The wibble section =plonk the plonk section =foo the foo section FILE open my $fin, '<', \$fileStr; my $obj = bless {fin => $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"; }
Prints:
foo: =foo the foo section wibble: =wibble The wibble section No 'bar' blocks in file
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: using sub routines and solving an issue i have with passing variables
by james28909 (Deacon) on Oct 01, 2014 at 00:02 UTC | |
by GrandFather (Saint) on Oct 01, 2014 at 00:45 UTC | |
by Anonymous Monk on Oct 01, 2014 at 00:33 UTC | |
|
Re^2: using sub routines and solving an issue i have with passing variables
by james28909 (Deacon) on Sep 30, 2014 at 21:22 UTC |