On 10/08/2006 12:15 PM, Shawn Hinchy wrote:

Thank you Mumia and Ovid. I appreciate both of your responses. I agree with you Ovid, you make some very good points. The point I forgot to include is that these variables are actually configuration-type constants. I misnamed them by calling them variables.
[...]

If you want to put some constants in a module, one of the many options is to use the "constant" and "Exporter" modules like so:

# file: AppConstans.pm
use strict;
use warnings;
package AppConstants;
use base 'Exporter';
use constant DB_HOST => 'my.host.example.com';
use constant DB_USER => 'my_user';
use constant DB_PASS => 'my_pass';
use constant LIST_OPTIONS => {
    recursive => 1,
    classes => 'd,s',
};

our @EXPORT = qw(DB_HOST DB_USER DB_PASS LIST_OPTIONS);

1;

__END__


In your main program you might do this:

use AppConstants;
...
my $dsn = "DBI:mysql:database=mydb;hostname=" . DB_HOST;
my $dbh = DBI->connect($dsn, DB_USER, DB_PASS);
...

Here is some useful documentation:
perldoc constant
perldoc Exporter
perldoc base
perldoc perlmod

P.S.
It's usually best to not export things by default, but I decided to make this example simple.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to