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).


In reply to Re: Passing Hash to Subroutine by theorbtwo
in thread Passing Hash to Subroutine by mjmaresca

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.