Is it possible - in Perl - to access the name of the current package (for example, to print it in a customized error report) ?
A Perl package is a collection of code which resides in its own namespace. Perl module is a package defined in a file having the same name as that of the package and having extension . pm. Two different modules may contain a variable or a function of the same name.
In perl namespaces are called "packages" and the package declaration tells the compiler which namespace to prefix to our variables and unqualified dynamic names.
From perldoc perlmod:
The special symbol __PACKAGE__ contains the current package, but cannot (easily) be used to construct variable names.
__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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With