Here is a small program that displays the exact problem I encountered. It has to have at least two files to display this behavior.

The first is a file I call temp.pl which contains

use temp1; temp1::init(); print "Calling work1: "; temp1::work1('$var'); print "\nCalling work2: "; temp1::work2('$var');

The second file is a package temp1.pm which contains

package temp1; my $var; sub init { $var = 1 } sub work1 { print eval $_[0]; } sub work2 { print eval $_[0]; $var = $var; } 1;

The resulting output is:

Calling work1: Calling work2: 1

What is really impressive is that the print in &work2 works even though the modification is after the print statement! This is beyond quantum.

If I change the my $var to our $var everything works.

tye explains that perl, not seeing any mention of the lexical $var in &work1, assumes that &work1 does not use it, so the context of the eval does not define that variable. Doing the 'right thing' would be too expensive.

This is clearly an unsafe optimization, so it appears that some warning is needed. The documentation about the eval function mentions nothing of the sort. Clearly having compiler warnings for every instance of eval seems ridiculous, but I feel that something about this needs to be in the main documentation about the eval function.


In reply to Re: quantum behavior in perl? by b4swine
in thread quantum behavior in perl? by b4swine

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.