in reply to Passing Hash to Subroutine

OK, reading through this code, I've got several reactions.

First off, avoid giving your subs confusing names. The problem refered to in your subject line seems to be in fubar... but you never call fubar, only foobar. It took my second reading of the code before I noticed that problem...and on my third reading, I noticed that it wasn't a problem after all, and you did say fu where you wanted fu, and foo where you wanted foo.

Secondly, don't use prototypes in perl unless you really know what you're doing, and why you need them. Your fubar will do what it appears you're trying to do if you simply remove the prototype (that is, (%) including the parenthises).

Er, sorry, I'm really horrible today. It won't quite work.

\$array[$nbr] creates a reference to the array element $array[$nbr]. You don't need that. $array[$nbr] is alreay a hashref there, so you're ending up a ref to a ref to a hash. In fact, what you want the way you've written fubar, assuming you get rid of that prototype, is just a plain hash. So you should call it with %{$array[$nbr]}. The %{...} construct returns the hash referenced by the thing inside the curly braces. You can also call it with just plain $array[$nbr], assuming you add in derefrencing -> arrows, as in $myhash->{id}.

You could, if you really feel the need, continue writing with the prototypes, in which case, perl will play strange games behind your back... but don't do that.


Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).