Why am I getting 0E0 for the return value of the execute DBI method?

I wrote a sample Perl program to remove data from a database table.

This is the code I wrote,

use DBI; my $dbh = DBI->connect("DBI:Pg:host=192.168.12.23;port=5432;", "adhi"); if ( $dbh ) { print "Connected successfully\n"; my $exe = $dbh->prepare("delete from perl_test.test"); my $res = $exe->execute(); if ( $res ) { print "deleted the table successfully of rows: $res\n"; } } 

If I did the above, it should print a successful message, and then the number of lines deleted.

If the table was empty, it printed 0E0 instead of 0 . I don't know how it returns a value like this?

Can someone explain to me how it works?

+5
source share
1 answer

This is done in such a way as to check whether the operation was successful. The reason is that '0E0' (as a string) is a true value, but 0 is a false value in Perl. Therefore:

  • you can check the return value in if to determine if the operation was successful (true means success),

  • but you can also use the return value as a number to find out the exact number of rows deleted since 0E0 evaluates to 0 when used as a number.

If you just need the number of lines deleted, you can use $res + 0 or $res * 1 . But only after you have verified that the operation was successful.

+9
source

All Articles