Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Tell me how it works!

by oshalla (Deacon)
on Jan 28, 2009 at 14:49 UTC ( #739589=note: print w/ replies, xml ) Need Help??


in reply to Tell me how it works!

Because *a is implicitly a collection of pointers to the various $a, @a, etc values. So *b = *a copies those pointers, and undef *a wipes out the original collection of pointers, but not the copy.

Update: my apologies, I omitted one level of indirection. I am grateful to brother citromatik for pointing out the error of my ways. (I will now go and mediate on the problem of hubris.)

Correcting my earlier explanation... What is happening is that *a implicitly refers to a structure which contains a collection of pointers to the various $a, @a, etc values. So *b = *a makes *b implicitly refer to the same structure as *a. The operation undef *a wipes out the implicit reference -- it does not affect *b, or the structure, or the values pointed to by that structure.


Comment on Re: Tell me how it works!
Select or Download Code
Re^2: Tell me how it works!
by citromatik (Curate) on Jan 28, 2009 at 15:01 UTC
    So *b = *a copies those pointers, and undef *a wipes out the original collection of pointers, but not the copy.

    That is not true, typeglob assignment performs an aliasing operation:

    $x = 10; @x = qw(1 2 3); *y = *x; $x = 12; print $y; # <- Prints 12 undef $x; print $y; # <- Warns "Use of uninitialized value in..."

    See perlmod

    citromatik

      Thank you, I understood your reply.

      *a=*b;

      But, I read somewhere that assigning the typeglob(*a) will make both to point the same location(address/ memory).

      So, When I undef the *a, how the *b remains the same?

      Can you clear me in this?

        undefing @a would've cleared it, affecting one of the things that the symbol "b" also points to:
        $ perl -le '@x = qw(1 2 3); $x = 1; *y = *x; undef @x; print "$y [@y]" +' 1 []
        but undefing *a clears the entries for "a" in the symbol table, without affecting the things that the symbol "b" also points to:
        $ perl -le '@x = qw(1 2 3); $x = 1; *y = *x; undef *x; print "$y [@y]" +' 1 [1 2 3]
        []s, HTH, Massa (κς,πμ,πλ)
        But, I read somewhere that assigning the typeglob(*a) will make both to point the same location(address/ memory).

        That is incomplete... it's not the typeglob that ends up pointing to the same location, but the contents of the typeglob that end up pointing to the same locations (one for each type of value).

        Update: the more accurate answer is that yes, *a and *b end up as pointing to the same location, but what that location contains is a set of pointers, one for each type of value. (I'm sorry, I had previously left out one level of indirection.)

        The typeglob *a implicitly points a structure which contains, amongst other things: a pointer to the SCALAR value $a; a pointer to the ARRAY value @a; a pointer to the HASH value %a; etc.

        When you use $a you are implicitly using the SCALAR value pointed to by the respective pointer in the structure refered to by *a. (There is are two implicit dereferences.)

        The assignment *b = *a makes them both point to the same set of pointers to values. So, $a and $b now implicitly refer to the same value (the same location in memory). So changing $a changes $b. And similarly for @a and @b, %a and %b, and so on.

        So, undef *a wipes out its pointer to the structure containing the pointers to the values. It does not wipe out that structure or the values it points to. The glob *b still points at the structure, and hence to values that *a used to point to, so it and those values remain unchanged.

        The typeglob *a contains, amongst other things: a pointer to the SCALAR value $a; a pointer to the ARRAY value @a; a pointer to the HASH value %a; etc.

        When you use $a you are implicitly using the SCALAR value pointed to by the respective pointer in *a. (There is an implicit dereference.)

        The assignment *b = *a copies the pointers. So, $a and $b now implicitly refer to the same value (the same location in memory). So changing $a changes $b. And similarly for @a and @b, %a and %b, and so on. Or, in other words, $a and $b are aliases of each other.

        So, undef *a wipes out its pointers. It does not wipe out the values. The glob *b still points at the values that *a used to point to, so it and those values remain unchanged.

        Dear monks,

        Now, I have another question, how can I clear the *b when undef the *a, in the above scenario?

        That is, when doing

        *b = *a; undef *a;
        The contents of *b is not cleared. Is it possible to clear that too with the help of glob?

        Yes, ofcourse without using undef *b; And also, it is understood that undef $a; or undef @a will affect $b and @b.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://739589]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2014-12-26 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (165 votes), past polls