in reply to Modifying values by reference: what works and what doesn't

I'd formulate it so: If you pass a reference, you get a copy of that reference. So you have two references pointing at the same thing. If you overwrite the received ref with something else (a hashref in your first example), you only have one of the two references left and the var that held the second reference before now has a reference to the hashref.


Ordinary morality is for ordinary people. -- Aleister Crowley
  • Comment on Re: Modifying values by reference: what works and what doesn't

Replies are listed 'Best First'.
Re^2: Modifying values by reference: what works and what doesn't
by pg (Canon) on Aug 25, 2005 at 19:32 UTC

    This code shows that you were not that precise :-) (Update: See below reply from phaylon, sounds like I have misunderstood what he said, and that was my fault. I up voted both of his posts.)

    use strict; use warnings; my $ref = {"a" => 1}; print $ref, "\n"; foo($ref); sub foo { my $ref = shift; print $ref, "\n"; $ref = { "all" => "new" }; print $ref, "\n"; }

    Run it, you will see that the first two print statements print the same addresses, but not the third one.

      That's actually what I've meant ;) Though I'm not a native speaker, so I may have screwed it up a byte. I meant it copies the contents of the scalar, the reference. That results in a second reference (in counting refcounts) to the same address.

      Sorry to confuse, if I have :)

      Ordinary morality is for ordinary people. -- Aleister Crowley
Re^2: Modifying values by reference: what works and what doesn't
by ysth (Canon) on Aug 25, 2005 at 19:12 UTC
    Passing doesn't make a copy of the reference; it's the assignment to $x that makes a copy.