in reply to CORE::GLOBAL::caller reports strange lines around coderef definitions
Interesting. I can't say why this happens, but if you pre-build the cref before you insert it into the hash, caller() will report the first line of the href as the actual caller (line 4):
sub foo { print caller(0); } my $cref = sub { return 1; }; my $h = { a => 1, b => foo(), d => 'foo', c => $cref, m => 'bar', };
Perhaps upon building the hash, the anonymous sub has to be seen/compiled before foo() actually gets called (which is why caller states the line immediately after)? perlsub says that anonysubs are compiled at runtime, so this actually makes sense.
Update:Note that the following works exactly as you'd expect. perl has to delve into the hash far enough to compile the anonymous sub, and that's it. If there isn't any runtime compiling to do in the hash, the caller will actually appear as the first line of the hash definition:
sub foo { print caller(0); } my $h = { a => 1, c => sub { return 1; }, b => foo(), d => 'foo', m => 'bar', };
|
|---|