Thanks very much for your detailed reply ..... things are now much clearer if a little different from what I am used to ;)
"Bob Showalter" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Andrew Kennard wrote: >> Hi all >> >> I've got the basic hang of scope of vars in Perl which is a bit >> different to other languages I've used. >> >> Having split a large program into packages I would like to use a >> global var across multiple packages. Perl does not seem to have a 'C' >> type 'extern' command to say a var is declared in another package. >> Perhaps it does not need one ? Would I be correct in thinking that >> the %DataFromHTMLPage in the following example would always be >> pointing to the same bit of memory across the packages ? > > Yes, because you've used the Export module, which works in conjunction > with > "use" to provide "sharing" of global variables. > > All package (aka "global", or "symbol table") variables have a > "fully-qualified" name, of the format PackageName::SymbolName. So the > "full" > name of your hash is: > > %myglob::DataFromHTMLPage > > You can unambiguously reference that hash from any package by using its > fully qualified name. If you leave off the package name and just write > %DataFromHTMLPage, then the *current* package is used. > > Because of the 3 lines beginning with "require Exporter", when you "use > myglob" from another package, Exporter creates an "alias" to the variable > back in package myglob. > > So, if you have the following: > > package Foo; > use strict; > use myglob; > print keys %DataFromHTMLPage; > > The last line is referencing the hash %Foo::DataFromHTMLPage. However, > because of the Exporter stuff within myglob.pm, %Foo::DataFromHTMLPage is > actually an alias to %myglob::DataFromHTMLPage. So the two packages are > now > "sharing" the same hash. > >> >> "myglob.pm" >> package myglob; >> use strict; >> use CGI::Carp qw(fatalsToBrowser); >> require Exporter; >> our @ISA = qw(Exporter); >> our @EXPORT = qw(%DataFromHTMLPage); >> our %DataFromHTMLPage; >> --------------------------------- >> "myscript.pl" >> use strict; >> use CGI::Carp qw(fatalsToBrowser); >> use myglob; >> use mypkg1; >> use mypkg2; >> >> # load data into %DataFromHTMLPage and do stuff with it >> -------------------------------------- >> "mypkg1.pm" >> package mypkg1; >> use strict; >> use CGI::Carp qw(fatalsToBrowser); >> use myglob; >> #Do stuff here with %DataFromHTMLPage using functions etc >> ----------------------------------------- >> "mypkg2.pm" >> package mypkg2; >> use strict; >> use CGI::Carp qw(fatalsToBrowser); >> use myglob; >> #Do stuff here with %DataFromHTMLPage using functions etc >> >> >> If I don't "use myglob" in the package files then strict causes an >> error to say that %DataFromHTMLPage is undefined. > > Yes, because variables imported (via Exporter) get a "free pass" from use > strict. > >> >> My main question is if you define a var with 'our' if there is a >> 2nd/3rd occurance of the definition of it what happens ? > > Nothing really. "our" is just a declaration that says "let me use this > variable name without the full package qualifier from here to the end of > the > enclosing block or file". -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>