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 274603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:38:07+00:00 2026-05-12T00:38:07+00:00

I have a query on a PostgreSQL system returning a boolean: my $sth =

  • 0

I have a query on a PostgreSQL system returning a boolean:

my $sth = $dbh->prepare("select 'f'::boolean");
$sth->execute;
my @vals = $sth->fetchrow_array;

According to the DBD::Pg docs,

The current implementation of
PostgreSQL returns ‘t’ for true and
‘f’ for false. From the Perl point of
view, this is a rather unfortunate
choice. DBD::Pg therefore translates
the result for the BOOL data type in a
Perlish manner: ‘f’ becomes the number
0 and ‘t’ becomes the number 1. This
way the application does not have to
check the database-specific returned
values for the data-type BOOL because
Perl treats 0 as false and 1 as true.
You may set the pg_bool_tf attribute
to a true value to change the values
back to ‘t’ and ‘f’ if you wish.

So, that statement should return a 0, which it does, so long as pg_bool_tf returns 0, which it does. However, somewhere along the way JSON::XS (and plain JSON) interprets the returned 0 as a string:

use JSON::XS qw(encode_json);
my $options =
{
    layout => 0,
    show_widget_help => $vals[0] // 1,
};
die encode_json($options);

…dies with:

{“layout”:0,”show_widget_help”:”0″}

…which would be fine, except that my JavaScript is expecting a boolean there, and the non-empty string “0” gets evaluated to true. Why is the latter 0 quoted and the former not?

According to the JSON::XS docs, this is a main feature:

round-trip integrity

When you serialise a perl data
structure using only data types
supported by JSON, the deserialised
data structure is identical on the
Perl level. (e.g. the string “2.0”
doesn’t suddenly become “2” just
because it looks like a number). There
minor are exceptions to this, read the
MAPPING section below to learn about
those.

…which says:

Simple Perl scalars (any scalar that
is not a reference) are the most
difficult objects to encode: JSON::XS
will encode undefined scalars as JSON
null values, scalars that have last
been used in a string context before
encoding as JSON strings, and anything
else as number value.

But I never use @vals[0] in a string context. Maybe DBD::Pg uses its boolean 0 as a string somewhere before returning it?

  • 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-05-12T00:38:07+00:00Added an answer on May 12, 2026 at 12:38 am

    The JSON::XS doc says the following will be converted to true/false

    • references to the integers 0 and 1, ie. \0 and \1
    • JSON::XS::true and JSON::XS::false

    Using one of these should solve your problem

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are a couple of variants of a rename command,… May 12, 2026 at 12:33 pm
  • Editorial Team
    Editorial Team added an answer There is currently no way to build CLS-compliant assemblies from… May 12, 2026 at 12:33 pm
  • Editorial Team
    Editorial Team added an answer You might want to look at Google Protocol Buffers or… May 12, 2026 at 12:33 pm

Related Questions

I'm trying to create an access control system. Here's a stripped down example of
I'm implementing home brew ACL system in web app and it gives me hard
I have a query in my application that runs very fast when there are
I've got a problem that keeps coming up with normalized databases and was looking

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.