First off, I find it helpful to use enclosing braces to control scope when cramming multiple packages into one file. Also, enclosing the package in a BEGIN block makes it work more like a proper use
was used to load it, but this is mostly if I am cramming the package into the main script.
use Foo
is the same as BEGIN { require Foo; Foo->import }
.
So, you have two choices:
- call
BEGIN{ Foo::Whizzy->import; }
in your main script.
- make
Foo::Bar::import
trigger Foo::Whizzy::import
on the calling module.
In Foo/Bar.pm:
{ package Foo::Bar;
use Exporter qw( export_to_level );
# Special custom import. Not needed if you call Foo::Whizzy->import
sub import {
shift;
export_to_level('Foo::Whizzy', 1, @_ );
}
# stuff
# stuff
}
{ package Foo::Whizzy;
require Exporter;
our @ISA=qw(Exporter);
our @EXPORT=qw(x);
use constant { x=>1 };
}
1; # return true
In your main code:
use Foo::Bar;
# If you don't do a custom import for Foo::Bar, add this line:
BEGIN { Foo::Whizzy->import };