Akira71 has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Perl Anomaly
by dragonchild (Archbishop) on Jan 14, 2005 at 16:09 UTC
    1. Variables declared with my are lexical variables. As such, they are not put into the package's symbol table.
    2. The syntax $y = 'z'; $x = ${$y}; is an example of using a soft reference. Aside from the fact that you shouldn't use them (instead, use hashes), this looks the variable name up in the package's symbol table.
    3. Since $B in your first example is a lexical variable, it cannot be found with the soft reference.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      Thank you veyr much for the simple and understood answer. It makes much sense to me now. I did not think I should use it that way, but once I stumbled onto the issue I wished to pursue it to a greater depth.

      Sincerely,

      Akira
      Just as a side note, I decided to force the variable $c into the symbol table using use vars qw{$c}; and then the use of my works in the first case.

      Not practicle, but enjoyable to look into.
        and then the use of my works in the first case.
        This seems unlikely. What happens when you declare a variable with "our" or "use vars" and then declare it again with "my" is that the second one essentially replaces the first one for the remainder of that scope. So if you refer to the variable through a sympolic reference, you get the global one, but if you refer to it directly you get the lexical one. For example,
        use vars qw($B); my $A='B'; my $B=7; my $C=${$A}; print "A = $A - B = $B - C = $C\n";
        prints "A = B - B = 7 - C =" This happens because no value is ever assigned to the global variable called $B. Wacky, huh?