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?
$@ 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.
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.
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.
Strictly speaking, the default scoping of variables in Perl is the package global. Variables that do not have to be declared.
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...
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:
>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 fall outside of ASCII's character set. For simplicity, I only listed the code points that fall inside of ASCII's character set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With