in reply to How do I get the name of the file from which the class was loaded?

I'm a little lost. Is there something wrong with using this:
package This::Module; use CONSTANT _Filename => __FILENAME__; ... package Whatever; print $This::Module::_Filename;

-- Randal L. Schwartz, Perl hacker


Update: My memory cache was bad while typing this.... the code in "RE: RE: Re: How do I get the name of the file from which the class was loaded?" works a whole lot better.
  • Comment on Re: How do I get the name of the file from which the class was loaded?
  • Download Code

Replies are listed 'Best First'.
RE: Re: How do I get the name of the file from which the class was loaded?
by Rudif (Hermit) on Oct 23, 2000 at 15:55 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}
        >Something more like this is gonna work a whole lot better.
        > use vars qw(_Filename); > _Filename = __FILE__;<br>
        ... yes, after a tweak or two ;-)
        use strict; use vars qw( $_Filename ); $_Filename = __FILE__; print $_Filename;
        Rudif