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

I have the following problem: in a method of some Perl class I need to know the directory in which resides the class's module file.
I found a solution, shown in the module below.
Question to brethren: Is there a better and/or simpler way to obtain this information?

TIA
Rudif

#! perl -w # file h:/devperl/perlmonks/Test/ModDir.pm package Test::ModDir; use strict; use File::Basename; sub new { my ($class, %args) = @_; my $self = {}; bless $self, $class; return $self; } sub moduleDir { my $self = shift; my $class = ref $self; # "ModuleDir::ModDir" (my $moduleFile = "$class.pm") =~ s!::!/!; my $modulePath = $INC{$moduleFile}; # print "module $_ from file $INC{$_}\n" foreach keys %INC; my $moduleDir = dirname $modulePath; } 1; __END__ #! perl -w # file h:/devperl/perlmonks/TestModuleDir.pm use strict; use lib qw ( h:/devperl/perlmonks ); use Test::ModDir; my $md = Test::ModDir->new; printf "===%s===\n", $md->moduleDir; ### prints ===h:/devperl/perlmonks/Test=== __END__
  • Comment on How do I get the name of the file from which the class was loaded?
  • Download Code

Replies are listed 'Best First'.
Re: How do I get the name of the file from which the class was loaded?
by merlyn (Sage) on Oct 23, 2000 at 01:07 UTC
      What's wrong? It doesn't work, but it can be fixed.
      __FILE__ is the special literal that I need, not __FILENAME__. This works:
      #! perl -w use strict; package This::Module; our $_Filename = __FILE__; # ... package main; print "$This::Module::_Filename\n";
      I found this in perldata (which I should have read beforehand)
      The special literals __FILE__, __LINE__, and __PACKAGE__ represent the current filename, line number, and package name at that point in your program.

      However, after I replaced __FILENAME__ by __FILE__ in your code, I had trouble getting the constant out of the package.

      #! perl -w use strict; package This::Module; use CONSTANT _Filename => __FILE__; # ... package main; #print "$This::Module::_Filename\n"; #Use of uninitialized value in concatenation (.) at getfilename1.pl li +ne 10. print " @{[ This::Module::_Filename ]}\n"; #Bareword "This::Module::_Filename" not allowed while "strict subs" in + use at getfilename1.pl line 13.
      Now I am a little lost. Questions:
      What is the syntax for getting a package constant outside the package? Should I export it?
      Is 'use CONSTANT' same as 'use constant', from constant.pm?
      TIA
      Rudif
        Yes, it's amazing how many things can be wrong in a single post when you type it late at night. Or whenver I was typing it. {grin}

        Something more like this is gonna work a whole lot better.

        use vars qw(_Filename); _Filename = __FILE__;
        Sorry for the wild goose chase.

        -- Randal L. Schwartz, Perl hacker


        Update: OK, I give up on this thread. See the right way to do it, below. {grin}
RE: How do I get the name of the file from which the class was loaded?
by Adam (Vicar) on Oct 23, 2000 at 22:40 UTC
    Can't you get this from caller?
    (caller(0))[1]
    aught to be the file name the routine is declared in. (Not that I've tested that or anything.)

    Of course, getting it from __FILE__ is more straight-forward. 8-)