シアジョナサン
シアジョナサン

Reputation: 41

Perl struct How to declare an array of struct within a struct

I have two structs

use Class::Struct;

struct Point =>
{
    name  => '$',
    bins => '@',
};

struct Group =>
[
    name     => '$',
    points  => '@point',
];

But this is not working. How can this be done? I am new to perl and have some knowledge in OOP using C# earlier.

After this, how can I access the elements? For example:

my $g = Group->new();
my $p = Point->new(name => "point_1", bins => ["bin_1","bin_2"],);
print $g->points[0]->name;

I know there are tons of mistake in my example above, but I hope it can help you guys understand what I am trying to do here. Thanks in advance.

Upvotes: 1

Views: 288

Answers (1)

choroba
choroba

Reputation: 241918

It's not possible to combine the @ with a class name.

The -> after an accessor is not optional. Moreover, you should specify the index as the argument to the accessor.

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

use Class::Struct;

struct Point => {
    name  => '$',
    bins => '@',
};

struct Group => [
    name     => '$',
    points  => '@',
];

my $p = Point->new(name => "point_1", bins => ["bin_1","bin_2"],);
my $g = Group->new(name => 'group_1', points => [$p]);
print $g->points(0)->name;

Upvotes: 2

Related Questions