in reply to Relative Merits of References

It would make a difference if you wanted to pass two hashes to a sub.

#!/usr/bin/perl use strict; use warnings; my $h1 = { one => 1, two => 2, }; my $h2 = { three => 3, four => 4, }; my_sub($h1, $h2); sub my_sub { my ($h1, $h2) = @_; print "$_ $h1->{$_}\n" for keys %{$h1}; print "$_ $h2->{$_}\n" for keys %{$h2}; }
In these circumstances refs are, imo, the only way to go. :-)

Replies are listed 'Best First'.
Re^2: Relative Merits of References
by Unanimous Monk (Sexton) on May 11, 2006 at 15:04 UTC
    Or alternatively:

    #!/usr/bin/perl use strict; use warnings; my %h1 = ( one => 1, two => 2, ); my %h2 = ( three => 3, four => 4, ); my_sub(\%h1, \%h2); sub my_sub { my ($ref1, $ref2) = @_; my %h1 = %{$ref1}; my %h2 = %{$ref2}; # Or "my %h1 = %{(shift)};" will work too, but I don't really like it. print "$_ $h1{$_}\n" for keys %h1; print "$_ $h2{$_}\n" for keys %h2; }

    if you want to keep the hash. This is really the only way to go :)

      Now you're making an expensive copy of a hash. You could just be using the references with:

      my ($r1, $r2) = @_; ... print "$_ $h1{$_}\n" for keys %$r1; print "$_ $h2{$_}\n" for keys %$r2;

      Update:Thanks again David, yes, I meant:

      print "$_ $r1->{$_}\n" for keys %$r1; print "$_ $r2->{$_}\n" for keys %$r2;



      pbeckingham - typist, perishable vertebrate.
        print "$_ $h1{$_}\n" for keys %$r1;

        ITYM $h1->{$_}.

        --
        David Serrano