[prog] Perl and including files

Jacinta Richardson jarich at perltraining.com.au
Thu Mar 20 12:43:17 EST 2003


On Wed, 19 Mar 2003, Sue Stones wrote:

> 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. 

Certainly.  In your specific case I'd do the following:

______ GKLog.pm __________
package GKLog;
use strict;

use vars qw/$rootDir $logDir $fileDir/;
$rootDir = "$ENV{DOCUMENT_ROOT}";
$logDir = "/sue";
$fileDir = $rootDir . $logDir;

1;
__END__

(although technically I'd have:
$rootDir = "$ENV{DOCUMENT_ROOT}/";
$logDir = "sue";	# or "sue/"

because otherwise if $logDir is used on its own you might attempt to /sue
which would probably be a bad thing, especially if it existed.

In the other files I'd do:

---- otherfile.pl ------
use GKLog;

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

.... code

__END__

This should work fine.

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

Did you include that magic and very important line:

package GKLog;

?

> BEGIN {

BEGIN starts a begin block.  In a sense, this code is executed as soon as
it is seen rather than once the program begins to run.  You rarely need to
use BEGIN blocks when you're just defining variables, but it won't hurt
much.  

> 	use Exporter();
> 		@GKLog::ISA = qw(Exporter);
> 		@GKLog::EXPORT = qw();
> 		@GKLog::EXPORT_OK = qw($rootDir $logDir $FileDir);
> }

Exporter (perldoc Exporter) is a very useful module for exporting things
from the package name space and into the current name space.  It is
typically used for exporting subroutines.  For example, had you exported
a "fish_fingers" subroutine from GKLog then you could do:

---- program.pl ----
use GKLog qw/fish_fingers/;

fish_fingers(1,2,3);  # Notice that I didn't need to prepend with GKLog::

__END__

see, the fish_fingers subroutine, now having been exported can be called
from your main package without the GKLog prefix.

Exporter is not usually used in a BEGIN block, but it won't break
anything.

> use vars qw($rootDir $logDir $fileDir);

Right, so now you've said you want to export your variables, and that
they are global to this name space.  Great.  Now you need to give them
values.... which I trust you did, but didn't include in here.

> In the other files I have the following
> use GKLog qw($logDir $fileDir);
> 
> my $logDir = GKLog::logDir;
> my $fileDir = GKLog::fileDir;

Once you've imported the values as you have in your use line up there
you can simply just use the values normally.  ie:

--- program.pl -----
#!/usr/bin/perl -w
use strict;
use GKLog qw/$logDir $fileDir $rootDir/;

print $rootDir , "\n";			# these both print $rootDir as
print $GKLog::rootDir , "\n";		# defined in GKLog.pm

__END__

so you don't need to prepend the "GKLog::" bit to them.

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

That's because you forgot the "package GKLog;" at the top of your
GKLog.pm file I think.

Using the program.pl file just above and 

---- GKLog.pm ----
package GKLog;
use strict;
BEGIN {
        use Exporter();
        @GKLog::ISA = qw(Exporter);
        @GKLog::EXPORT = qw();
        @GKLog::EXPORT_OK = qw($rootDir $logDir $fileDir);
}
use vars qw/$rootDir $logDir $fileDir/;
$rootDir = "Fish";

1;
__END__

I find that $rootDir is exported and printed out correctly each time.

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

I hope so.  Hopefully this will have cleared up any confusion.

> 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? 

That's the one.  Programming Perl is often referred to as the Camel or the
Camel book because that's the animal that's on the front.  It's kind of silly
really but noone complains.... :)

Hope this helps.

	Jacinta Richardson

--
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson	    |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001 	    |  
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |



More information about the Programming mailing list