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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-29 10:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found