0

I have objA which has some level of abstraction, and I'm trying to create objB that has it's own attributes but one of them is array of objA. I want to be able to create objB and populate it's array of objA with values. How this can be coded in perl?

1 Answer 1

2

If your ObjA objects are based on hashes then you can have a field that corresponds to an array reference to store the list of ObjB objects.

You don't say how you want to use or access these items, but you could, say, write your methods like this

package ObjA;

sub new {
  my ($class);
  bless {}, $class;
}

sub add_b_object {
  my ($self, $b_obj);
  push @{ $self->{b_objects} }, $b_obj;
}

Then your code would look like

my $a_obj = ObjA->new;

for (1..3) {
  my $b_obj = ObjB->new;
  $a_obj->add_b_object($b_obj);
}

I hope that helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.