Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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, common.pm 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 common.pm will end up only in the first package that loads the file.

What you want to do is make common.pm 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.

Sample common.pm...

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


Comment on Re: Single module spanning multiple Packages,
Select or Download Code
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 common.pm and the problem is resolved :


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

    Many thanks again to both of you,
    Peter.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017813]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-12-20 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (97 votes), past polls