Results 1 to 1 of 1
Need Understanding Forum: HTML ForumReplies: 2
Need help with understanding hosting and search engines Forum: Web Hosting ForumReplies: 1
04-15-2005, 05:37 AM #1
Globals - Packages - Lexicals - help with understanding them please!
I have tried to start using ( use strict; )
I'ts caused me major problems with bad variable usage I was unaware of because I didn't fully understand the way they worked. After investigation I have come to this conclusion and would like clarification if I have grasped the use of variables correctly.
Perl's global variables are only global in the package they are in.
Each variable type has a heirachical structure so a $main::varname is only a global in the package "main" and ( my $varname ) although stored in the "main" package is in it's own area for lexicals and they have precidence over "package globals".
so lexical $varname does not overwrite $main::varname
but "print $varname;" will only access lexical $varname.
so if you just use "$varname = value" (use strict) will moan that it doesn't know if the variable is a lexical $varname or a package global $varname and even what package $varname is meant to be in.
Even if the default package is main - you still have to tell it what package you are refering to - go figure, why have a default package but can't default to it (ok unless you use [ our $varname ]) but that falls appart if you inadvertantly create a lexical of the same name!.
However I now see that you can create another package "on the fly" just by defining the variable $glob::varname.
so if i want in effect to use application globals via a require file, i define a package for those variables and refer to them correctly by specifying the package name infront of the variable i want to use.
HOWEVER - the use of a global in my context is really for a CONSTANT, I have now found this and would like clarification..
could I have saved all this time and hastle if i'd know that you can set Globals for the use i need with...
use constant DOM = "http://www.mydomain.com";
would i then simply use it like so
print "<img src=\"" . DOM . "/images/imagename.jpg\">";
From reading this article http://www.perl.com/doc/manual/html/lib/constant.html
I understand contants to be at the highest level of heirachy, and are either a list or a scalar so do not require a qualifier infront of for scalar i.e. ($).
you just use VARNAME.
is that right.