Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confusion with my variable in perl

Tags:

perl

If using use strict and use warning and if I specify my datatype, it works; if I do not specify the my it says error.

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

If I declare the array variable name as a number:

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

then when I run that program it says error:

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

If I remove that my and run that program, it runs without an error.

So please can anyone tell me why my variable is not supported with a numeric array variable name?

like image 518
vara Avatar asked May 16 '13 05:05

vara


People also ask

What is$@ in Perl?

$@ The Perl syntax error or routine error message from the last eval, do-FILE, or require command. If set, either the compilation failed, or the die function was executed within the code of the eval.

What does @_ do in Perl?

Using the Parameter Array (@_) Perl lets you pass any number of parameters to a function. The function decides which parameters to use and in what order. The @_ array is used like any other array.

How do you set a global variable in Perl?

Global variables can directly use and are accessible from every part of the program. Example 1: The variable $name is declared at the beginning of the code. It will be visible till the end of the file everywhere. Even inside blocks.

What is the default scope of Perl variables?

Strictly speaking, the default scoping of variables in Perl is the package global. Variables that do not have to be declared.


2 Answers

From perldoc perlvar:

Perl identifiers that begin with digits, control characters, or punctuation characters are exempt from the effects of the "package" declaration and are always forced to be in package "main"; they are also exempt from "strict 'vars'" errors. A few other names are also exempt in these ways...

like image 180
erickb Avatar answered Oct 13 '22 19:10

erickb


As an unintentional side-effect of creating $1, $2, $3, $100, etc (to hold the results of regex captures); @1, @2, @3, @100, etc also get created.

However, the name of these and most of the special vars in perlvar aren't legal names except for package vars. For example, you can't do my $(; or sub (; even though though $( is a valid name for a package variable[1].

When lexicals were added to Perl in 5.6, it was surely deemed to be to confusing to allow such names for user variables. In fact, I doubt anyone even entertained the thought.

Sigil aside, lexical vars must start with a character in [a-zA-Z_][2] and can be followed by a number of characters in [a-zA-Z0-9_][2]. As such, @100 is not a valid name for a lexical variable.


Notes:

  1. >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.
    
  2. More code points are actually allowed, but they fall outside of ASCII's character set. For simplicity, I only listed the code points that fall inside of ASCII's character set.

like image 45
ikegami Avatar answered Oct 13 '22 19:10

ikegami