If the package correctly exports variables/subs, they will be available in the namespace you are in. In the case of the CGI module, you can choose which elements are exported, eg# CGI post max $CGI::POST_MAX = 4096; # if you are using strict, the global # variable age in your 'main' script # would need to be like this $main::age = 31; # or (same) with implicit namespace of 'main' $::age = 31;
Take a look at the module for examples of other possible values.use CGI qw/:standard/; print header(), start_html(), h3('hello'), end_html();
Using 'my' to declare variables is best practice where possible. If you really do need truely global vars, then define by prepending "namespace::".
If you are not using strict, just defining a variable implies that the variable is global, ie:
# no strict, so.. $age = 31 # is the same as these (in the 'main' script): $::age = 31 $main::age = 31
If you look, you will see that CGI.pm is NOT using 'strict', so it can define global variables such as $POST_MAX that are global, without having to explicitly using the package name.
But because these are global, you can access them from other packages through the namespace, eg:
$CGI::NPH=1
Still not very clear, is it? :) Why not create some play packages to experiment until this all sinks in?
Just my morning .02
cLive ;-)
Update: - please read tilly's comments below as well for info on stuff I didn't know about (our & vars - I still use Camel V2...). Also, added 'of main' to 'implicit namespace in code to avoid (possible) confusion. (All I meant was that $main:: and $:: are equivalent, with the main being implied in the $:: case. Not as Tilly thought, that $:: implies the current package.
In reply to Re: About packages and scopes
by cLive ;-)
in thread About packages and scopes
by nlafferty
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |