Re: sending scalar and hash to function [SUMMARY]
[email protected] (Mike Williams) Mon, 27 Jun 2011 16:54:30 -0400
| Newsgroups | perl.beginners.cgi |
|---|---|
| Message-ID | <[email protected]> |
--20cf3071d02e25a75004a6b7c314 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Jun 27, 2011 at 2:44 PM, <[email protected]> wrote: > > Found the solution (my apologies). > > I am receiving both an scalar, the second one as a reference, then it must > be assigned to an other var. > > No. It does not have to be assigned to another var. Instead you should dereference the reference. sub dosomething > { > ($myopt,$myparams) = @_; > ## %myparams = $myparams; # skip the asssignment and creation oa a > new hash > print "opt = $myopt\n"; > #### while( my ($k, $v) = each %myparams ) while( my ($k, $v) = each %{$myparams} ) # dereference the reference with %{$ref} > { print "$k = $v \n"; } > } > > The assignment you had assigns the hash reference to a key of the hash you created, with nothing assigned as a value. It is a real, real bad idea (for many reasons) to create variables of different types with the same names. A major benefit of passing a reference is that you only move one item, the reference, instead of the entire hash. It doesn't make a lot of difference in this case, but if you had a hash with thousands of key/value pairs passing a reference and using that reference uses just a few bytes of memory. Copying the entire hash uses many thousands of bytes. Happy hacking, Mike --20cf3071d02e25a75004a6b7c314--