Confusion with my perl variable

When using use strict and use warning and if I specify the data type my , it works; if I do not specify my , it says an error.

 use strict; use warnings; my $test=10; my @arr=(10,20,30); 

If I declare the name of the array variable as a number:

  use strict; use warnings; my @100=(10,20,30); 

then when you run this program it says an error:

  Can't use global @100 in "my" at number_sclar.pl line 28, near "my @100" 

If I remove this my and run this program, it will work without errors.

So please, can someone tell me why the variable my not supported with the variable name of a number array?

+6
source share
2 answers

As an unintended side effect of creating $ 1, $ 2, $ 3, $ 100, etc. (to store the results of capturing regular expressions); @ 1, @ 2, @ 3, @ 100, etc. Also created.

However, the names of these and most special vars in perlvar are not legal names other than vars packages. For example, you cannot do my $(; or sub (; , although though $( is a valid name for the package variable [1] .

When vocabulary was added to Perl in 5.6, it was considered confusing to allow such names for user variables. In fact, I doubt that someone even entertained this thought.

Sigil aside, lexical vars must begin with a character in [a-zA-Z_] [2] and can be followed by several characters in [a-zA-Z0-9_] [2] . Thus, @100 not a valid name for a lexical variable.


Notes:

  •  >perl -e"our $(;" >perl -e"my $(;" Can't use global $( in "my" at -e line 1, near "my $(" Execution of -e aborted due to compilation errors. >perl -e"sub (;" Prototype not terminated at -e line 1. 
  • More code points are actually allowed, but they go beyond the ASCII character set. For simplicity, I have listed only code points that fall within the ASCII character set.

+3
source

From perldoc perlvar :

Perl identifiers starting with numbers, control characters or punctuation are freed from the effects of the “package”, the declaration is always forced to be in the package “main”; they are also exempted from "severe errors" vars. Several other names are also freed in this way ...

+9
source

Source: https://habr.com/ru/post/945124/


All Articles