Ephemeral methods, or what to call 'dispatch to a variable containing a subref'?
In my last post, I use a Perl dispatch pattern where you store a subroutine reference in a variable and then use that as the method to call on an object. Like so:
my $method = sub { ... };
$invocant->$method(@args);
Is there a name for this pattern? If not I propose to call the “method” an “ephemeral method”. Does this work for everyone?
Note that you really can’t call it a lexical method for several reasons.
Personally I call it a lexical method.
Recent versions of Moops have facilities for lexical methods and lexical accessors. (Example.)
I wonder how this would relate to the return value of can(), and overloading can to return a method specialized to the instance that it was called on.
IMO it can’t be called a lexical for two reasons. 1) there now are lexical subroutines which can behave as standard methods in that scope 2) the variable need not be lexical, a package variable can be used too!
Actually, lexical subroutines can’t be called as methods…
… unless you jump through some serious hoops…
I didn’t know that, I thought I had seen a demo where that works. Anyway even so the name might be confusing. I still support my idea. :-)