[prog] Perl and including files

Sue Stones suzo at bigpond.net.au
Wed Mar 19 18:06:20 EST 2003


jacinta, that was very helpfull, and I have one more question.  

I  want to be able to set up these variables in the .pm file so that they are 
available to the other files.

$rootDir = "$ENV{DOCUMENT_ROOT}";
$logDir = "/sue";
$fileDIr = $rootDir . $logDir;

The aim is to minimize the changes to be made when the scripts are transerferd 
to other machines.  The variables won't be changed by other code.

I have tried including the following in the GKLog.pm file (withput 
reallyknowing what it is doing).

BEGIN {
	use Exporter();
		@GKLog::ISA = qw(Exporter);
		@GKLog::EXPORT = qw();
		@GKLog::EXPORT_OK = qw($rootDir $logDir $FileDir);
}
use vars qw($rootDir $logDir $fileDir);

In the other files I have the following
sue GKLog qw($logDir $fileDir);

my $logDir = GKLog::logDir;
my $fileDir = GKLog::fileDir;

(NOTE: I have retyped this here, not copied it, spelling mistakes are 
extreemly likely under all conditions!)

However when I run this $logDir and $fileDir are uninitialised.

Am I missing something here, or misunderstanding something?  Can you give me 
any help/advise?

jacinta, I noticed that your website (which was very usefull) and others that 
I looked at once your answer told me what to look for, mentioned the cammel 
book as the place to go to for these questions.  Which book is that? 
Programming Perl?

sue


On Mon, 17 Mar 2003 10:16 am, Jacinta Richardson wrote:
> > Is it possible to include files in perl.  I have just written something
> > that has 3 files in it and they all contain the same constants and a
> > couple of common functions.  It would be helpfull if these could be kept
> > in a common file that could be included in all three as one would with C.
> > The constants do need to be changed and it would be better if they don't
> > need to be changed 3 times.
>
> Yes it is very possible and done all the time.  One way is the following:
>
> ______ file:  MyConfig.pm ________
>
> package MyConfig;	# This starts a new package space
> use strict;
>
> $::db = "mydatabase";   # Create these variables in the main global
> $::username = "jarich"; # namespace (something that you shouldn't
> $::password = "jarich"; # really do)
>
> sub fish		# This sub is in the MyConfig name space
> {
>      return "one fish, two fish, red fish, blue fish";
> }
>
> 1;		# Perl packages have to end with something that is true.
>
> ______ file: stuff.pl  _______
> #!/usr/bin/perl -w
> use strict;
> 			# These are global.  Need to declare them before
> 			# we set them
> use vars qw/$db $username $password/;
>
> use MyConfig;		# use our config file
>
> 			# these values are set now.
> print "$db, $username, $password\n";
>
> 			# Note that to call this function we have to say
> 			# from which package.
> print MyConfig::fish(), "\n";
>
> __END__
>
> This will work and is the way I suggest you go about doing this if you've
> already written the code and aren't able to make many changes.  A nicer
> way to do the same thing is as follows:
>
> ______ file:  MyConfig.pm ________
>
> package MyConfig;       # This starts a new package space
> use strict;
> 			# Make these values global in the MyConfig
> 			# namespace only
> use vars qw/$db $username $password/;
>
> $db = "mydatabase";	# Set the values
> $username = "jarich";
> $password = "jarich";
>
> sub fish                # This sub is in the MyConfig name space
> {
>      return "one fish, two fish, red fish, blue fish";
> }
>
> 1;              # Perl packages have to end with something that is true.
>
>
> ______ file: stuff.pl  _______
> #!/usr/bin/perl -w
> use strict;
> use MyConfig;           # use our config file
>
> my $db = $MyConfig::db;			# Pull each value from MyConfig when
> my $username = $MyConfig::username;	# we need it.
> my $password = $MyConfig::password;
>
>                         # these values are set now.
> print "$db, $username, $password\n";
>
>                         # Note that to call this function we have to say
>                         # from which package.
> print MyConfig::fish(), "\n";
>
> __END__
>
>
> This is a much neater way of doing it because we're not messing around
> with the main package space from outside it.  Note that if you declare
> your variables inside of MyConfig.pm with "my"  instead of "use vars" (or
> "our" - "perldoc -f our") then almost no matter what you try you will not
> be able to access them outside of MyConfig.pm.
>
> Of course you can do:
>
> --- file MyConfig.pm  ---
> package MyConfig;
> use strict;
>
> my $dbname;
> sub get_dbname
> {
>      return $dbname;
> }
>
> 1;
> ----- file stuff.pl -----
> #!/usr/bin/perl -w
> use strict;
> use MyConfig;
>
> my $dbname = MyConfig::get_dbname;
>
> __END__
>
> so it's not completely impossible.... but only if the get_dbname sub
> exists.  Accessing $MyConfig::db will only return undefined if $db was
> declared with "my" inside MyConfig.pm.
>
> Anyway for some more information about packages and modules, check out our
> OO training notes at http://www.perltraining.com.au/notes.html  you don't
> need to understand OO programming to understand the chapters on these,
> it's chapters 3 and 4 if I remember correctly.
>
> Hope this helps.
>
>            Jacinta Richardson



More information about the Programming mailing list