in reply to my() and our()
And if the othe various posts haven't illuminated yet, it's worth walking through some other examples step by step. (Including strict and warnings.)
Using all fully qualified globals:
use strict; use warnings; $main::var = 1; { $main::var = 2; print $main::var, "\n"; # prints 2 } print $main::var, "\n"; # also prints 2
Declaring within the block:
use strict; use warnings; $main::var = 1; { our $var = 2; print $var, "\n"; # prints 2 } print $main::var, "\n"; # also prints 2
Declaring within the block and trying to use unqualified later (on line 9) causes a compile-time error under strict -- the "our" only has its effect of allowing use of the variable name without fully qualifiying it while within the block:
use strict; use warnings; $main::var = 1; { our $var = 2; print $var, "\n"; # prints 2 } print $var, "\n"; # also prints 2
Typical usage "at the top" applies to the whole file:
use strict; use warnings; our $var = 1; { $var = 2; print $var, "\n"; # prints 2 } print $var, "\n"; # also prints 2
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
|
|---|