Scarborough has asked for the wisdom of the Perl Monks concerning the following question:

Can anybody point me in the direction of a good explanation of dereferencing. I have some inherited code which at the end of a sub I have the following line of code

$$RESPONSE = 0;

I cant get to the bottom of what is being assigned the value 0.
Thanks in advance for your usual good advice

Replies are listed 'Best First'.
Re: Dereferencing
by tinita (Parson) on Mar 30, 2004 at 14:09 UTC
    $$var is just a short form for ${$var}. the $var inside the braces is a reference; in this case a reference to a scalar.
    you dereference a variable by putting ${ } around it. it $var were a reference to an array, you'd dereference it by putting an @{ } around it, and %{ } for a hash.
    please read more about it in perldata, perlref etc...
    if the $var is not a reference but just a simple string, then we're dealing with symbolic references here, which you should avoid...
Re: Dereferencing
by Limbic~Region (Chancellor) on Mar 30, 2004 at 14:08 UTC
    Scarborough,
    Try References Quick Reference in tutorials. In this particular case, $RESPONSE is a reference (relatively small) to a presumably much larger scalar. To change the referant that the reference points to you would dereference in the assignment: $$RESPONSE = 0;
    Cheers - L~R

    Update: Swapped referant/reference per Abigail-II's comment below

      Actually, $RESPONSE is the reference, not $$RESPONSE. As for the quick reference, that just explains how you use references, it assumes you already know what a reference is.

      Abigail

Re: Dereferencing
by dreadpiratepeter (Priest) on Mar 30, 2004 at 14:20 UTC
    While you should read the above mentioned docs, here is a very simple explanation.
    Somewhere before this line, $RESPONSE is being set to point to another variable. Something like:
    $x = 3; $RESPONSE=\$x;
    After this, $RESPONSE contains the memory address of the $x variable.
    If you print $RESPONSE you will get a strange string. Something like "SCALAR(0xec94c)". Which says that $RESPONSE is a reference to a scalar (at memory address 0xec94c).
    To get to the value that $RESPONSE is pointing to you need to dereference it. Putting the extra $ before the variable, is a way of dereferencing the variable (there are others, see the docs). Thus if you print $$RESPONSE, you will get 3.
    You can also assign to the dereferenced variable. Doing so would change the value stored in $x.
    In your case, whatever $RESPONSE is pointing to will have its value changed to 0
    Hope that helps,


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: Dereferencing
by monkey_boy (Priest) on Mar 30, 2004 at 14:22 UTC
    simple way to find out what it *WAS*:
    use Data::Dumper; print Dumper($RESPONSE);
    should all become clear!
    I should really do something about this apathy ... but i just cant be bothered
Re: Dereferencing
by MCS (Monk) on Mar 30, 2004 at 14:33 UTC

    $RESPONSE is a reference and it is basically a memory address. Now it's great to pass that around instead of copying the actual value which is one of the reasons people use it. Now, you can't just say something like $RESPONSE = 0 because that would assign the memory address of $RESPONSE to 0... not the actual value. So in order to dereference it (or in other words to use the actual value) you put a $ in the front. This basically says, I don't want the address, instead I want the value at that address. In your case you are assigning the value of 0 to it instead of getting a value. It works both ways.

    Hope that helped. The docs are pretty good too, I just thought I would give a brief description.

      $RESPONSE is a reference and it is basically a memory address

      In interest of full-disclosure, it is not really a memory address, but basically like a memory address. I'm saying this for new-user benefit since MCS probably knows this. A reference is an entry in a symbol-table, if you like...not quite like C/C++ pointers. In Java, well, objects just act as references, so the line is blurred in a rather confusing way if that's where you are coming from.

Re: Dereferencing
by Fletch (Bishop) on Mar 30, 2004 at 14:09 UTC

    perldoc perlreftut, perldoc perlref.

Re: Dereferencing
by dawn (Novice) on Mar 30, 2004 at 16:49 UTC
    I found this tutorial on Perl Monks much easier to understand than the perldocs.
Re: Dereferencing
by pbeckingham (Parson) on Mar 30, 2004 at 14:28 UTC

    You may already understand this well - it depends on what other languages you know well, as there are analogous features in many other languages. What do you know well?