Forum Moderators: coopster & phranque

Message Too Old, No Replies

I'm New to Perl and Seeking Advice.

External files and the like for MySQL...

         

Jaxel

6:18 pm on Apr 19, 2008 (gmt 0)

10+ Year Member



Hi, I used to use PERL about 10 years ago, but I've since completely forgetten everything that I knew about it. Right now I'm trying to figure out how to handle external files in perl... For security reasons, I would like to have a single file with the config information for my MySQL database. This is a WEBSITE I am working on, so the code will be handled on a webpage, not at the linux console.

For instance, all my scripts will be contained in the dir:
/includes/
Each Perl function I make will be in its own file in that dir.

The config file will be located at /includes/config.cgi
The only file that will have information about my database, such as location of the server, usernames and passwords will be that config.cgi file. I would like to set it up so that all files will reference this config file at the top. How would I go about doing this? I tried "use includes::config;" but that didn't work. Is there something else I need to do?

As well, all the other files will be referenced by other html files in the root dir.

So if I have "/input.html" that recieves 2 variables, on submit, those two variables will go into "/includes/process.cgi", and then new variabes will be returned to "/output.html". How would I go about doing this?

Jaxel

6:22 pm on Apr 19, 2008 (gmt 0)

10+ Year Member



Oh, forgot... something to add to my first question. Once I reference the config file in my other perl files, I will be able to use all the variable names in the config file, as long as they dont have "MY" on them; right?

fabricator

11:10 am on Apr 20, 2008 (gmt 0)

10+ Year Member



The recommended way is this sort of setup.

mysql.pl
sub databaseConnect {
my $DBuser="mysql";
my $DBpass="";
my $databaseName = "dbi:mysql:databaseName";
$dbh = DBI->connect($databaseName, $DBuser, $DBpass) ¦¦ die $DBI::errstr;
$dbh->{mysql_auto_reconnect} = 1;

# clear values for security reasons.
$DBuser="";
$DBpass="";
}
1; # vital

then just use this in your main file:

require "includes/mysql.pl";
databaseConnect(); # $dbh comes from this;

rocknbil

5:00 pm on Apr 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The recommended method is to create a valid perl module of the include and export it as such, then

use MyConfig;

But require will work, it's deprecated, but easy, I "use" it all the time. :-)

perl_diver

8:46 pm on Apr 20, 2008 (gmt 0)

10+ Year Member



"require" is not deprecated. Either way will work but I would probaly also use a well constructed module and include it with a "use" instead of "require".

rocknbil

5:31 pm on Apr 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Huh, I could swear I picked up that bit of (errant) info from somewhere. I still like to use "require."

In general, use Module () is recommended over require Module, because it determines module availability at compile time, not in the middle of your program's execution. An exception would be if two modules each tried to use each other, and each also called a function from that other module. In that case, it's easy to use require instead.

Docs [search.cpan.org]

Jaxel

7:03 pm on Apr 21, 2008 (gmt 0)

10+ Year Member



Thanks guys, this stuff is really helping and my script is coming along nicely...

Next question, what should I CHMOD my /includes/ directory so that no one can view the contents within it (such as the config file which has my database access information in it)

perl_diver

8:30 pm on Apr 21, 2008 (gmt 0)

10+ Year Member



"require" can be easier to implement, but "use" is more "powerful". Neither is deprecated though.

Jaxel,

You should put your sensitive data in a folder that is not web accessible. If you can't then use the lowest possible CHMOD setting until your script can find the data.