The "our" case makes sense to me if the for loop is localizing the variable inside the loop. Thus when you call the subs outside the loop $name is undefined until you set it to something different (outside the loop). Here is the output if you comment out the second set of tests:
----- $name is constant inside sub ------ Use of uninitialized value $name in concatenation (.) or string at /tm +p/test.pl line 17. red: <FONT COLOR=''></FONT> Use of uninitialized value $name in concatenation (.) or string at /tm +p/test.pl line 17. violet: <FONT COLOR=''></FONT> ----- sub used inside loop that defined it ------ trying out red: <FONT COLOR='red'></FONT> trying out blue: <FONT COLOR='blue'></FONT> trying out green: <FONT COLOR='green'></FONT> trying out yellow: <FONT COLOR='yellow'></FONT> trying out orange: <FONT COLOR='orange'></FONT> trying out purple: <FONT COLOR='purple'></FONT> trying out violet: <FONT COLOR='violet'></FONT> ----- sub used outside of loop that assigned it ------ Use of uninitialized value $name in concatenation (.) or string at /tm +p/test.pl line 52. red: <FONT COLOR=''></FONT> green: <FONT COLOR='something wacky and wonderful'></FONT> violet: <FONT COLOR='something wacky and wonderful'></FONT>
I'm not so sure what is happening in the my case.
Good Day,
Dean
In reply to Re: How do closures and variable scope (my,our,local) interact in perl?
by duelafn
in thread How do closures and variable scope (my,our,local) interact in perl?
by ELISHEVA
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |