On Mon, 21 May 2001 18:15:02 -0400, "Arthur Cohen" <[EMAIL PROTECTED]>
wrote:

>: >
>: >sub add2hash2 {
>: >    # refer to data indirectly using local (my) hash
>: >    my $thRef = shift @_;
>: >    my %th = %$thRef;
>: 
>: You are making a *copy* of the original hash here.
>
>This is the part that puzzles me. I understand *how* it's working but
>not why. 
>
>What I'd *like* to do is simply declare a local variable to manipulate
>the same hash that lives at $thRef (instead of having it populate a copy
>of the original hash). Simply to make the syntax within the function a
>little clearer. Is this possible? (under -w and use strict?) 

It is not possible to declare a "local" hash that is an alias to the one
passed in as a reference; it is always a new hash.  Even a scalar is
always a new variable. The only way to modify scalar parameters directly
and not through a reference is by accessing them through @_ (which
contains aliases, not references to the original parameters).

You have to use hash references with the corresponding syntax to access
the parameter.  You can use $$thRef{key} instead of $thRef->{key} but
personally I prefer the later.  Note that you cannot even pass an array or
a hash as a direct argument; you always either pass a reference or get the
data structure flattened out as a list.

-Jan

_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activeperl

Reply via email to