Connect to the DB for Multiple .cgi Web Services with PERL

Most Web-based Perl applications use more than five .cgi Perl scripts. Instead of typing all the DBI information in the script, I created a function you can use that gets the information from a .dat file on the server, and parses it for the DBI.

It’s much more secure, since you’re not hand-typing DB login information on the script.

Here is the package script:

################ connectsql.pl #####################################################3#!c:/perl/bin/perl.exepackage connectsql;use Exporter;our @ISA = qw (Exporter);our @EXPORT = qw (connectsql $config{'dbUser'} $config{'dbServer'} $config{'dbPass'} $config{'datasource'});sub connectsql{use DBI; our $a; our $b; our $c; our $d; our %config; open FILE, "config.dat" or die "Can not open file.txt $!
"; while () {    chomp;      ($a,$b,$c,$d) = split(/:/); } $config{'dbServer'} = $b; $config{'dbUser'} = $a; $config{'dbPass'} = $d; $config{'dbName'} = $c; $config{'dataSource'} = "DBI:mysql:$config{'dbName'}:$config{'dbServer'}"; our $dbh = DBI->connect($config{'dataSource'},$config{'dbUser'},$config{'dbPass'}) || die"error"; 		 }

Now, all you need to do is add this to your code before the DBI connection:

...........require "connectsql.pl";&connectsql::connectsql();my $dbh = DBI->connect($connectsql::config{'dataSource'},$connectsql::config{'dbUser'},$connectsql::config{'dbPass'}) || die"error"; 	............

The config.dat file is easy:

################config.dat###############username:host:databasename:password:
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may