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

Re: Single module spanning multiple Packages,

by tobyink (Abbot)
on Feb 08, 2013 at 13:18 UTC ( #1017813=note: print w/replies, xml ) Need Help??

in reply to Single module spanning multiple Packages,

Reading between the lines, doesn't have a package statement at the top?

Perl will only require a file once. So if you do things the way you're doing them, the functions defined in will end up only in the first package that loads the file.

What you want to do is make into a package in its own right, which uses Exporter or Sub::Exporter or similar to copy its functions into every package that imports it.


package common; use Exporter 'import'; our @EXPORT = qw( do_this do_that ); sub do_this { print "doing this!\n" } sub do_that { print "doing that!\n" } 1;

Then your other modules can just:

use common;

And they automatically get do_this and do_that functions.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Replies are listed 'Best First'.
Re^2: Single module spanning multiple Packages,
by prhodes (Novice) on Feb 08, 2013 at 14:37 UTC
    Thankyou daxim and tobyink for your prompt replies.

    In reply to daxim, I concur with you that code excerpts would assist the respondent to be certain that he had given a definitive answer.

    I considered doing that against a descriptive approach and sided with the latter because I anticipated that the "show all code" approach might have put off more people than it attracted. But I take your valuable feedback on-board, and thanks again for your time and advice.

    In response to tobyink - thank you so much. You've probably seen this before, or you're very "switched on" as you appear to have understood the problem immediately and described the solution clearly and explicitly.

    I made the following changes to and the problem is resolved :

    package common;
    use Exporter 'import';
    our @EXPORT=qw( doit basename );

    Many thanks again to both of you,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017813]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2018-06-25 16:45 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (127 votes). Check out past polls.