3 ms·
I work in Perl for the most part, and the alternative I've been using lately for functions is to assert that for a given generated sub, the text 'sub routine_na
by jerf 13y ago
I work in Perl for the most part, and the alternative I've been using lately for functions is to assert that for a given generated sub, the text 'sub routine_name' exists literally in the code somewhere. This takes two forms in my case:
my %to_generate = (
'sub first' => { ... },
'sub second' => { ... }
);
generate_from_spec(%to_generate);
generate_from_spec then strips off the 'sub ', and errors out if it is not found.
The second case I have is a function that itself then generates a function, but is trying to look like a sort of normal function itself, in which case I end up with:
generate_sub routine_name =>
... whatever arguments ...
"abusing" Perl's => operator, which functions like a comma except that it forces stringification of the left argument, to once again make the literal, greppable "sub routine_name" appear in the codebase. Here "routine_name" is then just a standard string argument, the alterative being
generate_sub "routine_name",
which is then harder to grep for. (Still possible, obviously, with a different grep query, but only if you already know up front you need to add the other possibilities.)
Note this actually goes a step beyond what you are proposing in that it makes the declaration site clear; my counterproposal for your JS case would be
["function getPosition", "function getDirection"].each(...)
and using string manipulation to do whatever you need to do to get the right info out of the function name.