Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Using Multiple Modules

by hmeharward (Novice)
on Jan 19, 2016 at 23:04 UTC ( [id://1153131]=perlquestion: print w/replies, xml ) Need Help??

hmeharward has asked for the wisdom of the Perl Monks concerning the following question:

I've created a new module to use with my driver program but the driver program is already using subroutines from another module (not created by me) that I'd like to also use. How to do include the subroutines from both modules? I tried something like below but that only includes the functions from the first module.

use lib "$codes_dir"; require ($codes_dir . "/perl_libs/perl_db_interface.pl"); use lib "$runner_lib"; require ($runner_lib . "/runner_libs/CodesModule.pm");

Replies are listed 'Best First'.
Re: Using Multiple Modules
by choroba (Cardinal) on Jan 19, 2016 at 23:10 UTC
    Specify the whole path as "lib", and only the file as "required". As use executes at compile time, be sure to set the variables in a BEGIN block:
    my ($codes_dir, $runner_lib); BEGIN { $codes_dir = '...'; $runner_lib = '...'; } use lib "$codes_dir/perl_libs", "$runner_lib/runner_libs"; require 'perl_db_interface.pl'; require 'CodesModule.pm';

    Note that normally, you shouldn't require files but packages:

    require CodesModule;

    Why don't you use them directly, anyway?

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Everytime I've tried to use them directly I haven't been successful. The code compiles but stops when it gets to the first subroutine

        How does the .pm export its subroutines? If it uses Exporter, you need to run
        require CodesModule; 'CodesModule'->import;

        Or you might even need to name the subroutines as the import's parameters if the module doesn't use @EXPORT but @EXPORT_OK.

        Update: if you switched to

        use CodesModule;

        you need to name the subs for @EXPORT_OK in this way:

        use CodesModule qw{ func1 func2 };
        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Thank you! I will try this instead. I was a little confused on what should be required but this clears things up!

Re: Using Multiple Modules
by Anonymous Monk on Jan 20, 2016 at 00:36 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1153131]
Approved by sweetblood
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-16 05:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found