jimav has asked for the wisdom of the Perl Monks concerning the following question:
Aliasing symbol table entries does not work unless the aliasing assignment is executed by code in a different package. Why is this? BEGIN{ *main::mainvar = *someotherpkg::whatever } does not define $mainvar in package main if that assignment is executed in package main... or so it seems (see demo script below).
Can someone explain what is going on here? I've read 'perlmod' carefully and think I understand Perl symbol tables. But.
#!/usr/bin/env perl use strict; use warnings; use feature qw/say/; package Foo; our $foovar; BEGIN{ $foovar = 42 } package main; # This does not work (gets "Variable $mainvar is not imported") BEGIN{ package main; *main::mainvar = \$Foo::foovar; } # But this does work ... why? #BEGIN{ package xyzzx; *main::mainvar = \$Foo::foovar; } say $mainvar;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Why don't *glob assignments affect the current pacakge?
by hv (Prior) on Aug 26, 2023 at 04:13 UTC | |
by haukex (Archbishop) on Aug 26, 2023 at 14:27 UTC | |
by tobyink (Canon) on Aug 26, 2023 at 14:00 UTC |