MarcoS
MarcoS

Reputation: 17711

Perl, how to print the package name?

Is it possible - in Perl - to access the name of the current package (for example, to print it in a customized error report) ?

Upvotes: 26

Views: 12538

Answers (2)

ikegami
ikegami

Reputation: 385764

__PACKAGE__ will get you the package in which the code was compiled.

Alternatively, you might want caller. It gets the package of the code that called the current sub.

package Report;

sub gen_report {
   my $report = "This report is generated for ".caller().".\n";  # MyModule
   ...
}

package MyModule;

Report::gen_report();

Upvotes: 11

martin clayton
martin clayton

Reputation: 78105

From perldoc perlmod:

The special symbol __PACKAGE__ contains the current package,
but cannot (easily) be used to construct variable names.

Upvotes: 35

Related Questions