[prog] Perl and including files

Jacinta Richardson jarich at perltraining.com.au
Mon Mar 17 11:16:10 EST 2003


> 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

--
   ("`-''-/").___..--''"`-._          |  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