http://www.perlmonks.org?node_id=248787


in reply to Re: Surpised by foreach iterator limitation
in thread Surpised by foreach iterator limitation

Umm.. I wasn't attempting to iterate through a hash. I was trying to extend a validation function that takes a hash as it's argument to accept multiple values in the laziest way possible. $hash{key} is a scalar variable and can be localized, so I assumed it could be used any place a scalar could which was the point of the post.

-Lee

"To be civilized is to deny one's nature."
  • Comment on Re: Re: Surpised by foreach iterator limitation

Replies are listed 'Best First'.
Re: Re: Re: Surpised by foreach iterator limitation
by pg (Canon) on Apr 08, 2003 at 03:12 UTC
    Okay, now I see, however, $hash{key} is even not a valid name for a scalar variable.br>
    Be careful about this subtle difference: $hash{key} is the valid syntax to point to a hash element, which is a scalar, but it is not the valid syntax to name a scalar.

      Note that you can do:

      local $hash{$key} = 1; local $array[$index] = 3;

      Since foreach() works a lot more like local() than my(), there is no reason why it should not work, other than the fact that it currently does not. Somebody suitably motivated could make a patch to get this to work, and with enough convincing, the perl5-porters may accept it into mainstream perl 5.10.

        Wrong.

        use Data::Dumper; %hash = (key => 1); local $hash{key} = 2; print Dumper(\%hash);# print 2 print $hash{key};# also print 2.
        This clearly demos that $hash{key} is not another scalar, it simply refers to a hash element, which is a scalar. Again, please try to understand the subtle difference between "valid syntax to name an element of a collective, which happened to be a scalar", and "valid syntax for scalar name".

        They will support it in Perl 5.10? Hope this will never happen ;-)

        Let's look at from a different perspective. In c, you can say,
        int a[200];
        You can also say,
        a[100] = 2;
        but is a[100] a valid variable name? no, it is NOT. It can be used to call an array element, but it is not a valid variable name. It is the same situation here.
      But it is a valid scalar value which was why I was suprised.

      -Lee

      "To be civilized is to deny one's nature."