Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Single module spanning multiple Packages,

by tobyink (Canon)
on Feb 08, 2013 at 13:18 UTC ( [id://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

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 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1017813]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-23 13:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found