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.


In reply to Re^4: Tell me how it works! by gone2015
in thread Tell me how it works! by nagalenoj

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.