Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass reference to $self->class->method?

Tags:

perl

I am calling a method as follows:

$self->class->method

I would like to pass a reference to this method as a parameter into a subroutine.

I have tried

\&{ $self->class->method }

but I get the following error:

Unable to create sub named ""

Any ideas on how to do this?

like image 702
Mauritz Hansen Avatar asked Jun 23 '26 20:06

Mauritz Hansen


1 Answers

You can take a reference to static class method, but in your case you could use anonymous closure to achieve similar,

my $ref = sub { $self->class->method };

# ..
my $result = $ref->();
like image 100
mpapec Avatar answered Jun 28 '26 20:06

mpapec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!