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', };

In reply to Re: CORE::GLOBAL::caller reports strange lines around coderef definitions by stevieb
in thread CORE::GLOBAL::caller reports strange lines around coderef definitions by ed_hoch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.