Thread: Perl won't eval PgSQL boolean value
I've got an if statement that checks if a boolean value is true: if ($cust_data->{'hold'} eq 't') But perl will not evaluate the value. $cust_data->{'hold'} is taken from a PgSQL boolean field (either t or f). If I use the construct above it tell me that 'eq' is not defined thinking it's a string. If I use: if ($cust_data->{'hold'} == 't') I get an error saying $cust_data->{'hold'} is a non-numeric value. If I do this: if ($cust_data->{'hold'}) it will always evaluate to true whether the value is 't' or 'f'. HELP. How do you evaluate boolean values from PgSQL in Perl? -- Randy Perry sysTame Mac Consulting/Sales phn 561.589.6449 mobile email help@systame.com
When comparing strings in perl you usually use the eq operator, not == e.g. if($cust_data->{hold} eq 't') - Andrew > -----Original Message----- > From: pgsql-general-owner@postgresql.org > [mailto:pgsql-general-owner@postgresql.org] On Behalf Of Randall Perry > Sent: Monday, 3 September 2001 10:01 AM > To: pgsql-general@postgresql.org > Subject: [GENERAL] Perl won't eval PgSQL boolean value > > > I've got an if statement that checks if a boolean value is true: > > if ($cust_data->{'hold'} eq 't') > > But perl will not evaluate the value. $cust_data->{'hold'} is > taken from a PgSQL boolean field (either t or f). If I use > the construct above it tell me that 'eq' is not defined > thinking it's a string. > > If I use: > if ($cust_data->{'hold'} == 't') > > I get an error saying $cust_data->{'hold'} is a non-numeric value. > > If I do this: > if ($cust_data->{'hold'}) > > it will always evaluate to true whether the value is 't' or 'f'. > > > HELP. How do you evaluate boolean values from PgSQL in Perl? > > > -- > Randy Perry > sysTame > Mac Consulting/Sales > > phn 561.589.6449 > mobile email help@systame.com > > > > > ---------------------------(end of > broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster >
Hi all, I've updated the version of the PostgreSQL Log Analyzer. This version fix many bugs and include some cool charts. There's now hourly statistics and some average calculation. You can take a look at the output at : http://www.samse.fr/GPL/pg_analyzer/sample/ Download here: http://www.samse.fr/GPL/pg_analyzer/pg_analyzer-1.1.tar.gz You also need some other perl modules, see requirement into the README. Note that if you use the previous version it is better to remove previous output and rerun the log analyzer... Ok, in next version you should be able to have I/O statistics, I've planed it to the end of the year :-) Regards, Gilles Darold
On Sunday 02 September 2001 19:01, Randall Perry wrote: > I've got an if statement that checks if a boolean value is true: > > if ($cust_data->{'hold'} eq 't') > > But perl will not evaluate the value. $cust_data->{'hold'} is taken from a > PgSQL boolean field (either t or f). If I use the construct above it tell > me that 'eq' is not defined thinking it's a string. This should work just fine. It sounds like a quote is being dropped somewhere... I just tested it here and it works just fine. > If I use: > if ($cust_data->{'hold'} == 't') > > I get an error saying $cust_data->{'hold'} is a non-numeric value. Which is what it should do. '==' is for numerics only. > If I do this: > if ($cust_data->{'hold'}) > > it will always evaluate to true whether the value is 't' or 'f'. Again, what it should do, both 't' and 'f' are non-zero as far as Perl is concerned. > > HELP. How do you evaluate boolean values from PgSQL in Perl? Your first choice (value eq 't') should do the trick. However, it sounds like there is another problem interfering with it. Could we see the code? GB -- GB Clark II | Roaming FreeBSD Admin gclarkii@VSServices.COM | General Geek CTHULU for President - Why choose the lesser of two evils?
On Sun, 02 Sep 2001 20:01:27 -0400, Randall Perry wrote: > I've got an if statement that checks if a boolean value is true: > > if ($cust_data->{'hold'} eq 't') > > But perl will not evaluate the value. $cust_data->{'hold'} is taken from a > PgSQL boolean field (either t or f). If I use the construct above it tell > me that 'eq' is not defined thinking it's a string. Assuming that you're using DBI and DBD::Pg, the problem is that DBD::Pg returns 1 and 0 for booleans, rather than the expected 't' and 'f'. This means that if you know you're always going to use Postgres (and that DBD::Pg isn't going to change), you can simply say: if($cust_data->{hold}) However, if you want cross-database portability (and protection from DBD::Pg's behaviour changing), you have to do this: if($cust_data->{hold}=~/[t1]/) -- Peter Haworth pmh@edison.ioppublishing.com perl -e '$|=s""\rJust another Perl hacker,";s/\w/$&\b#/g; s/(${\chr(97+rand$=)}).#/$1/ig&&sleep print while/#/;die$/' -- Ilmari Karonen, Yanick and Abigail