Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can someone please confirm my Synopsis 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
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.

I would like someone to confirm that I have grasped variable storage so I can modify my scripts to all use (use strict).

But if i'm wrong I don't want to waste my time changing already bad code to just as bad code - that would we pointless.

Regards,
1DMF
 
Also can someone tell me if I could have saved all this time and hastle if i'd know that you can set Globals for the use i need with...
Code:
use constant DOM = "[URL unfurl="true"]http://www.mydomain.com";[/URL]

would i then simply use it like so
Code:
print "<img src=\"" . DOM . "/images/imagename.jpg\">";

From reading this article 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.
 
Not sure on the constants stuff but your take on the globals is correct.

Keep in mind that these are all local to the process running perl. So in a web context such global declarations are not shared across apache memory space. You can use modperl for that if you'd like. I've built some high performance systems that load all of the strings required for different languages into global memory space across all apache children. It works pretty cleanly.
 
modperl - is that basically writting a module and then all of your scripts use that module to get access to the globals - coz that's the way i've started to code mine.

If you mean something different - I think i'll wait till i've re-written the application properly using modules and the (use strict) pragma - then - i might look to do something a bit cleverer - after last weeks crash course in modules - variables and complex data structures - I need a holiday just to recover :)
 
modperl is a tool that integrates perl directly into the apache webserver resulting in huge speedups and lots of new functionality.

One of those functions is the ability to continue to maintain data between requests. With CGI after each request all your data is lost or unreliable.

 
AHH I see. I don't run my own web server so unless it's already installed - which is unlikely as it's shared hosting and that could cause serious problems if we got variable availability across the server.

sounds like fun though :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top