Well, it does work, as local not only squirrels away the
old global value, but sets it to undef for the loop.
However, upon writing some example code, I noticed a
strange difference between two global variables, $/
and $^T. Both mean something, and always have values
(and are thus defined) at the start of a script. However,
$/ gets set to undef when used with a local, while $^T
gets set to "0". [Luckily, $/ does not get set to 0,
or my examples would not work. :)] Anyone know why
$^T acts like that? Here's the code, and the results.
This is perl 5.005_03, FWIW.
$MY = 1;
$LOC = 1;
{
local $LOC; local $LOC2; local $/; local $^T;
my $MY; my $MY2;
printf "LOC defined: %s\n", defined $LOC ? "YES" : "NO";
printf "LOC2 defined: %s\n", defined $LOC2 ? "YES" : "NO";
printf "IRS defined: %s\n", defined $/ ? "YES" : "NO";
printf "TIME defined: %s\n", defined $^T ? "YES" : "NO";
printf "MY defined: %s\n", defined $MY ? "YES" : "NO";
printf "MY2 defined: %s\n", defined $MY2 ? "YES" : "NO";
print " LOC=$LOC, LOC2=$LOC2, TIME=$^T, IRS=$/, MY=$MY, MY2=$MY2\n"
+;
}
print "AFTER LOOP:\n";
print " LOC=$LOC, LOC2=$LOC2, TIME=$^T, IRS=$/, MY=$MY, MY2=$MY2\n";
Which produces:
LOC defined: NO
LOC2 defined: NO
IRS defined: NO
TIME defined: YES
MY defined: NO
MY2 defined: NO
LOC=, LOC2=, TIME=0, IRS=, MY=, MY2=
AFTER LOOP:
LOC=1, LOC2=, TIME=960648360, IRS=
, MY=1, MY2=
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.