Reputation: 87
Here is my code that I try to open the file to get data and change it to UTF-8, then read each line and store it in variable my $abstract_text and send it back in JSON structure.
my $fh;
if (!open($fh, '<:encoding(UTF-8)',$path))
{
returnApplicationError("Cannot read abstract file: $path ($!)\nERRORCODE|111|\n");
}
printJsonHeader;
my @lines = <$fh>;
my $abstract_text = '';
foreach my $line (@lines)
{
$abstract_text .= $line;
}
my $json = encode_json($abstract_text);
close $fh;
print $json;
By using that code, I get this error;
hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)
error message also point out that the problem is in this line; my $json = encode_json($abstract_text);
I want to send the data back as a string (which is in UTF-8). Please help.
Upvotes: 1
Views: 2594
Reputation: 34632
I assume you're using either JSON
or JSON::XS
.
Both allow for non-reference data, but not via the procedural encode_json
routine.
You'll need to use the object-oriented approach:
use strict; # obligatory
use warnings; # obligatory
use JSON::XS;
my $encoder = JSON::XS->new();
$encoder->allow_nonref();
print $encoder->encode('Hello, world.');
# => "Hello, world."
Upvotes: 4