Perl too smart require()ing libs

clscott on 2007-06-22T05:35:48

Background

At $work we have a bunch of perl 4 style libraries that get required into apps when we need their functionality.

As they are not packages they automatically pull a whole pile of subroutine and variable definitions into the main:: namespace. I wasn't feeling very happy about that.

So in a effort to begin refactoring I wrote packages as wrappers around these libraries. The packages use Exporter and @EXPORT_OK so we could use the package and import only the subroutines our apps actually needed. This allows us to incrementally migrate the code base to the package style as we need to, by keeping the original libraries intact.

As these types of things usually do, there are interdependencies between the libraries, for example bar.pl calls a sub defined in foo.pl so the package for bar.pl (BarWrapper.pm) needs to require both bar.pl and foo.pl

The problem

When I "use" two wrapper packages and they both "require" the same lib, the second package gets shafted and doesn't get the subroutines defined and imported into its namespace.

The Question

How the heck can I work around or overcome this?

Or what's an alternate solution that meets my goals?

Code and discussion on PerlMonks


delete $INC{$pl_script}

danboo on 2007-06-22T12:13:55

One hack to try:

delete $INC{'foo.pl'}; require 'foo.pl';