开发者

dereferencing hash without creating a local copy

I the code bellow line 9 creates a local copy of a hash. Any changes to the %d will not provide changes to global %h variable (line: 5). I have to use reference (line: 8) to provide changes to %h.

Is there any way to dereference hash in a sub without creating a local copy? I am asking since I have complicated record with many references and navigating around it with dereferences would be much easier.

  1 #!/usr/bin/perl -w
  2 use strict;
  3 use warnings;
  4 
  5 my %h;
  6 sub a {
  7 
  8     my $href = shift;
  9     my(%d) = %{$href};   # this will make a copy of global %h
 10     
 11     $$href{1}=2;     # this will make a change in global %h
 12     $d{2}=2;         # this will not a change in global %h
 13 }   
 14 a(\%h);
 15 print scalar (keys %h) . "\n";

----------------

Thanks for the replies.

The question is can I make some kind of "alias/bind" to %h in a sub. I would like to change the context of %h in a sub with %d. Whenever I create %d he makes local copy of %h - is there any way to avoid that or do I have to use references all the time?

----------------

One more time :) I know how $href's work. I read tutorial / manuals / docs etc. I didn't find the answer there - I assume that this is not possible since it wasn't written there, but who knows.

I want to accomplish such behavior:

  6 sub a {
  7     $h{"1"}=2;
  8 }

Which is equivalent to that:

  6 sub a {
  8      my $href = shift;
  11     $$href{1}=2;     # this will make a change in global %h
  11     $href->{1}=2;    # this will make a change in global %h

Now how to do that with the help of %d - is it actually possible?

6 sub a {
7        my %d = XXXXXXXXX
.. }

What sho开发者_运维知识库uld I put under XXXXXXXXX to point to %h without creating a local copy?


To create a local alias of the value, you need to use Perl's package variables, which can be aliased using the typeglob syntax (and local to scope the alias):

#!/usr/bin/perl -w
use strict;
use warnings;

my %h;

sub a {
    my $href = shift;

    our %alias; # create the package variable (for strict)

    local *alias = $href;
        # here we tell perl to install the hashref into the typeglob 'alias'
        # perl will automatically put the hashref into the HASH slot of
        # the glob which makes %alias refer to the passed in hash.
        # local is used to limit the change to the current dynamic scope.
        # effectively it is doing:  *{alias}{HASH} = $href

    $$href{1}=2;     # this will make a change in global %h
    $alias{2}=2;     # this will also make a change in global %h
}
a(\%h);
print scalar (keys %h) . "\n";  # prints 2

This is a fairly advanced technique, so be sure to read the Perl docs on local and typeglobs so you understand exactly what is going on (in particular, any subs called from within the a subroutine after the local will also have %alias in scope, since local denotes a dynamic scope. The localization will end when a returns.)

If you can install Data::Alias or one of the other aliasing modules from CPAN you can avoid the package variable and create a lexical alias. The above method is the only way to do it without additional modules.


Two ways of using a hash reference:

  1. One you yourself were using

    $$href{'key2'} = "key2";
    
  2. Pointed out above:

    $href->{'key1'} = "key1";
    

http://perldoc.perl.org/perlreftut.html


Does this help you

$href->{1} = 2;
print scalars (keys %{$href});

?


Following code shows how to dereference using the arrow operator (and yes, without creating a local variable)

Read this for a tutorial on dereferencing and the different types of dereferencing possible.

use warnings;
use Data::Dumper::Simple;

my %h;
sub a {
    my $href = shift;

    #dereference using the arrow operator - *preferred*
    $href->{name} = "John"; 

}

a(\%h); #Passing the reference of the hash
print Dumper(%h);

Why do you need to pass the hash which is a global, as an argument to the sub-routine in the first place?

  1. Avoid using global variables.
  2. Preferably, don't change the state of any variables after they have been assigned.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜