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

I'm running Perl 5.8 on AIX 5.2. I'm trying to modularize a program for maintenance sake, and having no luck. I'd also like to be able to pull in just the functions I need at runtime with require, but I'm willing to do without that.

I pulled out several sets of functions and placed each set in a separate .pl file, and then ran pl2pm on each file. I put them in a subdirectory of the current directory called focus, and added "use lib ./focus" to the front of the main program. However, when I run the program, I still get output such as the following:

Can't locate base in @INC(@INC contains: ./focus /usr/opt/perl5/lib/5.8.0/aix-thread-multi /usr/opt/perl5/lib/5.8.0 /usr/opt/perl5/lib/site_perl/5.8.0/aix-thread-multi /usr/opt/perl5/lib/site_perl/5.8.0 /usr/opt/perl5/lib/site_perl .) at sl_observe.pl line 216.

I've tried using the modules in the same directory, in the subdirectory, with different names, etc. Following is the block of code containing my require statement:

foreach $focus_area (@focus_areas_list) {
	if ($areas =~ /$focus_area/) {
		chop $focus_area;
		push @focus_areas, $focus_area;
		require "$focus_area";
	}
}

Replies are listed 'Best First'.
Re: can't find my module?
by Abigail-II (Bishop) on Nov 19, 2003 at 22:23 UTC
    If you are using require with a string as argument (instead of a bare word), make sure the string ends with ".pm" (assuming the module does as well).

    Abigail

Re: can't find my module?
by iburrell (Chaplain) on Nov 19, 2003 at 22:52 UTC
    Also, don't use the names of existing core modules for your modules. "base" is a core module that helps with object inheiritance. Lowercase names are reserved for prgma modules. Other modules usually begin with capital letter and are mixed case.

      I read through all of perlmod, and am familiar with the conventions. However, since this is a completely private module, I wanted to be able to use existing values to access the modules directly (although I've since implemented a hash using the values).

      I stated before, that I've tried several different names for each of several .pm files (base was probably a bad example :) Because I'm using the hash, I can really name them anything I want to now. However, this opens up a new question:

      Shouldn't my program have been able to

      require base
      if base is a preexisting module? (My feeling is that, because it's a core module, it can't be directly used or required.)

      45 minutes later:I was able to find a different module (haven't played with the "base" one again), but I had to name it something different than what I had intended. (I had intended Nfs.pm, but that was never found, so I used "Nfs_module.pm", and it found that just fine). So I guess my question now becomes, why can't I use an arbitrary name for a perl module?

        Yes, you should be able to use:
        require base;
        But that's not what the original code does. It does the equivalent of:
        require "base";
        which is different. The former goes out looking for a file called 'base.pm', while the latter goes out looking for a file called 'base'.
        $ perl -wle 'require "base"' Can't locate base in @INC (@INC contains: /home/abigail/Perl /opt/ +perl/lib/5.8.2/i686-linux-64int-ld /opt/perl/lib/5.8.2 /opt/perl/lib/ +site_perl/5.8.2/i686-linux-64int-ld /opt/perl/lib/site_perl/5.8.2 /op +t/perl/lib/site_perl/5.8.1/i686-linux-64int-ld /opt/perl/lib/site_per +l/5.8.1 /opt/perl/lib/site_perl/5.8.0 /opt/perl/lib/site_perl .) at - +e line 1. $ perl -wle 'require "base.pm"' $

        This is documented in perldoc -f require.

        Abigail

        Shouldn't my program have been able to require base if base is a preexisting module?

        Yes it should. If doing require base gets you an error there is something wrong with your Perl installation since it has been core since Perl v5.005. What error did you get?

        was able to find a different module (haven't played with the "base" one again), but I had to name it something different than what I had intended. (I had intended Nfs.pm, but that was never found, so I used "Nfs_module.pm", and it found that just fine). So I guess my question now becomes, why can't I use an arbitrary name for a perl module?

        Nfs.pm is a valid module name and should work perfectly - what error did you get?

Re: can't find my module?
by ChemBoy (Priest) on Nov 19, 2003 at 22:52 UTC

    This isn't in answer to your question at all (I think Abigail-II has you covered there), but note that you probably don't want to name one of your own modules "base.pm", since there's already a standard module by that name. In general, modules with all-lower-case names are taken to be pragmas (or pragmata, depending who you ask), as noted in perlmod: unless you're writing such a module, you probably want to make sure all your modules are capitalized.

    Good luck with your modularization! It may be painful, but it'll pay off in the long run, we promise. :-)



    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

      In general, modules with all-lower-case names are taken to be pragmas
      Then why do we have base.pm instead of Base.pm?

      Abigail

        perlmodlib says (emphasis mine): "They work somewhat like compiler directives (pragmata) in that they tend to affect the compilation of your program, and thus will usually work well only when used within a use, or no. ... Some pragmas are lexically scoped--typically those that affect the $^H hints variable. Others affect the current package instead, like use vars and use subs..."

        Well, if I were into mind-reading, I would speculate that somebody considered a module that defined the parent classes of the current module to be more of a pragma-type-thingy than not (in the generally hand-waving spirit that seems to be popular in the Perl world). And if I were a computer scientist or otherwise deeply versed in the historical useage of the term "pragma" in a programming context, I might add some commentary on the wisdom of this choice. As I can claim none of these accomplishments, I will merely point to the words "in general" and note that I was paraphrasing perlmod, and cheerfully punt away the responsibility for anything that irks you about this arguable inconsistency onto more august shoulders than mine.



        If God had meant us to fly, he would *never* have given us the railroads.
            --Michael Flanders

        The same reason we have vars.pm instead of Vars.pm (they're pragmatic).