jroberts has asked for the wisdom of the Perl Monks concerning the following question:

heresy I know, but what is wrong here:
use subdir::Somemodule qw(%myhash); print join ("\n", keys %myhash), "\n"; __END__
subdir is a directory directly under my current directory and directory where the script resides. @INC contains '.' If I do the following...
$>perl -I. import_test.pl
Perl finds the module, but the import function is not called and %myhash in main is empty (which it is not in Somemodule.pm) However if I change the code above to the following...
use subdir::Somemodule; import Somemodule qw(%myhash); print join ("\n", keys %myhash), "\n"; __END__
Everything is a-ok. Furthermore, if i change the code to be...
# delete the 'subdir' from the use # use Somemodule qw(%myhash); print join ("\n", keys %myhash), "\n"; __END__
And invoke perl as follows...
$> perl -I 'subdir' importest.pl
This also works. Any ideas? Here's the version of perl i'm running on Win 2000 Pro.
This is perl, v5.6.1 built for MSWin32-x86-multi-thread
Thanks in advance, jr

Replies are listed 'Best First'.
Re: Perl Bug? import function
by wog (Curate) on Sep 13, 2001 at 03:17 UTC
    The reason why the import is not called is that when you load the file subdir/Somemodule.pm, the module is still called Somemodule (unless that file it loads calls it something else, of course). Thus, when use tries to run somedir::Somemodule->import(...), nothing happens because there is no import method loaded for the package somedir::Somemodule, only Somemodule.

    Modifying @INC directly is better then using this use hack to make the subdirectory part of the module name you load for this reason.

      Thanks, I never would have thought of that. I guess the "use hack" to which you refer is the "::" notation. This seems widely used, but maybe it shouldn't be. Is this one of those "features" that works by some strange quirk in perl itself or was this a design feature? Just curious about the history. Thanks again, jr
        Using the :: notation is not a "use hack" by itself. This is used, quite properly, to have modules be in catergories, e.g. XML::Parser, IO::Socket, etc.

        What I was referring to was taking advantage of this notation to load a module based on its location, instead of based on its name. The way to do this task in a way which does not have cavaets is to modify @INC (e.g. using lib) so you can load the module in question using its real name. Even if this approach did work, I find it to be bad style -- a use line should tell you what module you are using, it should not be something that needs to be modified when you just move files around.

Re: Perl Bug? import function
by dragonchild (Archbishop) on Sep 13, 2001 at 17:04 UTC
    For Module::SomeModule to load correctly, the following has to be true:
    1. There is a file called SomeModule.pm in a directory called Module. This is stackable. Thus, One::Two::Three means that the One/Two/Three.pm exists.
    2. Within SomeModule.pm, there is the following line:
      package Module::SomeModule;
    3. The directory Module is within one of the directories listed in @INC.
    You can modify @INC by doing the following:
    use lib '/Parent/Directory/Of/Module';
    That's the cleanest way of specifying a directory where you want a set of modules to live.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.