Miek Miek - 1 year ago 201
Perl Question

How to include a config type file in a perl script

Okay , I'm giving up and asking for help;

I have a script to access info in my MySQL database. I want my credentials buried in a file I can include for security. To make it even I little neater, I tried to create a sub directory (include) in my cgi-bin directory.

This is a simple example omitting all the db stuff (because that's not my problem).

config.pl (inside the include dir)

use strict;
use warnings;

$data_base = 'dbi::mysql::test_db';
$db_user = 'some_user';
$db_pw = 'password';

test_driver.pl (inside the cgi-bin directory)

use strict;
use warnings;
use DBI;


$param1 = $data_base;
$param2 = $db_user;
$param3 = $db_pw;

The first error I'm getting is cannot find include. I've also seen many posts that show adding
in front of the vars. Also most posts mention not using
at all, but for simple things I would rather.

I ended up following some tutorials on how to make a Module. I got it to work but that seems like a lot of effort for a simple config file.

Answer Source

The path you provided in the require is off: You specified an absolute path, but you mistook the docroot of the server for the actual filesystem root.

/include/config.pl is anchored at the filesystem root, the same way /usr/bin/perl is.

./include/config.pl would be a path relative to the current dir (presumably the cgi-bin folder) and might work better. If you know the absolute path of the cgi-bin dir, you can use an absolute path: /var/www/whatever/structure/the/host/has/htdocs/cgi-bin/include/config.pl

If you are using a web framework (I hope you are) instead of plain, dated, CGI, there may already be a configuration file present. E.g. Dancer has a YAML file with lots of settings you could expand.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download