Connie Chan wrote: > In package A, I did sth like this : > > package A; > use strict; > require Exporter; > our @ISA = qw (Exporter); # What this actually for ?!
Exporter provides an easy way for you to make your module's variables and functions available for another package to use. the: our @ISA =qw(Exporter); thingy essentially translates to: "package A is a Exporter" when Perl sees this statement, it knows that your package wants to be a child class of Exporter so it makes all Exporter's publicly available methods for your package to use. it's(with the @ISA array) basically how Perl modules inheritance in OO design. One very simply examples might carify things a bit: ##### Base.pm: package Base; sub new{ return bless {'name'=>'base'} => shift; } sub get_name{ return shift->{'name'}; } 1; #### Child.pm: package Child; use Base; #-- here i make Child a sub-class of Base #-- so it reads "Child ISA(is a) Base" @ISA=qw(Base); #-- nothing else. Child doesn't have any function of it's own! 1; ###### test.pl: #!/usr/bin/perl -w use strict; use Child; #-- calls Base.pm's new() function. Because Child is a sub-class of #-- Base and Child do not has it's own new() function so Perl uses #-- Base's new() function instead my $c = new Child; #-- calls Base.pm's get_name() function. same reason as above. print $c->get_name(),"\n"; #-- prints base notice that i didn't use Exporter anywhere in Base.pm because i am not Exporting anything explicitly so no need for Exporter. notice also that i didn't use Exporter in Chil.pm also becuase i don't need any functionality from Expoter.pm, i only want the functions in Base so i set my @ISA to include Base > our @EXPORT = qw (%hash); > our %hash ; > > # code code code code and code ..... > # Do sth to assign values for %hash anyway > > 1; > > Then , in the main script, I will use A, B, and C, and D. > but what packages B, C, D are required to use A too. > > My question is, will perl re- assign the hash whenever > a 'use package A' declared ? or when Perl found the package > had loaded once, it won't load again ? > > The other question is , whatever y/n above, will the outcome(reload or > not) be difference when : > > 1. I directly assign values (ie $hash{A} = 1, $hash{B} = 2), > 2. using sub to assign value (ie. .... our %hash ; sub GenHash { ..... } > GenHash; 1; ) > > Rgds, > Connie the hash won't be loaded again even you have many other packages use it. again, consider the following: #### A.pm: package A; use Exporter; @ISA=qw(Exporter); @EXPORT=qw(%hash); $hash{'a'} = 1; 1; ####### test.pl: #!/usr/bin/perl -w use strict; use A; print $hash{'a'},"\n"; #-- prints 1; #-- now change it: $hash{'a'} = 9; #-- switch to another package package Another; use A; #-- now, if the 'use A' statement again cause %hash in A.pm to be loaded #-- again, it initializes itself '$hash{'a'} = 1' again right? print $hash{'a'},"\n"; #-- prints 9 instead __END__ if you think about it. if Perl allows you to export something, change it and loads it again and again when different package refers to them. your data will be inconsistence. becuase one module will loads it, modify it, use it. another will again loads it, modify it and use it. in that case, you will have different things in different module. david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]