Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Fellow monks, I have a puzzlement... I'm trying to figure out how to re-export subs from one module into another. What's particularly puzzling is that, while I can come up with a version that works (although it's a bit kludgey-er than I'd like), when I swap to use base, it doesn't.

Here's what I've got that works:

### External.pm package External; use strict; use base qw(Exporter); use vars qw(@EXPORT); @EXPORT = qw(external); sub external { print "I'm in the external package!\n"; } 1; ### Foo.pm package Foo; use strict; use External; use base qw(Exporter); use vars qw(@EXPORT); push @EXPORT, @External::EXPORT; sub from_base { print "In base...\n"; external(); print "Done in base...\n"; } 1; ### Bar.pm package Bar; use strict; use Foo; use vars qw(@ISA); @ISA = qw(Foo); #use base qw(Foo); my $x = bless { }, 'Bar'; $x->from_base(); external(); 1; ### base-tester #!/usr/bin/perl -w use strict; use Bar; print "Done\n";

With the above bits, I get the correct behavior - which is to say that I can access external() from Bar.pm. However, if I comment out the lines for use Foo;, use vars..., and @ISA... and uncomment use base qw(Foo);, it breaks.

tye had suggested looking into Exporter's export_to_level() or re-calling import, except the idea is to, ideally, need the fewest moving parts in Foo.pm (and none at all in Bar.pm).

And, yes, I'm aware of Spiffy, but I'd prefer something that's a little lighter-weight.

Thoughts, comments, suggestions?


In reply to Mixing @EXPORT and use base by bmcatt

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-04-19 11:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found