On 8/6/07, Ken Foskey <[EMAIL PROTECTED]> wrote:
>
> I have a script that must update the reconciliation system using a
> module.
>
> >From a design perspective how safe is it to use DB2 updates in an END
> block?
snip

They are fairly safe since this is pretty much what they were designed
for.  There aren't any gotchas that I aware of regarding END blocks
and the DBI.  It is important to note that END blocks run in reverse
order of declaration.  That is

#!/usr/bin/perl

use strict;
use warnings;

END { print "this runs second\n" }
END { print "this runs first\n" }

The other important thing to note is that END blocks run at the end
regardless of what else has happened, so you should try to make sure
that the code is in a reasonable state:

[EMAIL PROTECTED]:~$ cat t.pl
#!/usr/bin/perl

use strict;
use warnings;

use DBI;

BEGIN { $SIG{__DIE__} = sub { our $error = 1 } }

END {
        our ($error, $dbh);
        if (not $error and defined $dbh) {
                $dbh->do("insert into foo (x) values ($$)");
                $dbh->disconnect;
        } else {
                print STDERR "we had an error\n";
        }
}

our $dbh = DBI->connect(
        'dbi:SQLite:dbname=foo.db',
        '',
        '',
        {
                ChopBlanks       => 1,
                AutoCommit       => 1,
                RaiseError       => 1,
                PrintError       => 0,
                FetchHashKeyName => 'NAME_lc'
        }
);

$dbh->do("create table foo (x int)");

[EMAIL PROTECTED]:~$ perl t.pl
[EMAIL PROTECTED]:~$ sqlite3 foo.db "select * from foo;"
10227
[EMAIL PROTECTED]:~$ perl t.pl
DBD::SQLite::db do failed: table foo already exists(1) at dbdimp.c
line 271 at t.pl line 33.
we had an error
[EMAIL PROTECTED]:~$ sqlite3 foo.db "select * from foo;"
10227

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to