Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Difference in Module loading and install??

by Lewis (Initiate)
on Nov 16, 2021 at 08:28 UTC ( [id://11138880]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I'm very beginner of Perl, I have some problem about module loading(?)

I'm running simulation with some script file. This script contains line "package That::Module" to use the subroutine of a package in a module.(blessed subroutine) However, that module is located in the root directory "other/root/directory/Module.pm" different from my simulation directory "My/Directory/Simulation/script".

I think this is possible because @INC already has a path to find the module. (I checked with perl -V, but fail to find exact module path. but script runs successfully anyway) script can run succesfully without line "package That::Module" too. I think it's because the path is environmentally defined and there is no subroutine with the same name in @INC.

For the test, I created another directory and put same script file like this.

My/test/Directory/script2 (same code except "That::Module" -> "This::Module". Intentionally changed to prevent conflicts with existing module "That::Module".) And module file My/test/Directory/This/Module.pm

But for the successful execution, I had to add line "use This::Module" in script2 before(or after) line "package This::Module". I tried both <perl option: perl -I My/test/Directory/This script2> and <use lib '/My/.../This';>line in script2. but still need "use This::Module" line.

I want to know how my simulation script runs successfully without the phrase "use". (Is it defined in a location I don't know?) And want to know how to add(or load? or install? I have some confusion about this.) module and run script2 without the phrase "use" in my test directory.

Replies are listed 'Best First'.
Re: Difference in Module loading and install??
by haukex (Archbishop) on Nov 16, 2021 at 08:47 UTC

    Unfortunately, I'm having a bit of trouble understanding parts of your description. A Short, Self-Contained, Correct Example that we could use to reproduce your situation would go a long way in demonstrating your exact situation and would allow us to answer quicker and better.

    This script contains line "package That::Module" to use the subroutine of a package in a module.

    Note that package does not load another module, all it does is define that the following code is in that namespace. Using package to access the code defined in another package is not recommended - by that I mean, for example, if there is a module Other::Module containing a sub foo, one could theoretically write package Other::Module; to switch to that namespace and then call foo(), provided that some other piece of code has loaded Other::Module. Instead, one should either write Other::Module::foo() to call that function, or use an import mechanism like use Other::Module qw/foo/; (provided that Other::Module has implemented this).

    I want to know how my simulation script runs successfully without the phrase "use".

    I have to guess, but perhaps this is because some other module somewhere is already loading that module. So for example, if your script does use My::Module;, and My::Module does use Other::Module;, then your script could access Other::Module. However, this is not a best practice, and your script should explicitly load all of the modules it wants to make use of.

    And want to know how to add(or load? or install? I have some confusion about this.) module and run script2 without the phrase "use" in my test directory.

    The use is generally necessary to load a module. There are some alternatives like require but that is often not the best practice. If your question is rather about @INC, which lists the directories where use looks for modules, there are multiple ways to add custom directories there as well.

    The term "installing" a module typically refers to downloading a module e.g. from the CPAN using a tool like cpanm. Sometimes, it is used more loosely to mean copying a .pm file into one of the @INC directories.

    Edit: Typo fixes.

Re: Difference in Module loading and install??
by bliako (Monsignor) on Nov 16, 2021 at 09:38 UTC

    Note that /My/.../This is a directory, not a file. The functionality of use lib "<dir>" (or multiple dirs with: use lib ("<dir1>", "<dir2>"); ) is to tell perl interpreter where to look for packages, which are files. You achieve the same with perl -I"<dir>" -I"<dir2>". That's the @INC - an array of directories to look for package files in. As with any include-files system there are precedence rules in order to resolve what happens in same filenames in different directories. Simply: first-comes-first-served.

    package XYZ; is used in Perl as a means to define and implement a module. Which you use (or require) later on.

    bw, bliako

Re: Difference in Module loading and install??
by NERDVANA (Deacon) on Nov 18, 2021 at 22:43 UTC
    Maybe look in your environment?

    env | grep PERL

    There is in fact a PERL5OPT environment variable that can load modules into every perl program by default, but most of us would consider that a bad habit to get into. it's really just meant for diagnostics and workarounds like PERL5OPT=-MCarp::Always perl myscript.pl for temporary increased debugging.

    I think there might also be some place in /etc that can cause perl to load modules on startup. I don't use it though.

    "use" is a good thing. It helps you document where your code is coming from, so that other people know what is going on. .... such as your own case :-) If the script you were using as a reference declared "use" lines for everything, you wouldn't need to ask how the mystery libraries got loaded.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-03-28 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found