This is happening because you're passing the keys/values of the hash into push_1() instead of a reference to the hash.

When you pass a hash like this: push_1(%hash), the hash is being flattened into a list of key/value pairs, and they're being assigned into @_. You should be passing the hash like this: push_1(\%hash), and then properly dereferencing it like this within the sub:

my $href = shift; for my $key ( keys %{ $href } ) { push @{$href->{$key}}, 1; }

Hope this helps...

Update: Oh shoot, this is a botched answer. In testing and tinkering with your script I see that, as expected, the hash inside the sub is a different one than the one outside the sub, and in deparsing the code I see that as you pass the hash, it's being passed as a flattened list. So I can't explain the behavior you've identified.

Update 2: Duh, how blind could I be? Thanks tilly for spelling it out here (and reminding me in CB): The value of each hash element, in your example, is an arrayref, and that value passes unchanged into the subroutine. That means that you can dereference the value held in the lexical hash within the sub just as you would the value of the lexical hash outside the sub; they're different hashes, but their values hold references to the same anonymous arrays.


Dave


In reply to Re: Pass by value acts like pass by reference by davido
in thread Pass by value acts like pass by reference by Anonymous Monk

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.