#!/usr/bin/perl -w use strict; package Item; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; bless ($self, $class); return $self; } sub add_attribute { my $self = shift; my ($key,$value) = @_; $self->{$key} = $value; } 1; #included in case Item becomes a real Module someday. package Section; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; $self->{Items} = []; bless ($self, $class); return $self; } sub add_attribute { my $self = shift; my ($key,$value) = @_; $self->{$key} = $value; } sub add_item { my $self = shift; my $item = Item->new(); push @{$self->{Items}},$item; return $item; } 1; #Ditto package UberObject; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = []; bless ($self, $class); return $self; } sub add_section { my $self = shift; my $section = Section->new(); push @$self,$section; return $section; } 1; #again package Main; use Data::Dumper; my $uo = new UberObject(); my ($section,$item) = (undef,undef); while (<>) { #dammit Jim, parse the data already! if (/begin_section/) { $section = $uo->add_section(); } elsif (/end_section/) { undef $section; } elsif (/begin_item/) { $item = $section->add_item(); } elsif (/end_item/) { undef $item; } elsif (/=/) { if (defined($item)) { $item->add_attribute(split(/=/)); } elsif (defined($section)) { $section->add_attribute(split(/=/)); } else { die "Bad attribute at line $."; } } } #probably should do something interesting ..... # .... nah Just Dump it all! my $d=Data::Dumper->new([$uo],['UberObject']); $d->Indent(1); print $d->Dump();