spoonervt has asked for the wisdom of the Perl Monks concerning the following question:
I know that exporting variables/functions is bad style possibly leading to collisions, but I expected it to work.
I have a scenario where I am placing a package (w.pm) in a subdirectory and when doing so the export/import seems to fail.
when I load the package with:
use w; # the package lives in one of the directories in @INCeverything works as expected -> exported variables and functions are available in (and pollute) main's namespace.
When I load the package with: use A::w; # ./A is not in @INC the package loads fine (the BEGIN{} startup message is issued) and I can use variables/functions when specifying the package name with w::, but not without it.Am I misreading how to use Exporter, @EXPORT, or is my understanding of how to create packages in a folder hierarchy wrong?
Any insight is appreciated.
-Steffen (using v5.8.8)
main:
package w:#!/tool/pandora64/bin/perl -w -I ./ use strict; use warnings; use v; # variables/funcs work without v::, the package code is a +lmost the same) #use w; # This makes variables/funcs work without w:: use A::w; # this requires w:: to identify the package sub_in_v(); w::sub_in_w(); $var_in_v = "v-variable"; $w::var_in_w = "w-variable"; # works only because of the w:: when use- +ing A::w printf("%s\n", $var_in_v); printf("%s\n", $w::var_in_w); # works only because of the w:: when us +e-ing A::w print(join("\n", @INC)); 1;
package w; # in ./A use Data::Dumper; use Exporter; our $var_in_w; our @EXPORT= ('$var_in_w', 'sub_in_w'); our @ISA = qw(Exporter); sub sub_in_w{ print("this is from a sub in package SRAM::w!\n"); } BEGIN{ print('this is w\'s BEGIN, @ISA = ', Dumper @ISA, "\n"); 1; } END{ print("w ending now.\n"); } 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: variable import from package fails even though package apparently loads
by ikegami (Patriarch) on Jan 04, 2025 at 05:53 UTC | |
by spoonervt (Initiate) on Jan 06, 2025 at 19:20 UTC | |
|
Re: variable import from package fails even though package apparently loads
by Paladin (Vicar) on Jan 04, 2025 at 03:38 UTC |