Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a function tell which module it was called from?

Tags:

perl

package Bar;
use Foo;

sub bar { fooit "hello from bar"; }

package Foo;

sub fooit {
   # Somehow I want this function to know it was called
   # from the "Bar" module (in this case).
}

Preferably, this would be done without explicitly passing an argument containing the calling module's name.

like image 507
gcbenison Avatar asked Jan 14 '23 00:01

gcbenison


1 Answers

The builtin caller function can be used to get information about the current call stack.

 sub fooit {
     my ($pkg, $file, $line) = caller;
     print STDERR "fooit was called from the $pkg package, $file:$line\n";
 }
like image 146
mob Avatar answered Jan 29 '23 14:01

mob