David B
David B

Reputation: 29968

How can I tell which subroutine I'm in?

Is there a way to get the name of the enclosing subroutine of a piece of perl code? For example:

sub foo { print where_am_i(); }

will output 'foo'.

Upvotes: 5

Views: 231

Answers (1)

Eugene Yarmash
Eugene Yarmash

Reputation: 149726

Use the caller function:

my $function = (caller(0))[3];

Upvotes: 17

Related Questions