in reply to strict, scope, my and foreach - not behaving as expected

To answer your last question about. The use of "our" keeps the variable in scope and does not restrict $color to the current lexical scope. Basically, when used outside of any brace delimited block, it lasts through the end of the compilation unit ( Learning Perl p133) which, in your case, should be the entire program. Consequently, when you are inside the loop, $color refers to the variable you expect it to and not the locally scoped variable that occurs with "my". "our" also allows the subroutine print_color() to access the variable which the "my" declaration would not since lexical variables are hidden from any subroutine called from their scope.

PJ
We are drowning in information and starving for knowledge - Rutherford D. Rogers
What good is knowledge if you have to pull teeth to get it - anonymous
  • Comment on Re: strict, scope, my and foreach - not behaving as expected