Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do Perl identifiers starting with an asterisk * represent?

Tags:

regex

perl

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.

like image 968
Ravi Ranjan Singh Avatar asked Jan 29 '16 11:01

Ravi Ranjan Singh


People also ask

What are identifiers in Perl?

A Perl identifier is a name used to identify a variable, function, class, module, or other objects. A Perl variable name starts with either $, @ or % followed by zero or more letters, underscores, and digits (0 to 9). Perl does not allow punctuation characters such as @, $, and % within identifiers.

What is the meaning of $1 in Perl regex?

$1 equals the text " brown ".

How do I declare a variable in Perl?

Perl variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.


2 Answers

*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

like image 95
Borodin Avatar answered Oct 15 '22 11:10

Borodin


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

like image 3
Sobrique Avatar answered Oct 15 '22 12:10

Sobrique