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

Dear Monks

Is there a special var or some other way that tells me the filename of the current module?

I want to do something like:

my $package = __PACKAGE_FILENAME__; my $related = $package; $related =~ s/this.pm/other.pm/; if ( -e $related ) { require $related; else { die "related package missing: $related"; }

Thanks!

Jeff

Replies are listed 'Best First'.
Re: Filename of current package
by amw1 (Friar) on Mar 22, 2004 at 18:39 UTC
    caller may give you what you want.
    foo.pl
    use Data::Dumper; sub callstack { return caller(); } print Dumper(callstack());
    This prints
    $VAR1 = 'main'; $VAR2 = 'foo.pl'; $VAR3 = 9;
    Var1 is the calling package (in this case main) Var2 is the calling file Var3 is the line where it was called

      Thanks for the pointer, but I'm still not able to find the filename of the package. I have played a little with caller(), but it seems to provide details of the calling program, rather than the included module?

      Here is more what I am looking to do:

      # my.pl use My::Factory; my $obj = My::Factory->new('Apple'); ... # My/Factory.pm package My::Factory; 1; sub new{ my $factoryclass = shift; my $class = "My::" . shift; # My::Apple my $filename = $class; $filename =~ s/::/\//g; $filename .= '.pm'; # My/Apple.pm if ( -e $filename ) { require $filename; } else { die "not found: $filename"; } }

      Which all looks good and dandy, except that My::Factory is picked up from @INC at runtime. I thought that a package might know its own filename?

      Note that I don't want to pick up My::Apple from somewhere else in @INC - I want it to be in the same place as My/Factory.pm

      Apologies if that is not clear...

      Off to see if I can figure out how Carp does it...

      Regards

      Jeff

Re: Filename of current package
by ambrus (Abbot) on Mar 22, 2004 at 20:08 UTC

    See perldoc perldata:

    The special literals __FILE__, __LINE__, and __PACKAGE__ represent the current filename, line number, and package name at that point in your program. They may be used only as separate tokens; they will not be interpolated into strings. If there is no current package (due to an empty "package;" directive), __PACKAGE__ is the undefined value.

      Even better than caller() - thank you very much! +++
Re: Filename of current package
by jaa (Friar) on Mar 22, 2004 at 19:38 UTC

    amwl - thank you, caller() is the key, and Carp::Heavy has the detail - my problem was that I was doing it in the new() - checking caller in a sub-routine of the same package provides the answer:

    package My::Factory; 1; sub new { my $factory = shift; my $name = shift; my $classfile = package_name(); $classfile =~ s/Factory.pm$/$name.pm/; if ( -e $classfile ) { require $classfile; my $classname = "My::$name"; return $classname->new(@_); } else { die "class not found: $classfile"; } } sub package_name { my (@called) = caller(0); return $called[1]; }