user288609
user288609

Reputation: 13035

Chained reference in Perl

How to understand the following two lines of Perl codes:

%{$self->{in1}->{sv1}} = %{$cs->{out}->{grade}};

and

@{$self->{in1}->{sv1value}} = @{$cs->{out}->{forcast}};

Both of them involve using hashes and hash reference in a chain manner, except the first one uses % and the second one is an array object using @. What are the resulting differences here, about which I am not very clear.

Upvotes: 0

Views: 105

Answers (2)

Sodved
Sodved

Reputation: 8598

In the first one $self->{in1}->{sv1} and $cs->{out}->{grade} are both references to hashes. So the line:

%{$self->{in1}->{sv1}} = %{$cs->{out}->{grade}};

Is replacing the contents of the has refrenced by $self->{in1}->{sv1} with the contents of the hash referenced by $cs->{out}->{grade}.

NOTE: This is very different to:

$self->{in1}->{sv1} = $cs->{out}->{grade}

Which just makes them reference the same hash.

The second line is doing the same thing except it is arrays which are referenced, not hashes.

Upvotes: 5

ennuikiller
ennuikiller

Reputation: 46965

You answered your own question. The first line copies a hash to a hash and the second line copies an array to an array!! In other words $self->{in1}->{sv1} is a reference to a hash and $self->{in1}->{sv1value} is a reference to an array.

Upvotes: 2

Related Questions