6

I have this sub-routine which has identifiers defined like

*VALID_NAME_REG_EX = \"[ a-zA-Z0-9_#.:@=-]+";
*MACRO_VALID_NAME  = \"MACRO_VALID_NAME";

I looked into the file further. They are referenced as $MACRO_VALID_NAME.

I guess it's substituting the value with right side of string, but I am not sure of this and want a confirmation.

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • 1
    More information on constant variables and typeglobs can also be found in Chapter 7 in [Mastering Perl](http://shop.oreilly.com/product/0636920012702.do) and chapter 1.21 in [Perl Cookbook](http://shop.oreilly.com/product/9780596003135.do) – Håkon Hægland Jan 29 '16 at 11:40
  • Possible duplicate of [Demystifying the Perl glob (\*)](http://stackoverflow.com/questions/4865447/demystifying-the-perl-glob) – tripleee Jan 29 '16 at 11:46

2 Answers2

10
*VALID_NAME_REG_EX = \"[ a-zA-Z0-9_#.:@=-]+";

The effect this has is to assign $VALID_NAME_REG_EX as an identifier for the Perl string literal "[ a-zA-Z0-9_#.:@=-]+"

This is different from saying

$VALID_NAME_REG_EX = "[ a-zA-Z0-9_#.:@=-]+"

which copies the string into the space assigned to $VALID_NAME_REG_EX so it may later be altered

Perl literals have to be read-only to make any sense, so the result of the assignment is to make $VALID_NAME_REG_EX a read-only variable, otherwise known as a constant. If you try assigning to it you will get a message something like

Modification of a read-only value attempted

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • 3
    @mattfreake: It's not the typeglob that makes it read-only, it's that it's pointing at a Perl string literal, which is inherently unmodifiable. If I say `*var_b = \$var_a` then I have made `$var_b` refer to the same place as `$var_a`, which is read-write – Borodin Jan 29 '16 at 17:48
  • hi @Borodin, could you share more on why $VALID_NAME_REG_EX is read-only in this case? I posted a new question at: https://stackoverflow.com/questions/53190329/perl-where-to-find-deep-knowledge-on-a-read-only-variable-created-by-var-0 – vts Nov 07 '18 at 13:25
3

* in perl denotes a typeglob

Perl uses an internal type called a typeglob to hold an entire symbol table entry. The type prefix of a typeglob is a * , because it represents all types. This used to be the preferred way to pass arrays and hashes by reference into a function, but now that we have real references, this is seldom needed.

The main use of typeglobs in modern Perl is create symbol table aliases. This assignment:

*this = *that;

makes $this an alias for $that, @this an alias for @that, %this an alias for %that, &this an alias for &that, etc. Much safer is to use a reference.

Community
  • 1
  • 1
Sobrique
  • 52,974
  • 7
  • 60
  • 101