Thanks, got it :-)

One more question about lexical variables. If I define a lexical variable in a block, what happens to the memory storing the value of the lexical variable after the code exits the block?

For example, in the script below. I cannot directly access $tricky outside the block. But it can be done with a reference. My question is if the memory storing the contents of $tricky has been released right after the block ends? If so, does it mean some other programs can write to that memory and when I try to retrieve the value of $tricky with a reference after the block, I may get something unexpected(memory got overwritten by other program)?

#!/usr/bin/pere my $reference; { my $tricky = "TRICKY"; $reference = \$tricky; } print "\$tricky is not defined\n" unless $tricky ; # What happens to the memory storing the contents of $tricky? # Has the memory storing $tricky been released? print "Using a reference, here you are: $$reference\n";

In reply to Re^2: Scope of lexical variables in the main script by sophate
in thread Scope of lexical variables in the main script by sophate

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.