Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7843159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:29:11+00:00 2026-06-02T16:29:11+00:00

When trying to run my perl script via cmd prompt my json string is

  • 0

When trying to run my perl script via cmd prompt my json string is returning [] I have read other posts, and fixed my database to be utf8 and the error still persists. I have tried two didfferent way to encode my perl string the first was $json = encode_json @temp_array which returns this error hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this However, when I use this line $json_text = $json->encode(@temp_array) I just get []

Here is my perl:

my $json_text;
my $json = JSON->new->utf8;
my @temp_array =[];
my $temp_array;
while (@data = $query_handle->fetchrow_array()) 
    {
    my %json_hash = ();
    my $hash_ref;
    %json_hash = (
                "User ID" => $data[0],
                "Status" => $data[1],
                "Last Password Reset" => $data[2],
                "Reset Needed" => $data[3]
                );
    $hash_ref = \%json_hash;
    push (@temp_array, $hash_ref);
    }   

print $json = encode_json @temp_array . "\n";   #encode with error
print $json_text = $json->encode(@temp_array) . "\n"; #encode with []
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json_text; #Prints []

So in my own testing, via the cmd prompt I know the while is retrieving the data from my db correctly and is building a hash, which I am assuming is correct.

Is it the fact I am pushing my hash reference to the array instead of the hash itself? Once I get this string built correctly, I will be calling it to an html via jquery

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:29:11+00:00Added an answer on June 2, 2026 at 4:29 pm

    JSON expects references:

    print $json = encode_json(\@temp_array) . "\n";   
    print $json_text = $json->encode(\@temp_array) . "\n";
    

    Edit: Unless you enable allow_nonref.

    Another edit: This line is wrong–

    my @temp_array =[]; ## should be my @temp_array = ();
    

    and this line overwrites the $json variable:

    print $json = encode_json @temp_array . "\n"; ## the next line in your script shouldn't work
    

    Last edit – untested:

    my $json = JSON->new->utf8;
    my @temp_array;
    while (my @data = $query_handle->fetchrow_array()) {
        my %json_hash = (
                "User ID" => $data[0],
                "Status" => $data[1],
                "Last Password Reset" => $data[2],
                "Reset Needed" => $data[3]
        );
        push (@temp_array, \%json_hash);
    }   
    
    print $json->encode(\@temp_array) . "\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to run a Perl script to read an YAML file, but I
I have the following Perl script. I am trying to run it in Windows
I'm trying to run sample Perl script on Windows 7 and I configured IIS
I am trying to run a Java program from my Perl script. I would
I am trying to run bp_genbank2gff3.pl (bioperl package) from another perl script that gets
I'm trying to run a perl script through C# code. The perl script takes
I am trying to run simple perl dbi example script to connect to mysql
Here is what I am trying to do. I have a perl script that
I am trying to have my perl script get an Xxml file from online
I am trying to run this script mail.pl #!/usr/bin/perl print Content-type: text/html \n\n; #HTTP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.