in reply to Returning data

Here, string contcatenation doesn't work with hashes

%output .= ( 'items' => [("title" => "$title")] );

I'm guessing you want something like this:

$output{items} ||= []; push @{$output{items}}, {title => $title};

You should probably take a look at perldsc to see how you can use nested hashes & arrays

Also, the content type here should most likely be 'application/json'

print "Content-type: text/html\n\n"; print $results;

🐪

Replies are listed 'Best First'.
Re^2: Returning data
by cavac (Prior) on Apr 20, 2024 at 22:34 UTC

      am still getting the same error even if i use that module

      Can't modify private hash in concatenation (.) or string

Re^2: Returning data
by frank1 (Monk) on Apr 21, 2024 at 08:53 UTC

    am getting this error: cannot encode reference to scalar 'SCALAR(0x55c89ffbc4f0)' unless the scalar is 0 or 1

    my $dbh = DBI->connect("DBI:mysql:$dbname:$host", $usr, $pwd, { RaiseError => 1, }) or die $DBI::errstr; my $sth = $dbh->prepare("SELECT titles FROM tbl_1"); $sth->execute(); my $Data = $sth->fetchall_arrayref(); my $output; foreach my $Data (@$Data) { my ($title) = @$Data; $output{items} ||= []; push @{$output{items}}, {title => $title}; } my $encoded = encode_json \$output; print $Cgi->header( -type => 'application/json' ),$encoded;