Search code examples
perlmoduledirectory-structure

changing directories in a perl script to find modules


If I have a script.pl for example in the path C:\folder1\folder2 when I use Cwd it will of course return C:\folder1\folder2 as my current working directory. I was wondering though if I could change my working directory within my script.pl using

chdir "C:/folder1"; 

so I can now look in C:\folder1 for any modules I need.


Solution

  • Yes, you can do exactly that, but it won't change where use and require look for the modules you are including. All it will affect is where file operations like open will look for files specified with a relative path.

    What you need is the lib pragma, like this

    use lib 'C:/folder1';
    

    at the head of your program. This will add the directory to @INC which is the built-in array of directories that Perl searches for included modules.