Perl - differences in the BEGIN block of non-BEGIN

I have a question regarding the declaration of const in perl, and could not understand the differences, please kindly indicate the differences.

Below is the code:

BEGIN {
  *SIZE = sub() { 2 };
}  
*ITEM = sub() { 10 };

print 'size=', SIZE, "\n";
print 'item=', &ITEM, "\n";

Now the question is, Why

  print 'item=', &ITEM, "\n";
Line

should have the value '&' before ITEM, but

  print 'size=', SIZE, "\n";   
Line

need not have '&' before SIZE.

And I know that the BEGIN block is run at compile time.

+3
source share
3 answers

Since the BEGIN block is started at compile time, the compiler knows about the assignment *SIZE, but the assignment *ITEMhas not yet occurred.

*ITEM, ITEM , &. , " " - .

use strict, , .

use constant , .

+7

BEGIN {
  *SIZE = sub() { 2 };
}  
*ITEM = sub() { 10 };

print 'size=', SIZE, "\n";
print 'item=', ITEM, "\n";

, :

  • .
    • BEGIN.
      • *SIZE = sub() { 2 };
    • BEGIN.
      • *SIZE = sub() { 2 };
    • *ITEM = sub() { 10 };
    • print 'size=', SIZE, "\n";
    • print 'item=', ITEM, "\n";
  • .
    • *ITEM = sub() { 10 };
    • print 'size=', SIZE, "\n";
    • print 'item=', ITEM, "\n";

, print 'item=', ITEM, "\n"; , . ITEM , . ITEM . - , .

>perl -E"my $x = ITEM; say $x;"
ITEM

>perl -E"my $x = 'ITEM'; say $x;"
ITEM

, Perl :

>perl -E"use strict; my $x = ITEM; say $x;"
Bareword "ITEM" not allowed while "strict subs" in use at -e line 1.
Execution of -e aborted due to compilation errors.

, ITEM .

print 'item=', ITEM(), "\n";

&ITEM , & Perl .

+2

: " , , ". , parens , , / , , .

@Sean McMillan.

+1

All Articles