How can I get the name of the current subroutine in Perl?

前端 未结 4 1666
误落风尘
误落风尘 2020-12-29 03:14

In Perl we can get the name of the current package and current line number Using the predefined variables like __PACKAGE__ and __LINE__.

Li

4条回答
  •  隐瞒了意图╮
    2020-12-29 03:28

    Use the caller() function:

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

    This will give you the name of the current subroutine, including its package (e.g. 'main::test'). Closures return names like 'main::__ANON__'and in eval it will be '(eval)'.

提交回复
热议问题