perl5-dbi / dbi

DBI - The Perl 5 Database Interface
Other
82 stars 58 forks source link

local $dbh->{HandleError} = ... is not localized #114

Open damil opened 9 months ago

damil commented 9 months ago

A localized installation of a HandleError handler remains active even after exiting the localizing block. See test below (perl 5.32.0, DBI v1.643).

use strict;
use warnings;
use Test::More;
use DBI;

run_tests(SQLITE => DBI->connect('dbi:SQLite:dbname=:memory:', '', '', {RaiseError => 1}));
run_tests(CSV    => DBI->connect('dbi:CSV:', '', '', {f_ext => "foo.csv", RaiseError => 1}));

sub run_tests {
  my ($db_name, $dbh) = @_;

  eval {my $bug = $dbh->prepare("BAD SQL")};
  unlike $@, qr/FROM HandleError/, "$db_name: HandleError not installed yet";

  {
    local $dbh->{HandleError} = sub {die shift . " FROM HandleError"};
    eval {my $bug = $dbh->prepare("BAD SQL")};
    like $@, qr/FROM HandleError/, "$db_name: HandleError installled";
  }

  eval {my $bug = $dbh->prepare("BAD SQL")};
  unlike $@, qr/FROM HandleError/, "$db_name: HandleError should no longer be active";

}

done_testing;