How to get a link to a function from a function?

Possible duplicate:
In Perl, how can a routine get code that points to itself?

Is there a way to get a function reference from this function without using a name?

I recently discovered that I repeatedly write code that smells like an anti-pattern. Data :: Dump supports filters, but (since version 1.16) they are not applied recursively. To get around this, I wrote these things:

sub filter {
    my ($context, $node) = @_;
    # ...
    return { dump => dumpf($something, \&filter) };
}

It works, but the link \&filterstarts to scare me. This creates maintenance costs if the function is renamed or copied elsewhere as a template for a new filter. I would like to replace it with something like __SUB__(if Perl had one).

+3
1

caller, , :

sub foo {
    state $self = \&{(caller(0))[3]};
    #...
    # call $self->();
}

, "" main::__ANON__.

+3

Source: https://habr.com/ru/post/1751232/


All Articles