Code line 9 below creates a local copy of the hash. Any changes to% d will not contain changes to the global variable% h (line: 5). I have to use the link (line: 8) to make changes to% h.
Is there any way to hash dereferencing in sub without creating a local copy? I ask because I have a complicated record with many links, and moving around it using breaks would be much easier.
1
2 use strict;
3 use warnings;
4
5 my %h;
6 sub a {
7
8 my $href = shift;
9 my(%d) = %{$href};
10
11 $$href{1}=2;
12 $d{2}=2;
13 }
14 a(\%h);
15 print scalar (keys %h) . "\n";
----------------
Thanks for answers.
The question is, can I do some kind of "alias / binding" to% h in sub. I would like to change the context of% h to sub from% d. Whenever I create% d, it makes a local copy of% h - is there a way to avoid this, or do I need to constantly use links?
----------------
:) , $href. // ..
- , , , .
:
6 sub a {
7 $h{"1"}=2;
8 }
:
6 sub a {
8 my $href = shift;
11 $$href{1}=2;
11 $href->{1}=2;
, % d - ?
6 sub a {
7 my %d = XXXXXXXXX
.. }
XXXXXXXXX, % h ?