in reply to Filename of current package

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

Replies are listed 'Best First'.
Re: Re: Filename of current package
by jaa (Friar) on Mar 22, 2004 at 19:27 UTC

    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