Class::Struct - want to access structure within structure

Class::Struct - want to access structure within structure

am 29.04.2006 21:33:33 von nelson331

I want to access a structure within a structure. Below is what I had
in mind. Please help.

#!/perl/bin/perl
use Class::Struct;

struct Step => {
number => '$',
otherstuff => '$',
};

struct Block => {
number => '$',
steps => '@', #should be an array of "Step"s
};

my $step1 = Step->new();
$step1->number(1);

my $step2 = Step->new();
$step2->number(2);

#will eventually be in side a loop @stepArray = (@stepArray,
$tempStep);
@stepArray = ($step1, $step2);

my $block1 = Block->new();
$block1->number(1);
$block1->steps(@stepArray); #this isn't working

@blockArray = (@blockArray, $block1);

#would then like to access the Steps within the Blocks
foreach $tempBlock (@blockArray) {
foreach $tempStep ($tempBlock->steps) { #
print $tempStep->number;
}
}

Re: Class::Struct - want to access structure within structure

am 01.05.2006 17:24:21 von metaperl

nelson331 wrote:

> my $block1 = Block->new();
> $block1->number(1);
> $block1->steps(@stepArray); #this isn't working

The docs for Class::Struct
http://search.cpan.org/~jhi/perl-5.8.0/lib/Class/Struct.pm

do not show that type of usage. Only this usage:
With one or two arguments, the first argument is an index specifying
one element of the array; the second argument, if present, is assigned
to the array element. If the element type is '@', the accessor returns
the array element value. If the element type is '*@', a reference to
the array element is returned.

-- meaning, that you can only put in one element at a time.


Also:
1 - what are you resorting to objects for in this case anyway? The
standard Perl data structures look like they can handle this problem.

2 - Class::Accessor is much more popular for this sort of thing. I
prefer Class::Prototyped myself.

3 - You might find some more help on perlmonks.org for this type of
question.

Good luck