Code does work in different packages only

F

Ferry Bolhar

Hi folks,

While looking at the code in vars.pm (the "use vars" pragma), I tried to
reproduce its behaviour:

use strict;
use vars $x;
$x = 3;

by writing:

use strict;
BEGIN {
*main::x = \$main::x;
}
$x = 3;

instead. However, this code doesnt work, I get the messages

Variable "$x" not imported at line 5
Global symbol "$x" requires explicit package name at line 5

(Line 5 contains the assignment to scalar $x)

However, when including a "package hugo;" in the BEGIN block (or even
outside, and a "package main;" thereafter), the code works as expected. It
also works when the BEGIN block runs in package "main" and the scalar
assignment in a different package. It fails, whenever both assignments
(typeglob and scalar) are executed in the same package.

Can someone explain why? The typeglob assignment occurs full-qualified, so
it shouldn't matter which package is currently active. However, it does.
Why?

Thanks for your answers and kind greetings from Vienna,

Ferry
 
A

Anno Siegel

Ferry Bolhar said:
Hi folks,

While looking at the code in vars.pm (the "use vars" pragma), I tried to
reproduce its behaviour:

use strict;
use vars $x;
$x = 3;

by writing:

use strict;
BEGIN {
*main::x = \$main::x;
}
$x = 3;

instead. However, this code doesnt work, I get the messages

Variable "$x" not imported at line 5
Global symbol "$x" requires explicit package name at line 5

(Line 5 contains the assignment to scalar $x)

However, when including a "package hugo;" in the BEGIN block (or even
outside, and a "package main;" thereafter), the code works as expected. It
also works when the BEGIN block runs in package "main" and the scalar
assignment in a different package. It fails, whenever both assignments
(typeglob and scalar) are executed in the same package.

Can someone explain why? The typeglob assignment occurs full-qualified, so
it shouldn't matter which package is currently active. However, it does.
Why?

That's a documented fact. Unfortunately I forget where exactly it is
documented, but an assignment to a typeglob only counts as importation
when if happens in a package that is different from the typeglob's.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top