Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Single module spanning multiple Packages,

by prhodes (Novice)
on Feb 08, 2013 at 13:03 UTC ( #1017810=perlquestion: print w/replies, xml ) Need Help??
prhodes has asked for the wisdom of the Perl Monks concerning the following question:

I have one script, two packages, and a module. Let's call them,,, and

I have alot of code, but I've stripped out as much content as possible in order to make my question simple and non-ambiguous.

Both and are packages in their own right (in fact they are OO packages containing methods), and start with the Package keyword in order to define them as such.
In each package there is just one constructor function which works absolutely fine.

This is where the trouble starts ....
I have which is full of lots of useful functions that we use across our site, and I want it to be accessible from everywhere !
So I've placed use common within,, and

However, when I proceed I get an error like this :
Undefined subroutine &pkga::doit called at line 10.
doit is the function within that I am calling from within the constructor method in - although it could be any method.

It's clear that perl doesnt like to load the same module within each package - most likely related to the symbol table and keeping namespaces clean.

I have discovered a fix - by simply creating another module file as a symbolic link e.g.
ln -s and then
use common_for_pkga; within the package file.

However, this seems like a crude fix, and I feel like my understanding of this entire is unsatisfactory.
I've done alot of google and monks searches but haven't been able to find a deeper exaplanation, FIX, or workaround.

This perl disciple/fan would appeciate any help or pointers.
Thanks in advance. Peter.

  • Comment on Single module spanning multiple Packages,

Replies are listed 'Best First'.
Re: Single module spanning multiple Packages,
by tobyink (Abbot) on Feb 08, 2013 at 13:18 UTC

    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
      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,

Re: Single module spanning multiple Packages,
by daxim (Chaplain) on Feb 08, 2013 at 13:18 UTC
    Undefined subroutine &pkga::doit called at line 10. doit is the function within that I am calling from within the constructor method in
    You forgot to import doit from common into pkga. I might be wrong, but no one can know for sure because you didn't show your code. English prose is a poor substitute for code.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1017810]
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2017-07-23 05:22 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (345 votes). Check out past polls.