Basically that opinion shows faulty but questions still remains. When is $i evaled? Or in what dictionary/environment/whatever-mechanism-is-used?

In your case, $i is evaluated when the string containing a literal '$i' is eval()ed. You could evaluate $i earlier by interpolating $i into the string (evaluating $i when the string is created), but then, that would convert the value of $i into a string, which isn't always what you want:

makeLazy(qq(print "\nnow its $i"));
Note that this also decouples the eval'd code in makeLazy from the actual current value of $i.

Because of the same problem i run into with my code (but i didnt knew until now that it was the same problem): if you defer the declaration of $i then you get Global symbol "$i" requires explicit package name
Well, what do you want it to do? Either you want a lexical variable, but then you must declared it in its lexical scope, or you may want a (possibly local()ized package variable) - which you don't have to declare, if you prepend them with the package name (or you *could* switch of strict 'vars').

update: Note that in my alternative version, using my $defer = sub  { something_with($i) } syntax, $i is evaluated when $defer->() is called, but the scoping of $i is resolved at compile time, and figuring out the actual instance of $i may be resolved when the $defer = .. assignment is executed (that last part is important if you're generating these $defer closures from within another subroutine, with $i declared lexical in the outer subroutine).

You may find this column useful


In reply to Re^3: Not-so-lazy evaluation? by Joost
in thread Not-so-lazy evaluation? by mtroost

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.