Let us say if I have a Perl module Resounces.pm with this code snippet
my $my_class = new MY_CLASS;
our resource => { '/list' => $my_class->list_files() };
So can I call this resource variable in another perl script something similar like this?
use Resources;
$Resources::resource->{'/list'};
My intention is to execute list_files() method when I use that last statement. Is that even possible?
One option is to use an anonymous sub:
Should you change what
$my_classreferences, it will also be changed in the anonymous function. This may be what you want; if not, keep reading for a function (bindMethod) that binds an object and a method.If you don’t want the anonymous function to appear on the stack:
Note that this form of
gotoisn’t the usual goto statement—it’s a tail call.You could define a helper method to set up the binding, though honestly it doesn’t gain you much.
Here’s a version of
bindMethodthat doesn’t require the class to be specified with the method name.