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?

Code to demonstrate

#runme.pl use lib '.'; use ExampleClass; my $ec = ExampleClass->new; print $ec->does_work,"\n";
#ExampleClass.pm package ExampleClass; use lib '.'; use BarWrapper; use FooWrapper qw/can_i_see_you/; sub new { my $class = shift; bless {}, $class; } sub does_work { can_i_see_you(); } 1;
#BarWrapper.pm package BarWrapper; use vars qw/@ISA @EXPORT_OK @SUBS/; require Exporter; @ISA = qw(Exporter); @EXPORT_OK = qw(i_am_another_sub); require "bar.pl"; require "foo.pl"; 1;
#bar.pl sub i_am_another_sub { return "subber!"; } 1;
#FooWrapper.pm package FooWrapper; use vars qw/@ISA @EXPORT_OK @SUBS/; require Exporter; @ISA = qw(Exporter); @EXPORT_OK = qw(can_i_see_you dont_import_me ); require "foo.pl"; 1;
#foo.pl sub can_i_see_you { return "You can see me!"; } sub dont_import_me { return "Don't import me"; } 1;
--
Clayton

In reply to Perl too smart require()ing libs by clscott

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.