Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Why do multiple requires not work?

by srinigs (Novice)
on Dec 06, 2012 at 11:45 UTC ( #1007543=perlquestion: print w/replies, xml ) Need Help??
srinigs has asked for the wisdom of the Perl Monks concerning the following question:

Hi

Supposing i have a module Mod.pm which has a statement "require /a/b/script" and there is a script run.pl which has the statements "use Mod" and "require /a/b/script", then, when any routine inside /a/b/script is called from run.pl, I get the "Undefined routine ..." error. This error does not appear when the require statement is removed from Mod.pm module, but the vice versa is not true (ie when I remove the require statement from run.pl and retain the one in Mod.pm, the error still persists).

Can you please explain why multiple use statements are not a problem but multiple "require" statements cause an issue?

Replies are listed 'Best First'.
Re: Why do multiple requires not work?
by bart (Canon) on Dec 06, 2012 at 11:53 UTC
    You can't use slashes in the name of the required file. You can use either double colons, and no file extension (".pm" is assumed), or you can use a string (a quoted literal or a variable) with a path with slashes and with file extension. So you can do
    require "/a/b/script";
    if it has no file extension, or
    require "/a/b/script.pl";
    if it has ".pl" as file extension.
Re: Why do multiple requires not work?
by DrHyde (Prior) on Dec 06, 2012 at 11:52 UTC

    require checks %INC to see if a file has already been loaded. This is explained in the documentation.

    use also checks %INC (because it is exactly equivalent to BEGIN { require Module; Module->import( LIST ); }) but regardless of whether the module was already loaded or not it still calls the import() method.