in reply to DBI fetchall_hashref convert to scalar

Because a hash reference is already a scalar, I'm assuming that you mean you want it into a scalar *string*. Here's one way that you can exploit a scalar reference to be used as a memory file, and write the output of Data::Dumper directly to that scalar as a string value.

use warnings; use strict; use Data::Dumper; my $href = { a => 1, b => [ 2, 3, ], c => { 25 => 'y', 26 => 'z', }, d => 4, }; my $memfile; open my $mfh, '>', \$memfile or die $!; print $mfh Dumper($href); close $mfh; print $memfile;

Output:

$VAR1 = { 'c' => { '25' => 'y', '26' => 'z' }, 'b' => [ 2, 3 ], 'd' => 4, 'a' => 1 };

Replies are listed 'Best First'.
Re^2: DBI fetchall_hashref convert to scalar
by hippo (Archbishop) on Feb 03, 2017 at 09:52 UTC

    Does the use of $memfile as a memory file actually buy you anything, stevieb? I can't see how what you have there differs from the simpler alternative:

    use warnings; use strict; use Data::Dumper; my $href = { a => 1, b => [ 2, 3, ], c => { 25 => 'y', 26 => 'z', }, d => 4, }; my $text = Dumper ($href); print $text;

    What am I missing?

Re^2: DBI fetchall_hashref convert to scalar
by huck (Prior) on Feb 03, 2017 at 00:18 UTC
    in using this method you might find this helpfull
    local $Data::Dumper::Indent=0;

      Hey huck,

      If you're recommending that switch, could you please show us an example of it in use (ie. what happens when it's used), and let us know the purpose of using that var?