Solved

"Use Module" throws "Can't locate Module" even when inside a subroutine that is not not called.

Posted on 2003-11-23
3
260 Views
Last Modified: 2010-03-04
I have a settings file that says $module = 0; Then in my script, I have code similar to:

if ($module == 1) {
  use Module;
  do stuff...etc, etc, etc

}

So, when I execute the script, I would think that it would not try to interpret "use module". However, I get an error saying that the module can't be located in @INC, etc... My question is, why does this happen...is it supposed to do that? I think I will just throw an eval around it, but I wanted to see if I was missing something.

Thanks.
0
Comment
Question by:emartin24
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
kandura earned 100 total points
ID: 9807576
from the perlfunc manpage for 'use':
"It is exactly equivalent to
    BEGIN { require Module; import Module LIST; }"

from the perlmod manpage: "Because the use statement implies a BEGIN block, the importing of semantics happens as soon as the use statement is compiled, before the rest of the file is compiled. " and "In general, use Module () is recommended over require Module, because it determines module availability at compile time, not in the middle of your program's execution."

0
 

Author Comment

by:emartin24
ID: 9807634
So if I use require Module, in my example, it would only be compiled if $module == 1? Also, that means that it does not matter where a use statement is in Perl script...they will all be compiled in the begining?
0
 
LVL 18

Expert Comment

by:kandura
ID: 9807659
Yes, that is correct. But remember to do the 'import Module LIST' part if you want to import stuff from that module.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now