Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl, how to print the package name?

Tags:

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

like image 805
MarcoS Avatar asked Aug 10 '11 13:08

MarcoS


People also ask

What is package name in perl?

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.

What is a perl namespace?

In perl namespaces are called "packages" and the package declaration tells the compiler which namespace to prefix to our variables and unqualified dynamic names.


2 Answers

From perldoc perlmod:

The special symbol __PACKAGE__ contains the current package, but cannot (easily) be used to construct variable names. 
like image 177
martin clayton Avatar answered Sep 23 '22 02:09

martin clayton


__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(); 
like image 37
ikegami Avatar answered Sep 19 '22 02:09

ikegami