Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^4: How to pass hash site variables into sql connection using perl?

by finddata (Sexton)
on Mar 30, 2017 at 06:21 UTC ( [id://1186454]=note: print w/replies, xml ) Need Help??


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

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
  • Comment on Re^4: How to pass hash site variables into sql connection using perl?
  • Download Code

Replies are listed 'Best First'.
Re^5: How to pass hash site variables into sql connection using perl?
by poj (Abbot) on Mar 30, 2017 at 06:31 UTC

    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 ?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1186454]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-25 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found