Reputation: 97
I want to create a Perl class and populate its attributes. The object attributes will be populated like from a yaml files like below.
$data = LoadFile("$mydir/$ARGV[$j]");
my $X= $data->{a}{b}{c};
package Person;
sub new {
my $class = shift;
my $self = {
a=>shift;
};
bless $self, $class;
return $self;
}
my $p=Person->new();
I want to access the attributes like this. How can I do this?
$p->a($data->{a}{b}{c});
Upvotes: 2
Views: 1134
Reputation: 385839
$p->a($data->{a}{b}{c})
makes no sense. Do you mean $p->a->{a}{b}{c}
?
package Person;
sub new {
my ($class, $data) = @_;
my $self = bless({}, $class);
$self->{data} = $data;
return $self;
}
sub data { $_[0]{data} }
1;
my $data = LoadFile(...);
my $p = Person->new($data);
say $p->data->{a}{b}{c};
Upvotes: 2