in reply to Eval not working

Two things,
  1. eval is not failing
  2. An update command that does nothing is not an error so if the condition(id = 1737) is not met nothing happens but no error is thrown.

Update: However the documentation on execute in DBI notes that

For a non-SELECT statement, execute returns the number of rows affected, if known. If no rows were affected, then execute returns "0E0", which Perl will treat as 0 but will regard as true. Note that it is not an error for no rows to be affected by a statement.
Perhaps you could assign the result of $sth->execute() and check to ensure that something was done

print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

Replies are listed 'Best First'.
Re^2: Eval not working
by DooDah (Novice) on Sep 03, 2010 at 11:11 UTC
    Thanks Utilitarian for explaining that.

    Am I right in saying the only way to catch a problem like that would be to use the 'affected rows' function?
      $dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; my $id = 737; my $rows; if (!eval { my $sql = "UPDATE stockhistory SET invoiced = 1 WHERE id = ?"; my $sth = $dbh->prepare($sql); $rows = $sth->execute($id); $dbh->commit(); 1 }) { print "Transaction aborted because $@"; $dbh->rollback(); } die("Record $id not found") if $rows < 1;

      Note: Can't check if $rows is true since "0E0" (equal to zero but true) is returned when no rows are affected.

        Thanks for all your help monks.
        I'll re-write my script to include your suggestions.

      Exactly.

      Your query might successfully update every record in the table, or only one, or none at all, but it ran successfully. The number of rows affected is provided by:

      print 'The query updated '. $sth->rows . " rows.\n";

      As Occam said: Entia non sunt multiplicanda praeter necessitatem.