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

Hi all, first post here. :) I'm a pretty green newbie to Perl but I'm trying, so I hope this question makes sense.

I'm setting up a code repository using a variety of common modules that we write here at my work for things like serial ports, our own software, etc. The way our CVS tree is setup we have a tree called Common, and under that are a series of libraries split up by type. So Common-->Perl-->Networking-->Windows-->Serialport (etc).

What I'm trying to do is write a simple "Hello world" script in the root, that will search the tree for something that I include. Right now "utils.pm" exists in common\perl\utils\net\.

I'm trying to figure out how to get my script to use utils.pm as an include without having to specify the whole path to it. I thought of globbing the whole tree, but I'm not sure how I would go about extracting the path back. Any insight, even a point in the right direction (aka: "Read up on xxxxx") would be great. Thanks! - Seventh
  • Comment on Need help with includes in subdirectories

Replies are listed 'Best First'.
Re: Need help with includes in subdirectories
by davido (Cardinal) on Sep 20, 2004 at 15:04 UTC

    Don't make things harder than they need to be. Perl will do just fine if you put modules in paths that make sense.

    For example, if you put Utils.pm in the perl/ directory, and then push into @INC the path "common\perl", Perl will search there for it. If you had a module named Utils::Foo, you sould put foo.pm in "Common\Perl\Utils\", and with the same "common\perl" entry in @INC, Perl will find it. Add entries to @INC with the use lib pragma, or by pushing within a BEGIN{} block.

    But if you put Utils::Foo in "Common\Perl\Bar\", Perl won't find it. The '::' in the module name is an indicator to Perl that it needs to start with the paths listed in @INC, plus the directory that is the same as whatever lies to the left of the :: (in this case Utils), and then look for the file named after whatever comes to the right of :: (in this case Foo.pm).

    Any other module placement strategy is just going to create more work.

    See perlvar for an explanation of @INC, and then perlmod for discussion of modules. perlmodlib also offers a few suggestions on module naming and placement within the file structure.


    Dave

Re: Need help with includes in subdirectories
by sintadil (Pilgrim) on Sep 20, 2004 at 14:55 UTC

    What I'm trying to do is write a simple "Hello world" script in the root, that will search the tree for something that I include. Right now "utils.pm" exists in common\perl\utils\net\.

    Perhaps you want to use the lib pragma? Something like:

    use lib q#common\\perl\\utils\\net#;
    sounds like it'd give you what you want. Remember to properly escape backslashes in your paths.

Re: Need help with includes in subdirectories
by rinceWind (Monsignor) on Sep 20, 2004 at 15:04 UTC
    Perl looks in the special variable @INC for paths to locate modules in. If you issue the command "perl -V", this will tell you the list of paths in your configuration.

    If you want to add more paths, you have two options: adding the line use lib '/home/mydir'; will add mydir to the list of paths that are searched, before any paths in the config. The alternative is to define the environment variable PERL5LIB to be a colon separated list of paths (like PATH). This has the advantage of not requiring any script changes.

    Note that @INC etc. are referring to root directories, hence if the code says

    use Foo::Bar;
    This will look for a file called Bar.pm in a directory called Foo off each of these paths.

    For more on this see perldoc perlrun

    Hope this helps.

    --
    I'm Not Just Another Perl Hacker

Re: Need help with includes in subdirectories
by edan (Curate) on Sep 20, 2004 at 15:05 UTC

    In general you want to create modules with the whole namespace, or "path" as you put it. But if your module "utils.pm" (you shouldn't start module names with lowercase) really declares itself with "package utils", then you need to add the right directories to @INC. Read up on perldoc lib.

    --
    edan

      Thanks very much for all the responses guys. :) I've got the script working with the include, using just a short use Perl::net::Utils - what I'm trying to do now though is to get the script to check out the modules that it needs instead of just checking out the entire tree.