In what area is the undeclared variable used deep in the code?

# my @arr;   
for (1..100)
{
    for (1..100)
    {
        for (1..100)
        {
            push @arr, 1;
        }
    }
}

What is the scope @arr? Does it look like it was declared in the comment line at the top?

+5
source share
1 answer

@arr - This is a global variable that is created when it is first encountered by the analyzer and then viewed in the entire package.

use warnings;
#use strict;

for (1..3) {
    #my @arr;
    for ( qw(a b c) ) {
        push @arr, $_;
    }   
}

print "@arr\n";

It prints

abcabcabc

This is one of the bad things about globals that they “emit” throughout the code.

With use strict;on we get

Possible unintended interpolation of @arr in string at scope.pl line 11.
Global symbol "@arr" requires explicit package name at scope.pl line 7.
Global symbol "@arr" requires explicit package name at scope.pl line 11.
Execution of scope.pl aborted due to compilation errors.

strict @arr , @arr , @arr ( , ).

my , , , . my , ( , ).

A my () , eval. , .

, .

, ( ), ( ). @arr, , .

Possible unintended interpolation of @arr in string at scope.pl line 11.
Name "main::arr" used only once: possible typo at scope.pl line 11.

main::arr , , .

my() perlsub.

+9

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


All Articles