Anonymous functions which are a key in a hash will get the line number of the hash declaration.
Opened this issue · 2 comments
schwern commented
Here's an example.
#line 15
my $hash = {
over => 'under',
left => 'right',
key => func() {
return __LINE__;
},
foo => "bar",
up => "down",
};
is $hash->{key}->(), 19;
is __LINE__, 26;
It doesn't matter how many hash pairs come before the function, it will always return 16. The function thinks it starts on the same line as the hash.
This has been a problem since at least v20090424. This does not appear to be a mistake on our part, but something wrong with the compiler / Devel::Declare.
This a particular problem in classes which use Moose.
has foo =>
is => 'rw',
default => method() {
return $self->bar + 23;
};
schwern commented
Wrapping the function in a do block fixes the problem. Unfortunately we have no way of doing that.
schwern commented
Function::Parameters doesn't have this problem. It uses the keyword plugin API instead of Devel::Declare. It might be more prudent to adapt it to Method::Signatures. That would mean raising the minimum requirement to 5.14.