Solved

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

Posted on 2003-11-23
3
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

734 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