Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Setting variables inside a hash: newbie question

by bprew (Monk)
on Nov 14, 2004 at 23:30 UTC ( [id://407737]=note: print w/replies, xml ) Need Help??


in reply to Setting variables inside a hash: newbie question

It sounds like you're passing around a hash to various subroutines, when you want to pass a hash-reference. Passing a reference to a hash allows you to change the hash in a subroutine and have that change affect the rest of accesses to that hash. As an example:
my %regular_hash = ( question1 => 'How are you?', question2 => 'What is your name', ); print $regular_hash{question1}; # prints 'How are you?' change_question1(%regular_hash); print $regular_hash{question1}; # still prints 'How are you?' sub change_question1 { my %regular_hash = @_; $regular_hash{question1} = 'What is your mothers name?'; }
Now, if you were using a hash reference, you would be able to change the value:
my $hash_rev = { #notice the change of opening brace question1 => 'How are you?', question2 => 'What is your name', }; # and the corresponding close # and slightly different access methods print $hash_ref->{question1}; # prints 'How are you?' change_question1($hash_ref); print $hash_ref->{question1}; # now prints 'What is your mothers name? +' sub change_question1 { my $hash_ref = shift @_; $hash_ref->{question1} = 'What is your mothers name?'; }
The difference is that Perl will pass its arguments by value, which means that it will create a copy of the data that you passed. However, when you pass a reference to a data-type, Perl passes the a memory location (similar to a pointer in C/C++). That memory location is a container for the values. So, when you change a value in the original container, it changes it everywhere. If that's not what you're doing, perhaps the code you're working with would help.

--
Ben

Replies are listed 'Best First'.
Re^2: Setting variables inside a hash: newbie question
by hmerrill (Friar) on Nov 15, 2004 at 12:57 UTC
    Ben is absolutely right - pass a hash *by reference* - that is, pass a reference to a hash instead of the hash itself.

    One concept that Ben didn't mention that I think is worth acknowledging is that when parameters are passed to a subroutine, that argument list is "flattened" out into a list of scalars - arrays are flattened out into a list of scalars, and hashes are flattened out into a list of scalars - the result is one big list of scalars getting passed to the subroutine. This isn't a problem unless you pass an array or hash first, and *then* other parameter(s) - the array or hash that receives the 1st argument in the subroutine will likely "swallow up" all the arguments being passed in, since it can't tell from the one big "list" of scalars coming in which ones are intended for it and which ones are intended for other parameters.

    To avoid confusion when passing "things" to subroutines, pass each parameter as a scalar - this means

    * passing scalars as themselves * passing a "reference to an array" (which is itself a scalar) instead of the array itself * passing a "reference to a hash" (which is itself a scalar) instead of the hash itself
    Then if you really want to work with a "copy" of the array or hash (instead of just using the reference to refer to elements in the array or hash) whose reference was passed into the subroutine, you can create a copy like this:
    sub do_something($my_scalar, $my_arrayref, $my_hashref) { my @my_array = @$my_arrayref; my %my_hash = %$my_hashref; ### more code here ### }
    HTH.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://407737]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (1)
As of 2024-04-15 17:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found