Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting module name within a perl module 1

Status
Not open for further replies.

rsg00usa

Programmer
Jun 25, 2002
3
US
I'd like to print the module name of the module I'm currently within. So say program foo.pl calls module bar.pm. Within bar.pm how do I get it's name. $0 refers to foo.pl not bar.pm.

Thanks,
Scott
 
The variable __PACKAGE__ contains the name of the current package.

jaa
 
So how do you actually look at the variable __PACKAGE__ in a script?

Is it
Code:
my $version = "__PACKAGE__ ";
In the begining
Let us first assume that there was nothing to begin with.
 
Well, if you were writing your own, you can create a method that returns the package name
Code:
sub get_package {

    return __PACKAGE__;
}
and call it from the main program as
Code:
use mypackage;

my $x = new mypackage;
my $package = $x->get_package;
Although, in the main program you can get the package name of an object by looking at its ref() value.
Code:
my $x = new mypackage;
my $package = ref($x);
It depends on where and why you need the packge name.
Take a look at the perlmod and perlref perldocs for some details.

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top