perl: where to find deep knowledge on a read-only variable created by *VAR = \0

Viewed 85

I've learned a perl statement like:

*VAR = \0;

create a read-only $VAR in answer https://stackoverflow.com/a/35083240/2492255, I'm trying to find more documents to understand why $VAR is read-only in this case, but with no luck, could someone help to explain or point me to some documents on this topic?

1 Answers

*VAR is the symbol table entry for the following:

  • $VAR
  • @VAR
  • %VAR
  • &VAR

(It also has a slot for a file handle, a directory handle, a format and more.)

Because they store a bunch of variables of different types, the symbol table entries are called "typeglobs", or "globs" for short.

Assigning a reference to a to a glob sets the slot of the appropriate type to the referenced variable. This means that assigning a reference to a scalar to *VAR sets *VAR{SCALAR}, the value returned by $VAR. Since you are passing a reference to a constant, $VAR returns that constant.


*VAR = *OTHER;, on the other hand, makes the left-hand side name an alias for the right-hand side name.

  • It makes $VAR equivalent to $OTHER.
  • It makes @VAR equivalent to @OTHER.
  • It makes %VAR equivalent to %OTHER.
  • It makes &VAR equivalent to &OTHER.
  • etc

*VAR = 0; is treated as *VAR = *{"0"}; which means *VAR = *0;. This means, among other things, that $VAR will return the current script's name (as $0 would).


Reference: Typeglobs and Filehandles

Related