hesco has asked for the wisdom of the Perl Monks concerning the following question:

Hello all:

I'm working on an application which has outgrown its previous configuration arrangement. I'm now getting the following error, on the following line, which is calling the subroutine reproduced below.

How can I make this error go away and this script give me my database connection from the new configuration scheme? Why does it wait till the db_name to choke, while allowing the db_host_name to pass through without complaint?

-- Hugh

Software error:

Can't use string ("databasename ") as a HASH ref while "strict refs" in use at /usr/lib/cgi-bin/bc/supporters.cgi line 265.

(Yes, I see that I still need to strip that trailing space. That is a different issue, I think).

line 265 reads:

my $dbh = vol::connect_new($config{'db'}{qw(db_host_name db_name db_us +er db_pw)});
that subroutine reads:

sub connect_new { my($host,$db,$user,$pw) = @_; # my $host = "$config{'db'}{'db_host_name'}"; # my $db = "$config{'db'}{'db_name'}"; # my $user = "$config{'db'}{'db_user'}"; # my $pw = "$config{'db'}{'db_pw'}"; my $dsn = "DBI:mysql:host=$host;database=$db"; return (DBI->connect($dsn,$user,$pw, {PrintError => 0, RaiseError => 1})); }

Replies are listed 'Best First'.
Re: Can't use string ("") as a HASH ref while "strict refs"
by bart (Canon) on Nov 26, 2005 at 00:13 UTC
    my $dbh = vol::connect_new($config{'db'}{qw(db_host_name db_name db_user db_pw)});
    Hmm, in Perl, that is nonsense. Try something like
    my $dbh = vol::connect_new(@{$config{'db'}}{qw(db_host_name db_name db +_user db_pw)});

    I can't seem to find anything else wrong with your code, at first sight, so let's just start by trying that out, will you?

      Bart: Thanks for that. It did not change the error message one iota. Here is the block that defines $config{'db'}. It parses a plain text config.db file which defines four connection variables (host, database, user, password). Similar blocks parse configuration parameters for different elements of the %config hash, which is returned with the Exporter with:
      our %config;
      while (<DB>) { chomp; next if /^\s*\#/; # Allow comments next if /^\s*$/; # Allow blank lines unless (/=/) { # All other lines must look like: KEY + = VAL die "invalid variable assignment in supporters.db: $_"; } my ($key, $val) = split(/\s*=\s*/,$_,2); # Key and value are separ +ated by equals and maybe space $key =~ s/^\s*//; # Strip any leading space from the key # $val =~ s/(\$(\w+))/$config{$2}/g; # Very simple (read: brittle) v +ariable interpolation $val =~ s/ *$//g; # Strip trailing white space from value $config{'db'}{'$key'} = $val; } close DB;
      should this be creating references, instead of simply hash elements? Sorry for delay in responding. I was interrupted by a customer here. Thanks for your help and insight.

        $config{'db'}{'$key'} = $val;
        should be
        $config{'db'}{$key} = $val;
        but I don't think that will fix your error. The error seems to indicate that $config{'db'} was assigned '' or undef. Are you sure both %config are the same variable?

        A reply falls below the community's threshold of quality. You may see it by logging in.
        Ugh! You're using %config both as input, for "variable values", thus containing strings. as for the output, in $config{'db'}, as a hash ref. My gut feeling tells me this, or something very similar, is the cause of your problem. You may have tried to use $config{'db'} as a string value first, which could well cause your problem.

        Just put your input and output in different variables.

Re: Can't use string ("") as a HASH ref while "strict refs"
by bradcathey (Prior) on Nov 26, 2005 at 00:01 UTC

    You didn't show the actual values, but I would imagine you have an escape issue.


    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot