Outside a situation that expects a bareword (such as the LHS of => and print's first argument), a bareword is compiled into a function call only if a function by that name exists at that time. Otherwise, it is compiled as a string constant (strict off) or results in an error (strict on).

use executes as soon as it compiles, so the function it imports are present before the rest of the file is compiled. However, you deferred the execution of use to when the eval is executed, which is only after the entire file (including what you want to be a call to gettimeofday) has been compiled.

Remember that use Module; is the same as BEGIN { require Module; import Module; }, so the solution is to reintroduce BEGIN. Put a BEGIN block around the while and you'll get the desired behaviour. The loop will execute as soon as it finishes compiling, which is before the compiler reaches "gettimeofday".

Update: Reorganized text to improve readability.


In reply to Re: Interesting behavior in eval block by ikegami
in thread Interesting behavior in eval block by l2kashe

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.