Perl,如何打印包名?

Mar*_*coS 24 perl package

是否可以 - 在Perl中 - 访问当前包的名称(例如,在自定义错误报告中打印)?

mar*_*ton 34

来自perldoc perlmod:

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


ike*_*ami 10

__PACKAGE__ 将为您提供编译代码的包.

或者,您可能想要caller.它获取调用当前sub的代码包.

package Report;

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

package MyModule;

Report::gen_report();
Run Code Online (Sandbox Code Playgroud)