I have recently run into a situation I found odd and thought perhaps those monks with some greater inner vision than myself could shed some light.
I have the following per program:
#! /usr/bin/perl
my $A='B';
my $B=7;
my $C=${$A};
print "A = $A - B = $B - C = $C\n";
The output of this snippet is this:
A = B - B = 7 - C =
I expected to see 7. It seems as though $c is empty in this case. I did not expect this at all as all of the variables are declared in the same scope. Thinking I was perhaps a little crazy and certianly understanding less perl than I should I embarked upon a solution to this problem. I did eventually came up with this:
#! /usr/bin/perl
my $A='B';
local $B=7;
my $C=${$A};
print "A = $A - B = $B - C = $C\n";
As expected the output is:
A = B - B = 7 - C = 7
Also of note, is that declaring all the variables as local or not declaring any of them with local or my works as expected. I understand vaguely the difference between my and local but the explanation for why all variables declared as my would fail this simple test.
Did I stumble across a defect?
Is this something I should not have done in Perl?
Please enlighten a lost soul.
Sincerely,
Akira
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.