Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: my() and our()

by xdg (Monsignor)
on Aug 09, 2005 at 18:48 UTC ( [id://482367]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://482367]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-26 08:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found