perlquestion
puterboy
For various portability reasons, I need to include a couple of custom packages in the same file as my application.
<br><br>
I have no problem wrapping each package in a BEGIN block and accessing methods the usual way. I can also export subroutines using exporter/import. However, I can't seem to export global variables. My code uses the following paradigm:
<code>
use strict;
use warnings;
MyPackage->import;
#My main code
$globalVar=1;
myfunction();
#=====================
BEGIN{
package MyPackage;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(globalVar myfunction);
our $globalVar;
sub myfunction {}
#Rest of package code...
}
</code>
Running, yields the error 'symbol "$globalVar" requires explicit package name'
<br><br>
When I dump the namespace for %main::, I get:
<code>
$VAR79 = 'globalVar';
$VAR80 = *::globalVar;
</code>
So, $globalVar seems to be in the namespace, I just can't seem to be able to use it (unless of course I explicitly address it using its originating namespace location as $MyPackage::globalVar)
<br><br>
If I put the package in a separate file and include it using 'use MyPackage', I get the same listing in the %main:: namespace as above but I am then able to access the global error directly without error.
<br><br>
Any ideas what is causing $globalVar not to be recognized when the package is in the same file as main?