Update: Wow, it took me a long time to test and compose this (there weren't any replies when I started). The other replies indicate a closure problem, so I may be barking up the wrong tree here.

Update 2: Yup. Wrong tree. Wrong yard, even. For some reason (I'll blame fatigue) I completely missed the fact that the ellipses in the eval meant "some code deleted here" (even though I understood that perfectly well in the rest of the OP) rather than literal dot-dot-dot's as part of the print statement. Duh. :-)


From eval for eval EXPR:

the return value of EXPR is parsed and executed as if it were a little Perl program
and later, as part of an example, we are told that eval "$x"; "run[s] the code contained in the variable $x".

I'm going to jump to the end in an attempt to explain what I think might be going on. Your example has $var = 'this string', but since eval is running code it might be easier to follow what's going on if we changed that to something else, say $var = 'print "hi\n"'. I also added a call to mysub() and simplified that routine.

my $var; mysub(); sub initialize { $var = 'print "hi\n"'; print "in initialize: $var\n"; } sub mysub { print "in mysub: $var\n"; } BEGIN { initialize() }
This prints:
in initialize: print "hi\n" in mysub: print "hi\n"
This example shows that $var is set to the expected value in mysub, so the problem must be with the eval.

Now change mysub as follows (a simplification of your original eval):

sub mysub { print "in mysub: $var\n"; print eval("$var"), "\n"; }
The output is:
in initialize: print "hi\n" in mysub: print "hi\n" hi 1
Adding eval("$var") added two lines of output, hi and 1. The first line ("hi") is from evaluating the code in $var (print "hi\n"), and the second is from printing the return value ("1") from the eval.

Now change mysub to your original code and print $@ in case there are any errors in the eval:

sub mysub { print "in mysub: $var\n"; print eval("...$var..."), "\n"; print $@ if $@; }
Output:
in initialize: print "hi\n" in mysub: print "hi\n" syntax error at (eval 1) line 1, near "..."
$var is interpolated and the code in the eval is executed, but that code (...print "hi\n"...) contains a syntax error (it's a bit more obvious now, isn't it?).

Finally, change mysub to:

sub mysub { print "in mysub: $var\n"; print eval("$var"), "\n"; }
(you can also remove the quotes around $var in the eval). Output:
in initialize: print "hi\n" in mysub: print "hi\n" hi 1
which behaves as we would expect.

In summary, the problem had nothing to do with initializing the variable, it was due to the fact that the ellipses in your print statement created syntactically invalid code. Moral of the story: check $@ after an eval. :-)


In reply to Re: quantum behavior in perl? by bobf
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.