First I made the class holding the whole structure. I called it Seismic.
use strict;
package Seismic;
package Seismic::Station;
use Moose;
has 'Easting' => (isa => 'Int', is => 'rw', required => 1);
has 'Northing' => (isa => 'Int', is => 'rw', required => 1);
has 'Elevation' => (isa => 'Int', is => 'rw', required => 1);
has 'Id' => (isa => 'Str', is => 'rw', required => 1);
package Seismic::Line;
use Moose;
use MooseX::AttributeHelpers;
has 'Length' => (isa => 'Int', is => 'rw');
has 'GroupInterval' => (isa => 'Int', is => 'rw');
has 'Id' => (isa => 'Str', is => 'rw', req
+uired => 1);
has 'Stations' => (
metaclass => 'Collection::Hash',
is => 'rw',
isa => 'HashRef[Seismic::Station]',
default => sub { {} },
provides => {
exists => 'station_exists',
keys => 'station_ids',
get => 'get_station',
set => 'add_station',
count => 'count_stations',
delete => 'delete_station',
clear => 'delete_all_stations',
},
);
package Seismic::Grid;
use Moose;
has 'Lines' => (
metaclass => 'Collection::Hash',
is => 'rw',
isa => 'HashRef[Seismic::Line]',
default => sub { {} },
provides => {
exists => 'line_exists',
keys => 'line_ids',
get => 'get_line',
set => 'add_line',
count => 'count_lines',
delete => 'delete_line',
clear => 'delete_all_lines',
},
);
1;
use strict;
use Seismic;
my $grid = Seismic::Grid->new();
for my $line (1 .. 1000) {
my $lineobject = Seismic::Line->new(
{
Id => $line,
Length => int(1000 * rand(90)),
GroupInterval => int( 100 * rand(90)),
}
);
$grid->add_line($lineobject->Id, $lineobject);
for my $station (1 .. 10) {
my $stationobject = Seismic::Station->new(
{
Id => $line . '_' . $station,
Northing => int(1e6 * rand(90)),
Easting => int(1e6 * rand(90)),
Elevation => int( 10 * rand(90)),
}
);
$lineobject->add_station($stationobject->Id, $stationobject);
}
}
print "line station position elev.\n";
for my$line_id ($grid->line_ids) {
my $line = $grid->get_line($line_id);
for my $station_id ($line->station_ids) {
my $station = $line->get_station($station_id);
printf "%08d %-8s %08dN%08dW %4d\n",
$line->Id,
$station->Id,
$station->Northing,
$station->Easting,
$station->Elevation;
}
}
objects.
Of course, behind the scenes, it is all hashes and arrays and hashes of hashes and ... . And of course it is much slower than programming these data structures directly, but then you don't get these nice accessors, mutators, type-checking, default values, ...