I take it you've never programmed in C or assembly :-)

What you're doing in the first version is overwriting a copy of the reference:

my %hash = ( all => 'old' ); # simple hash value foo(\%hash); # pass reference to hash sub foo { # recieve reference aliased as $_[0 +] my $ref = shift; # copy reference to $ref (and throw + away the $_[0] alias) $ref = { all => 'new' }; # overwrite the copy of $ref # this creates a *new* hash and a n +ew reference in $ref # %hash is not modified }
In the second version you're overwriting a key in the hash pointed to by the reference:
my %hash = ( all => 'old' ); # simple hash value foo(\%hash); # pass reference to hash sub foo { # recieve reference as $_[0] my $ref = shift; # copy reference to $ref (and throw + away $_[0]) $ref->{ all } = 'new'; # overwrite the value to the key 'a +ll' # in the hash pointed to by $ref (w +hich is %hash) }

The key concept is that a reference only points to a value (under the hood, a reference in perl contains only slightly more information than the memory address of the data structure it's referring to) - it does not "contain" the value itself - so modifying the reference does not change the value it's pointing to - you can use it to modify the referenced data only as long as it's actually pointing to that data.

update: by the way: the fact that parameters to a function are aliased in @_ means you *can* change parameters even without passing them by reference (by assigning to $_[0] et al), but that will a) only work if the parameters are non-constant scalars, b) confuse the heck out of your coworkers.

updated code: diotalevi's right.


In reply to Re: Modifying values by reference: what works and what doesn't by Joost
in thread Modifying values by reference: what works and what doesn't by markjugg

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.