In scriptA.pl
, there is use DBI
In scriptB.pl
, there is require "scriptA.pl"
But we still cannot use DBI package in scriptB.pl
Any way to handle this except repeating use DBI
in scriptB.pl
?
In scriptA.pl
, there is use DBI
In scriptB.pl
, there is require "scriptA.pl"
But we still cannot use DBI package in scriptB.pl
Any way to handle this except repeating use DBI
in scriptB.pl
?
There are ways, but they are all more ugly, hacky and unclean than simply typing use DBI;
in every file that uses it. This is the best practice and is quite normal.
The scoped nature of use
is a documented feature:
use Module
Imports some semantics into the current package from the named module, generally by aliasing certain subroutine or variable names into your package.
You could do what you want by going back to the stone age as in the following example, but please don't.
Using MyModule
as a stand-in for DBI
:
package MyModule;
use Exporter 'import';
our @EXPORT = qw/ foo /;
sub foo { print "$_[0]!\n" }
1;
and then calling MyModule::foo
from scriptA.pl
foo "from scriptA";
and from scriptB.pl
foo "from scriptB";
all kicked off from a main program of
#! /usr/bin/perl
use warnings;
use strict;
use MyModule;
do "scriptA.pl" or die;
do "scriptB.pl" or die;
print "done.\n";
gives the following output:
from scriptA! from scriptB! done.
You also could disable the scoping safety-feature with nasty eval
games, but please don't do that either.
If your design needs improvement—maybe scriptA
and scriptB
belong in the same package—that would be a far better investment of your time. Otherwise, bite the bullet and expend nine keystrokes.
Please note that executing Perl libraries at runtime via do
or require
are a seriously dated approaches. The perlmod documentation describes the modern approach.
I find myself wondering what it is you are attempting to achieve?
If you simply want to reduce boilerplate code (15 common use Foo
declarations in every file), you can use a module like ToolKit to create a standard set of modules to use:
Put this into Powerboy.pm:
package Powerboy;
use base 'ToolSet';
ToolSet->use_pragma( 'strict' );
ToolSet->use_pragma( 'warnings' );
ToolSet->export(
'DBI' => undef, # Export the default set of symbols
);
1;
And then in your scripts, simply do:
use Powerboy;
# We have strict, warnings and DBI now.