Answer the question
In order to leave comments, you need to log in
How to find out the name of the current function?
Good day !
Help solve the problem! you need to return the name of the called method for an anonymous function!
package method;
use strict;
use warnings FATAL => 'all';
my @list_method = qw(method_1 method_2 method_3);
sub new {
my ($class, $options) = @_;
__genereate_method();
return bless {}, $class;
}
sub __genereate_method {
no strict 'refs';
foreach my $element (@list_method)
{
*{__PACKAGE__."::$element"} = sub {
my $self = shift;
my $sub_name = (caller(0))[3];
return $sub_name ;
}
}
}
1;
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use FindBin;
use lib "$FindBin::Bin";
use method;
my $method ||= method->new();
isa_ok($method, "method");
foreach my $element (qw(method_1 method_2 method_3))
{
can_ok($method, $element);
}
is($method->method_1, 'method_1');
is($method->method_2, 'method_2');
is($method->method_3, 'method_3');
done_testing();
Answer the question
In order to leave comments, you need to log in
It's decided.
package method;
use strict;
use warnings FATAL => 'all';
my @list_method = qw(method_1 method_2 method_3);
sub new {
my ($class, $options) = @_;
__genereate_code();
return bless {}, $class;
}
sub __make_method {
my ( $method, $code, $pkg ) = @_;
$pkg ||= caller();
no strict 'refs';
*{ $pkg . "::$method" } = $code;
}
sub __genereate_code {
foreach my $element (@list_method)
{
__make_method($element, sub { return $element; } );
}
}
1;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question