Ravi Ranjan Singh
Ravi Ranjan Singh

Reputation: 85

What do Perl identifiers starting with an asterisk * represent?

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.

Upvotes: 6

Views: 1504

Answers (2)

Borodin
Borodin

Reputation: 126722

*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

Upvotes: 10

Sobrique
Sobrique

Reputation: 53478

* 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.

Upvotes: 3

Related Questions