in reply to use lib and use autouse 'module'

You can always unshift directories into @INC whenever you want. What you haven't specified is how are you going to let the program know what directory it should unshift. Once you get it into the program, it's easy:

## File: FOO.pm package FOO; print "loading foo\n"; sub foo { print "in foo::foo\n"; } 1;
## main program use autouse 'FOO' => qw/foo/; unshift @INC, '.'; foo; __END__ loading foo in foo::foo

--
David Serrano

Replies are listed 'Best First'.
Re^2: use lib and use autouse 'module'
by cvg2mco (Novice) on Jul 28, 2006 at 23:48 UTC
    Thanks for your quick response. I have information on the directory that this module will reside. The perl script is designed as a file watcher and the process files with the functions from the module. In the loop (for file watcher) i look to see if the common mount point exists and if it does then I call the modules function to process the file. so if I understand you correct, I could do
    ## PERL SCRIPT ## #!/usr/bin/perl; use strict; my $g_commonMount = "/common_mount"; my $g_moduleDir = "$g_commonMount/include"; sub fileWatcher { while(1) { if ( -d $g_moduleDir ) { use autouse 'foo' => qw/foo/; #using your example unshift @INC, '$g_moduleDir'; #foo.pm is under $g_moduleDir foo; } sleep (10); } } #### MAIN ### &fileWatcher; __END__

      Ah, the directory is known from the beginning. That makes things easier. There's no need to unshift anything, since you can use lib(*).

      #!/usr/bin/perl ## untested use warnings; use strict; my ($g_commonMount, $g_moduleDir); BEGIN { $g_commonMount = "/common_mount"; $g_moduleDir = "$g_commonMount/include"; } use lib $g_moduleDir; sub file_watcher { while (1) { sleep 10, next unless -d $g_moduleDir; use autouse 'foo' => qw/foo/; foo; } } file_watcher;

      I'm not particularly proud of the BEGIN block but maybe there's another way to do it. Also, instead of autouse(*), you can use require.

      (*) CPAN seems down now, so the links to the modules may be wrong, since they are core and I don't know if one can link to them in the same fashion as any other.

      --
      David Serrano

        Thanks for your response. The directory name (only)is known at the beginning. The directory itself gets created (mounted) at a later time. So if this is the case, would the
        use lib $g_moduleDir;
        give a runtime error? When I tested this syntax in my perl script, It gave a message saying - Empty compile time value given to use lib. But then when I created the include directory, and called
        require MY_MODULE;
        it loaded the module and gave me access to all the functions and variables defined in the module. I noticed that this is not possible in autouse since we would have to specifiy all the functions and variables in the qw(). Appreciate your comments.