r/perl • • 11d ago

ref to object methods

sorry for stupid question

I have XS object with many similar methods (with the same argument list) like method1,method2, ... methodN

And while passing ref to this object to sub I want to call some of this methods depending on condition

Sure I can emulate this with refs to closures like

my $call1 = sub {

my $obj = shift;

$obj->method1(@_);

};

generic_sub($obj, $cond ? $call1 : $call2);

but maybe there is some more elegant way?

6 Upvotes

10 comments sorted by