in reply to Hash assignment "odd" ness

The offending line, as reported in your error, is %hash = ($tmp);. The left hand-side of that statement is a hash and the right-hand side is a list with one element $tmp. The issue you are having is that you are assembling a string and trying to assign that to the hash instead of just storing directly in the hash. You could get Perl to do what you are asking it to do with eval, but why do that when you could just assign the values in the first place? This smells of an XY Problem.
while ( my ($key, $value) = each(%$po) ) { $hash{$key} = $value ? $value : "a"; }

Replies are listed 'Best First'.
Re^2: Hash assignment "odd" ness
by phigment (Initiate) on Jun 30, 2010 at 06:27 UTC

    Thank you!

    "list" was my disconnect. I tried the direct assignment construct multiple times, before I tried building the string and, every time Dumper out put would look like:

    $VAR1 = 'Comments'; $VAR2 = ''; $VAR3 = 'InstallAddID'; $VAR4 = ''; . . .

    When what I was expecting was:

    Comments = ''; InstallAddID = '';

    So the "X" is "%hash = ($never_gonna_work)" and the "Y" is "Why doesn't Dumper read my mind."

    print Dumper(\$po); would have solved my problem.<\p>

    Thanks again for the clarity. And this now works.

    #!/usr/bin/perl use strict; use warnings; use diagnostics; use Data::Dumper; use DBI; my $dbargs = {AutoCommit => 0,PrintError => 1}; my $dbc = DBI->connect("dbi:SQLite:dbname=da_hrb.db","","",$dbargs); # get the default po my $sth = $dbc->prepare('select * from der_po where poid = 0'); $sth->execute; my $po = $sth->fetchrow_hashref; # corrected. thanks ikegami print Dumper($po);
      Dumper takes a list of scalars for input. If you want to dump a hash, you need to pass a reference to the hash.
      print Dumper(%hash); # Dumps the result of evaluating the hash. Meh. print Dumper(\%hash); # Dumps the ref and the referenced hash. Good.

      On the other hand, $po is a scalar. No need to pass a reference to it.

      print Dumper(\$po); # Why?? print Dumper($po); # Good.
        print Dumper(\$po);  # Why??

        I wanted to be doubly sure!

        Truly, it was an artifact from a "print Dumper(\%hash);" test.

        It has been corrected.