http://www.perlmonks.org?node_id=1186447


in reply to Re^2: How to pass hash site variables into sql connection using perl?
in thread How to pass hash site variables into sql connection using perl?

Please post the code that gives that error.
I guess you need to rename your sub to dbconnect like shown here. You can't use connect because it is a DBI method.

poj

Replies are listed 'Best First'.
Re^4: How to pass hash site variables into sql connection using perl?
by Anonymous Monk on Mar 30, 2017 at 06:26 UTC
    I have posted the code .Please look into it.
Re^4: How to pass hash site variables into sql connection using perl?
by finddata (Sexton) on Mar 30, 2017 at 06:12 UTC
    I had to pass like this how can i pass it for the above script which you had included? $site{$site}{'current'} = "a_$site\_current";
Re^4: How to pass hash site variables into sql connection using perl?
by finddata (Sexton) on Mar 30, 2017 at 06:21 UTC
    use Getopt::Long; GetOptions("site=s" => \my $site) or die "Error in command line arguments\n"; defined $site or die "usage: $0 site_name\n"; my %site_map = ( site1 => { host => 'am.d.aog.com', db=>'site1', user=>'db_cad', pw= +>'Cad123' }, site2 => { host => 'am.d.aog.com', db=>'site2', user=>'db_cad', pw +=>'Cad123' }, ); my $dbh = dbconnect($site); print $dbh; sub dbconnect { my $s = shift; die "bad site $s" unless $site_map{$s}; my $dsn = "DBI:mysql:database=$site_map{$s}{db};host=$site_map{$s} +{host}"; my $dbh = DBI->connect($dsn, $site_map{$s}{'current'}{user}, $site +_map{$s}{'current'}{pw}) or die "ERROR: can't connect to database server site $s"; return $dbh; }
    This is how i tried from your code.
    i got the following error
    usage: jan/bin/r.cgi site_name

      The error is because you have added another level 'current' into the hash here

      $site_map{$s}{'current'}{user},$site_map{$s}{'current'}{pw}

      Why ?

Re^4: How to pass hash site variables into sql connection using perl?
by finddata (Sexton) on Mar 30, 2017 at 06:36 UTC
    i had used current because every database name will have current.So i tried to include those things inside it.

      But you didn't change the hash to suit like this

      #!perl use strict; my %site_map = ( site1 => { host => 'dam.sd.aog.com', db => 'a_site1_current', current => { user => 'ad', pw => '123' } }, site2 => { host => 'dam.sd.aog.com', db => 'a_site2_current', current => { user => 'ad', pw => '123' } }, site3 => { host => 'dam.sd.aog.com', db => 'a_site3_current', current => { user => 'ad', pw => '123' } }, );
        Here in db we had fetched the a_site3_current then why we want to use the current with user and pw?