简体   繁体   中英

perl - help troubleshooting dbi statement that uses double and single quotes

I am trying to figure out why I could not get my variables working properly using Getopt::Std .

For example in this perl DBI statement where the user and password are enclosed in single quotes, everything works fine:

my $dbh = DBI->connect("DBI:mysql:database=$dbsrc;host=$node",'foo-bar','mypw');

But I want to change this statement so that I can pass in a variable for $user, $pw. I am using Getopt::Std; to pass in these values:

my $dbh = DBI->connect("DBI:mysql:database=$dbsrc;host=$node", '$user','$pw');

THIS WORKS NOW:

my $dbh = DBI->connect("DBI:mysql:database=$dbsrc;host=$node", $user,$pw);

I get the following error mesg:

DBI connect('database=;host=hostname','',...) failed:

UPDATED: After input from everybody, the best way to troubleshoot this for me was to see if my vars were being followed through. I basically was passing in the wrong values. So, simply I tested the dbi piece with getopts for those 2 values and nothing else and was able to get it to work.

引号是多余的( …306", $user, $pw)可以解决相同的问题),但是如果第一个版本有效,那么问题出在变量的值,而不是变量的使用方式。

You aren't showing all of the error message, but the part you do show seems to have an empty value for the user name. Are you sure your $user and $pw are actually set up right at that point in your program?

The difference is that when you use double quotes, the variables interpolate in the string, so the final string will be the value you have in your variables. With single quotes, the final string will not be interpolated by the variable value, so it will be '$user', no matter the value of $user variable.

my $dbh = DBI->connect("DBI:mysql:database=$dbsrc;host=$node;mysql_compression=1;port=3306", $user,$pw);

should work, provided that $dbsrc , $node , $user , and $pw are set correctly. What is the rest of your error message?

my $dbh = DBI->connect("DBI:mysql:database=$dbsrc;host=$node;mysql_compression=1;port=3306", '$user','$pw');

doesn't work because the single quotes cause the literal strings "$user" and "$pw" to be sent as login credentials rather than the values of the variables $user and $pw .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM