1

I create a module called Base.pm in the same directory as the Perl program that wants to use it but the module doesn't seem to be loaded. Change the name of the module to anything other than Base and things work just fine. What is special about Base.pm and how does one override this behavior? Thanks for any insight.

package Base;
use strict;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION     = 1.00;
@ISA         = qw(Exporter);
@EXPORT      = qw(func);
sub func { return "foo"; }
1;

with

use Base;
print func();

yields

Undefined subroutine &main::func called at test0.pl line 2.

whereas

package Case;
use strict;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION     = 1.00;
@ISA         = qw(Exporter);
@EXPORT      = qw(func);
sub func { return "foo"; }
1;

with

use Case;
print func();

yields

foo.
user90346
  • 266
  • 1
  • 6
  • While not a duplicate, [this question](http://stackoverflow.com/q/658955/8355) is relevant to your situation. – cjm Oct 17 '14 at 20:12

2 Answers2

8

base.pm is a core system "pragma" module (these days, it's deprecated for parent.pm)

use base 'MyBaseClass'; # Same as: our @ISA = ( 'MyBaseClass' );

It's probably a Windows case thing. Perl looks for "Base.pm", but windows returns "base.pm". When you named it "anything other than Base", you probably didn't rename it to another Perl module.

One way you can check this is to run the following:

  • use Data::Dumper
  • print a dump of %INC (a map of all loaded modules and where they were loaded from)

    use Data::Dumper;
    ...
    use Base;
    ...
    print Dumper( \%INC );
    

Look for 'base.pm' or 'Base.pm' to see what Perl loaded.

Axeman
  • 29,660
  • 2
  • 47
  • 102
2

Inspect %INC to see what's actually being loaded:

use Base;

print $INC{'Base.pm'};

Outputs:

/Users/miller/perl5/perlbrew/perls/perl-5.20.0/lib/5.20.1/Base.pm

I'm on a non-case sensitive OSX partition, so this file corresponds to the base pragma.

To avoid this type of issue, give your projects a private namespace. This way there's less risk of conflicting with a current or future Perl Core or CPAN module:

package MyProject::Base;
Miller
  • 34,962
  • 4
  • 39
  • 60