in reply to Re: use lib and use autouse 'module'
in thread use lib and use autouse 'module'

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__

Replies are listed 'Best First'.
Re^3: use lib and use autouse 'module'
by Hue-Bond (Priest) on Jul 29, 2006 at 00:10 UTC

    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.
        would the use lib $g_moduleDir;give a runtime error?

        Didn't yesterday when I posted that, and didn't right now. This is what I tried:

        use lib '/home/hue/nonexistent'; use autouse 'FOO' => qw/foo/; sleep 10; foo;

        Then, in another console, did a mkdir nonexistent; mv FOO.pm nonexistent. No warnings or errors from the program, it just run seamlessly.

        Empty compile time value given to use lib

        It seems you passed an empty variable to lib. Check it.

        --
        David Serrano